Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 17 | #include "../dispatcher/InputDispatcher.h" |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 18 | #include "../BlockingQueue.h" |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 19 | #include "FakeApplicationHandle.h" |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 20 | #include "FakeInputTracingBackend.h" |
Prabir Pradhan | e3b28dd | 2023-10-06 04:19:29 +0000 | [diff] [blame] | 21 | #include "TestEventMatchers.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 22 | |
Cody Heiner | 166a5af | 2023-07-07 12:25:00 -0700 | [diff] [blame] | 23 | #include <NotifyArgsBuilders.h> |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 24 | #include <android-base/logging.h> |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 25 | #include <android-base/properties.h> |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 26 | #include <android-base/silent_death_test.h> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 27 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 28 | #include <android-base/thread_annotations.h> |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 29 | #include <binder/Binder.h> |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 30 | #include <com_android_input_flags.h> |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 31 | #include <fcntl.h> |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 32 | #include <flag_macros.h> |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 33 | #include <gmock/gmock.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 34 | #include <gtest/gtest.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 35 | #include <input/Input.h> |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 36 | #include <input/PrintTools.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 37 | #include <linux/input.h> |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 38 | #include <sys/epoll.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 39 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 40 | #include <cinttypes> |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 41 | #include <compare> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 42 | #include <thread> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 43 | #include <unordered_set> |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 44 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 45 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 46 | using android::base::StringPrintf; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 47 | using android::gui::FocusRequest; |
| 48 | using android::gui::TouchOcclusionMode; |
| 49 | using android::gui::WindowInfo; |
| 50 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 51 | using android::os::InputEventInjectionResult; |
| 52 | using android::os::InputEventInjectionSync; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 53 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 54 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 55 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 56 | using namespace ftl::flag_operators; |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 57 | using testing::AllOf; |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 58 | using testing::Not; |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 59 | |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 60 | namespace { |
| 61 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 62 | // An arbitrary time value. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 63 | static constexpr nsecs_t ARBITRARY_TIME = 1234; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 64 | |
| 65 | // An arbitrary device id. |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 66 | static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 67 | static constexpr int32_t SECOND_DEVICE_ID = 2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 68 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 69 | // An arbitrary display id. |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 70 | static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
| 71 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 72 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 73 | // Ensure common actions are interchangeable between keys and motions for convenience. |
| 74 | static_assert(AMOTION_EVENT_ACTION_DOWN == AKEY_EVENT_ACTION_DOWN); |
| 75 | static_assert(AMOTION_EVENT_ACTION_UP == AKEY_EVENT_ACTION_UP); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 76 | static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN; |
| 77 | static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE; |
| 78 | static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP; |
| 79 | static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 80 | static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 81 | static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 82 | static constexpr int32_t ACTION_SCROLL = AMOTION_EVENT_ACTION_SCROLL; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 83 | static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 84 | static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 85 | /** |
| 86 | * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the |
| 87 | * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at |
| 88 | * index 0) is the new pointer going down. The same pointer could have been placed at a different |
| 89 | * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In |
| 90 | * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if |
| 91 | * pointer id=0 leaves but the pointer id=1 remains. |
| 92 | */ |
| 93 | static constexpr int32_t POINTER_0_DOWN = |
| 94 | AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 95 | static constexpr int32_t POINTER_1_DOWN = |
| 96 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 97 | static constexpr int32_t POINTER_2_DOWN = |
| 98 | AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 99 | static constexpr int32_t POINTER_3_DOWN = |
| 100 | AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 101 | static constexpr int32_t POINTER_0_UP = |
| 102 | AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 103 | static constexpr int32_t POINTER_1_UP = |
| 104 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 105 | static constexpr int32_t POINTER_2_UP = |
| 106 | AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 107 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 108 | // The default pid and uid for windows created on the primary display by the test. |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 109 | static constexpr gui::Pid WINDOW_PID{999}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 110 | static constexpr gui::Uid WINDOW_UID{1001}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 111 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 112 | // The default pid and uid for the windows created on the secondary display by the test. |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 113 | static constexpr gui::Pid SECONDARY_WINDOW_PID{1010}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 114 | static constexpr gui::Uid SECONDARY_WINDOW_UID{1012}; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 115 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 116 | // An arbitrary pid of the gesture monitor window |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 117 | static constexpr gui::Pid MONITOR_PID{2001}; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 118 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 119 | /** |
| 120 | * If we expect to receive the event, the timeout can be made very long. When the test are running |
| 121 | * correctly, we will actually never wait until the end of the timeout because the wait will end |
| 122 | * when the event comes in. Still, this value shouldn't be infinite. During development, a local |
| 123 | * change may cause the test to fail. This timeout should be short enough to not annoy so that the |
| 124 | * developer can see the failure quickly (on human scale). |
| 125 | */ |
| 126 | static constexpr std::chrono::duration CONSUME_TIMEOUT_EVENT_EXPECTED = 1000ms; |
| 127 | /** |
| 128 | * When no event is expected, we can have a very short timeout. A large value here would slow down |
| 129 | * the tests. In the unlikely event of system being too slow, the event may still be present but the |
| 130 | * timeout would complete before it is consumed. This would result in test flakiness. If this |
| 131 | * occurs, the flakiness rate would be high. Since the flakes are treated with high priority, this |
| 132 | * would get noticed and addressed quickly. |
| 133 | */ |
| 134 | static constexpr std::chrono::duration CONSUME_TIMEOUT_NO_EVENT_EXPECTED = 10ms; |
| 135 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 136 | static constexpr int expectedWallpaperFlags = |
| 137 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 138 | |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 139 | using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID; |
| 140 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 141 | /** |
| 142 | * Return a DOWN key event with KEYCODE_A. |
| 143 | */ |
| 144 | static KeyEvent getTestKeyEvent() { |
| 145 | KeyEvent event; |
| 146 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 147 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 148 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 149 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 150 | return event; |
| 151 | } |
| 152 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 153 | // --- FakeInputDispatcherPolicy --- |
| 154 | |
| 155 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 156 | struct AnrResult { |
| 157 | sp<IBinder> token{}; |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 158 | std::optional<gui::Pid> pid{}; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 159 | }; |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 160 | /* Stores data about a user-activity-poke event from the dispatcher. */ |
| 161 | struct UserActivityPokeEvent { |
| 162 | nsecs_t eventTime; |
| 163 | int32_t eventType; |
| 164 | int32_t displayId; |
| 165 | |
| 166 | bool operator==(const UserActivityPokeEvent& rhs) const = default; |
| 167 | |
| 168 | friend std::ostream& operator<<(std::ostream& os, const UserActivityPokeEvent& ev) { |
| 169 | os << "UserActivityPokeEvent[time=" << ev.eventTime << ", eventType=" << ev.eventType |
| 170 | << ", displayId=" << ev.displayId << "]"; |
| 171 | return os; |
| 172 | } |
| 173 | }; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 174 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 175 | public: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 176 | FakeInputDispatcherPolicy() = default; |
| 177 | virtual ~FakeInputDispatcherPolicy() = default; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 178 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 179 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 180 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 181 | ASSERT_EQ(event.getType(), InputEventType::KEY); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 182 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 183 | |
| 184 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 185 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 186 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 187 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 188 | } |
| 189 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 190 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 191 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 192 | ASSERT_EQ(event.getType(), InputEventType::MOTION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 193 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 194 | |
| 195 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 196 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 197 | EXPECT_EQ(motionEvent.getAction(), args.action); |
Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 198 | EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 199 | EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
| 200 | EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 201 | EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 202 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 203 | } |
| 204 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 205 | void assertFilterInputEventWasNotCalled() { |
| 206 | std::scoped_lock lock(mLock); |
| 207 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 208 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 209 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 210 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 211 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 212 | ASSERT_TRUE(mConfigurationChangedTime) |
| 213 | << "Timed out waiting for configuration changed call"; |
| 214 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 215 | mConfigurationChangedTime = std::nullopt; |
| 216 | } |
| 217 | |
| 218 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 219 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 220 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 221 | // 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] | 222 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 223 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 224 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 225 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 226 | mLastNotifySwitch = std::nullopt; |
| 227 | } |
| 228 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 229 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 230 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 231 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 232 | mOnPointerDownToken.clear(); |
| 233 | } |
| 234 | |
| 235 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 236 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 237 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 238 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 241 | // This function must be called soon after the expected ANR timer starts, |
| 242 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 243 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 244 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 245 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 246 | std::unique_lock lock(mLock); |
| 247 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 248 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 249 | ASSERT_NO_FATAL_FAILURE( |
| 250 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 251 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 254 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 255 | const sp<WindowInfoHandle>& window) { |
| 256 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 257 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 258 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 259 | } |
| 260 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 261 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 262 | const sp<IBinder>& expectedToken, |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 263 | std::optional<gui::Pid> expectedPid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 264 | std::unique_lock lock(mLock); |
| 265 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 266 | AnrResult result; |
| 267 | ASSERT_NO_FATAL_FAILURE(result = |
| 268 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 269 | ASSERT_EQ(expectedToken, result.token); |
| 270 | ASSERT_EQ(expectedPid, result.pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 271 | } |
| 272 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 273 | /** 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] | 274 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 275 | std::unique_lock lock(mLock); |
| 276 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 277 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); |
| 278 | const auto& [token, _] = result; |
| 279 | return token; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 282 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 283 | std::optional<gui::Pid> expectedPid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 284 | std::unique_lock lock(mLock); |
| 285 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 286 | AnrResult result; |
| 287 | ASSERT_NO_FATAL_FAILURE( |
| 288 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 289 | ASSERT_EQ(expectedToken, result.token); |
| 290 | ASSERT_EQ(expectedPid, result.pid); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** 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] | 294 | sp<IBinder> getResponsiveWindowToken() { |
| 295 | std::unique_lock lock(mLock); |
| 296 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 297 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 298 | const auto& [token, _] = result; |
| 299 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | void assertNotifyAnrWasNotCalled() { |
| 303 | std::scoped_lock lock(mLock); |
| 304 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 305 | ASSERT_TRUE(mAnrWindows.empty()); |
| 306 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 307 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 308 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 311 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 312 | std::unique_lock lock(mLock); |
| 313 | base::ScopedLockAssertion assumeLocked(mLock); |
| 314 | |
| 315 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 316 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 317 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 318 | enabled; |
| 319 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 320 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 321 | << ") to be called."; |
| 322 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 323 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 324 | auto request = *mPointerCaptureRequest; |
| 325 | mPointerCaptureRequest.reset(); |
| 326 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void assertSetPointerCaptureNotCalled() { |
| 330 | std::unique_lock lock(mLock); |
| 331 | base::ScopedLockAssertion assumeLocked(mLock); |
| 332 | |
| 333 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 334 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 335 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 336 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 337 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 338 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 341 | void assertDropTargetEquals(const InputDispatcherInterface& dispatcher, |
| 342 | const sp<IBinder>& targetToken) { |
| 343 | dispatcher.waitForIdle(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 344 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 345 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 346 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 347 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 348 | } |
| 349 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 350 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 351 | std::unique_lock lock(mLock); |
| 352 | base::ScopedLockAssertion assumeLocked(mLock); |
| 353 | std::optional<sp<IBinder>> receivedToken = |
| 354 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 355 | mNotifyInputChannelBroken); |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 356 | ASSERT_TRUE(receivedToken.has_value()) << "Did not receive the broken channel token"; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 357 | ASSERT_EQ(token, *receivedToken); |
| 358 | } |
| 359 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 360 | /** |
| 361 | * Set policy timeout. A value of zero means next key will not be intercepted. |
| 362 | */ |
| 363 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { |
| 364 | mInterceptKeyTimeout = timeout; |
| 365 | } |
| 366 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 367 | std::chrono::nanoseconds getKeyWaitingForEventsTimeout() override { return 500ms; } |
| 368 | |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 369 | void setStaleEventTimeout(std::chrono::nanoseconds timeout) { mStaleEventTimeout = timeout; } |
| 370 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 371 | void assertUserActivityNotPoked() { |
| 372 | std::unique_lock lock(mLock); |
| 373 | base::ScopedLockAssertion assumeLocked(mLock); |
| 374 | |
| 375 | std::optional<UserActivityPokeEvent> pokeEvent = |
| 376 | getItemFromStorageLockedInterruptible(500ms, mUserActivityPokeEvents, lock, |
| 377 | mNotifyUserActivity); |
| 378 | |
| 379 | ASSERT_FALSE(pokeEvent) << "Expected user activity not to have been poked"; |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 382 | /** |
| 383 | * Asserts that a user activity poke has happened. The earliest recorded poke event will be |
| 384 | * cleared after this call. |
| 385 | * |
| 386 | * If an expected UserActivityPokeEvent is provided, asserts that the given event is the |
| 387 | * earliest recorded poke event. |
| 388 | */ |
| 389 | void assertUserActivityPoked(std::optional<UserActivityPokeEvent> expectedPokeEvent = {}) { |
| 390 | std::unique_lock lock(mLock); |
| 391 | base::ScopedLockAssertion assumeLocked(mLock); |
| 392 | |
| 393 | std::optional<UserActivityPokeEvent> pokeEvent = |
| 394 | getItemFromStorageLockedInterruptible(500ms, mUserActivityPokeEvents, lock, |
| 395 | mNotifyUserActivity); |
| 396 | ASSERT_TRUE(pokeEvent) << "Expected a user poke event"; |
| 397 | |
| 398 | if (expectedPokeEvent) { |
| 399 | ASSERT_EQ(expectedPokeEvent, *pokeEvent); |
| 400 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 403 | void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids) { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 404 | ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms)); |
| 405 | } |
| 406 | |
| 407 | void assertNotifyDeviceInteractionWasNotCalled() { |
| 408 | ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms)); |
| 409 | } |
| 410 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 411 | void setUnhandledKeyHandler(std::function<std::optional<KeyEvent>(const KeyEvent&)> handler) { |
| 412 | std::scoped_lock lock(mLock); |
| 413 | mUnhandledKeyHandler = handler; |
| 414 | } |
| 415 | |
| 416 | void assertUnhandledKeyReported(int32_t keycode) { |
| 417 | std::unique_lock lock(mLock); |
| 418 | base::ScopedLockAssertion assumeLocked(mLock); |
| 419 | std::optional<int32_t> unhandledKeycode = |
| 420 | getItemFromStorageLockedInterruptible(100ms, mReportedUnhandledKeycodes, lock, |
| 421 | mNotifyUnhandledKey); |
| 422 | ASSERT_TRUE(unhandledKeycode) << "Expected unhandled key to be reported"; |
| 423 | ASSERT_EQ(unhandledKeycode, keycode); |
| 424 | } |
| 425 | |
| 426 | void assertUnhandledKeyNotReported() { |
| 427 | std::unique_lock lock(mLock); |
| 428 | base::ScopedLockAssertion assumeLocked(mLock); |
| 429 | std::optional<int32_t> unhandledKeycode = |
| 430 | getItemFromStorageLockedInterruptible(10ms, mReportedUnhandledKeycodes, lock, |
| 431 | mNotifyUnhandledKey); |
| 432 | ASSERT_FALSE(unhandledKeycode) << "Expected unhandled key NOT to be reported"; |
| 433 | } |
| 434 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 435 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 436 | std::mutex mLock; |
| 437 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 438 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 439 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 440 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 441 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 442 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 443 | |
| 444 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 445 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 446 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 447 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 448 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 449 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 450 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 451 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 452 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 453 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 454 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 455 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 456 | |
| 457 | std::condition_variable mNotifyUserActivity; |
| 458 | std::queue<UserActivityPokeEvent> mUserActivityPokeEvents; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 459 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 460 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 461 | |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 462 | std::chrono::nanoseconds mStaleEventTimeout = 1000ms; |
| 463 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 464 | BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 465 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 466 | std::condition_variable mNotifyUnhandledKey; |
| 467 | std::queue<int32_t> mReportedUnhandledKeycodes GUARDED_BY(mLock); |
| 468 | std::function<std::optional<KeyEvent>(const KeyEvent&)> mUnhandledKeyHandler GUARDED_BY(mLock); |
| 469 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 470 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 471 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 472 | // first entry from the container and erase it. |
| 473 | template <class T> |
| 474 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 475 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 476 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 477 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 478 | // before checking if ANR was called. |
| 479 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 480 | // to provide it some time to act. 100ms seems reasonable. |
| 481 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 482 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 483 | std::optional<T> token = |
| 484 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 485 | if (!token.has_value()) { |
| 486 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 487 | return {}; |
| 488 | } |
| 489 | |
| 490 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 491 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 492 | // the dispatcher started counting before this function was called |
| 493 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 494 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 495 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 496 | << "ms, but waited " |
| 497 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 498 | << "ms instead"; |
| 499 | } |
| 500 | return *token; |
| 501 | } |
| 502 | |
| 503 | template <class T> |
| 504 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 505 | std::queue<T>& storage, |
| 506 | std::unique_lock<std::mutex>& lock, |
| 507 | std::condition_variable& condition) |
| 508 | REQUIRES(mLock) { |
| 509 | condition.wait_for(lock, timeout, |
| 510 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 511 | if (storage.empty()) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 512 | return std::nullopt; |
| 513 | } |
| 514 | T item = storage.front(); |
| 515 | storage.pop(); |
| 516 | return std::make_optional(item); |
| 517 | } |
| 518 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 519 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 520 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 521 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 522 | } |
| 523 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 524 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<gui::Pid> pid, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 525 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 526 | std::scoped_lock lock(mLock); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 527 | mAnrWindows.push({connectionToken, pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 528 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 529 | } |
| 530 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 531 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 532 | std::optional<gui::Pid> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 533 | std::scoped_lock lock(mLock); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 534 | mResponsiveWindows.push({connectionToken, pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 535 | mNotifyAnr.notify_all(); |
| 536 | } |
| 537 | |
| 538 | void notifyNoFocusedWindowAnr( |
| 539 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 540 | std::scoped_lock lock(mLock); |
| 541 | mAnrApplications.push(applicationHandle); |
| 542 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 543 | } |
| 544 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 545 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 546 | std::scoped_lock lock(mLock); |
| 547 | mBrokenInputChannels.push(connectionToken); |
| 548 | mNotifyInputChannelBroken.notify_all(); |
| 549 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 550 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 551 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 552 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 553 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 554 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 555 | const std::vector<float>& values) override {} |
| 556 | |
| 557 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 558 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 559 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 560 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 561 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 562 | bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 563 | std::scoped_lock lock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 564 | switch (inputEvent.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 565 | case InputEventType::KEY: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 566 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent); |
| 567 | mFilteredEvent = std::make_unique<KeyEvent>(keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 568 | break; |
| 569 | } |
| 570 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 571 | case InputEventType::MOTION: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 572 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent); |
| 573 | mFilteredEvent = std::make_unique<MotionEvent>(motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 574 | break; |
| 575 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 576 | default: { |
| 577 | ADD_FAILURE() << "Should only filter keys or motions"; |
| 578 | break; |
| 579 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 580 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 581 | return true; |
| 582 | } |
| 583 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 584 | void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override { |
| 585 | if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 586 | // Clear intercept state when we handled the event. |
| 587 | mInterceptKeyTimeout = 0ms; |
| 588 | } |
| 589 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 590 | |
Yeabkal Wubshit | 88a9041 | 2023-12-21 18:23:04 -0800 | [diff] [blame] | 591 | void interceptMotionBeforeQueueing(int32_t, uint32_t, int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 592 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 593 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 594 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 595 | // Clear intercept state so we could dispatch the event in next wake. |
| 596 | mInterceptKeyTimeout = 0ms; |
| 597 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 598 | } |
| 599 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 600 | std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent& event, |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 601 | uint32_t) override { |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 602 | std::scoped_lock lock(mLock); |
| 603 | mReportedUnhandledKeycodes.emplace(event.getKeyCode()); |
| 604 | mNotifyUnhandledKey.notify_all(); |
| 605 | return mUnhandledKeyHandler != nullptr ? mUnhandledKeyHandler(event) : std::nullopt; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 606 | } |
| 607 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 608 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 609 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 610 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 611 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 612 | * essentially a passthrough for notifySwitch. |
| 613 | */ |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 614 | mLastNotifySwitch = |
| 615 | NotifySwitchArgs(InputEvent::nextId(), when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 616 | } |
| 617 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 618 | void pokeUserActivity(nsecs_t eventTime, int32_t eventType, int32_t displayId) override { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 619 | std::scoped_lock lock(mLock); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 620 | mNotifyUserActivity.notify_all(); |
| 621 | mUserActivityPokeEvents.push({eventTime, eventType, displayId}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 622 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 623 | |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 624 | bool isStaleEvent(nsecs_t currentTime, nsecs_t eventTime) override { |
| 625 | return std::chrono::nanoseconds(currentTime - eventTime) >= mStaleEventTimeout; |
| 626 | } |
| 627 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 628 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 629 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 630 | mOnPointerDownToken = newToken; |
| 631 | } |
| 632 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 633 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 634 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 635 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 636 | mPointerCaptureChangedCondition.notify_all(); |
| 637 | } |
| 638 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 639 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 640 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 641 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 642 | mDropTargetWindowToken = token; |
| 643 | } |
| 644 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 645 | void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 646 | const std::set<gui::Uid>& uids) override { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 647 | ASSERT_TRUE(mNotifiedInteractions.emplace(deviceId, uids)); |
| 648 | } |
| 649 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 650 | void assertFilterInputEventWasCalledInternal( |
| 651 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 652 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 653 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 654 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 655 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 656 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 657 | }; |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 658 | } // namespace |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 659 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 660 | // --- InputDispatcherTest --- |
| 661 | |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 662 | // The trace is a global variable for now, to avoid having to pass it into all of the |
| 663 | // FakeWindowHandles created throughout the tests. |
| 664 | // TODO(b/210460522): Update the tests to avoid the need to have the trace be a global variable. |
| 665 | static std::shared_ptr<VerifyingTrace> gVerifyingTrace = std::make_shared<VerifyingTrace>(); |
| 666 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 667 | class InputDispatcherTest : public testing::Test { |
| 668 | protected: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 669 | std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 670 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 671 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 672 | void SetUp() override { |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 673 | gVerifyingTrace->reset(); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 674 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 675 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, |
| 676 | std::make_unique<FakeInputTracingBackend>( |
| 677 | gVerifyingTrace)); |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 678 | |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 679 | mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 680 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 681 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 684 | void TearDown() override { |
Prabir Pradhan | 0eaf140 | 2024-02-05 22:43:04 +0000 | [diff] [blame] | 685 | ASSERT_NO_FATAL_FAILURE(gVerifyingTrace->verifyExpectedEventsTraced()); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 686 | ASSERT_EQ(OK, mDispatcher->stop()); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 687 | mFakePolicy.reset(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 688 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 689 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 690 | |
| 691 | /** |
| 692 | * Used for debugging when writing the test |
| 693 | */ |
| 694 | void dumpDispatcherState() { |
| 695 | std::string dump; |
| 696 | mDispatcher->dump(dump); |
| 697 | std::stringstream ss(dump); |
| 698 | std::string to; |
| 699 | |
| 700 | while (std::getline(ss, to, '\n')) { |
| 701 | ALOGE("%s", to.c_str()); |
| 702 | } |
| 703 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 704 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 705 | void setFocusedWindow(const sp<WindowInfoHandle>& window) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 706 | FocusRequest request; |
| 707 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 708 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 709 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 710 | request.displayId = window->getInfo()->displayId; |
| 711 | mDispatcher->setFocusedWindow(request); |
| 712 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 713 | }; |
| 714 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 715 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 716 | KeyEvent event; |
| 717 | |
| 718 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 719 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 720 | INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 721 | /*action=*/-1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 722 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 723 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 724 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 725 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 726 | << "Should reject key events with undefined action."; |
| 727 | |
| 728 | // 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] | 729 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 730 | 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] | 731 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 732 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 733 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 734 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 735 | << "Should reject key events with ACTION_MULTIPLE."; |
| 736 | } |
| 737 | |
| 738 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 739 | MotionEvent event; |
| 740 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 741 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 742 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 743 | pointerProperties[i].clear(); |
| 744 | pointerProperties[i].id = i; |
| 745 | pointerCoords[i].clear(); |
| 746 | } |
| 747 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 748 | // Some constants commonly used below |
| 749 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 750 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 751 | constexpr int32_t metaState = AMETA_NONE; |
| 752 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 753 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 754 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 755 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 756 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 757 | /*action=*/-1, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 758 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 759 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 760 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 761 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 762 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 763 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 764 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 765 | << "Should reject motion events with undefined action."; |
| 766 | |
| 767 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 768 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 769 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 770 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 771 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 772 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 773 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 774 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 775 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 776 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 777 | << "Should reject motion events with pointer down index too large."; |
| 778 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 779 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 780 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 781 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 782 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 783 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 784 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 785 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 786 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 787 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 788 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 789 | << "Should reject motion events with pointer down index too small."; |
| 790 | |
| 791 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 792 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 793 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 794 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 795 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 796 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 797 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 798 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 799 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 800 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 801 | << "Should reject motion events with pointer up index too large."; |
| 802 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 803 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 804 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 805 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 806 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 807 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 808 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 809 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 810 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 811 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 812 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 813 | << "Should reject motion events with pointer up index too small."; |
| 814 | |
| 815 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 816 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 817 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 818 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 819 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 820 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 821 | /*pointerCount=*/0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 822 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 823 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 824 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 825 | << "Should reject motion events with 0 pointers."; |
| 826 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 827 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 828 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 829 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 830 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 831 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 832 | /*pointerCount=*/MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 833 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 834 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 835 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 836 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 837 | |
| 838 | // Rejects motion events with invalid pointer ids. |
| 839 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 840 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 841 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 842 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 843 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 844 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 845 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 846 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 847 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 848 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 849 | << "Should reject motion events with pointer ids less than 0."; |
| 850 | |
| 851 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 852 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 853 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 854 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 855 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 856 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 857 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 858 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 859 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 860 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 861 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 862 | |
| 863 | // Rejects motion events with duplicate pointer ids. |
| 864 | pointerProperties[0].id = 1; |
| 865 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 866 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 867 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 868 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 869 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 870 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 871 | /*pointerCount=*/2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 872 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 873 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 874 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 875 | << "Should reject motion events with duplicate pointer ids."; |
| 876 | } |
| 877 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 878 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 879 | |
| 880 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 881 | constexpr nsecs_t eventTime = 20; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 882 | mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 883 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 884 | |
| 885 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 886 | } |
| 887 | |
| 888 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 889 | NotifySwitchArgs args(InputEvent::nextId(), /*eventTime=*/20, /*policyFlags=*/0, |
| 890 | /*switchValues=*/1, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 891 | /*switchMask=*/2); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 892 | mDispatcher->notifySwitch(args); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 893 | |
| 894 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 895 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 896 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 897 | } |
| 898 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 899 | namespace { |
| 900 | |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 901 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 902 | // Default input dispatching timeout if there is no focused application or paused window |
| 903 | // from which to determine an appropriate dispatching timeout. |
| 904 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 905 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 906 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 907 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 908 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 909 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 910 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 911 | : mConsumer(std::move(clientChannel)), mName(name) {} |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 912 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 913 | std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = false) { |
| 914 | auto [consumeSeq, event] = receiveEvent(timeout); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 915 | if (!consumeSeq) { |
| 916 | return nullptr; |
| 917 | } |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 918 | finishEvent(*consumeSeq, handled); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 919 | return std::move(event); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | /** |
| 923 | * Receive an event without acknowledging it. |
| 924 | * Return the sequence number that could later be used to send finished signal. |
| 925 | */ |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 926 | std::pair<std::optional<uint32_t>, std::unique_ptr<InputEvent>> receiveEvent( |
| 927 | std::chrono::milliseconds timeout) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 928 | uint32_t consumeSeq; |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 929 | std::unique_ptr<InputEvent> event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 930 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 931 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 932 | status_t status = WOULD_BLOCK; |
| 933 | while (status == WOULD_BLOCK) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 934 | InputEvent* rawEventPtr = nullptr; |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 935 | status = mConsumer.consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 936 | &rawEventPtr); |
| 937 | event = std::unique_ptr<InputEvent>(rawEventPtr); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 938 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 939 | if (elapsed > timeout) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 940 | break; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | if (status == WOULD_BLOCK) { |
| 945 | // Just means there's no event available. |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 946 | return std::make_pair(std::nullopt, nullptr); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | if (status != OK) { |
| 950 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 951 | return std::make_pair(std::nullopt, nullptr); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 952 | } |
| 953 | if (event == nullptr) { |
| 954 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 955 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 956 | return std::make_pair(consumeSeq, std::move(event)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 957 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 958 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 959 | /** |
| 960 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 961 | */ |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 962 | void finishEvent(uint32_t consumeSeq, bool handled = true) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 963 | const status_t status = mConsumer.sendFinishedSignal(consumeSeq, handled); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 964 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 965 | } |
| 966 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 967 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 968 | const status_t status = mConsumer.sendTimeline(inputEventId, timeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 969 | ASSERT_EQ(OK, status); |
| 970 | } |
| 971 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 972 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 973 | std::optional<int32_t> expectedDisplayId, |
| 974 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 975 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 976 | |
| 977 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 978 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 979 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 980 | << mName.c_str() << " expected " << ftl::enum_string(expectedEventType) |
| 981 | << " event, got " << *event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 982 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 983 | if (expectedDisplayId.has_value()) { |
| 984 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 985 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 986 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 987 | switch (expectedEventType) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 988 | case InputEventType::KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 989 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 990 | ASSERT_THAT(keyEvent, WithKeyAction(expectedAction)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 991 | if (expectedFlags.has_value()) { |
| 992 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 993 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 994 | break; |
| 995 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 996 | case InputEventType::MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 997 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 998 | ASSERT_THAT(motionEvent, WithMotionAction(expectedAction)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 999 | if (expectedFlags.has_value()) { |
| 1000 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 1001 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 1002 | break; |
| 1003 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1004 | case InputEventType::FOCUS: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1005 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 1006 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1007 | case InputEventType::CAPTURE: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1008 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 1009 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1010 | case InputEventType::TOUCH_MODE: { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1011 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 1012 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1013 | case InputEventType::DRAG: { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1014 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 1015 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 1016 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1017 | } |
| 1018 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1019 | std::unique_ptr<MotionEvent> consumeMotion() { |
| 1020 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1021 | |
| 1022 | if (event == nullptr) { |
| 1023 | ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one."; |
| 1024 | return nullptr; |
| 1025 | } |
| 1026 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1027 | if (event->getType() != InputEventType::MOTION) { |
| 1028 | ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1029 | return nullptr; |
| 1030 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1031 | return std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event.release())); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1035 | std::unique_ptr<MotionEvent> motionEvent = consumeMotion(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1036 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; |
| 1037 | ASSERT_THAT(*motionEvent, matcher); |
| 1038 | } |
| 1039 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1040 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1041 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1042 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1043 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1044 | ASSERT_EQ(InputEventType::FOCUS, event->getType()) |
| 1045 | << "Instead of FocusEvent, got " << *event; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1046 | |
| 1047 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1048 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1049 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1050 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1051 | EXPECT_EQ(hasFocus, focusEvent.getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1052 | } |
| 1053 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1054 | void consumeCaptureEvent(bool hasCapture) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1055 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 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::CAPTURE, event->getType()) |
| 1059 | << "Instead of CaptureEvent, got " << *event; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1060 | |
| 1061 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1062 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1063 | |
| 1064 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 1065 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 1066 | } |
| 1067 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1068 | void consumeDragEvent(bool isExiting, float x, float y) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1069 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1070 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1071 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1072 | ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1073 | |
| 1074 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1075 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1076 | |
| 1077 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 1078 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 1079 | EXPECT_EQ(x, dragEvent.getX()); |
| 1080 | EXPECT_EQ(y, dragEvent.getY()); |
| 1081 | } |
| 1082 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1083 | void consumeTouchModeEvent(bool inTouchMode) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1084 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1085 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1086 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1087 | ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType()) |
| 1088 | << "Instead of TouchModeEvent, got " << *event; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1089 | |
| 1090 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1091 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1092 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 1093 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 1094 | } |
| 1095 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 1096 | void assertNoEvents(std::chrono::milliseconds timeout) { |
| 1097 | std::unique_ptr<InputEvent> event = consume(timeout); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1098 | if (event == nullptr) { |
| 1099 | return; |
| 1100 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1101 | if (event->getType() == InputEventType::KEY) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1102 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1103 | ADD_FAILURE() << "Received key event " << keyEvent; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1104 | } else if (event->getType() == InputEventType::MOTION) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1105 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1106 | ADD_FAILURE() << "Received motion event " << motionEvent; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1107 | } else if (event->getType() == InputEventType::FOCUS) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1108 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1109 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 1110 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1111 | } else if (event->getType() == InputEventType::CAPTURE) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1112 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 1113 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 1114 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1115 | } else if (event->getType() == InputEventType::TOUCH_MODE) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1116 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 1117 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 1118 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1119 | } |
| 1120 | FAIL() << mName.c_str() |
| 1121 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1124 | sp<IBinder> getToken() { return mConsumer.getChannel()->getConnectionToken(); } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1125 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 1126 | int getChannelFd() { return mConsumer.getChannel()->getFd(); } |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1127 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1128 | private: |
| 1129 | InputConsumer mConsumer; |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1130 | DynamicInputEventFactory mEventFactory; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1131 | |
| 1132 | std::string mName; |
| 1133 | }; |
| 1134 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1135 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1136 | public: |
| 1137 | static const int32_t WIDTH = 600; |
| 1138 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1139 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1140 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1141 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1142 | int32_t displayId, bool createInputChannel = true) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1143 | : mName(name) { |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1144 | sp<IBinder> token; |
| 1145 | if (createInputChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1146 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1147 | dispatcher->createInputChannel(name); |
| 1148 | token = (*channel)->getConnectionToken(); |
| 1149 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | inputApplicationHandle->updateInfo(); |
| 1153 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1154 | |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1155 | mInfo.token = token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1156 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1157 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1158 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1159 | mInfo.alpha = 1.0; |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 1160 | mInfo.frame = Rect(0, 0, WIDTH, HEIGHT); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1161 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1162 | mInfo.globalScaleFactor = 1.0; |
| 1163 | mInfo.touchableRegion.clear(); |
| 1164 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1165 | mInfo.ownerPid = WINDOW_PID; |
| 1166 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1167 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1168 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1169 | } |
| 1170 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1171 | sp<FakeWindowHandle> clone(int32_t displayId) { |
| 1172 | sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)"); |
| 1173 | handle->mInfo = mInfo; |
| 1174 | handle->mInfo.displayId = displayId; |
| 1175 | handle->mInfo.id = sId++; |
| 1176 | handle->mInputReceiver = mInputReceiver; |
| 1177 | return handle; |
| 1178 | } |
| 1179 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1180 | void setTouchable(bool touchable) { |
| 1181 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1182 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1183 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1184 | void setFocusable(bool focusable) { |
| 1185 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1186 | } |
| 1187 | |
| 1188 | void setVisible(bool visible) { |
| 1189 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1190 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1191 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1192 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1193 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1196 | void setPaused(bool paused) { |
| 1197 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1198 | } |
| 1199 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1200 | void setPreventSplitting(bool preventSplitting) { |
| 1201 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | void setSlippery(bool slippery) { |
| 1205 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1206 | } |
| 1207 | |
| 1208 | void setWatchOutsideTouch(bool watchOutside) { |
| 1209 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1210 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1211 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1212 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1213 | |
| 1214 | void setInterceptsStylus(bool interceptsStylus) { |
| 1215 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1216 | } |
| 1217 | |
| 1218 | void setDropInput(bool dropInput) { |
| 1219 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1220 | } |
| 1221 | |
| 1222 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1223 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1224 | } |
| 1225 | |
| 1226 | void setNoInputChannel(bool noInputChannel) { |
| 1227 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1228 | } |
| 1229 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1230 | void setDisableUserActivity(bool disableUserActivity) { |
| 1231 | mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity); |
| 1232 | } |
| 1233 | |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 1234 | void setGlobalStylusBlocksTouch(bool shouldGlobalStylusBlockTouch) { |
| 1235 | mInfo.setInputConfig(WindowInfo::InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH, |
| 1236 | shouldGlobalStylusBlockTouch); |
| 1237 | } |
| 1238 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1239 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1240 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1241 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1242 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1243 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1244 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1245 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 1246 | mInfo.frame = frame; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1247 | mInfo.touchableRegion.clear(); |
| 1248 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1249 | |
| 1250 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1251 | ui::Transform translate; |
| 1252 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1253 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1254 | } |
| 1255 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1256 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1257 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1258 | void setIsWallpaper(bool isWallpaper) { |
| 1259 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1260 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1261 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1262 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1263 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1264 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1265 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1266 | void setTrustedOverlay(bool trustedOverlay) { |
| 1267 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1268 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1269 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1270 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1271 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1272 | } |
| 1273 | |
| 1274 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1275 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1276 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1277 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1278 | std::unique_ptr<KeyEvent> consumeKey(bool handled = true) { |
| 1279 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED, handled); |
| 1280 | if (event == nullptr) { |
| 1281 | ADD_FAILURE() << "No event"; |
| 1282 | return nullptr; |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1283 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1284 | if (event->getType() != InputEventType::KEY) { |
| 1285 | ADD_FAILURE() << "Instead of key event, got " << event; |
| 1286 | return nullptr; |
| 1287 | } |
| 1288 | return std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event.release())); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | void consumeKeyEvent(const ::testing::Matcher<KeyEvent>& matcher) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1292 | std::unique_ptr<KeyEvent> keyEvent = consumeKey(); |
| 1293 | ASSERT_NE(nullptr, keyEvent); |
| 1294 | ASSERT_THAT(*keyEvent, matcher); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1297 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1298 | consumeKeyEvent(AllOf(WithKeyAction(ACTION_DOWN), WithDisplayId(expectedDisplayId), |
| 1299 | WithFlags(expectedFlags))); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1300 | } |
| 1301 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1302 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1303 | consumeKeyEvent(AllOf(WithKeyAction(ACTION_UP), WithDisplayId(expectedDisplayId), |
| 1304 | WithFlags(expectedFlags))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1307 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1308 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1309 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId), |
| 1310 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1314 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1315 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 1316 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1320 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1321 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1322 | } |
| 1323 | |
| 1324 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1325 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1326 | consumeMotionEvent( |
| 1327 | AllOf(WithMotionAction(ACTION_DOWN), |
| 1328 | testing::Conditional(expectedDisplayId.has_value(), |
| 1329 | WithDisplayId(*expectedDisplayId), testing::_), |
| 1330 | testing::Conditional(expectedFlags.has_value(), WithFlags(*expectedFlags), |
| 1331 | testing::_))); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1332 | } |
| 1333 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1334 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1335 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1336 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1337 | const int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1338 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1339 | consumeMotionEvent(AllOf(WithMotionAction(action), WithDisplayId(expectedDisplayId), |
| 1340 | WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1344 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1345 | const int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1346 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1347 | consumeMotionEvent(AllOf(WithMotionAction(action), WithDisplayId(expectedDisplayId), |
| 1348 | WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1352 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1353 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDisplayId(expectedDisplayId), |
| 1354 | WithFlags(expectedFlags))); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1357 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1358 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1359 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE), |
| 1360 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1361 | } |
| 1362 | |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1363 | void consumeMotionOutsideWithZeroedCoords() { |
| 1364 | consumeMotionEvent( |
| 1365 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE), WithRawCoords(0, 0))); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1366 | } |
| 1367 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1368 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1369 | ASSERT_NE(mInputReceiver, nullptr) |
| 1370 | << "Cannot consume events from a window with no receiver"; |
| 1371 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1372 | } |
| 1373 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1374 | void consumeCaptureEvent(bool hasCapture) { |
| 1375 | ASSERT_NE(mInputReceiver, nullptr) |
| 1376 | << "Cannot consume events from a window with no receiver"; |
| 1377 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1378 | } |
| 1379 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1380 | std::unique_ptr<MotionEvent> consumeMotionEvent( |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1381 | const ::testing::Matcher<MotionEvent>& matcher = testing::_) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1382 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
| 1383 | if (event == nullptr) { |
| 1384 | ADD_FAILURE() << "No event"; |
| 1385 | return nullptr; |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 1386 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1387 | if (event->getType() != InputEventType::MOTION) { |
| 1388 | ADD_FAILURE() << "Instead of motion event, got " << *event; |
| 1389 | return nullptr; |
| 1390 | } |
| 1391 | std::unique_ptr<MotionEvent> motionEvent = |
| 1392 | std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event.release())); |
| 1393 | EXPECT_THAT(*motionEvent, matcher); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1394 | return motionEvent; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1395 | } |
| 1396 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1397 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1398 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1399 | } |
| 1400 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1401 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1402 | ASSERT_NE(mInputReceiver, nullptr) |
| 1403 | << "Cannot consume events from a window with no receiver"; |
| 1404 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1405 | } |
| 1406 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1407 | std::pair<std::optional<uint32_t>, std::unique_ptr<InputEvent>> receiveEvent() { |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 1408 | return receive(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | void finishEvent(uint32_t sequenceNum) { |
| 1412 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1413 | mInputReceiver->finishEvent(sequenceNum); |
| 1414 | } |
| 1415 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1416 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1417 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1418 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1419 | } |
| 1420 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 1421 | void assertNoEvents(std::chrono::milliseconds timeout = CONSUME_TIMEOUT_NO_EVENT_EXPECTED) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1422 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1423 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1424 | return; // Can't receive events if the window does not have input channel |
| 1425 | } |
| 1426 | ASSERT_NE(nullptr, mInputReceiver) |
| 1427 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 1428 | mInputReceiver->assertNoEvents(timeout); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1429 | } |
| 1430 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1431 | sp<IBinder> getToken() { return mInfo.token; } |
| 1432 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1433 | const std::string& getName() { return mName; } |
| 1434 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 1435 | void setOwnerInfo(gui::Pid ownerPid, gui::Uid ownerUid) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1436 | mInfo.ownerPid = ownerPid; |
| 1437 | mInfo.ownerUid = ownerUid; |
| 1438 | } |
| 1439 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 1440 | gui::Pid getPid() const { return mInfo.ownerPid; } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1441 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1442 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1443 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1444 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1445 | |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 1446 | // FakeWindowHandle uses this consume method to ensure received events are added to the trace. |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1447 | std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = true) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1448 | if (mInputReceiver == nullptr) { |
| 1449 | LOG(FATAL) << "Cannot consume event from a window with no input event receiver"; |
| 1450 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1451 | std::unique_ptr<InputEvent> event = mInputReceiver->consume(timeout, handled); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1452 | if (event == nullptr) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1453 | ADD_FAILURE() << "Consume failed: no event"; |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1454 | } |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 1455 | expectReceivedEventTraced(event); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1456 | return event; |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1457 | } |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 1458 | |
| 1459 | private: |
| 1460 | FakeWindowHandle(std::string name) : mName(name){}; |
| 1461 | const std::string mName; |
| 1462 | std::shared_ptr<FakeInputReceiver> mInputReceiver; |
| 1463 | static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger |
| 1464 | friend class sp<FakeWindowHandle>; |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 1465 | |
| 1466 | // FakeWindowHandle uses this receive method to ensure received events are added to the trace. |
| 1467 | std::pair<std::optional<uint32_t /*seq*/>, std::unique_ptr<InputEvent>> receive() { |
| 1468 | if (mInputReceiver == nullptr) { |
| 1469 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1470 | return std::make_pair(std::nullopt, nullptr); |
| 1471 | } |
| 1472 | auto out = mInputReceiver->receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED); |
| 1473 | const auto& [_, event] = out; |
| 1474 | expectReceivedEventTraced(event); |
| 1475 | return std::move(out); |
| 1476 | } |
| 1477 | |
| 1478 | void expectReceivedEventTraced(const std::unique_ptr<InputEvent>& event) { |
| 1479 | if (!event) { |
| 1480 | return; |
| 1481 | } |
| 1482 | |
| 1483 | switch (event->getType()) { |
| 1484 | case InputEventType::KEY: { |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 1485 | gVerifyingTrace->expectKeyDispatchTraced(static_cast<KeyEvent&>(*event), mInfo.id); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 1486 | break; |
| 1487 | } |
| 1488 | case InputEventType::MOTION: { |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 1489 | gVerifyingTrace->expectMotionDispatchTraced(static_cast<MotionEvent&>(*event), |
| 1490 | mInfo.id); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 1491 | break; |
| 1492 | } |
| 1493 | default: |
| 1494 | break; |
| 1495 | } |
| 1496 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1497 | }; |
| 1498 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1499 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1500 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1501 | class FakeMonitorReceiver { |
| 1502 | public: |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1503 | FakeMonitorReceiver(InputDispatcher& dispatcher, const std::string name, int32_t displayId) |
| 1504 | : mInputReceiver(*dispatcher.createInputMonitor(displayId, name, MONITOR_PID), name) {} |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1505 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1506 | sp<IBinder> getToken() { return mInputReceiver.getToken(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1507 | |
| 1508 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1509 | mInputReceiver.consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1510 | expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | std::optional<int32_t> receiveEvent() { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1514 | const auto [sequenceNum, _] = mInputReceiver.receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED); |
| 1515 | return sequenceNum; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1518 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver.finishEvent(consumeSeq); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1519 | |
| 1520 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1521 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 1522 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1526 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 1527 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1531 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, |
| 1532 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1536 | mInputReceiver.consumeMotionEvent( |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1537 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 1538 | WithDisplayId(expectedDisplayId), |
| 1539 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
| 1540 | } |
| 1541 | |
| 1542 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 1543 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1544 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1545 | mInputReceiver.consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT, |
| 1546 | /*expectedFlags=*/0); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1550 | mInputReceiver.consumeMotionEvent(matcher); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1553 | std::unique_ptr<MotionEvent> consumeMotion() { return mInputReceiver.consumeMotion(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1554 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 1555 | void assertNoEvents() { mInputReceiver.assertNoEvents(CONSUME_TIMEOUT_NO_EVENT_EXPECTED); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1556 | |
| 1557 | private: |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1558 | FakeInputReceiver mInputReceiver; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1559 | }; |
| 1560 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1561 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1562 | InputDispatcher& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1563 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1564 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1565 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1566 | bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1567 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1568 | KeyEvent event; |
| 1569 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1570 | |
| 1571 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1572 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1573 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 1574 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1575 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1576 | if (!allowKeyRepeat) { |
| 1577 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1578 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1579 | // Inject event until dispatch out. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1580 | return dispatcher.injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1581 | } |
| 1582 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1583 | static void assertInjectedKeyTimesOut(InputDispatcher& dispatcher) { |
| 1584 | InputEventInjectionResult result = |
| 1585 | injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
| 1586 | InputEventInjectionSync::WAIT_FOR_RESULT, CONSUME_TIMEOUT_NO_EVENT_EXPECTED); |
| 1587 | if (result != InputEventInjectionResult::TIMED_OUT) { |
| 1588 | FAIL() << "Injection should have timed out, but got " << ftl::enum_string(result); |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | static InputEventInjectionResult injectKeyDown(InputDispatcher& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1593 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1594 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1595 | } |
| 1596 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1597 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1598 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1599 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1600 | static InputEventInjectionResult injectKeyDownNoRepeat(InputDispatcher& dispatcher, |
| 1601 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1602 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1603 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1604 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1605 | } |
| 1606 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1607 | static InputEventInjectionResult injectKeyUp(InputDispatcher& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1608 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1609 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1610 | } |
| 1611 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1612 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1613 | InputDispatcher& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1614 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1615 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1616 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1617 | return dispatcher.injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1618 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1619 | } |
| 1620 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1621 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1622 | InputDispatcher& dispatcher, int32_t action, int32_t source, int32_t displayId, |
| 1623 | const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1624 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1625 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1626 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1627 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1628 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1629 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1630 | MotionEventBuilder motionBuilder = |
| 1631 | MotionEventBuilder(action, source) |
| 1632 | .displayId(displayId) |
| 1633 | .eventTime(eventTime) |
| 1634 | .rawXCursorPosition(cursorPosition.x) |
| 1635 | .rawYCursorPosition(cursorPosition.y) |
| 1636 | .pointer( |
| 1637 | PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y)); |
| 1638 | if (MotionEvent::getActionMasked(action) == ACTION_DOWN) { |
| 1639 | motionBuilder.downTime(eventTime); |
| 1640 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1641 | |
| 1642 | // Inject event until dispatch out. |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1643 | return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode, |
| 1644 | targetUid, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1645 | } |
| 1646 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1647 | static InputEventInjectionResult injectMotionDown(InputDispatcher& dispatcher, int32_t source, |
| 1648 | int32_t displayId, |
| 1649 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1650 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1653 | static InputEventInjectionResult injectMotionUp(InputDispatcher& dispatcher, int32_t source, |
| 1654 | int32_t displayId, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1655 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1656 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1659 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1660 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1661 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1662 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 1663 | AINPUT_SOURCE_KEYBOARD, displayId, POLICY_FLAG_PASS_TO_USER, action, |
| 1664 | /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1665 | |
| 1666 | return args; |
| 1667 | } |
| 1668 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1669 | static NotifyKeyArgs generateSystemShortcutArgs(int32_t action, |
| 1670 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1671 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1672 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1673 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 1674 | AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_C, KEY_C, |
| 1675 | AMETA_META_ON, currentTime); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1676 | |
| 1677 | return args; |
| 1678 | } |
| 1679 | |
| 1680 | static NotifyKeyArgs generateAssistantKeyArgs(int32_t action, |
| 1681 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1682 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1683 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1684 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 1685 | AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_ASSIST, |
| 1686 | KEY_ASSISTANT, AMETA_NONE, currentTime); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1687 | |
| 1688 | return args; |
| 1689 | } |
| 1690 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1691 | [[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
| 1692 | int32_t displayId, |
| 1693 | const std::vector<PointF>& points) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1694 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1695 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1696 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1697 | } |
| 1698 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1699 | PointerProperties pointerProperties[pointerCount]; |
| 1700 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1701 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1702 | for (size_t i = 0; i < pointerCount; i++) { |
| 1703 | pointerProperties[i].clear(); |
| 1704 | pointerProperties[i].id = i; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1705 | pointerProperties[i].toolType = ToolType::FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1706 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1707 | pointerCoords[i].clear(); |
| 1708 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1709 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1710 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1711 | |
| 1712 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1713 | // Define a valid motion event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1714 | NotifyMotionArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, source, |
| 1715 | displayId, POLICY_FLAG_PASS_TO_USER, action, /*actionButton=*/0, |
| 1716 | /*flags=*/0, AMETA_NONE, /*buttonState=*/0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1717 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1718 | pointerCoords, /*xPrecision=*/0, /*yPrecision=*/0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1719 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1720 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /*videoFrames=*/{}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1721 | |
| 1722 | return args; |
| 1723 | } |
| 1724 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1725 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1726 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1727 | } |
| 1728 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1729 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1730 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1731 | } |
| 1732 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1733 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1734 | const PointerCaptureRequest& request) { |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1735 | return NotifyPointerCaptureChangedArgs(InputEvent::nextId(), systemTime(SYSTEM_TIME_MONOTONIC), |
| 1736 | request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1737 | } |
| 1738 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 1739 | } // namespace |
| 1740 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1741 | /** |
| 1742 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1743 | * broken channel. |
| 1744 | */ |
| 1745 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1746 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1747 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1748 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1749 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1750 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1751 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1752 | |
| 1753 | // Window closes its channel, but the window remains. |
| 1754 | window->destroyReceiver(); |
| 1755 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1756 | } |
| 1757 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1758 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1759 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1760 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1761 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1762 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1763 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1764 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1765 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1766 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1767 | |
| 1768 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1769 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1770 | } |
| 1771 | |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 1772 | using InputDispatcherDeathTest = InputDispatcherTest; |
| 1773 | |
| 1774 | /** |
| 1775 | * When 'onWindowInfosChanged' arguments contain a duplicate entry for the same window, dispatcher |
| 1776 | * should crash. |
| 1777 | */ |
| 1778 | TEST_F(InputDispatcherDeathTest, DuplicateWindowInfosAbortDispatcher) { |
| 1779 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
| 1780 | ScopedSilentDeath _silentDeath; |
| 1781 | |
| 1782 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1783 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1784 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1785 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 1786 | {{*window->getInfo(), *window->getInfo()}, {}, 0, 0}), |
| 1787 | "Incorrect WindowInfosUpdate provided"); |
| 1788 | } |
| 1789 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1790 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1791 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1792 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1793 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1794 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1795 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1796 | // Inject a MotionEvent to an unknown display. |
| 1797 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1798 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1799 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1800 | |
| 1801 | // Window should receive motion event. |
| 1802 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1803 | } |
| 1804 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1805 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1806 | * Calling onWindowInfosChanged once should not cause any issues. |
| 1807 | * This test serves as a sanity check for the next test, where onWindowInfosChanged is |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1808 | * called twice. |
| 1809 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1810 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1811 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1812 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1813 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1814 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1815 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1816 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1817 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1818 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1819 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1820 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1821 | |
| 1822 | // Window should receive motion event. |
| 1823 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1824 | } |
| 1825 | |
| 1826 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1827 | * Calling onWindowInfosChanged twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1828 | */ |
| 1829 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1830 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1831 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1832 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1833 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1834 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1835 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1836 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1837 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1838 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1839 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1840 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1841 | |
| 1842 | // Window should receive motion event. |
| 1843 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1844 | } |
| 1845 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1846 | // The foreground window should receive the first touch down event. |
| 1847 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1848 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1849 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1850 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1851 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1852 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1853 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1854 | mDispatcher->onWindowInfosChanged( |
| 1855 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1856 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1857 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1858 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1859 | |
| 1860 | // 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] | 1861 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1862 | windowSecond->assertNoEvents(); |
| 1863 | } |
| 1864 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1865 | /** |
| 1866 | * Two windows: A top window, and a wallpaper behind the window. |
| 1867 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1868 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1869 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1870 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1871 | */ |
| 1872 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1873 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1874 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1875 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1876 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1877 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1878 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1879 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1880 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1881 | mDispatcher->onWindowInfosChanged( |
| 1882 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1883 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1884 | injectMotionEvent(*mDispatcher, |
| 1885 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1886 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(200)) |
| 1887 | .build())) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1888 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1889 | |
| 1890 | // Both foreground window and its wallpaper should receive the touch down |
| 1891 | foregroundWindow->consumeMotionDown(); |
| 1892 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1893 | |
| 1894 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1895 | injectMotionEvent(*mDispatcher, |
| 1896 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1897 | .pointer(PointerBuilder(0, ToolType::FINGER).x(110).y(200)) |
| 1898 | .build())) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1899 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1900 | |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1901 | foregroundWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1902 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1903 | |
| 1904 | // Now the foreground window goes away, but the wallpaper stays |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1905 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1906 | foregroundWindow->consumeMotionCancel(); |
| 1907 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1908 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1909 | } |
| 1910 | |
| 1911 | /** |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1912 | * Two fingers down on the window, and lift off the first finger. |
| 1913 | * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event |
| 1914 | * contains a single pointer. |
| 1915 | */ |
| 1916 | TEST_F(InputDispatcherTest, CancelAfterPointer0Up) { |
| 1917 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1918 | sp<FakeWindowHandle> window = |
| 1919 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1920 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1921 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1922 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1923 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1924 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1925 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1926 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1927 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1928 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1929 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1930 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1931 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1932 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1933 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1934 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1935 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1936 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 1937 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1938 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 1939 | |
| 1940 | // Remove the window. The gesture should be canceled |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1941 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1942 | const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}}; |
| 1943 | window->consumeMotionEvent( |
| 1944 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers))); |
| 1945 | } |
| 1946 | |
| 1947 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1948 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1949 | * with the following differences: |
| 1950 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1951 | * clean up the connection. |
| 1952 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1953 | * Ensure that there's no crash in the dispatcher. |
| 1954 | */ |
| 1955 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1956 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1957 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1958 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1959 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1960 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1961 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1962 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1963 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1964 | mDispatcher->onWindowInfosChanged( |
| 1965 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1966 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1967 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1968 | {100, 200})) |
| 1969 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1970 | |
| 1971 | // Both foreground window and its wallpaper should receive the touch down |
| 1972 | foregroundWindow->consumeMotionDown(); |
| 1973 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1974 | |
| 1975 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1976 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1977 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1978 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1979 | |
| 1980 | foregroundWindow->consumeMotionMove(); |
| 1981 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1982 | |
| 1983 | // Wallpaper closes its channel, but the window remains. |
| 1984 | wallpaperWindow->destroyReceiver(); |
| 1985 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 1986 | |
| 1987 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 1988 | // is no longer valid. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1989 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1990 | foregroundWindow->consumeMotionCancel(); |
| 1991 | } |
| 1992 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1993 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 1994 | public ::testing::WithParamInterface<bool> {}; |
| 1995 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 1996 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1997 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1998 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1999 | * The top window gets a multitouch gesture. |
| 2000 | * Ensure that wallpaper gets the same gesture. |
| 2001 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2002 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2003 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2004 | sp<FakeWindowHandle> foregroundWindow = |
| 2005 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 2006 | foregroundWindow->setDupTouchToWallpaper(true); |
| 2007 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2008 | |
| 2009 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2010 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2011 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2012 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2013 | mDispatcher->onWindowInfosChanged( |
| 2014 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2015 | |
| 2016 | // Touch down on top window |
| 2017 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2018 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2019 | {100, 100})) |
| 2020 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2021 | |
| 2022 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2023 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2024 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2025 | |
| 2026 | // Second finger down on the top window |
| 2027 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2028 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2029 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2030 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2031 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2032 | .build(); |
| 2033 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2034 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2035 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2036 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2037 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2038 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
| 2039 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2040 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2041 | |
| 2042 | const MotionEvent secondFingerUpEvent = |
| 2043 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 2044 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2045 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2046 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2047 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2048 | .build(); |
| 2049 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2050 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2051 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2052 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2053 | foregroundWindow->consumeMotionPointerUp(0); |
| 2054 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2055 | |
| 2056 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2057 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 2058 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2059 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2060 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2061 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 2062 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 2063 | .x(100) |
| 2064 | .y(100)) |
| 2065 | .build(), |
| 2066 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2067 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2068 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2069 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | /** |
| 2073 | * Two windows: a window on the left and window on the right. |
| 2074 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 2075 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 2076 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 2077 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 2078 | * ACTION_POINTER_DOWN(1). |
| 2079 | */ |
| 2080 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 2081 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2082 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2083 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2084 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2085 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2086 | |
| 2087 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2088 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2089 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2090 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2091 | |
| 2092 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2093 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2094 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2095 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2096 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2097 | mDispatcher->onWindowInfosChanged( |
| 2098 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 2099 | {}, |
| 2100 | 0, |
| 2101 | 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2102 | |
| 2103 | // Touch down on left window |
| 2104 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2105 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2106 | {100, 100})) |
| 2107 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2108 | |
| 2109 | // Both foreground window and its wallpaper should receive the touch down |
| 2110 | leftWindow->consumeMotionDown(); |
| 2111 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2112 | |
| 2113 | // Second finger down on the right window |
| 2114 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2115 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2116 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2117 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2118 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2119 | .build(); |
| 2120 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2121 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2122 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2123 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2124 | |
| 2125 | leftWindow->consumeMotionMove(); |
| 2126 | // Since the touch is split, right window gets ACTION_DOWN |
| 2127 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2128 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2129 | expectedWallpaperFlags); |
| 2130 | |
| 2131 | // Now, leftWindow, which received the first finger, disappears. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2132 | mDispatcher->onWindowInfosChanged( |
| 2133 | {{*rightWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2134 | leftWindow->consumeMotionCancel(); |
| 2135 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 2136 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2137 | |
| 2138 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 2139 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 2140 | const MotionEvent secondFingerMoveEvent = |
| 2141 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2142 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2143 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2144 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2145 | .build(); |
| 2146 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2147 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2148 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 2149 | rightWindow->consumeMotionMove(); |
| 2150 | |
| 2151 | leftWindow->assertNoEvents(); |
| 2152 | rightWindow->assertNoEvents(); |
| 2153 | wallpaperWindow->assertNoEvents(); |
| 2154 | } |
| 2155 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2156 | /** |
| 2157 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 2158 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 2159 | * The right window should receive ACTION_DOWN. |
| 2160 | */ |
| 2161 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2162 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2163 | sp<FakeWindowHandle> leftWindow = |
| 2164 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2165 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2166 | leftWindow->setDupTouchToWallpaper(true); |
| 2167 | leftWindow->setSlippery(true); |
| 2168 | |
| 2169 | sp<FakeWindowHandle> rightWindow = |
| 2170 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2171 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2172 | |
| 2173 | sp<FakeWindowHandle> wallpaperWindow = |
| 2174 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2175 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2176 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2177 | mDispatcher->onWindowInfosChanged( |
| 2178 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 2179 | {}, |
| 2180 | 0, |
| 2181 | 0}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2182 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2183 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2184 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2185 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2186 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2187 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2188 | |
| 2189 | // Both foreground window and its wallpaper should receive the touch down |
| 2190 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2191 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2192 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2193 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2194 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2195 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2196 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2197 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2198 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2199 | leftWindow->consumeMotionCancel(); |
| 2200 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2201 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2204 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2205 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2206 | * interactive, it might stop sending this flag. |
| 2207 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 2208 | * to have a consistent input stream. |
| 2209 | * |
| 2210 | * Test procedure: |
| 2211 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 2212 | * DOWN (new gesture). |
| 2213 | * |
| 2214 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 2215 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 2216 | * |
| 2217 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 2218 | * regular window below) to emulate the actual situation where it happens on the device. |
| 2219 | */ |
| 2220 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 2221 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2222 | sp<FakeWindowHandle> spyWindow = |
| 2223 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2224 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2225 | spyWindow->setTrustedOverlay(true); |
| 2226 | spyWindow->setSpy(true); |
| 2227 | |
| 2228 | sp<FakeWindowHandle> window = |
| 2229 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2230 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2231 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2232 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2233 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2234 | |
| 2235 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2236 | mDispatcher->notifyMotion( |
| 2237 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2238 | .deviceId(touchDeviceId) |
| 2239 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2240 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2241 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2242 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2243 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2244 | .deviceId(touchDeviceId) |
| 2245 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2246 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2247 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2248 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2249 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2250 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2251 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2252 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2253 | |
| 2254 | // Cancel the current gesture. Send the cancel without the default policy flags. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2255 | mDispatcher->notifyMotion( |
| 2256 | MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 2257 | .deviceId(touchDeviceId) |
| 2258 | .policyFlags(0) |
| 2259 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2260 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2261 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2262 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2263 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2264 | |
| 2265 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 2266 | // 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] | 2267 | mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2268 | |
| 2269 | // Start new gesture |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2270 | mDispatcher->notifyMotion( |
| 2271 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2272 | .deviceId(touchDeviceId) |
| 2273 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2274 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2275 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2276 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2277 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2278 | |
| 2279 | // No more events |
| 2280 | spyWindow->assertNoEvents(); |
| 2281 | window->assertNoEvents(); |
| 2282 | } |
| 2283 | |
| 2284 | /** |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 2285 | * Same as the above 'TwoPointerCancelInconsistentPolicy' test, but for hovers. |
| 2286 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2287 | * interactive, it might stop sending this flag. |
| 2288 | * We've already ensured the consistency of the touch event in this case, and we should also ensure |
| 2289 | * the consistency of the hover event in this case. |
| 2290 | * |
| 2291 | * Test procedure: |
| 2292 | * HOVER_ENTER -> HOVER_MOVE -> (stop sending POLICY_FLAG_PASS_TO_USER) -> HOVER_EXIT |
| 2293 | * HOVER_ENTER -> HOVER_MOVE -> HOVER_EXIT |
| 2294 | * |
| 2295 | * We expect to receive two full streams of hover events. |
| 2296 | */ |
| 2297 | TEST_F(InputDispatcherTest, HoverEventInconsistentPolicy) { |
| 2298 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2299 | |
| 2300 | sp<FakeWindowHandle> window = |
| 2301 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2302 | window->setFrame(Rect(0, 0, 300, 300)); |
| 2303 | |
| 2304 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2305 | |
| 2306 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2307 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2308 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2309 | .build()); |
| 2310 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2311 | |
| 2312 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2313 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2314 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2315 | .build()); |
| 2316 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2317 | |
| 2318 | // Send hover exit without the default policy flags. |
| 2319 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2320 | .policyFlags(0) |
| 2321 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2322 | .build()); |
| 2323 | |
| 2324 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2325 | |
| 2326 | // Send a simple hover event stream, ensure dispatcher not crashed and window can receive |
| 2327 | // right event. |
| 2328 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2329 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2330 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(200).y(201)) |
| 2331 | .build()); |
| 2332 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2333 | |
| 2334 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2335 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2336 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 2337 | .build()); |
| 2338 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2339 | |
| 2340 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2341 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2342 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 2343 | .build()); |
| 2344 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2345 | } |
| 2346 | |
| 2347 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2348 | * Two windows: a window on the left and a window on the right. |
| 2349 | * Mouse is hovered from the right window into the left window. |
| 2350 | * Next, we tap on the left window, where the cursor was last seen. |
| 2351 | * The second tap is done onto the right window. |
| 2352 | * The mouse and tap are from two different devices. |
| 2353 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 2354 | * explicitly helps during debugging. |
| 2355 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2356 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 2357 | */ |
| 2358 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 2359 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2360 | sp<FakeWindowHandle> leftWindow = |
| 2361 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2362 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2363 | |
| 2364 | sp<FakeWindowHandle> rightWindow = |
| 2365 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2366 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2367 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2368 | mDispatcher->onWindowInfosChanged( |
| 2369 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2370 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 2371 | // stale. |
| 2372 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2373 | const int32_t mouseDeviceId = 6; |
| 2374 | const int32_t touchDeviceId = 4; |
| 2375 | // Move the cursor from right |
| 2376 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2377 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2378 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2379 | AINPUT_SOURCE_MOUSE) |
| 2380 | .deviceId(mouseDeviceId) |
| 2381 | .downTime(baseTime + 10) |
| 2382 | .eventTime(baseTime + 20) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2383 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2384 | .build())); |
| 2385 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2386 | |
| 2387 | // .. to the left window |
| 2388 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2389 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2390 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2391 | AINPUT_SOURCE_MOUSE) |
| 2392 | .deviceId(mouseDeviceId) |
| 2393 | .downTime(baseTime + 10) |
| 2394 | .eventTime(baseTime + 30) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2395 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2396 | .build())); |
| 2397 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2398 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2399 | // Now tap the left window |
| 2400 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2401 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2402 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2403 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2404 | .deviceId(touchDeviceId) |
| 2405 | .downTime(baseTime + 40) |
| 2406 | .eventTime(baseTime + 40) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2407 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2408 | .build())); |
| 2409 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2410 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2411 | |
| 2412 | // release tap |
| 2413 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2414 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2415 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2416 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2417 | .deviceId(touchDeviceId) |
| 2418 | .downTime(baseTime + 40) |
| 2419 | .eventTime(baseTime + 50) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2420 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2421 | .build())); |
| 2422 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2423 | |
| 2424 | // Tap the window on the right |
| 2425 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2426 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2427 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2428 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2429 | .deviceId(touchDeviceId) |
| 2430 | .downTime(baseTime + 60) |
| 2431 | .eventTime(baseTime + 60) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2432 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2433 | .build())); |
| 2434 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2435 | |
| 2436 | // release tap |
| 2437 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2438 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2439 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2440 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2441 | .deviceId(touchDeviceId) |
| 2442 | .downTime(baseTime + 60) |
| 2443 | .eventTime(baseTime + 70) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2444 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2445 | .build())); |
| 2446 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2447 | |
| 2448 | // No more events |
| 2449 | leftWindow->assertNoEvents(); |
| 2450 | rightWindow->assertNoEvents(); |
| 2451 | } |
| 2452 | |
| 2453 | /** |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2454 | * Start hovering in a window. While this hover is still active, make another window appear on top. |
| 2455 | * The top, obstructing window has no input channel, so it's not supposed to receive input. |
| 2456 | * While the top window is present, the hovering is stopped. |
| 2457 | * Later, hovering gets resumed again. |
| 2458 | * Ensure that new hover gesture is handled correctly. |
| 2459 | * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly |
| 2460 | * to the window that's currently being hovered over. |
| 2461 | */ |
| 2462 | TEST_F(InputDispatcherTest, HoverWhileWindowAppears) { |
| 2463 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2464 | sp<FakeWindowHandle> window = |
| 2465 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2466 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2467 | |
| 2468 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2469 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2470 | |
| 2471 | // Start hovering in the window |
| 2472 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2473 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2474 | .build()); |
| 2475 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2476 | |
| 2477 | // Now, an obscuring window appears! |
| 2478 | sp<FakeWindowHandle> obscuringWindow = |
| 2479 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2480 | ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 2481 | /*createInputChannel=*/false); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2482 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2483 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2484 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2485 | obscuringWindow->setNoInputChannel(true); |
| 2486 | obscuringWindow->setFocusable(false); |
| 2487 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2488 | mDispatcher->onWindowInfosChanged( |
| 2489 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2490 | |
| 2491 | // While this new obscuring window is present, the hovering is stopped |
| 2492 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2493 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2494 | .build()); |
| 2495 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2496 | |
| 2497 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2498 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2499 | |
| 2500 | // And a new hover gesture starts. |
| 2501 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2502 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2503 | .build()); |
| 2504 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2505 | } |
| 2506 | |
| 2507 | /** |
| 2508 | * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to |
| 2509 | * the obscuring window. |
| 2510 | */ |
| 2511 | TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) { |
| 2512 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2513 | sp<FakeWindowHandle> window = |
| 2514 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2515 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2516 | |
| 2517 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2518 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2519 | |
| 2520 | // Start hovering in the window |
| 2521 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2522 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2523 | .build()); |
| 2524 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2525 | |
| 2526 | // Now, an obscuring window appears! |
| 2527 | sp<FakeWindowHandle> obscuringWindow = |
| 2528 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2529 | ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 2530 | /*createInputChannel=*/false); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2531 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2532 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2533 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2534 | obscuringWindow->setNoInputChannel(true); |
| 2535 | obscuringWindow->setFocusable(false); |
| 2536 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2537 | mDispatcher->onWindowInfosChanged( |
| 2538 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2539 | |
| 2540 | // While this new obscuring window is present, the hovering continues. The event can't go to the |
| 2541 | // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window. |
| 2542 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2543 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2544 | .build()); |
| 2545 | obscuringWindow->assertNoEvents(); |
| 2546 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2547 | |
| 2548 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2549 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2550 | |
| 2551 | // Hovering continues in the same position. The hovering pointer re-enters the bottom window, |
| 2552 | // so it should generate a HOVER_ENTER |
| 2553 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2554 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2555 | .build()); |
| 2556 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2557 | |
| 2558 | // Now the MOVE should be getting dispatched normally |
| 2559 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2560 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2561 | .build()); |
| 2562 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2563 | } |
| 2564 | |
| 2565 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2566 | * Hover mouse over a window, and then send ACTION_SCROLL. Ensure both the hover and the scroll |
| 2567 | * events are delivered to the window. |
| 2568 | */ |
| 2569 | TEST_F(InputDispatcherTest, HoverMoveAndScroll) { |
| 2570 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2571 | sp<FakeWindowHandle> window = |
| 2572 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2573 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2574 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2575 | |
| 2576 | // Start hovering in the window |
| 2577 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2578 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 2579 | .build()); |
| 2580 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2581 | |
| 2582 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 2583 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2584 | .build()); |
| 2585 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2586 | |
| 2587 | // Scroll with the mouse |
| 2588 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_SCROLL, AINPUT_SOURCE_MOUSE) |
| 2589 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2590 | .build()); |
| 2591 | window->consumeMotionEvent(WithMotionAction(ACTION_SCROLL)); |
| 2592 | } |
| 2593 | |
| 2594 | using InputDispatcherMultiDeviceTest = InputDispatcherTest; |
| 2595 | |
| 2596 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2597 | * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that |
| 2598 | * touch is dropped, because stylus should be preferred over touch. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2599 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2600 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2601 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2602 | sp<FakeWindowHandle> window = |
| 2603 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2604 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2605 | |
| 2606 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2607 | |
| 2608 | constexpr int32_t touchDeviceId = 4; |
| 2609 | constexpr int32_t stylusDeviceId = 2; |
| 2610 | |
| 2611 | // Stylus down |
| 2612 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2613 | .deviceId(stylusDeviceId) |
| 2614 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2615 | .build()); |
| 2616 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2617 | |
| 2618 | // Touch down |
| 2619 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2620 | .deviceId(touchDeviceId) |
| 2621 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2622 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2623 | |
| 2624 | // Touch move |
| 2625 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2626 | .deviceId(touchDeviceId) |
| 2627 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2628 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2629 | // Touch is ignored because stylus is already down |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2630 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2631 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2632 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2633 | .deviceId(stylusDeviceId) |
| 2634 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2635 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2636 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2637 | WithCoords(101, 111))); |
| 2638 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2639 | window->assertNoEvents(); |
| 2640 | } |
| 2641 | |
| 2642 | /** |
| 2643 | * One window and one spy window. Stylus down on the window. Next, touch from another device goes |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2644 | * down. Ensure that touch is dropped, because stylus should be preferred over touch. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2645 | * Similar test as above, but with added SPY window. |
| 2646 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2647 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2648 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2649 | sp<FakeWindowHandle> window = |
| 2650 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2651 | sp<FakeWindowHandle> spyWindow = |
| 2652 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2653 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2654 | spyWindow->setTrustedOverlay(true); |
| 2655 | spyWindow->setSpy(true); |
| 2656 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2657 | |
| 2658 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 2659 | |
| 2660 | constexpr int32_t touchDeviceId = 4; |
| 2661 | constexpr int32_t stylusDeviceId = 2; |
| 2662 | |
| 2663 | // Stylus down |
| 2664 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2665 | .deviceId(stylusDeviceId) |
| 2666 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2667 | .build()); |
| 2668 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2669 | spyWindow->consumeMotionEvent( |
| 2670 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2671 | |
| 2672 | // Touch down |
| 2673 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2674 | .deviceId(touchDeviceId) |
| 2675 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2676 | .build()); |
| 2677 | |
| 2678 | // Touch move |
| 2679 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2680 | .deviceId(touchDeviceId) |
| 2681 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2682 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2683 | |
| 2684 | // Touch is ignored because stylus is already down |
| 2685 | |
| 2686 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2687 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2688 | .deviceId(stylusDeviceId) |
| 2689 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2690 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2691 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2692 | WithCoords(101, 111))); |
| 2693 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2694 | WithCoords(101, 111))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2695 | |
| 2696 | window->assertNoEvents(); |
| 2697 | spyWindow->assertNoEvents(); |
| 2698 | } |
| 2699 | |
| 2700 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2701 | * One window. Stylus hover on the window. Next, touch from another device goes down. Ensure that |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2702 | * touch is dropped, because stylus hover takes precedence. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2703 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2704 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2705 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2706 | sp<FakeWindowHandle> window = |
| 2707 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2708 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2709 | |
| 2710 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2711 | |
| 2712 | constexpr int32_t touchDeviceId = 4; |
| 2713 | constexpr int32_t stylusDeviceId = 2; |
| 2714 | |
| 2715 | // Stylus down on the window |
| 2716 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2717 | .deviceId(stylusDeviceId) |
| 2718 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2719 | .build()); |
| 2720 | window->consumeMotionEvent( |
| 2721 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2722 | |
| 2723 | // Touch down on window |
| 2724 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2725 | .deviceId(touchDeviceId) |
| 2726 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2727 | .build()); |
| 2728 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2729 | .deviceId(touchDeviceId) |
| 2730 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2731 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2732 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2733 | // Touch is ignored because stylus is hovering |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2734 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2735 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2736 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2737 | .deviceId(stylusDeviceId) |
| 2738 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2739 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2740 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2741 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2742 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2743 | // and subsequent touches continue to be ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2744 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2745 | .deviceId(touchDeviceId) |
| 2746 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2747 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2748 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | /** |
| 2752 | * One window. Touch down on the window. Then, stylus hover on the window from another device. |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2753 | * Ensure that touch is canceled, because stylus hover should take precedence. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2754 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2755 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusHover) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2756 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2757 | sp<FakeWindowHandle> window = |
| 2758 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2759 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2760 | |
| 2761 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2762 | |
| 2763 | constexpr int32_t touchDeviceId = 4; |
| 2764 | constexpr int32_t stylusDeviceId = 2; |
| 2765 | |
| 2766 | // Touch down on window |
| 2767 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2768 | .deviceId(touchDeviceId) |
| 2769 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2770 | .build()); |
| 2771 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2772 | .deviceId(touchDeviceId) |
| 2773 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2774 | .build()); |
| 2775 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2776 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2777 | |
| 2778 | // Stylus hover on the window |
| 2779 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2780 | .deviceId(stylusDeviceId) |
| 2781 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2782 | .build()); |
| 2783 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2784 | .deviceId(stylusDeviceId) |
| 2785 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2786 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2787 | // Stylus hover movement causes touch to be canceled |
| 2788 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 2789 | WithCoords(141, 146))); |
| 2790 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 2791 | WithDeviceId(stylusDeviceId), WithCoords(100, 110))); |
| 2792 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2793 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2794 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2795 | // Subsequent touch movements are ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2796 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2797 | .deviceId(touchDeviceId) |
| 2798 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2799 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2800 | |
| 2801 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2802 | } |
| 2803 | |
| 2804 | /** |
| 2805 | * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that |
| 2806 | * the latest stylus takes over. That is, old stylus should be canceled and the new stylus should |
| 2807 | * become active. |
| 2808 | */ |
| 2809 | TEST_F(InputDispatcherMultiDeviceTest, LatestStylusWins) { |
| 2810 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2811 | sp<FakeWindowHandle> window = |
| 2812 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2813 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2814 | |
| 2815 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2816 | |
| 2817 | constexpr int32_t stylusDeviceId1 = 3; |
| 2818 | constexpr int32_t stylusDeviceId2 = 5; |
| 2819 | |
| 2820 | // Touch down on window |
| 2821 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2822 | .deviceId(stylusDeviceId1) |
| 2823 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100)) |
| 2824 | .build()); |
| 2825 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2826 | .deviceId(stylusDeviceId1) |
| 2827 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2828 | .build()); |
| 2829 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1))); |
| 2830 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2831 | |
| 2832 | // Second stylus down |
| 2833 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2834 | .deviceId(stylusDeviceId2) |
| 2835 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10)) |
| 2836 | .build()); |
| 2837 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2838 | .deviceId(stylusDeviceId2) |
| 2839 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11)) |
| 2840 | .build()); |
| 2841 | |
| 2842 | // First stylus is canceled, second one takes over. |
| 2843 | window->consumeMotionEvent( |
| 2844 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId1))); |
| 2845 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2))); |
| 2846 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2))); |
| 2847 | |
| 2848 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2849 | .deviceId(stylusDeviceId1) |
| 2850 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2851 | .build()); |
| 2852 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2853 | window->assertNoEvents(); |
| 2854 | } |
| 2855 | |
| 2856 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2857 | * One window. Touch down on the window. Then, stylus down on the window from another device. |
| 2858 | * Ensure that is canceled, because stylus down should be preferred over touch. |
| 2859 | */ |
| 2860 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusDown) { |
| 2861 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2862 | sp<FakeWindowHandle> window = |
| 2863 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2864 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2865 | |
| 2866 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2867 | |
| 2868 | constexpr int32_t touchDeviceId = 4; |
| 2869 | constexpr int32_t stylusDeviceId = 2; |
| 2870 | |
| 2871 | // Touch down on window |
| 2872 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2873 | .deviceId(touchDeviceId) |
| 2874 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2875 | .build()); |
| 2876 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2877 | .deviceId(touchDeviceId) |
| 2878 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2879 | .build()); |
| 2880 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2881 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2882 | |
| 2883 | // Stylus down on the window |
| 2884 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2885 | .deviceId(stylusDeviceId) |
| 2886 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2887 | .build()); |
| 2888 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 2889 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2890 | |
| 2891 | // Subsequent stylus movements are delivered correctly |
| 2892 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2893 | .deviceId(stylusDeviceId) |
| 2894 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2895 | .build()); |
| 2896 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2897 | WithCoords(101, 111))); |
| 2898 | } |
| 2899 | |
| 2900 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2901 | * Two windows: a window on the left and a window on the right. |
| 2902 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2903 | * down. Then, on the left window, also place second touch pointer down. |
| 2904 | * This test tries to reproduce a crash. |
| 2905 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2906 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2907 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2908 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2909 | sp<FakeWindowHandle> leftWindow = |
| 2910 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2911 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2912 | |
| 2913 | sp<FakeWindowHandle> rightWindow = |
| 2914 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2915 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2916 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2917 | mDispatcher->onWindowInfosChanged( |
| 2918 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2919 | |
| 2920 | const int32_t touchDeviceId = 4; |
| 2921 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2922 | |
| 2923 | // Start hovering over the left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2924 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2925 | .deviceId(mouseDeviceId) |
| 2926 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2927 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2928 | leftWindow->consumeMotionEvent( |
| 2929 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2930 | |
| 2931 | // Mouse down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2932 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2933 | .deviceId(mouseDeviceId) |
| 2934 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2935 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2936 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2937 | |
| 2938 | leftWindow->consumeMotionEvent( |
| 2939 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2940 | leftWindow->consumeMotionEvent( |
| 2941 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2942 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2943 | mDispatcher->notifyMotion( |
| 2944 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2945 | .deviceId(mouseDeviceId) |
| 2946 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2947 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2948 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2949 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2950 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2951 | |
| 2952 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2953 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2954 | .deviceId(touchDeviceId) |
| 2955 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2956 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2957 | leftWindow->assertNoEvents(); |
| 2958 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2959 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2960 | |
| 2961 | // Second touch pointer down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2962 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2963 | .deviceId(touchDeviceId) |
| 2964 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2965 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2966 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2967 | // Since this is now a new splittable pointer going down on the left window, and it's coming |
| 2968 | // from a different device, the current gesture in the left window (pointer down) should first |
| 2969 | // be canceled. |
| 2970 | leftWindow->consumeMotionEvent( |
| 2971 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2972 | leftWindow->consumeMotionEvent( |
| 2973 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2974 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2975 | // current implementation. |
| 2976 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2977 | rightWindow->consumeMotionEvent( |
| 2978 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2979 | |
| 2980 | leftWindow->assertNoEvents(); |
| 2981 | rightWindow->assertNoEvents(); |
| 2982 | } |
| 2983 | |
| 2984 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2985 | * Two windows: a window on the left and a window on the right. |
| 2986 | * Mouse is hovered on the left window and stylus is hovered on the right window. |
| 2987 | */ |
| 2988 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHover) { |
| 2989 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2990 | sp<FakeWindowHandle> leftWindow = |
| 2991 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2992 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2993 | |
| 2994 | sp<FakeWindowHandle> rightWindow = |
| 2995 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2996 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2997 | |
| 2998 | mDispatcher->onWindowInfosChanged( |
| 2999 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3000 | |
| 3001 | const int32_t stylusDeviceId = 3; |
| 3002 | const int32_t mouseDeviceId = 6; |
| 3003 | |
| 3004 | // Start hovering over the left window |
| 3005 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3006 | .deviceId(mouseDeviceId) |
| 3007 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 3008 | .build()); |
| 3009 | leftWindow->consumeMotionEvent( |
| 3010 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3011 | |
| 3012 | // Stylus hovered on right window |
| 3013 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3014 | .deviceId(stylusDeviceId) |
| 3015 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(300).y(100)) |
| 3016 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3017 | rightWindow->consumeMotionEvent( |
| 3018 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3019 | |
| 3020 | // Subsequent HOVER_MOVE events are dispatched correctly. |
| 3021 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 3022 | .deviceId(mouseDeviceId) |
| 3023 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 3024 | .build()); |
| 3025 | leftWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3026 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3027 | |
| 3028 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3029 | .deviceId(stylusDeviceId) |
| 3030 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(310).y(110)) |
| 3031 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3032 | rightWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3033 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3034 | |
| 3035 | leftWindow->assertNoEvents(); |
| 3036 | rightWindow->assertNoEvents(); |
| 3037 | } |
| 3038 | |
| 3039 | /** |
| 3040 | * Three windows: a window on the left and a window on the right. |
| 3041 | * And a spy window that's positioned above all of them. |
| 3042 | * Stylus down on the left window and remains down. Touch goes down on the right and remains down. |
| 3043 | * Check the stream that's received by the spy. |
| 3044 | */ |
| 3045 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy) { |
| 3046 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3047 | |
| 3048 | sp<FakeWindowHandle> spyWindow = |
| 3049 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3050 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 3051 | spyWindow->setTrustedOverlay(true); |
| 3052 | spyWindow->setSpy(true); |
| 3053 | |
| 3054 | sp<FakeWindowHandle> leftWindow = |
| 3055 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 3056 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3057 | |
| 3058 | sp<FakeWindowHandle> rightWindow = |
| 3059 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 3060 | |
| 3061 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3062 | |
| 3063 | mDispatcher->onWindowInfosChanged( |
| 3064 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3065 | |
| 3066 | const int32_t stylusDeviceId = 1; |
| 3067 | const int32_t touchDeviceId = 2; |
| 3068 | |
| 3069 | // Stylus down on the left window |
| 3070 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3071 | .deviceId(stylusDeviceId) |
| 3072 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 3073 | .build()); |
| 3074 | leftWindow->consumeMotionEvent( |
| 3075 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 3076 | spyWindow->consumeMotionEvent( |
| 3077 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 3078 | |
| 3079 | // Touch down on the right window |
| 3080 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3081 | .deviceId(touchDeviceId) |
| 3082 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3083 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3084 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3085 | rightWindow->consumeMotionEvent( |
| 3086 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3087 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3088 | // Spy window does not receive touch events, because stylus events take precedence, and it |
| 3089 | // already has an active stylus gesture. |
| 3090 | |
| 3091 | // Stylus movements continue. They should be delivered to the left window and to the spy window |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3092 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 3093 | .deviceId(stylusDeviceId) |
| 3094 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 3095 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3096 | leftWindow->consumeMotionEvent( |
| 3097 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 3098 | spyWindow->consumeMotionEvent( |
| 3099 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3100 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3101 | // Further MOVE events keep going to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3102 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3103 | .deviceId(touchDeviceId) |
| 3104 | .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110)) |
| 3105 | .build()); |
| 3106 | rightWindow->consumeMotionEvent( |
| 3107 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3108 | |
| 3109 | spyWindow->assertNoEvents(); |
| 3110 | leftWindow->assertNoEvents(); |
| 3111 | rightWindow->assertNoEvents(); |
| 3112 | } |
| 3113 | |
| 3114 | /** |
| 3115 | * Three windows: a window on the left, a window on the right, and a spy window positioned above |
| 3116 | * both. |
| 3117 | * Check hover in left window and touch down in the right window. |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3118 | * At first, spy should receive hover. Spy shouldn't receive touch while stylus is hovering. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3119 | * At the same time, left and right should be getting independent streams of hovering and touch, |
| 3120 | * respectively. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3121 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3122 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverBlocksTouchWithSpy) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3123 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3124 | |
| 3125 | sp<FakeWindowHandle> spyWindow = |
| 3126 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3127 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 3128 | spyWindow->setTrustedOverlay(true); |
| 3129 | spyWindow->setSpy(true); |
| 3130 | |
| 3131 | sp<FakeWindowHandle> leftWindow = |
| 3132 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 3133 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3134 | |
| 3135 | sp<FakeWindowHandle> rightWindow = |
| 3136 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 3137 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3138 | |
| 3139 | mDispatcher->onWindowInfosChanged( |
| 3140 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3141 | |
| 3142 | const int32_t stylusDeviceId = 1; |
| 3143 | const int32_t touchDeviceId = 2; |
| 3144 | |
| 3145 | // Stylus hover on the left window |
| 3146 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3147 | .deviceId(stylusDeviceId) |
| 3148 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 3149 | .build()); |
| 3150 | leftWindow->consumeMotionEvent( |
| 3151 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3152 | spyWindow->consumeMotionEvent( |
| 3153 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3154 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3155 | // Touch down on the right window. Spy doesn't receive this touch because it already has |
| 3156 | // stylus hovering there. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3157 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3158 | .deviceId(touchDeviceId) |
| 3159 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3160 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3161 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3162 | spyWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3163 | rightWindow->consumeMotionEvent( |
| 3164 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3165 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3166 | // Stylus movements continue. They should be delivered to the left window and the spy. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3167 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3168 | .deviceId(stylusDeviceId) |
| 3169 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 3170 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3171 | leftWindow->consumeMotionEvent( |
| 3172 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3173 | spyWindow->consumeMotionEvent( |
| 3174 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3175 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3176 | // Touch movements continue. They should be delivered to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3177 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3178 | .deviceId(touchDeviceId) |
| 3179 | .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101)) |
| 3180 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3181 | rightWindow->consumeMotionEvent( |
| 3182 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3183 | |
| 3184 | spyWindow->assertNoEvents(); |
| 3185 | leftWindow->assertNoEvents(); |
| 3186 | rightWindow->assertNoEvents(); |
| 3187 | } |
| 3188 | |
| 3189 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3190 | * On a single window, use two different devices: mouse and touch. |
| 3191 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 3192 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 3193 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 3194 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 3195 | * represent a new gesture. |
| 3196 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3197 | TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3198 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3199 | sp<FakeWindowHandle> window = |
| 3200 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3201 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3202 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3203 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3204 | |
| 3205 | const int32_t touchDeviceId = 4; |
| 3206 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3207 | |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3208 | // First touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3209 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3210 | .deviceId(touchDeviceId) |
| 3211 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3212 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3213 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3214 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3215 | .deviceId(touchDeviceId) |
| 3216 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3217 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3218 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3219 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3220 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3221 | .deviceId(touchDeviceId) |
| 3222 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3223 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3224 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3225 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3226 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3227 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 3228 | |
| 3229 | // Mouse down. The touch should be canceled |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3230 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3231 | .deviceId(mouseDeviceId) |
| 3232 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3233 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3234 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3235 | |
| 3236 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 3237 | WithPointerCount(1u))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3238 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3239 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3240 | mDispatcher->notifyMotion( |
| 3241 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3242 | .deviceId(mouseDeviceId) |
| 3243 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3244 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3245 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3246 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3247 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3248 | |
| 3249 | // Second touch pointer down. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3250 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3251 | .deviceId(touchDeviceId) |
| 3252 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3253 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3254 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3255 | // Since we already canceled this touch gesture, it will be ignored until a completely new |
| 3256 | // gesture is started. This is easier to implement than trying to keep track of the new pointer |
| 3257 | // and generating an ACTION_DOWN instead of ACTION_POINTER_DOWN. |
| 3258 | // However, mouse movements should continue to work. |
| 3259 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3260 | .deviceId(mouseDeviceId) |
| 3261 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3262 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110)) |
| 3263 | .build()); |
| 3264 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 3265 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3266 | window->assertNoEvents(); |
| 3267 | } |
| 3268 | |
| 3269 | /** |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3270 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels |
| 3271 | * the injected event. |
| 3272 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3273 | TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent) { |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3274 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3275 | sp<FakeWindowHandle> window = |
| 3276 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3277 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3278 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3279 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3280 | |
| 3281 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3282 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 3283 | // completion. |
| 3284 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3285 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3286 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3287 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3288 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3289 | .build())); |
| 3290 | window->consumeMotionEvent( |
| 3291 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3292 | |
| 3293 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer |
| 3294 | // should be canceled and the new gesture should take over. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3295 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3296 | .deviceId(touchDeviceId) |
| 3297 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3298 | .build()); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3299 | |
| 3300 | window->consumeMotionEvent( |
| 3301 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3302 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3303 | } |
| 3304 | |
| 3305 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3306 | * This test is similar to the test above, but the sequence of injected events is different. |
| 3307 | * |
| 3308 | * Two windows: a window on the left and a window on the right. |
| 3309 | * Mouse is hovered over the left window. |
| 3310 | * Next, we tap on the left window, where the cursor was last seen. |
| 3311 | * |
| 3312 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 3313 | * the left window. |
| 3314 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 3315 | * window (first), and then another on the left window (second). |
| 3316 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 3317 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 3318 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3319 | TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch) { |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3320 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3321 | sp<FakeWindowHandle> leftWindow = |
| 3322 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 3323 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3324 | |
| 3325 | sp<FakeWindowHandle> rightWindow = |
| 3326 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 3327 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3328 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3329 | mDispatcher->onWindowInfosChanged( |
| 3330 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3331 | |
| 3332 | const int32_t mouseDeviceId = 6; |
| 3333 | const int32_t touchDeviceId = 4; |
| 3334 | // Hover over the left window. Keep the cursor there. |
| 3335 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3336 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3337 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3338 | AINPUT_SOURCE_MOUSE) |
| 3339 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3340 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3341 | .build())); |
| 3342 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3343 | |
| 3344 | // Tap on left window |
| 3345 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3346 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3347 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3348 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3349 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3350 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3351 | .build())); |
| 3352 | |
| 3353 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3354 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3355 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3356 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3357 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3358 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3359 | .build())); |
| 3360 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3361 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3362 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 3363 | |
| 3364 | // First finger down on right window |
| 3365 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3366 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3367 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3368 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3369 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3370 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3371 | .build())); |
| 3372 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3373 | |
| 3374 | // Second finger down on the left window |
| 3375 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3376 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3377 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3378 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3379 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3380 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3381 | .build())); |
| 3382 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3383 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 3384 | |
| 3385 | // No more events |
| 3386 | leftWindow->assertNoEvents(); |
| 3387 | rightWindow->assertNoEvents(); |
| 3388 | } |
| 3389 | |
| 3390 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3391 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 3392 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 3393 | * touch is gone, stylus hovering should start working again. |
| 3394 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3395 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverIgnoresTouchTap) { |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3396 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3397 | sp<FakeWindowHandle> window = |
| 3398 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3399 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3400 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3401 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3402 | |
| 3403 | const int32_t stylusDeviceId = 5; |
| 3404 | const int32_t touchDeviceId = 4; |
| 3405 | // Start hovering with stylus |
| 3406 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3407 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3408 | MotionEventBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3409 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3410 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3411 | .build())); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3412 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3413 | |
| 3414 | // Finger down on the window |
| 3415 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3416 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3417 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3418 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3419 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3420 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3421 | // The touch device should be ignored! |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3422 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3423 | // Continue hovering with stylus. |
| 3424 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3425 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3426 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3427 | AINPUT_SOURCE_STYLUS) |
| 3428 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3429 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3430 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3431 | // Hovers continue to work |
| 3432 | window->consumeMotionEvent( |
| 3433 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3434 | |
| 3435 | // Lift up the finger |
| 3436 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3437 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3438 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3439 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3440 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3441 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3442 | .build())); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3443 | |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3444 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3445 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3446 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3447 | AINPUT_SOURCE_STYLUS) |
| 3448 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3449 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3450 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3451 | window->consumeMotionEvent( |
| 3452 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3453 | window->assertNoEvents(); |
| 3454 | } |
| 3455 | |
| 3456 | /** |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3457 | * If stylus is down anywhere on the screen, then touches should not be delivered to windows that |
| 3458 | * have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 3459 | * |
| 3460 | * Two windows: one on the left and one on the right. |
| 3461 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 3462 | * Stylus down on the left window, and then touch down on the right window. |
| 3463 | * Check that the right window doesn't get touches while the stylus is down on the left window. |
| 3464 | */ |
| 3465 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusDownBlocksTouch) { |
| 3466 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3467 | sp<FakeWindowHandle> leftWindow = |
| 3468 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
| 3469 | ADISPLAY_ID_DEFAULT); |
| 3470 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3471 | |
| 3472 | sp<FakeWindowHandle> sbtRightWindow = |
| 3473 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3474 | "Stylus blocks touch (right) window", ADISPLAY_ID_DEFAULT); |
| 3475 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3476 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 3477 | |
| 3478 | mDispatcher->onWindowInfosChanged( |
| 3479 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 3480 | |
| 3481 | const int32_t stylusDeviceId = 5; |
| 3482 | const int32_t touchDeviceId = 4; |
| 3483 | |
| 3484 | // Stylus down in the left window |
| 3485 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3486 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 3487 | .deviceId(stylusDeviceId) |
| 3488 | .build()); |
| 3489 | leftWindow->consumeMotionEvent( |
| 3490 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 3491 | |
| 3492 | // Finger tap on the right window |
| 3493 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3494 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3495 | .deviceId(touchDeviceId) |
| 3496 | .build()); |
| 3497 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3498 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3499 | .deviceId(touchDeviceId) |
| 3500 | .build()); |
| 3501 | |
| 3502 | // The touch should be blocked, because stylus is down somewhere else on screen! |
| 3503 | sbtRightWindow->assertNoEvents(); |
| 3504 | |
| 3505 | // Continue stylus motion, and ensure it's not impacted. |
| 3506 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 3507 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3508 | .deviceId(stylusDeviceId) |
| 3509 | .build()); |
| 3510 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3511 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3512 | .deviceId(stylusDeviceId) |
| 3513 | .build()); |
| 3514 | leftWindow->consumeMotionEvent( |
| 3515 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 3516 | leftWindow->consumeMotionEvent( |
| 3517 | AllOf(WithMotionAction(ACTION_UP), WithDeviceId(stylusDeviceId))); |
| 3518 | |
| 3519 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3520 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3521 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3522 | .deviceId(touchDeviceId) |
| 3523 | .build()); |
| 3524 | sbtRightWindow->consumeMotionEvent( |
| 3525 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3526 | } |
| 3527 | |
| 3528 | /** |
| 3529 | * If stylus is hovering anywhere on the screen, then touches should not be delivered to windows |
| 3530 | * that have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 3531 | * |
| 3532 | * Two windows: one on the left and one on the right. |
| 3533 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 3534 | * Stylus hover on the left window, and then touch down on the right window. |
| 3535 | * Check that the right window doesn't get touches while the stylus is hovering on the left window. |
| 3536 | */ |
| 3537 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusHoverBlocksTouch) { |
| 3538 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3539 | sp<FakeWindowHandle> leftWindow = |
| 3540 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
| 3541 | ADISPLAY_ID_DEFAULT); |
| 3542 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3543 | |
| 3544 | sp<FakeWindowHandle> sbtRightWindow = |
| 3545 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3546 | "Stylus blocks touch (right) window", ADISPLAY_ID_DEFAULT); |
| 3547 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3548 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 3549 | |
| 3550 | mDispatcher->onWindowInfosChanged( |
| 3551 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 3552 | |
| 3553 | const int32_t stylusDeviceId = 5; |
| 3554 | const int32_t touchDeviceId = 4; |
| 3555 | |
| 3556 | // Stylus hover in the left window |
| 3557 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3558 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 3559 | .deviceId(stylusDeviceId) |
| 3560 | .build()); |
| 3561 | leftWindow->consumeMotionEvent( |
| 3562 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3563 | |
| 3564 | // Finger tap on the right window |
| 3565 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3566 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3567 | .deviceId(touchDeviceId) |
| 3568 | .build()); |
| 3569 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3570 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3571 | .deviceId(touchDeviceId) |
| 3572 | .build()); |
| 3573 | |
| 3574 | // The touch should be blocked, because stylus is hovering somewhere else on screen! |
| 3575 | sbtRightWindow->assertNoEvents(); |
| 3576 | |
| 3577 | // Continue stylus motion, and ensure it's not impacted. |
| 3578 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3579 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3580 | .deviceId(stylusDeviceId) |
| 3581 | .build()); |
| 3582 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3583 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3584 | .deviceId(stylusDeviceId) |
| 3585 | .build()); |
| 3586 | leftWindow->consumeMotionEvent( |
| 3587 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3588 | leftWindow->consumeMotionEvent( |
| 3589 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId))); |
| 3590 | |
| 3591 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3592 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3593 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3594 | .deviceId(touchDeviceId) |
| 3595 | .build()); |
| 3596 | sbtRightWindow->consumeMotionEvent( |
| 3597 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3598 | } |
| 3599 | |
| 3600 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3601 | * A spy window above a window with no input channel. |
| 3602 | * Start hovering with a stylus device, and then tap with it. |
| 3603 | * Ensure spy window receives the entire sequence. |
| 3604 | */ |
| 3605 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 3606 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3607 | sp<FakeWindowHandle> spyWindow = |
| 3608 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3609 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3610 | spyWindow->setTrustedOverlay(true); |
| 3611 | spyWindow->setSpy(true); |
| 3612 | sp<FakeWindowHandle> window = |
| 3613 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3614 | window->setNoInputChannel(true); |
| 3615 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3616 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3617 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3618 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3619 | // Start hovering with stylus |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3620 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3621 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3622 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3623 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3624 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3625 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3626 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3627 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3628 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3629 | |
| 3630 | // Stylus touches down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3631 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3632 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3633 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3634 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3635 | |
| 3636 | // Stylus goes up |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3637 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3638 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3639 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3640 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 3641 | |
| 3642 | // Again hover |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3643 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3644 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3645 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3646 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3647 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3648 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3649 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3650 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3651 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3652 | |
| 3653 | // No more events |
| 3654 | spyWindow->assertNoEvents(); |
| 3655 | window->assertNoEvents(); |
| 3656 | } |
| 3657 | |
| 3658 | /** |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 3659 | * A stale stylus HOVER_EXIT event is injected. Since it's a stale event, it should generally be |
| 3660 | * rejected. But since we already have an ongoing gesture, this event should be processed. |
| 3661 | * This prevents inconsistent events being handled inside the dispatcher. |
| 3662 | */ |
| 3663 | TEST_F(InputDispatcherTest, StaleStylusHoverGestureIsComplete) { |
| 3664 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3665 | |
| 3666 | sp<FakeWindowHandle> window = |
| 3667 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3668 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3669 | |
| 3670 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3671 | |
| 3672 | // Start hovering with stylus |
| 3673 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3674 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3675 | .build()); |
| 3676 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3677 | |
| 3678 | NotifyMotionArgs hoverExit = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3679 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3680 | .build(); |
| 3681 | // Make this 'hoverExit' event stale |
| 3682 | mFakePolicy->setStaleEventTimeout(100ms); |
| 3683 | std::this_thread::sleep_for(100ms); |
| 3684 | |
| 3685 | // It shouldn't be dropped by the dispatcher, even though it's stale. |
| 3686 | mDispatcher->notifyMotion(hoverExit); |
| 3687 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3688 | |
| 3689 | // Stylus starts hovering again! There should be no crash. |
| 3690 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3691 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(51)) |
| 3692 | .build()); |
| 3693 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3694 | } |
| 3695 | |
| 3696 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3697 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 3698 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 3699 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 3700 | * While the mouse is down, new move events from the touch device should be ignored. |
| 3701 | */ |
| 3702 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 3703 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3704 | sp<FakeWindowHandle> spyWindow = |
| 3705 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3706 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3707 | spyWindow->setTrustedOverlay(true); |
| 3708 | spyWindow->setSpy(true); |
| 3709 | sp<FakeWindowHandle> window = |
| 3710 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3711 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3712 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3713 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3714 | |
| 3715 | const int32_t mouseDeviceId = 7; |
| 3716 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3717 | |
| 3718 | // Hover a bit with mouse first |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3719 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3720 | .deviceId(mouseDeviceId) |
| 3721 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3722 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3723 | spyWindow->consumeMotionEvent( |
| 3724 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3725 | window->consumeMotionEvent( |
| 3726 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3727 | |
| 3728 | // Start touching |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3729 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3730 | .deviceId(touchDeviceId) |
| 3731 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3732 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3733 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3734 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3735 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3736 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3737 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3738 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3739 | .deviceId(touchDeviceId) |
| 3740 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 3741 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3742 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3743 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3744 | |
| 3745 | // Pilfer the stream |
| 3746 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3747 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 3748 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3749 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3750 | .deviceId(touchDeviceId) |
| 3751 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 3752 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3753 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3754 | |
| 3755 | // Mouse down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3756 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3757 | .deviceId(mouseDeviceId) |
| 3758 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3759 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3760 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3761 | |
| 3762 | spyWindow->consumeMotionEvent( |
| 3763 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 3764 | spyWindow->consumeMotionEvent( |
| 3765 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3766 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3767 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3768 | mDispatcher->notifyMotion( |
| 3769 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3770 | .deviceId(mouseDeviceId) |
| 3771 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3772 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3773 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3774 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3775 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3776 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3777 | |
| 3778 | // Mouse move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3779 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3780 | .deviceId(mouseDeviceId) |
| 3781 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3782 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 3783 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3784 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3785 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3786 | |
| 3787 | // Touch move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3788 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3789 | .deviceId(touchDeviceId) |
| 3790 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 3791 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3792 | |
| 3793 | // No more events |
| 3794 | spyWindow->assertNoEvents(); |
| 3795 | window->assertNoEvents(); |
| 3796 | } |
| 3797 | |
| 3798 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3799 | * On the display, have a single window, and also an area where there's no window. |
| 3800 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 3801 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 3802 | */ |
| 3803 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 3804 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3805 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3806 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3807 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3808 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3809 | |
| 3810 | // Touch down on the empty space |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3811 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3812 | |
| 3813 | mDispatcher->waitForIdle(); |
| 3814 | window->assertNoEvents(); |
| 3815 | |
| 3816 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3817 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3818 | mDispatcher->waitForIdle(); |
| 3819 | window->consumeMotionDown(); |
| 3820 | } |
| 3821 | |
| 3822 | /** |
| 3823 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 3824 | * non-touchable window. |
| 3825 | */ |
| 3826 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 3827 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3828 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3829 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3830 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3831 | window1->setTouchable(false); |
| 3832 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3833 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3834 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3835 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3836 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3837 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3838 | // Touch down on the non-touchable window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3839 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3840 | |
| 3841 | mDispatcher->waitForIdle(); |
| 3842 | window1->assertNoEvents(); |
| 3843 | window2->assertNoEvents(); |
| 3844 | |
| 3845 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3846 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3847 | mDispatcher->waitForIdle(); |
| 3848 | window2->consumeMotionDown(); |
| 3849 | } |
| 3850 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3851 | /** |
| 3852 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 3853 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 3854 | */ |
| 3855 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 3856 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3857 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3858 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3859 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3860 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3861 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3862 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3863 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3864 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3865 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3866 | // Touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3867 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3868 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3869 | |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 3870 | const std::unique_ptr<MotionEvent> firstDown = |
| 3871 | window1->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 3872 | ASSERT_EQ(firstDown->getDownTime(), firstDown->getEventTime()); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3873 | window2->assertNoEvents(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3874 | |
| 3875 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3876 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3877 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 3878 | |
| 3879 | const std::unique_ptr<MotionEvent> secondDown = |
| 3880 | window2->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 3881 | ASSERT_EQ(secondDown->getDownTime(), secondDown->getEventTime()); |
| 3882 | ASSERT_NE(firstDown->getDownTime(), secondDown->getDownTime()); |
| 3883 | // We currently send MOVE events to all windows receiving a split touch when there is any change |
| 3884 | // in the touch state, even when none of the pointers in the split window actually moved. |
| 3885 | // Document this behavior in the test. |
| 3886 | window1->consumeMotionMove(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3887 | |
| 3888 | // Now move the pointer on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3889 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3890 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 3891 | |
| 3892 | window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime())); |
| 3893 | window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime())); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3894 | |
| 3895 | // Now add new touch down on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3896 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3897 | mDispatcher->waitForIdle(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3898 | |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 3899 | window2->consumeMotionEvent( |
| 3900 | AllOf(WithMotionAction(POINTER_1_DOWN), WithDownTime(secondDown->getDownTime()))); |
| 3901 | window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime())); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3902 | |
| 3903 | // Now move the pointer on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3904 | mDispatcher->notifyMotion( |
| 3905 | generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3906 | mDispatcher->waitForIdle(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3907 | |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 3908 | window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime())); |
| 3909 | window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime())); |
| 3910 | |
| 3911 | // Now add new touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3912 | mDispatcher->notifyMotion( |
| 3913 | generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3914 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 3915 | |
| 3916 | window1->consumeMotionEvent( |
| 3917 | AllOf(WithMotionAction(POINTER_1_DOWN), WithDownTime(firstDown->getDownTime()))); |
| 3918 | window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime())); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3919 | } |
| 3920 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3921 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3922 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3923 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3924 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3925 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3926 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3927 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3928 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3929 | |
| 3930 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3931 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3932 | mDispatcher->onWindowInfosChanged( |
| 3933 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3934 | |
| 3935 | // 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] | 3936 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3937 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3938 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3939 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3940 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3941 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3942 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3943 | |
| 3944 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3945 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3946 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3947 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3948 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3949 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3950 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3951 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3952 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3953 | |
| 3954 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3955 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3956 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3957 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3958 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3959 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3960 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3961 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3962 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3963 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3964 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3965 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3966 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3967 | AINPUT_SOURCE_MOUSE) |
| 3968 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3969 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3970 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3971 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3972 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3973 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3974 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3975 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3976 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3977 | AINPUT_SOURCE_MOUSE) |
| 3978 | .buttonState(0) |
| 3979 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3980 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3981 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3982 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3983 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3984 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3985 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3986 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3987 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3988 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3989 | .build())); |
| 3990 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3991 | |
| 3992 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3993 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3994 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3995 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3996 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3997 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3998 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3999 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4000 | |
| 4001 | // No more events |
| 4002 | windowLeft->assertNoEvents(); |
| 4003 | windowRight->assertNoEvents(); |
| 4004 | } |
| 4005 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4006 | /** |
| 4007 | * Put two fingers down (and don't release them) and click the mouse button. |
| 4008 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 4009 | * currently active gesture should be canceled, and the new one should proceed. |
| 4010 | */ |
| 4011 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 4012 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4013 | sp<FakeWindowHandle> window = |
| 4014 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4015 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4016 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4017 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4018 | |
| 4019 | const int32_t touchDeviceId = 4; |
| 4020 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4021 | |
| 4022 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4023 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4024 | .deviceId(touchDeviceId) |
| 4025 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4026 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4027 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4028 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4029 | .deviceId(touchDeviceId) |
| 4030 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4031 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 4032 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4033 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4034 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4035 | |
| 4036 | // Inject a series of mouse events for a mouse click |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4037 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4038 | .deviceId(mouseDeviceId) |
| 4039 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4040 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4041 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4042 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 4043 | WithPointerCount(2u))); |
| 4044 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 4045 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4046 | mDispatcher->notifyMotion( |
| 4047 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 4048 | .deviceId(mouseDeviceId) |
| 4049 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4050 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4051 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4052 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4053 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4054 | |
| 4055 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 4056 | // already canceled gesture, it should be ignored. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4057 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 4058 | .deviceId(touchDeviceId) |
| 4059 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 4060 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 4061 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4062 | window->assertNoEvents(); |
| 4063 | } |
| 4064 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4065 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 4066 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4067 | |
| 4068 | sp<FakeWindowHandle> spyWindow = |
| 4069 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 4070 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4071 | spyWindow->setTrustedOverlay(true); |
| 4072 | spyWindow->setSpy(true); |
| 4073 | sp<FakeWindowHandle> window = |
| 4074 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4075 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4076 | |
| 4077 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4078 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4079 | |
| 4080 | // Send mouse cursor to the window |
| 4081 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4082 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4083 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4084 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4085 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4086 | .build())); |
| 4087 | |
| 4088 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4089 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4090 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4091 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4092 | |
| 4093 | window->assertNoEvents(); |
| 4094 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4095 | } |
| 4096 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4097 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 4098 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4099 | |
| 4100 | sp<FakeWindowHandle> spyWindow = |
| 4101 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 4102 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4103 | spyWindow->setTrustedOverlay(true); |
| 4104 | spyWindow->setSpy(true); |
| 4105 | sp<FakeWindowHandle> window = |
| 4106 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4107 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4108 | |
| 4109 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4110 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4111 | |
| 4112 | // Send mouse cursor to the window |
| 4113 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4114 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4115 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4116 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4117 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4118 | .build())); |
| 4119 | |
| 4120 | // Move mouse cursor |
| 4121 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4122 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4123 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4124 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4125 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4126 | .build())); |
| 4127 | |
| 4128 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4129 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4130 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4131 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4132 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4133 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4134 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4135 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4136 | // Touch down on the window |
| 4137 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4138 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4139 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 4140 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4141 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4142 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4143 | .build())); |
| 4144 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4145 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4146 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4147 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4148 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4149 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4150 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4151 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4152 | |
| 4153 | // pilfer the motion, retaining the gesture on the spy window. |
| 4154 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 4155 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 4156 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4157 | |
| 4158 | // Touch UP on the window |
| 4159 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4160 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4161 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 4162 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4163 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4164 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4165 | .build())); |
| 4166 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4167 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4168 | |
| 4169 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 4170 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 4171 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 4172 | |
| 4173 | // One more tap - DOWN |
| 4174 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4175 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4176 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 4177 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4178 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4179 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4180 | .build())); |
| 4181 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4182 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4183 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4184 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4185 | |
| 4186 | // Touch UP on the window |
| 4187 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4188 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4189 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 4190 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4191 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4192 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4193 | .build())); |
| 4194 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4195 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4196 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4197 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4198 | |
| 4199 | window->assertNoEvents(); |
| 4200 | spyWindow->assertNoEvents(); |
| 4201 | } |
| 4202 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4203 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 4204 | // directly in this test. |
| 4205 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4206 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4207 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4208 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4209 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4210 | |
| 4211 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4212 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4213 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4214 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4215 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4216 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4217 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4218 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4219 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4220 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4221 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4222 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4223 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4224 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4225 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4226 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4227 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4228 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4229 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4230 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4231 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4232 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4233 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4234 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 4235 | AINPUT_SOURCE_MOUSE) |
| 4236 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4237 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4238 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4239 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4240 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4241 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4242 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4243 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4244 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 4245 | AINPUT_SOURCE_MOUSE) |
| 4246 | .buttonState(0) |
| 4247 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4248 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4249 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4250 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4251 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4252 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4253 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4254 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4255 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4256 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4257 | .build())); |
| 4258 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 4259 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 4260 | // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the |
| 4261 | // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail. |
| 4262 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4263 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4264 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 4265 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4266 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4267 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4268 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4269 | } |
| 4270 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4271 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4272 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 4273 | * is generated. |
| 4274 | */ |
| 4275 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 4276 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4277 | sp<FakeWindowHandle> window = |
| 4278 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4279 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 4280 | |
| 4281 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4282 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4283 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4284 | |
| 4285 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4286 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4287 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4288 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4289 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4290 | .build())); |
| 4291 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4292 | |
| 4293 | // Remove the window, but keep the channel. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4294 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4295 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 4296 | } |
| 4297 | |
| 4298 | /** |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4299 | * Test that invalid HOVER events sent by accessibility do not cause a fatal crash. |
| 4300 | */ |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 4301 | TEST_F_WITH_FLAGS(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash, |
| 4302 | REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(com::android::input::flags, |
| 4303 | a11y_crash_on_inconsistent_event_stream))) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4304 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4305 | sp<FakeWindowHandle> window = |
| 4306 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4307 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 4308 | |
| 4309 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4310 | |
| 4311 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 4312 | |
| 4313 | MotionEventBuilder hoverEnterBuilder = |
| 4314 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4315 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4316 | .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 4317 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4318 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 4319 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4320 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 4321 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4322 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4323 | } |
| 4324 | |
| 4325 | /** |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4326 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. |
| 4327 | */ |
| 4328 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { |
| 4329 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4330 | sp<FakeWindowHandle> window = |
| 4331 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4332 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4333 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4334 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4335 | |
| 4336 | const int32_t mouseDeviceId = 7; |
| 4337 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4338 | |
| 4339 | // Start hovering with the mouse |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4340 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4341 | .deviceId(mouseDeviceId) |
| 4342 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 4343 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4344 | window->consumeMotionEvent( |
| 4345 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 4346 | |
| 4347 | // Touch goes down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4348 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4349 | .deviceId(touchDeviceId) |
| 4350 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4351 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4352 | |
| 4353 | window->consumeMotionEvent( |
| 4354 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 4355 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 4356 | } |
| 4357 | |
| 4358 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4359 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4360 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 4361 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4362 | */ |
| 4363 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 4364 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4365 | sp<FakeWindowHandle> window = |
| 4366 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4367 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4368 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4369 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4370 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4371 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 4372 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4373 | ASSERT_NO_FATAL_FAILURE( |
| 4374 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4375 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4376 | |
| 4377 | // Tap on the window |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4378 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4379 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4380 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4381 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4382 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4383 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 4384 | |
| 4385 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4386 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4387 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4388 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4389 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4390 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4391 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4392 | ASSERT_NO_FATAL_FAILURE( |
| 4393 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4394 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4395 | } |
| 4396 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4397 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 4398 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4399 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 4400 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 4401 | ADISPLAY_ID_DEFAULT); |
| 4402 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 4403 | sp<FakeWindowHandle> windowSecondDisplay = |
| 4404 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 4405 | SECOND_DISPLAY_ID); |
| 4406 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 4407 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4408 | mDispatcher->onWindowInfosChanged( |
| 4409 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4410 | |
| 4411 | // Set cursor position in window in default display and check that hover enter and move |
| 4412 | // events are generated. |
| 4413 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4414 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4415 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4416 | AINPUT_SOURCE_MOUSE) |
| 4417 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4418 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4419 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4420 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4421 | |
| 4422 | // Remove all windows in secondary display and check that no event happens on window in |
| 4423 | // primary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4424 | mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0}); |
| 4425 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4426 | windowDefaultDisplay->assertNoEvents(); |
| 4427 | |
| 4428 | // Move cursor position in window in default display and check that only hover move |
| 4429 | // event is generated and not hover enter event. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4430 | mDispatcher->onWindowInfosChanged( |
| 4431 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4432 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4433 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4434 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4435 | AINPUT_SOURCE_MOUSE) |
| 4436 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4437 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4438 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4439 | windowDefaultDisplay->consumeMotionEvent( |
| 4440 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4441 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4442 | windowDefaultDisplay->assertNoEvents(); |
| 4443 | } |
| 4444 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4445 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4446 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4447 | |
| 4448 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4449 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4450 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4451 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4452 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4453 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4454 | |
| 4455 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4456 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4457 | mDispatcher->onWindowInfosChanged( |
| 4458 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4459 | |
| 4460 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 4461 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4462 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4463 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4464 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4465 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4466 | windowRight->assertNoEvents(); |
| 4467 | } |
| 4468 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4469 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4470 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4471 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4472 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4473 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4474 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4475 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4476 | setFocusedWindow(window); |
| 4477 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4478 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4479 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4480 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4481 | |
| 4482 | // Window should receive key down event. |
| 4483 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4484 | |
| 4485 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 4486 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4487 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 4488 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT, AKEY_EVENT_FLAG_CANCELED); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4489 | } |
| 4490 | |
| 4491 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4492 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4493 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4494 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4495 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4496 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4497 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4498 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4499 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4500 | |
| 4501 | // Window should receive motion down event. |
| 4502 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4503 | |
| 4504 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 4505 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4506 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 4507 | window->consumeMotionEvent( |
| 4508 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4509 | } |
| 4510 | |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 4511 | TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) { |
| 4512 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4513 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4514 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4515 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4516 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 4517 | |
| 4518 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 4519 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10)) |
| 4520 | .build()); |
| 4521 | |
| 4522 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 4523 | |
| 4524 | // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT |
| 4525 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
| 4526 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4527 | |
| 4528 | // After the device has been reset, a new hovering stream can be sent to the window |
| 4529 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 4530 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15)) |
| 4531 | .build()); |
| 4532 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 4533 | } |
| 4534 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4535 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 4536 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4537 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4538 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4539 | window->setFocusable(true); |
| 4540 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4541 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4542 | setFocusedWindow(window); |
| 4543 | |
| 4544 | window->consumeFocusEvent(true); |
| 4545 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4546 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4547 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 4548 | const nsecs_t injectTime = keyArgs.eventTime; |
| 4549 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4550 | mDispatcher->notifyKey(keyArgs); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4551 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 4552 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4553 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 4554 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 4555 | } |
| 4556 | |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 4557 | /** |
| 4558 | * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set. |
| 4559 | */ |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4560 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 4561 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4562 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4563 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4564 | window->setFocusable(true); |
| 4565 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4566 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4567 | setFocusedWindow(window); |
| 4568 | |
| 4569 | window->consumeFocusEvent(true); |
| 4570 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4571 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4572 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 4573 | |
| 4574 | // Set a value that's significantly larger than the default consumption timeout. If the |
| 4575 | // implementation is correct, the actual value doesn't matter; it won't slow down the test. |
| 4576 | mFakePolicy->setInterceptKeyTimeout(600ms); |
| 4577 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
| 4578 | // Window should receive key event immediately when same key up. |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4579 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 4580 | } |
| 4581 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4582 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4583 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 4584 | * WATCH_OUTSIDE_TOUCH. |
| 4585 | * Both windows are owned by the same UID. |
| 4586 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 4587 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 4588 | */ |
| 4589 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 4590 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4591 | sp<FakeWindowHandle> window = |
| 4592 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4593 | window->setFrame(Rect{0, 0, 100, 100}); |
| 4594 | |
| 4595 | sp<FakeWindowHandle> outsideWindow = |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4596 | sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window", |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4597 | ADISPLAY_ID_DEFAULT); |
| 4598 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 4599 | outsideWindow->setWatchOutsideTouch(true); |
| 4600 | // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4601 | mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4602 | |
| 4603 | // Tap on first window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4604 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4605 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4606 | {PointF{50, 50}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4607 | window->consumeMotionDown(); |
| 4608 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 4609 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 4610 | outsideWindow->consumeMotionEvent( |
| 4611 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 4612 | |
| 4613 | // Ensure outsideWindow doesn't get any more events for the gesture. |
| 4614 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4615 | ADISPLAY_ID_DEFAULT, {PointF{51, 51}})); |
| 4616 | window->consumeMotionMove(); |
| 4617 | outsideWindow->assertNoEvents(); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4618 | } |
| 4619 | |
| 4620 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4621 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 4622 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 4623 | * ACTION_OUTSIDE event is sent per gesture. |
| 4624 | */ |
| 4625 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 4626 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 4627 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4628 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4629 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4630 | window->setWatchOutsideTouch(true); |
| 4631 | window->setFrame(Rect{0, 0, 100, 100}); |
| 4632 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4633 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4634 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4635 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 4636 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4637 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 4638 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4639 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4640 | mDispatcher->onWindowInfosChanged( |
| 4641 | {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4642 | |
| 4643 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4644 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4645 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4646 | {PointF{-10, -10}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4647 | window->assertNoEvents(); |
| 4648 | secondWindow->assertNoEvents(); |
| 4649 | |
| 4650 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 4651 | // Now, `window` should get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4652 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4653 | ADISPLAY_ID_DEFAULT, |
| 4654 | {PointF{-10, -10}, PointF{105, 105}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4655 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 4656 | window->consumeMotionEvent( |
| 4657 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4658 | secondWindow->consumeMotionDown(); |
| 4659 | thirdWindow->assertNoEvents(); |
| 4660 | |
| 4661 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 4662 | // 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] | 4663 | mDispatcher->notifyMotion( |
| 4664 | generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4665 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4666 | window->assertNoEvents(); |
| 4667 | secondWindow->consumeMotionMove(); |
| 4668 | thirdWindow->consumeMotionDown(); |
| 4669 | } |
| 4670 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4671 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 4672 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4673 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4674 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4675 | window->setFocusable(true); |
| 4676 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4677 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4678 | setFocusedWindow(window); |
| 4679 | |
| 4680 | window->consumeFocusEvent(true); |
| 4681 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4682 | const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 4683 | const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 4684 | mDispatcher->notifyKey(keyDown); |
| 4685 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4686 | |
| 4687 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4688 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 4689 | |
| 4690 | // 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] | 4691 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4692 | |
| 4693 | window->consumeFocusEvent(false); |
| 4694 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4695 | mDispatcher->notifyKey(keyDown); |
| 4696 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4697 | window->assertNoEvents(); |
| 4698 | } |
| 4699 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4700 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 4701 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4702 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4703 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4704 | // Ensure window is non-split and have some transform. |
| 4705 | window->setPreventSplitting(true); |
| 4706 | window->setWindowOffset(20, 40); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4707 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4708 | |
| 4709 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4710 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4711 | {50, 50})) |
| 4712 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4713 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4714 | |
| 4715 | const MotionEvent secondFingerDownEvent = |
| 4716 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4717 | .displayId(ADISPLAY_ID_DEFAULT) |
| 4718 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4719 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 4720 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4721 | .build(); |
| 4722 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4723 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4724 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 4725 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4726 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 4727 | std::unique_ptr<MotionEvent> event = window->consumeMotionEvent(); |
| 4728 | ASSERT_NE(nullptr, event); |
| 4729 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 4730 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 4731 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 4732 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 4733 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4734 | } |
| 4735 | |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 4736 | /** |
| 4737 | * Two windows: a splittable and a non-splittable. |
| 4738 | * The non-splittable window shouldn't receive any "incomplete" gestures. |
| 4739 | * Send the first pointer to the splittable window, and then touch the non-splittable window. |
| 4740 | * The second pointer should be dropped because the initial window is splittable, so it won't get |
| 4741 | * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any |
| 4742 | * "incomplete" gestures. |
| 4743 | */ |
| 4744 | TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) { |
| 4745 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4746 | sp<FakeWindowHandle> leftWindow = |
| 4747 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window", |
| 4748 | ADISPLAY_ID_DEFAULT); |
| 4749 | leftWindow->setPreventSplitting(false); |
| 4750 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4751 | sp<FakeWindowHandle> rightWindow = |
| 4752 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window", |
| 4753 | ADISPLAY_ID_DEFAULT); |
| 4754 | rightWindow->setPreventSplitting(true); |
| 4755 | rightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 4756 | mDispatcher->onWindowInfosChanged( |
| 4757 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 4758 | |
| 4759 | // Touch down on left, splittable window |
| 4760 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4761 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4762 | .build()); |
| 4763 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4764 | |
| 4765 | mDispatcher->notifyMotion( |
| 4766 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4767 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 4768 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
| 4769 | .build()); |
| 4770 | leftWindow->assertNoEvents(); |
| 4771 | rightWindow->assertNoEvents(); |
| 4772 | } |
| 4773 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4774 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) { |
| 4775 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4776 | sp<FakeWindowHandle> window = |
| 4777 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4778 | window->setFrame(Rect(0, 0, 400, 400)); |
| 4779 | sp<FakeWindowHandle> trustedOverlay = |
| 4780 | sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay", |
| 4781 | ADISPLAY_ID_DEFAULT); |
| 4782 | trustedOverlay->setSpy(true); |
| 4783 | trustedOverlay->setTrustedOverlay(true); |
| 4784 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4785 | mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4786 | |
| 4787 | // Start a three-finger touchpad swipe |
| 4788 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4789 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4790 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4791 | .build()); |
| 4792 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 4793 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4794 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4795 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4796 | .build()); |
| 4797 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 4798 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4799 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4800 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 4801 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4802 | .build()); |
| 4803 | |
| 4804 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4805 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4806 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN)); |
| 4807 | |
| 4808 | // Move the swipe a bit |
| 4809 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4810 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4811 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4812 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4813 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4814 | .build()); |
| 4815 | |
| 4816 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 4817 | |
| 4818 | // End the swipe |
| 4819 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 4820 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4821 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4822 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4823 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4824 | .build()); |
| 4825 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 4826 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4827 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4828 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4829 | .build()); |
| 4830 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4831 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4832 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4833 | .build()); |
| 4834 | |
| 4835 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP)); |
| 4836 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 4837 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 4838 | |
| 4839 | window->assertNoEvents(); |
| 4840 | } |
| 4841 | |
| 4842 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) { |
| 4843 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4844 | sp<FakeWindowHandle> window = |
| 4845 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4846 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4847 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4848 | |
| 4849 | // Start a three-finger touchpad swipe |
| 4850 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4851 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4852 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4853 | .build()); |
| 4854 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 4855 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4856 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4857 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4858 | .build()); |
| 4859 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 4860 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4861 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4862 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 4863 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4864 | .build()); |
| 4865 | |
| 4866 | // Move the swipe a bit |
| 4867 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4868 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4869 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4870 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4871 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4872 | .build()); |
| 4873 | |
| 4874 | // End the swipe |
| 4875 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 4876 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4877 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4878 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4879 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4880 | .build()); |
| 4881 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 4882 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4883 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4884 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4885 | .build()); |
| 4886 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4887 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4888 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4889 | .build()); |
| 4890 | |
| 4891 | window->assertNoEvents(); |
| 4892 | } |
| 4893 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4894 | /** |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4895 | * Send a two-pointer gesture to a single window. The window's orientation changes in response to |
| 4896 | * the first pointer. |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4897 | * Ensure that the second pointer and the subsequent gesture is correctly delivered to the window. |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4898 | */ |
| 4899 | TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) { |
| 4900 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4901 | sp<FakeWindowHandle> window = |
| 4902 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4903 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4904 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4905 | |
| 4906 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4907 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4908 | .downTime(baseTime + 10) |
| 4909 | .eventTime(baseTime + 10) |
| 4910 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4911 | .build()); |
| 4912 | |
| 4913 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4914 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4915 | // Change the transform so that the orientation is now different from original. |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4916 | window->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4917 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4918 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4919 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4920 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4921 | .downTime(baseTime + 10) |
| 4922 | .eventTime(baseTime + 30) |
| 4923 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4924 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 4925 | .build()); |
| 4926 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4927 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4928 | |
| 4929 | // Finish the gesture and start a new one. Ensure all events are sent to the window. |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4930 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4931 | .downTime(baseTime + 10) |
| 4932 | .eventTime(baseTime + 40) |
| 4933 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4934 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 4935 | .build()); |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4936 | |
| 4937 | window->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 4938 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4939 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4940 | .downTime(baseTime + 10) |
| 4941 | .eventTime(baseTime + 50) |
| 4942 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4943 | .build()); |
| 4944 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4945 | window->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 4946 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4947 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4948 | .downTime(baseTime + 60) |
| 4949 | .eventTime(baseTime + 60) |
| 4950 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40)) |
| 4951 | .build()); |
| 4952 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4953 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4954 | } |
| 4955 | |
| 4956 | /** |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 4957 | * When there are multiple screens, such as screen projection to TV or screen recording, if the |
| 4958 | * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and |
| 4959 | * its coordinates should be converted by the transform of the windows of target screen. |
| 4960 | */ |
| 4961 | TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) { |
| 4962 | // This case will create a window and a spy window on the default display and mirror |
| 4963 | // window on the second display. cancel event is sent through spy window pilferPointers |
| 4964 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4965 | |
| 4966 | sp<FakeWindowHandle> spyWindowDefaultDisplay = |
| 4967 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 4968 | spyWindowDefaultDisplay->setTrustedOverlay(true); |
| 4969 | spyWindowDefaultDisplay->setSpy(true); |
| 4970 | |
| 4971 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 4972 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 4973 | ADISPLAY_ID_DEFAULT); |
| 4974 | windowDefaultDisplay->setWindowTransform(1, 0, 0, 1); |
| 4975 | |
| 4976 | sp<FakeWindowHandle> windowSecondDisplay = windowDefaultDisplay->clone(SECOND_DISPLAY_ID); |
| 4977 | windowSecondDisplay->setWindowTransform(2, 0, 0, 2); |
| 4978 | |
| 4979 | // Add the windows to the dispatcher |
| 4980 | mDispatcher->onWindowInfosChanged( |
| 4981 | {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(), |
| 4982 | *windowSecondDisplay->getInfo()}, |
| 4983 | {}, |
| 4984 | 0, |
| 4985 | 0}); |
| 4986 | |
| 4987 | // Send down to ADISPLAY_ID_DEFAULT |
| 4988 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4989 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4990 | {100, 100})) |
| 4991 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4992 | |
| 4993 | spyWindowDefaultDisplay->consumeMotionDown(); |
| 4994 | windowDefaultDisplay->consumeMotionDown(); |
| 4995 | |
| 4996 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken())); |
| 4997 | |
| 4998 | // windowDefaultDisplay gets cancel |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 4999 | std::unique_ptr<MotionEvent> event = windowDefaultDisplay->consumeMotionEvent(); |
| 5000 | ASSERT_NE(nullptr, event); |
| 5001 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event->getAction()); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5002 | |
| 5003 | // The cancel event is sent to windowDefaultDisplay of the ADISPLAY_ID_DEFAULT display, so the |
| 5004 | // coordinates of the cancel are converted by windowDefaultDisplay's transform, the x and y |
| 5005 | // coordinates are both 100, otherwise if the cancel event is sent to windowSecondDisplay of |
| 5006 | // SECOND_DISPLAY_ID, the x and y coordinates are 200 |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5007 | EXPECT_EQ(100, event->getX(0)); |
| 5008 | EXPECT_EQ(100, event->getY(0)); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5009 | } |
| 5010 | |
| 5011 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5012 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 5013 | * |
| 5014 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 5015 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 5016 | * space. |
| 5017 | */ |
| 5018 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 5019 | public: |
| 5020 | void SetUp() override { |
| 5021 | InputDispatcherTest::SetUp(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5022 | removeAllWindowsAndDisplays(); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5023 | } |
| 5024 | |
| 5025 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 5026 | gui::DisplayInfo info; |
| 5027 | info.displayId = displayId; |
| 5028 | info.transform = transform; |
| 5029 | mDisplayInfos.push_back(std::move(info)); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5030 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5031 | } |
| 5032 | |
| 5033 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 5034 | mWindowInfos.push_back(*windowHandle->getInfo()); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5035 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5036 | } |
| 5037 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5038 | void removeAllWindowsAndDisplays() { |
| 5039 | mDisplayInfos.clear(); |
| 5040 | mWindowInfos.clear(); |
| 5041 | } |
| 5042 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5043 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 5044 | // on the display. |
| 5045 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 5046 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 5047 | // respectively. |
| 5048 | ui::Transform displayTransform; |
| 5049 | displayTransform.set(2, 0, 0, 4); |
| 5050 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 5051 | |
| 5052 | std::shared_ptr<FakeApplicationHandle> application = |
| 5053 | std::make_shared<FakeApplicationHandle>(); |
| 5054 | |
| 5055 | // Add two windows to the display. Their frames are represented in the display space. |
| 5056 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5057 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5058 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5059 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 5060 | addWindow(firstWindow); |
| 5061 | |
| 5062 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5063 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5064 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5065 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 5066 | addWindow(secondWindow); |
| 5067 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 5068 | } |
| 5069 | |
| 5070 | private: |
| 5071 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 5072 | std::vector<gui::WindowInfo> mWindowInfos; |
| 5073 | }; |
| 5074 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5075 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5076 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5077 | // 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] | 5078 | // selected so that if the hit test was performed with the point and the bounds being in |
| 5079 | // different coordinate spaces, the event would end up in the incorrect window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5080 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5081 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5082 | {PointF{75, 55}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5083 | |
| 5084 | firstWindow->consumeMotionDown(); |
| 5085 | secondWindow->assertNoEvents(); |
| 5086 | } |
| 5087 | |
| 5088 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 5089 | // the event should be treated as being in the logical display space. |
| 5090 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 5091 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5092 | // Send down to the first window. The point is represented in the logical display space. The |
| 5093 | // point is selected so that if the hit test was done in logical display space, then it would |
| 5094 | // end up in the incorrect window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5095 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5096 | PointF{75 * 2, 55 * 4}); |
| 5097 | |
| 5098 | firstWindow->consumeMotionDown(); |
| 5099 | secondWindow->assertNoEvents(); |
| 5100 | } |
| 5101 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5102 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 5103 | // event should be treated as being in the logical display space. |
| 5104 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 5105 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5106 | |
| 5107 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5108 | ui::Transform injectedEventTransform; |
| 5109 | injectedEventTransform.set(matrix); |
| 5110 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 5111 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 5112 | |
| 5113 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5114 | .displayId(ADISPLAY_ID_DEFAULT) |
| 5115 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 5116 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5117 | .x(untransformedPoint.x) |
| 5118 | .y(untransformedPoint.y)) |
| 5119 | .build(); |
| 5120 | event.transform(matrix); |
| 5121 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5122 | injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5123 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 5124 | |
| 5125 | firstWindow->consumeMotionDown(); |
| 5126 | secondWindow->assertNoEvents(); |
| 5127 | } |
| 5128 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5129 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 5130 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5131 | |
| 5132 | // Send down to the second window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5133 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5134 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5135 | {PointF{150, 220}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5136 | |
| 5137 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5138 | std::unique_ptr<MotionEvent> event = secondWindow->consumeMotionEvent(); |
| 5139 | ASSERT_NE(nullptr, event); |
| 5140 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5141 | |
| 5142 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5143 | EXPECT_EQ(300, event->getRawX(0)); |
| 5144 | EXPECT_EQ(880, event->getRawY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5145 | |
| 5146 | // Ensure that the x and y values are in the window's coordinate space. |
| 5147 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 5148 | // the logical display space. This will be the origin of the window space. |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5149 | EXPECT_EQ(100, event->getX(0)); |
| 5150 | EXPECT_EQ(80, event->getY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5151 | } |
| 5152 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5153 | TEST_F(InputDispatcherDisplayProjectionTest, CancelMotionWithCorrectCoordinates) { |
| 5154 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5155 | // The monitor will always receive events in the logical display's coordinate space, because |
| 5156 | // it does not have a window. |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5157 | FakeMonitorReceiver monitor{*mDispatcher, "Monitor", ADISPLAY_ID_DEFAULT}; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5158 | |
| 5159 | // Send down to the first window. |
| 5160 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5161 | ADISPLAY_ID_DEFAULT, {PointF{50, 100}})); |
| 5162 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5163 | monitor.consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5164 | |
| 5165 | // Second pointer goes down on second window. |
| 5166 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5167 | ADISPLAY_ID_DEFAULT, |
| 5168 | {PointF{50, 100}, PointF{150, 220}})); |
| 5169 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80))); |
| 5170 | const std::map<int32_t, PointF> expectedMonitorPointers{{0, PointF{100, 400}}, |
| 5171 | {1, PointF{300, 880}}}; |
| 5172 | monitor.consumeMotionEvent( |
| 5173 | AllOf(WithMotionAction(POINTER_1_DOWN), WithPointers(expectedMonitorPointers))); |
| 5174 | |
| 5175 | mDispatcher->cancelCurrentTouch(); |
| 5176 | |
| 5177 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 5178 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 80))); |
| 5179 | monitor.consumeMotionEvent( |
| 5180 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedMonitorPointers))); |
| 5181 | } |
| 5182 | |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 5183 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeDownWithCorrectCoordinates) { |
| 5184 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5185 | |
| 5186 | // Send down to the first window. |
| 5187 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5188 | ADISPLAY_ID_DEFAULT, {PointF{50, 100}})); |
| 5189 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5190 | |
| 5191 | // The pointer is transferred to the second window, and the second window receives it in the |
| 5192 | // correct coordinate space. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5193 | mDispatcher->transferTouchGesture(firstWindow->getToken(), secondWindow->getToken()); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 5194 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 5195 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(-100, -400))); |
| 5196 | } |
| 5197 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5198 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverEnterExitWithCorrectCoordinates) { |
| 5199 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5200 | |
| 5201 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 5202 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 5203 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5204 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5205 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5206 | |
| 5207 | // Touch down at the same location and ensure a hover exit is synthesized. |
| 5208 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
| 5209 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5210 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5211 | WithRawCoords(300, 880))); |
| 5212 | secondWindow->consumeMotionEvent( |
| 5213 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5214 | secondWindow->assertNoEvents(); |
| 5215 | firstWindow->assertNoEvents(); |
| 5216 | } |
| 5217 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5218 | // Same as above, but while the window is being mirrored. |
| 5219 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 5220 | SynthesizeHoverEnterExitWithCorrectCoordinatesWhenMirrored) { |
| 5221 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5222 | |
| 5223 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5224 | ui::Transform secondDisplayTransform; |
| 5225 | secondDisplayTransform.set(matrix); |
| 5226 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 5227 | |
| 5228 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 5229 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 5230 | addWindow(secondWindowClone); |
| 5231 | |
| 5232 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 5233 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 5234 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5235 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5236 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5237 | |
| 5238 | // Touch down at the same location and ensure a hover exit is synthesized for the correct |
| 5239 | // display. |
| 5240 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
| 5241 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5242 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5243 | WithRawCoords(300, 880))); |
| 5244 | secondWindow->consumeMotionEvent( |
| 5245 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5246 | secondWindow->assertNoEvents(); |
| 5247 | firstWindow->assertNoEvents(); |
| 5248 | } |
| 5249 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5250 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverCancelationWithCorrectCoordinates) { |
| 5251 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5252 | |
| 5253 | // Send hover enter to second window |
| 5254 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
| 5255 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5256 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5257 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5258 | |
| 5259 | mDispatcher->cancelCurrentTouch(); |
| 5260 | |
| 5261 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5262 | WithRawCoords(300, 880))); |
| 5263 | secondWindow->assertNoEvents(); |
| 5264 | firstWindow->assertNoEvents(); |
| 5265 | } |
| 5266 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5267 | // Same as above, but while the window is being mirrored. |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5268 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 5269 | SynthesizeHoverCancelationWithCorrectCoordinatesWhenMirrored) { |
| 5270 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5271 | |
| 5272 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5273 | ui::Transform secondDisplayTransform; |
| 5274 | secondDisplayTransform.set(matrix); |
| 5275 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 5276 | |
| 5277 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 5278 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 5279 | addWindow(secondWindowClone); |
| 5280 | |
| 5281 | // Send hover enter to second window |
| 5282 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
| 5283 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5284 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5285 | WithCoords(100, 80), WithRawCoords(300, 880), |
| 5286 | WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 5287 | |
| 5288 | mDispatcher->cancelCurrentTouch(); |
| 5289 | |
| 5290 | // Ensure the cancelation happens with the correct displayId and the correct coordinates. |
| 5291 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5292 | WithRawCoords(300, 880), |
| 5293 | WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 5294 | secondWindow->assertNoEvents(); |
| 5295 | firstWindow->assertNoEvents(); |
| 5296 | } |
| 5297 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5298 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ |
| 5299 | class InputDispatcherDisplayOrientationFixture |
| 5300 | : public InputDispatcherDisplayProjectionTest, |
| 5301 | public ::testing::WithParamInterface<ui::Rotation> {}; |
| 5302 | |
| 5303 | // This test verifies the touchable region of a window for all rotations of the display by tapping |
| 5304 | // in different locations on the display, specifically points close to the four corners of a |
| 5305 | // window. |
| 5306 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { |
| 5307 | constexpr static int32_t displayWidth = 400; |
| 5308 | constexpr static int32_t displayHeight = 800; |
| 5309 | |
| 5310 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5311 | |
| 5312 | const auto rotation = GetParam(); |
| 5313 | |
| 5314 | // Set up the display with the specified rotation. |
| 5315 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 5316 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 5317 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 5318 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 5319 | logicalDisplayWidth, logicalDisplayHeight); |
| 5320 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 5321 | |
| 5322 | // Create a window with its bounds determined in the logical display. |
| 5323 | const Rect frameInLogicalDisplay(100, 100, 200, 300); |
| 5324 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); |
| 5325 | sp<FakeWindowHandle> window = |
| 5326 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 5327 | window->setFrame(frameInDisplay, displayTransform); |
| 5328 | addWindow(window); |
| 5329 | |
| 5330 | // The following points in logical display space should be inside the window. |
| 5331 | static const std::array<vec2, 4> insidePoints{ |
| 5332 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 5333 | for (const auto pointInsideWindow : insidePoints) { |
| 5334 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); |
| 5335 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5336 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5337 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5338 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5339 | window->consumeMotionDown(); |
| 5340 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5341 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 5342 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5343 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5344 | window->consumeMotionUp(); |
| 5345 | } |
| 5346 | |
| 5347 | // The following points in logical display space should be outside the window. |
| 5348 | static const std::array<vec2, 5> outsidePoints{ |
| 5349 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 5350 | for (const auto pointOutsideWindow : outsidePoints) { |
| 5351 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); |
| 5352 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5353 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5354 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5355 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5356 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5357 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 5358 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5359 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5360 | } |
| 5361 | window->assertNoEvents(); |
| 5362 | } |
| 5363 | |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame^] | 5364 | // This test verifies the occlusion detection for all rotations of the display by tapping |
| 5365 | // in different locations on the display, specifically points close to the four corners of a |
| 5366 | // window. |
| 5367 | TEST_P(InputDispatcherDisplayOrientationFixture, BlockUntrustClickInDifferentOrientations) { |
| 5368 | constexpr static int32_t displayWidth = 400; |
| 5369 | constexpr static int32_t displayHeight = 800; |
| 5370 | |
| 5371 | std::shared_ptr<FakeApplicationHandle> untrustedWindowApplication = |
| 5372 | std::make_shared<FakeApplicationHandle>(); |
| 5373 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5374 | |
| 5375 | const auto rotation = GetParam(); |
| 5376 | |
| 5377 | // Set up the display with the specified rotation. |
| 5378 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 5379 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 5380 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 5381 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 5382 | logicalDisplayWidth, logicalDisplayHeight); |
| 5383 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 5384 | |
| 5385 | // Create a window that not trusted. |
| 5386 | const Rect untrustedWindowFrameInLogicalDisplay(100, 100, 200, 300); |
| 5387 | |
| 5388 | const Rect untrustedWindowFrameInDisplay = |
| 5389 | displayTransform.inverse().transform(untrustedWindowFrameInLogicalDisplay); |
| 5390 | |
| 5391 | sp<FakeWindowHandle> untrustedWindow = |
| 5392 | sp<FakeWindowHandle>::make(untrustedWindowApplication, mDispatcher, "UntrustedWindow", |
| 5393 | ADISPLAY_ID_DEFAULT); |
| 5394 | untrustedWindow->setFrame(untrustedWindowFrameInDisplay, displayTransform); |
| 5395 | untrustedWindow->setTrustedOverlay(false); |
| 5396 | untrustedWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 5397 | untrustedWindow->setTouchable(false); |
| 5398 | untrustedWindow->setAlpha(1.0f); |
| 5399 | untrustedWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
| 5400 | addWindow(untrustedWindow); |
| 5401 | |
| 5402 | // Create a simple app window below the untrusted window. |
| 5403 | const Rect simpleAppWindowFrameInLogicalDisplay(0, 0, 300, 600); |
| 5404 | const Rect simpleAppWindowFrameInDisplay = |
| 5405 | displayTransform.inverse().transform(simpleAppWindowFrameInLogicalDisplay); |
| 5406 | |
| 5407 | sp<FakeWindowHandle> simpleAppWindow = |
| 5408 | sp<FakeWindowHandle>::make(application, mDispatcher, "SimpleAppWindow", |
| 5409 | ADISPLAY_ID_DEFAULT); |
| 5410 | simpleAppWindow->setFrame(simpleAppWindowFrameInDisplay, displayTransform); |
| 5411 | simpleAppWindow->setOwnerInfo(gui::Pid{2}, gui::Uid{202}); |
| 5412 | addWindow(simpleAppWindow); |
| 5413 | |
| 5414 | // The following points in logical display space should be inside the untrusted window, so |
| 5415 | // the simple window could not receive events that coordinate is these point. |
| 5416 | static const std::array<vec2, 4> untrustedPoints{ |
| 5417 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 5418 | |
| 5419 | for (const auto untrustedPoint : untrustedPoints) { |
| 5420 | const vec2 p = displayTransform.inverse().transform(untrustedPoint); |
| 5421 | const PointF pointInDisplaySpace{p.x, p.y}; |
| 5422 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5423 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5424 | {pointInDisplaySpace})); |
| 5425 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 5426 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5427 | {pointInDisplaySpace})); |
| 5428 | } |
| 5429 | untrustedWindow->assertNoEvents(); |
| 5430 | simpleAppWindow->assertNoEvents(); |
| 5431 | // The following points in logical display space should be outside the untrusted window, so |
| 5432 | // the simple window should receive events that coordinate is these point. |
| 5433 | static const std::array<vec2, 5> trustedPoints{ |
| 5434 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 5435 | for (const auto trustedPoint : trustedPoints) { |
| 5436 | const vec2 p = displayTransform.inverse().transform(trustedPoint); |
| 5437 | const PointF pointInDisplaySpace{p.x, p.y}; |
| 5438 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5439 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5440 | {pointInDisplaySpace})); |
| 5441 | simpleAppWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 5442 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 5443 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 5444 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5445 | {pointInDisplaySpace})); |
| 5446 | simpleAppWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, |
| 5447 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 5448 | } |
| 5449 | untrustedWindow->assertNoEvents(); |
| 5450 | } |
| 5451 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5452 | // Run the precision tests for all rotations. |
| 5453 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, |
| 5454 | InputDispatcherDisplayOrientationFixture, |
| 5455 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, |
| 5456 | ui::ROTATION_270), |
| 5457 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { |
| 5458 | return ftl::enum_string(testParamInfo.param); |
| 5459 | }); |
| 5460 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5461 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 5462 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5463 | |
| 5464 | class TransferTouchFixture : public InputDispatcherTest, |
| 5465 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 5466 | |
| 5467 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5468 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5469 | |
| 5470 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5471 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5472 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5473 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5474 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5475 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5476 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5477 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5478 | sp<FakeWindowHandle> wallpaper = |
| 5479 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 5480 | wallpaper->setIsWallpaper(true); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5481 | // Add the windows to the dispatcher, and ensure the first window is focused |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5482 | mDispatcher->onWindowInfosChanged( |
| 5483 | {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5484 | setFocusedWindow(firstWindow); |
| 5485 | firstWindow->consumeFocusEvent(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5486 | |
| 5487 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5488 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5489 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5490 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5491 | // Only the first window should get the down event |
| 5492 | firstWindow->consumeMotionDown(); |
| 5493 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5494 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5495 | // Dispatcher reports pointer down outside focus for the wallpaper |
| 5496 | mFakePolicy->assertOnPointerDownEquals(wallpaper->getToken()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5497 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5498 | // Transfer touch to the second window |
| 5499 | TransferFunction f = GetParam(); |
| 5500 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5501 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5502 | // The first window gets cancel and the second gets down |
| 5503 | firstWindow->consumeMotionCancel(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5504 | secondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5505 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5506 | // There should not be any changes to the focused window when transferring touch |
| 5507 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertOnPointerDownWasNotCalled()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5508 | |
| 5509 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5510 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5511 | ADISPLAY_ID_DEFAULT)); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5512 | // The first window gets no events and the second gets up |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5513 | firstWindow->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5514 | secondWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5515 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5516 | } |
| 5517 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5518 | /** |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5519 | * When 'transferTouchGesture' API is invoked, dispatcher needs to find the "best" window to take |
| 5520 | * touch from. When we have spy windows, there are several windows to choose from: either spy, or |
| 5521 | * the 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5522 | * natural to the user. |
| 5523 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 5524 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 5525 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 5526 | * the first window to the second. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5527 | * The main goal here is to test the behaviour of 'transferTouchGesture' API, but it's still valid |
| 5528 | * to test the other API, as well. |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5529 | */ |
| 5530 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 5531 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5532 | |
| 5533 | // Create a couple of windows + a spy window |
| 5534 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5535 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5536 | spyWindow->setTrustedOverlay(true); |
| 5537 | spyWindow->setSpy(true); |
| 5538 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5539 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5540 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5541 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5542 | |
| 5543 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5544 | mDispatcher->onWindowInfosChanged( |
| 5545 | {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5546 | |
| 5547 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5548 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5549 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5550 | // Only the first window and spy should get the down event |
| 5551 | spyWindow->consumeMotionDown(); |
| 5552 | firstWindow->consumeMotionDown(); |
| 5553 | |
| 5554 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5555 | // if f === 'transferTouchGesture'. |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5556 | TransferFunction f = GetParam(); |
| 5557 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5558 | ASSERT_TRUE(success); |
| 5559 | // The first window gets cancel and the second gets down |
| 5560 | firstWindow->consumeMotionCancel(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5561 | secondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5562 | |
| 5563 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5564 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5565 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5566 | // The first window gets no events and the second+spy get up |
| 5567 | firstWindow->assertNoEvents(); |
| 5568 | spyWindow->consumeMotionUp(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5569 | secondWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5570 | } |
| 5571 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5572 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5573 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5574 | |
| 5575 | PointF touchPoint = {10, 10}; |
| 5576 | |
| 5577 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5578 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5579 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5580 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5581 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5582 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5583 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5584 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5585 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5586 | |
| 5587 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5588 | mDispatcher->onWindowInfosChanged( |
| 5589 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5590 | |
| 5591 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5592 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5593 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5594 | {touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5595 | // Only the first window should get the down event |
| 5596 | firstWindow->consumeMotionDown(); |
| 5597 | secondWindow->assertNoEvents(); |
| 5598 | |
| 5599 | // Send pointer down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5600 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5601 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5602 | // Only the first window should get the pointer down event |
| 5603 | firstWindow->consumeMotionPointerDown(1); |
| 5604 | secondWindow->assertNoEvents(); |
| 5605 | |
| 5606 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5607 | TransferFunction f = GetParam(); |
| 5608 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5609 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5610 | // The first window gets cancel and the second gets down and pointer down |
| 5611 | firstWindow->consumeMotionCancel(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5612 | secondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 5613 | secondWindow->consumeMotionPointerDown(1, ADISPLAY_ID_DEFAULT, |
| 5614 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5615 | |
| 5616 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5617 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5618 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5619 | // The first window gets nothing and the second gets pointer up |
| 5620 | firstWindow->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5621 | secondWindow->consumeMotionPointerUp(1, ADISPLAY_ID_DEFAULT, |
| 5622 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5623 | |
| 5624 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5625 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5626 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5627 | // The first window gets nothing and the second gets up |
| 5628 | firstWindow->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5629 | secondWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5630 | } |
| 5631 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5632 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 5633 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5634 | |
| 5635 | // Create a couple of windows |
| 5636 | sp<FakeWindowHandle> firstWindow = |
| 5637 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5638 | ADISPLAY_ID_DEFAULT); |
| 5639 | firstWindow->setDupTouchToWallpaper(true); |
| 5640 | sp<FakeWindowHandle> secondWindow = |
| 5641 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5642 | ADISPLAY_ID_DEFAULT); |
| 5643 | secondWindow->setDupTouchToWallpaper(true); |
| 5644 | |
| 5645 | sp<FakeWindowHandle> wallpaper1 = |
| 5646 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 5647 | wallpaper1->setIsWallpaper(true); |
| 5648 | |
| 5649 | sp<FakeWindowHandle> wallpaper2 = |
| 5650 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 5651 | wallpaper2->setIsWallpaper(true); |
| 5652 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5653 | mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(), |
| 5654 | *secondWindow->getInfo(), *wallpaper2->getInfo()}, |
| 5655 | {}, |
| 5656 | 0, |
| 5657 | 0}); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5658 | |
| 5659 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5660 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5661 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5662 | |
| 5663 | // Only the first window should get the down event |
| 5664 | firstWindow->consumeMotionDown(); |
| 5665 | secondWindow->assertNoEvents(); |
| 5666 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5667 | wallpaper2->assertNoEvents(); |
| 5668 | |
| 5669 | // Transfer touch focus to the second window |
| 5670 | TransferFunction f = GetParam(); |
| 5671 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5672 | ASSERT_TRUE(success); |
| 5673 | |
| 5674 | // The first window gets cancel and the second gets down |
| 5675 | firstWindow->consumeMotionCancel(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5676 | secondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5677 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5678 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 5679 | expectedWallpaperFlags | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5680 | |
| 5681 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5682 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5683 | ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5684 | // The first window gets no events and the second gets up |
| 5685 | firstWindow->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5686 | secondWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5687 | wallpaper1->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5688 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, |
| 5689 | expectedWallpaperFlags | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5690 | } |
| 5691 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5692 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5693 | // 'transferTouchGesture' and 'transferTouchOnDisplay' are equivalent in behaviour. They only differ |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5694 | // for the case where there are multiple pointers split across several windows. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5695 | INSTANTIATE_TEST_SUITE_P( |
| 5696 | InputDispatcherTransferFunctionTests, TransferTouchFixture, |
| 5697 | ::testing::Values( |
| 5698 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, sp<IBinder> /*ignored*/, |
| 5699 | sp<IBinder> destChannelToken) { |
| 5700 | return dispatcher->transferTouchOnDisplay(destChannelToken, |
| 5701 | ADISPLAY_ID_DEFAULT); |
| 5702 | }, |
| 5703 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, sp<IBinder> from, |
| 5704 | sp<IBinder> to) { |
| 5705 | return dispatcher->transferTouchGesture(from, to, |
| 5706 | /*isDragAndDrop=*/false); |
| 5707 | })); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5708 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5709 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5710 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5711 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5712 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5713 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5714 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5715 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5716 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5717 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5718 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5719 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5720 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5721 | |
| 5722 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5723 | mDispatcher->onWindowInfosChanged( |
| 5724 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5725 | |
| 5726 | PointF pointInFirst = {300, 200}; |
| 5727 | PointF pointInSecond = {300, 600}; |
| 5728 | |
| 5729 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5730 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5731 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5732 | {pointInFirst})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5733 | // Only the first window should get the down event |
| 5734 | firstWindow->consumeMotionDown(); |
| 5735 | secondWindow->assertNoEvents(); |
| 5736 | |
| 5737 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5738 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5739 | ADISPLAY_ID_DEFAULT, |
| 5740 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5741 | // The first window gets a move and the second a down |
| 5742 | firstWindow->consumeMotionMove(); |
| 5743 | secondWindow->consumeMotionDown(); |
| 5744 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5745 | // Transfer touch to the second window |
| 5746 | mDispatcher->transferTouchGesture(firstWindow->getToken(), secondWindow->getToken()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5747 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 5748 | firstWindow->consumeMotionCancel(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5749 | secondWindow->consumeMotionPointerDown(1, ADISPLAY_ID_DEFAULT, |
| 5750 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5751 | |
| 5752 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5753 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5754 | ADISPLAY_ID_DEFAULT, |
| 5755 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5756 | // The first window gets nothing and the second gets pointer up |
| 5757 | firstWindow->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5758 | secondWindow->consumeMotionPointerUp(1, ADISPLAY_ID_DEFAULT, |
| 5759 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5760 | |
| 5761 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5762 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5763 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5764 | // The first window gets nothing and the second gets up |
| 5765 | firstWindow->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5766 | secondWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5767 | } |
| 5768 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5769 | // Same as TransferTouch_TwoPointersSplitTouch, but using 'transferTouchOnDisplay' api. |
| 5770 | // Unlike 'transferTouchGesture', calling 'transferTouchOnDisplay' when there are two windows |
| 5771 | // receiving touch is not supported, so the touch should continue on those windows and the |
| 5772 | // transferred-to window should get nothing. |
| 5773 | TEST_F(InputDispatcherTest, TransferTouchOnDisplay_TwoPointersSplitTouch) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5774 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5775 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5776 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5777 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5778 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5779 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5780 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5781 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5782 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5783 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5784 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5785 | |
| 5786 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5787 | mDispatcher->onWindowInfosChanged( |
| 5788 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5789 | |
| 5790 | PointF pointInFirst = {300, 200}; |
| 5791 | PointF pointInSecond = {300, 600}; |
| 5792 | |
| 5793 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5794 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5795 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5796 | {pointInFirst})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5797 | // Only the first window should get the down event |
| 5798 | firstWindow->consumeMotionDown(); |
| 5799 | secondWindow->assertNoEvents(); |
| 5800 | |
| 5801 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5802 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5803 | ADISPLAY_ID_DEFAULT, |
| 5804 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5805 | // The first window gets a move and the second a down |
| 5806 | firstWindow->consumeMotionMove(); |
| 5807 | secondWindow->consumeMotionDown(); |
| 5808 | |
| 5809 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5810 | const bool transferred = |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5811 | mDispatcher->transferTouchOnDisplay(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
| 5812 | // The 'transferTouchOnDisplay' call should not succeed, because there are 2 touched windows |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5813 | ASSERT_FALSE(transferred); |
| 5814 | firstWindow->assertNoEvents(); |
| 5815 | secondWindow->assertNoEvents(); |
| 5816 | |
| 5817 | // The rest of the dispatch should proceed as normal |
| 5818 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5819 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5820 | ADISPLAY_ID_DEFAULT, |
| 5821 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5822 | // The first window gets MOVE and the second gets pointer up |
| 5823 | firstWindow->consumeMotionMove(); |
| 5824 | secondWindow->consumeMotionUp(); |
| 5825 | |
| 5826 | // Send up event to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5827 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5828 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5829 | // The first window gets nothing and the second gets up |
| 5830 | firstWindow->consumeMotionUp(); |
| 5831 | secondWindow->assertNoEvents(); |
| 5832 | } |
| 5833 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5834 | // This case will create two windows and one mirrored window on the default display and mirror |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5835 | // two windows on the second display. It will test if 'transferTouchGesture' works fine if we put |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5836 | // the windows info of second display before default display. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5837 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5838 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5839 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5840 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5841 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5842 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5843 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5844 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5845 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5846 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5847 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5848 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5849 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5850 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5851 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5852 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5853 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5854 | |
| 5855 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5856 | mDispatcher->onWindowInfosChanged( |
| 5857 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 5858 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 5859 | *secondWindowInPrimary->getInfo()}, |
| 5860 | {}, |
| 5861 | 0, |
| 5862 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5863 | |
| 5864 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5865 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5866 | {50, 50})) |
| 5867 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5868 | |
| 5869 | // Window should receive motion event. |
| 5870 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5871 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5872 | // Transfer touch |
| 5873 | ASSERT_TRUE(mDispatcher->transferTouchGesture(firstWindowInPrimary->getToken(), |
| 5874 | secondWindowInPrimary->getToken())); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5875 | // The first window gets cancel. |
| 5876 | firstWindowInPrimary->consumeMotionCancel(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5877 | secondWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 5878 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5879 | |
| 5880 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5881 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5882 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 5883 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5884 | firstWindowInPrimary->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5885 | secondWindowInPrimary->consumeMotionMove(ADISPLAY_ID_DEFAULT, |
| 5886 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5887 | |
| 5888 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5889 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5890 | {150, 50})) |
| 5891 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5892 | firstWindowInPrimary->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5893 | secondWindowInPrimary->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5894 | } |
| 5895 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5896 | // Same as TransferTouch_CloneSurface, but this touch on the secondary display and use |
| 5897 | // 'transferTouchOnDisplay' api. |
| 5898 | TEST_F(InputDispatcherTest, TransferTouchOnDisplay_CloneSurface) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5899 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5900 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5901 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5902 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5903 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5904 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5905 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5906 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5907 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5908 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5909 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5910 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5911 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5912 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5913 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5914 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5915 | |
| 5916 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5917 | mDispatcher->onWindowInfosChanged( |
| 5918 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 5919 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 5920 | *secondWindowInPrimary->getInfo()}, |
| 5921 | {}, |
| 5922 | 0, |
| 5923 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5924 | |
| 5925 | // Touch on second display. |
| 5926 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5927 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 5928 | {50, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5929 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5930 | |
| 5931 | // Window should receive motion event. |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 5932 | firstWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5933 | |
| 5934 | // Transfer touch focus |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5935 | ASSERT_TRUE(mDispatcher->transferTouchOnDisplay(secondWindowInSecondary->getToken(), |
| 5936 | SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5937 | |
| 5938 | // The first window gets cancel. |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 5939 | firstWindowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5940 | secondWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, |
| 5941 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5942 | |
| 5943 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5944 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5945 | SECOND_DISPLAY_ID, {150, 50})) |
| 5946 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 5947 | firstWindowInSecondary->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5948 | secondWindowInSecondary->consumeMotionMove(SECOND_DISPLAY_ID, |
| 5949 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5950 | |
| 5951 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5952 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5953 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 5954 | firstWindowInSecondary->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5955 | secondWindowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5956 | } |
| 5957 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5958 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5959 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5960 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5961 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5962 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5963 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5964 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5965 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5966 | |
| 5967 | window->consumeFocusEvent(true); |
| 5968 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5969 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5970 | |
| 5971 | // Window should receive key down event. |
| 5972 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5973 | |
| 5974 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5975 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5976 | mFakePolicy->assertUserActivityPoked(); |
| 5977 | } |
| 5978 | |
| 5979 | TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) { |
| 5980 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5981 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5982 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5983 | |
| 5984 | window->setDisableUserActivity(true); |
| 5985 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5986 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5987 | setFocusedWindow(window); |
| 5988 | |
| 5989 | window->consumeFocusEvent(true); |
| 5990 | |
| 5991 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5992 | |
| 5993 | // Window should receive key down event. |
| 5994 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5995 | |
| 5996 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5997 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5998 | mFakePolicy->assertUserActivityNotPoked(); |
| 5999 | } |
| 6000 | |
| 6001 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) { |
| 6002 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6003 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6004 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 6005 | |
| 6006 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6007 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6008 | setFocusedWindow(window); |
| 6009 | |
| 6010 | window->consumeFocusEvent(true); |
| 6011 | |
| 6012 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 6013 | mDispatcher->waitForIdle(); |
| 6014 | |
| 6015 | // System key is not passed down |
| 6016 | window->assertNoEvents(); |
| 6017 | |
| 6018 | // Should have poked user activity |
| 6019 | mFakePolicy->assertUserActivityPoked(); |
| 6020 | } |
| 6021 | |
| 6022 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) { |
| 6023 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6024 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6025 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 6026 | |
| 6027 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6028 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6029 | setFocusedWindow(window); |
| 6030 | |
| 6031 | window->consumeFocusEvent(true); |
| 6032 | |
| 6033 | mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 6034 | mDispatcher->waitForIdle(); |
| 6035 | |
| 6036 | // System key is not passed down |
| 6037 | window->assertNoEvents(); |
| 6038 | |
| 6039 | // Should have poked user activity |
| 6040 | mFakePolicy->assertUserActivityPoked(); |
| 6041 | } |
| 6042 | |
| 6043 | TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) { |
| 6044 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6045 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6046 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 6047 | |
| 6048 | window->setDisableUserActivity(true); |
| 6049 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6050 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6051 | setFocusedWindow(window); |
| 6052 | |
| 6053 | window->consumeFocusEvent(true); |
| 6054 | |
| 6055 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 6056 | mDispatcher->waitForIdle(); |
| 6057 | |
| 6058 | // System key is not passed down |
| 6059 | window->assertNoEvents(); |
| 6060 | |
| 6061 | // Should have poked user activity |
| 6062 | mFakePolicy->assertUserActivityPoked(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6063 | } |
| 6064 | |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6065 | TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) { |
| 6066 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6067 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6068 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 6069 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6070 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6071 | |
| 6072 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6073 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6074 | ADISPLAY_ID_DEFAULT, {100, 100})) |
| 6075 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6076 | |
| 6077 | window->consumeMotionEvent( |
| 6078 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 6079 | |
| 6080 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 6081 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6082 | mFakePolicy->assertUserActivityPoked(); |
| 6083 | } |
| 6084 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6085 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6086 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6087 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6088 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6089 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6090 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6091 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6092 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6093 | mDispatcher->waitForIdle(); |
| 6094 | |
| 6095 | window->assertNoEvents(); |
| 6096 | } |
| 6097 | |
| 6098 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 6099 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6100 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6101 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6102 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6103 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6104 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6105 | |
| 6106 | // Send key |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6107 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6108 | // Send motion |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6109 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 6110 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6111 | |
| 6112 | // Window should receive only the motion event |
| 6113 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6114 | window->assertNoEvents(); // Key event or focus event will not be received |
| 6115 | } |
| 6116 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6117 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 6118 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6119 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6120 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6121 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 6122 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6123 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6124 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6125 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6126 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 6127 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6128 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6129 | |
| 6130 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6131 | mDispatcher->onWindowInfosChanged( |
| 6132 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6133 | |
| 6134 | PointF pointInFirst = {300, 200}; |
| 6135 | PointF pointInSecond = {300, 600}; |
| 6136 | |
| 6137 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6138 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 6139 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6140 | {pointInFirst})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6141 | // Only the first window should get the down event |
| 6142 | firstWindow->consumeMotionDown(); |
| 6143 | secondWindow->assertNoEvents(); |
| 6144 | |
| 6145 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6146 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6147 | ADISPLAY_ID_DEFAULT, |
| 6148 | {pointInFirst, pointInSecond})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6149 | // The first window gets a move and the second a down |
| 6150 | firstWindow->consumeMotionMove(); |
| 6151 | secondWindow->consumeMotionDown(); |
| 6152 | |
| 6153 | // Send pointer cancel to the second window |
| 6154 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6155 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6156 | {pointInFirst, pointInSecond}); |
| 6157 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6158 | mDispatcher->notifyMotion(pointerUpMotionArgs); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6159 | // The first window gets move and the second gets cancel. |
| 6160 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 6161 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 6162 | |
| 6163 | // Send up event. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6164 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6165 | ADISPLAY_ID_DEFAULT)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6166 | // The first window gets up and the second gets nothing. |
| 6167 | firstWindow->consumeMotionUp(); |
| 6168 | secondWindow->assertNoEvents(); |
| 6169 | } |
| 6170 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6171 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 6172 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6173 | |
| 6174 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6175 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6176 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6177 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 6178 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 6179 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 6180 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6181 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6182 | window->assertNoEvents(); |
| 6183 | mDispatcher->waitForIdle(); |
| 6184 | } |
| 6185 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6186 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 6187 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6188 | /** |
| 6189 | * Two entities that receive touch: A window, and a global monitor. |
| 6190 | * The touch goes to the window, and then the window disappears. |
| 6191 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 6192 | * for the monitor, as well. |
| 6193 | * 1. foregroundWindow |
| 6194 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 6195 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6196 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6197 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6198 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6199 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6200 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6201 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6202 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6203 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6204 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6205 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6206 | {100, 200})) |
| 6207 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6208 | |
| 6209 | // Both the foreground window and the global monitor should receive the touch down |
| 6210 | window->consumeMotionDown(); |
| 6211 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6212 | |
| 6213 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6214 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6215 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 6216 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6217 | |
| 6218 | window->consumeMotionMove(); |
| 6219 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6220 | |
| 6221 | // Now the foreground window goes away |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6222 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6223 | window->consumeMotionCancel(); |
| 6224 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 6225 | |
| 6226 | // If more events come in, there will be no more foreground window to send them to. This will |
| 6227 | // cause a cancel for the monitor, as well. |
| 6228 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6229 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6230 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 6231 | << "Injection should fail because the window was removed"; |
| 6232 | window->assertNoEvents(); |
| 6233 | // Global monitor now gets the cancel |
| 6234 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6235 | } |
| 6236 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6237 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6238 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6239 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6240 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6241 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6242 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6243 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6244 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6245 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6246 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6247 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6248 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6249 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6250 | } |
| 6251 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6252 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6253 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6254 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6255 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6256 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6257 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6258 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6259 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6260 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6261 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6262 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6263 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6264 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6265 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6266 | // Pilfer pointers from the monitor. |
| 6267 | // This should not do anything and the window should continue to receive events. |
| 6268 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6269 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6270 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6271 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6272 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6273 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6274 | |
| 6275 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6276 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6277 | } |
| 6278 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6279 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6280 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6281 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6282 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6283 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6284 | window->setWindowOffset(20, 40); |
| 6285 | window->setWindowTransform(0, 1, -1, 0); |
| 6286 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6287 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6288 | |
| 6289 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6290 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6291 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6292 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6293 | std::unique_ptr<MotionEvent> event = monitor.consumeMotion(); |
| 6294 | ASSERT_NE(nullptr, event); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6295 | // Even though window has transform, gesture monitor must not. |
| 6296 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 6297 | } |
| 6298 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6299 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6300 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6301 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6302 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6303 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6304 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6305 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 6306 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6307 | } |
| 6308 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6309 | /** |
| 6310 | * Two displays |
| 6311 | * The first monitor has a foreground window, a monitor |
| 6312 | * The second window has only one monitor. |
| 6313 | * We first inject a Down event into the first display, this injection should succeed and both |
| 6314 | * the foreground window and monitor should receive a down event, then inject a Down event into |
| 6315 | * the second display as well, this injection should fail, at this point, the first display |
| 6316 | * window and monitor should not receive a cancel or any other event. |
| 6317 | * Continue to inject Move and UP events to the first display, the events should be received |
| 6318 | * normally by the foreground window and monitor. |
| 6319 | */ |
| 6320 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCanceledWhenAnotherEmptyDisplayReceiveEvents) { |
| 6321 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6322 | sp<FakeWindowHandle> window = |
| 6323 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 6324 | |
| 6325 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6326 | FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 6327 | |
| 6328 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 6329 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6330 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6331 | {100, 200})) |
| 6332 | << "The down event injected into the first display should succeed"; |
| 6333 | |
| 6334 | window->consumeMotionDown(); |
| 6335 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6336 | |
| 6337 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6338 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 6339 | {100, 200})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6340 | << "The down event injected into the second display should fail since there's no " |
| 6341 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6342 | |
| 6343 | // Continue to inject event to first display. |
| 6344 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6345 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6346 | ADISPLAY_ID_DEFAULT, {110, 220})) |
| 6347 | << "The move event injected into the first display should succeed"; |
| 6348 | |
| 6349 | window->consumeMotionMove(); |
| 6350 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6351 | |
| 6352 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6353 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6354 | {110, 220})) |
| 6355 | << "The up event injected into the first display should succeed"; |
| 6356 | |
| 6357 | window->consumeMotionUp(); |
| 6358 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6359 | |
| 6360 | window->assertNoEvents(); |
| 6361 | monitor.assertNoEvents(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6362 | secondMonitor.assertNoEvents(); |
| 6363 | } |
| 6364 | |
| 6365 | /** |
| 6366 | * Two displays |
| 6367 | * There is a monitor and foreground window on each display. |
| 6368 | * First, we inject down events into each of the two displays, at this point, the foreground windows |
| 6369 | * and monitors on both displays should receive down events. |
| 6370 | * At this point, the foreground window of the second display goes away, the gone window should |
| 6371 | * receive the cancel event, and the other windows and monitors should not receive any events. |
| 6372 | * Inject a move event into the second display. At this point, the injection should fail because |
| 6373 | * the second display no longer has a foreground window. At this point, the monitor on the second |
| 6374 | * display should receive a cancel event, and any windows or monitors on the first display should |
| 6375 | * not receive any events, and any subsequent injection of events into the second display should |
| 6376 | * also fail. |
| 6377 | * Continue to inject events into the first display, and the events should all be injected |
| 6378 | * successfully and received normally. |
| 6379 | */ |
| 6380 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCancelWhenAnotherDisplayMonitorTouchCanceled) { |
| 6381 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6382 | sp<FakeWindowHandle> window = |
| 6383 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 6384 | sp<FakeWindowHandle> secondWindow = |
| 6385 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondForeground", |
| 6386 | SECOND_DISPLAY_ID); |
| 6387 | |
| 6388 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6389 | FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 6390 | |
| 6391 | // There is a foreground window on both displays. |
| 6392 | mDispatcher->onWindowInfosChanged({{*window->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
| 6393 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6394 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6395 | {100, 200})) |
| 6396 | << "The down event injected into the first display should succeed"; |
| 6397 | |
| 6398 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6399 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6400 | |
| 6401 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6402 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 6403 | {100, 200})) |
| 6404 | << "The down event injected into the second display should succeed"; |
| 6405 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6406 | secondWindow->consumeMotionDown(SECOND_DISPLAY_ID); |
| 6407 | secondMonitor.consumeMotionDown(SECOND_DISPLAY_ID); |
| 6408 | |
| 6409 | // Now second window is gone away. |
| 6410 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 6411 | |
| 6412 | // The gone window should receive a cancel, and the monitor on the second display should not |
| 6413 | // receive any events. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6414 | secondWindow->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 6415 | secondMonitor.assertNoEvents(); |
| 6416 | |
| 6417 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6418 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6419 | SECOND_DISPLAY_ID, {110, 220})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6420 | << "The move event injected into the second display should fail because there's no " |
| 6421 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6422 | // Now the monitor on the second display should receive a cancel event. |
| 6423 | secondMonitor.consumeMotionCancel(SECOND_DISPLAY_ID); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6424 | |
| 6425 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6426 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6427 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 6428 | << "The move event injected into the first display should succeed"; |
| 6429 | |
| 6430 | window->consumeMotionMove(); |
| 6431 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6432 | |
| 6433 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6434 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 6435 | {110, 220})) |
| 6436 | << "The up event injected into the second display should fail because there's no " |
| 6437 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6438 | |
| 6439 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6440 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6441 | {110, 220})) |
| 6442 | << "The up event injected into the first display should succeed"; |
| 6443 | |
| 6444 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6445 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6446 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6447 | window->assertNoEvents(); |
| 6448 | monitor.assertNoEvents(); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6449 | secondWindow->assertNoEvents(); |
| 6450 | secondMonitor.assertNoEvents(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6451 | } |
| 6452 | |
| 6453 | /** |
| 6454 | * One display with transform |
| 6455 | * There is a foreground window and a monitor on the display |
| 6456 | * Inject down event and move event sequentially, the foreground window and monitor can receive down |
| 6457 | * event and move event, then let the foreground window go away, the foreground window receives |
| 6458 | * cancel event, inject move event again, the monitor receives cancel event, all the events received |
| 6459 | * by the monitor should be with the same transform as the display |
| 6460 | */ |
| 6461 | TEST_F(InputDispatcherMonitorTest, MonitorTouchCancelEventWithDisplayTransform) { |
| 6462 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6463 | sp<FakeWindowHandle> window = |
| 6464 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 6465 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6466 | |
| 6467 | ui::Transform transform; |
| 6468 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 6469 | |
| 6470 | gui::DisplayInfo displayInfo; |
| 6471 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 6472 | displayInfo.transform = transform; |
| 6473 | |
| 6474 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
| 6475 | |
| 6476 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6477 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6478 | {100, 200})) |
| 6479 | << "The down event injected should succeed"; |
| 6480 | |
| 6481 | window->consumeMotionDown(); |
| 6482 | std::unique_ptr<MotionEvent> downMotionEvent = monitor.consumeMotion(); |
| 6483 | EXPECT_EQ(transform, downMotionEvent->getTransform()); |
| 6484 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, downMotionEvent->getAction()); |
| 6485 | |
| 6486 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6487 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6488 | ADISPLAY_ID_DEFAULT, {110, 220})) |
| 6489 | << "The move event injected should succeed"; |
| 6490 | |
| 6491 | window->consumeMotionMove(); |
| 6492 | std::unique_ptr<MotionEvent> moveMotionEvent = monitor.consumeMotion(); |
| 6493 | EXPECT_EQ(transform, moveMotionEvent->getTransform()); |
| 6494 | EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, moveMotionEvent->getAction()); |
| 6495 | |
| 6496 | // Let foreground window gone |
| 6497 | mDispatcher->onWindowInfosChanged({{}, {displayInfo}, 0, 0}); |
| 6498 | |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6499 | // Foreground window should receive a cancel event, but not the monitor. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6500 | window->consumeMotionCancel(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6501 | |
| 6502 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6503 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6504 | ADISPLAY_ID_DEFAULT, {110, 220})) |
| 6505 | << "The move event injected should failed"; |
| 6506 | // Now foreground should not receive any events, but monitor should receive a cancel event |
| 6507 | // with transform that same as display's display. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6508 | std::unique_ptr<MotionEvent> cancelMotionEvent = monitor.consumeMotion(); |
| 6509 | EXPECT_EQ(transform, cancelMotionEvent->getTransform()); |
| 6510 | EXPECT_EQ(ADISPLAY_ID_DEFAULT, cancelMotionEvent->getDisplayId()); |
| 6511 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, cancelMotionEvent->getAction()); |
| 6512 | |
| 6513 | // Other event inject to this display should fail. |
| 6514 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6515 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6516 | ADISPLAY_ID_DEFAULT, {110, 220})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6517 | << "The up event injected should fail because the touched window was removed"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6518 | window->assertNoEvents(); |
| 6519 | monitor.assertNoEvents(); |
| 6520 | } |
| 6521 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6522 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6523 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6524 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6525 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6526 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6527 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6528 | |
| 6529 | NotifyMotionArgs motionArgs = |
| 6530 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6531 | ADISPLAY_ID_DEFAULT); |
| 6532 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6533 | mDispatcher->notifyMotion(motionArgs); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6534 | // Window should receive motion down event. |
| 6535 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6536 | |
| 6537 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6538 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6539 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6540 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 6541 | motionArgs.pointerCoords[0].getX() - 10); |
| 6542 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6543 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 6544 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT, /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6545 | } |
| 6546 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6547 | /** |
| 6548 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 6549 | * the device default right away. In the test scenario, we check both the default value, |
| 6550 | * and the action of enabling / disabling. |
| 6551 | */ |
| 6552 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6553 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6554 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6555 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6556 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6557 | |
| 6558 | // Set focused application. |
| 6559 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6560 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6561 | |
| 6562 | SCOPED_TRACE("Check default value of touch mode"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6563 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6564 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6565 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6566 | |
| 6567 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6568 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6569 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6570 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6571 | |
| 6572 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6573 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6574 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6575 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6576 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6577 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6578 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6579 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6580 | |
| 6581 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6582 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6583 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6584 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6585 | |
| 6586 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6587 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6588 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6589 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6590 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6591 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6592 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6593 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6594 | |
| 6595 | window->assertNoEvents(); |
| 6596 | } |
| 6597 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6598 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6599 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6600 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6601 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6602 | |
| 6603 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6604 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6605 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6606 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6607 | setFocusedWindow(window); |
| 6608 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6609 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6610 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6611 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 6612 | mDispatcher->notifyKey(keyArgs); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6613 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6614 | std::unique_ptr<KeyEvent> event = window->consumeKey(); |
| 6615 | ASSERT_NE(event, nullptr); |
| 6616 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6617 | ASSERT_NE(verified, nullptr); |
| 6618 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 6619 | |
| 6620 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 6621 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 6622 | ASSERT_EQ(keyArgs.source, verified->source); |
| 6623 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 6624 | |
| 6625 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 6626 | |
| 6627 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6628 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6629 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6630 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 6631 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 6632 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 6633 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 6634 | } |
| 6635 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6636 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6637 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6638 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6639 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6640 | |
| 6641 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6642 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 6643 | ui::Transform transform; |
| 6644 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 6645 | |
| 6646 | gui::DisplayInfo displayInfo; |
| 6647 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 6648 | displayInfo.transform = transform; |
| 6649 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6650 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6651 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6652 | const NotifyMotionArgs motionArgs = |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6653 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6654 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6655 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6656 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6657 | std::unique_ptr<MotionEvent> event = window->consumeMotionEvent(); |
| 6658 | ASSERT_NE(nullptr, event); |
| 6659 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6660 | ASSERT_NE(verified, nullptr); |
| 6661 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 6662 | |
| 6663 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 6664 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 6665 | EXPECT_EQ(motionArgs.source, verified->source); |
| 6666 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 6667 | |
| 6668 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 6669 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 6670 | const vec2 rawXY = |
| 6671 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 6672 | motionArgs.pointerCoords[0].getXYValue()); |
| 6673 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 6674 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6675 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6676 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6677 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6678 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 6679 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 6680 | } |
| 6681 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 6682 | /** |
| 6683 | * Ensure that separate calls to sign the same data are generating the same key. |
| 6684 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 6685 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 6686 | * tests. |
| 6687 | */ |
| 6688 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 6689 | KeyEvent event = getTestKeyEvent(); |
| 6690 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 6691 | |
| 6692 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 6693 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 6694 | ASSERT_EQ(hmac1, hmac2); |
| 6695 | } |
| 6696 | |
| 6697 | /** |
| 6698 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 6699 | */ |
| 6700 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 6701 | KeyEvent event = getTestKeyEvent(); |
| 6702 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 6703 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 6704 | |
| 6705 | verifiedEvent.deviceId += 1; |
| 6706 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6707 | |
| 6708 | verifiedEvent.source += 1; |
| 6709 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6710 | |
| 6711 | verifiedEvent.eventTimeNanos += 1; |
| 6712 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6713 | |
| 6714 | verifiedEvent.displayId += 1; |
| 6715 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6716 | |
| 6717 | verifiedEvent.action += 1; |
| 6718 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6719 | |
| 6720 | verifiedEvent.downTimeNanos += 1; |
| 6721 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6722 | |
| 6723 | verifiedEvent.flags += 1; |
| 6724 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6725 | |
| 6726 | verifiedEvent.keyCode += 1; |
| 6727 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6728 | |
| 6729 | verifiedEvent.scanCode += 1; |
| 6730 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6731 | |
| 6732 | verifiedEvent.metaState += 1; |
| 6733 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6734 | |
| 6735 | verifiedEvent.repeatCount += 1; |
| 6736 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6737 | } |
| 6738 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6739 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 6740 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6741 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6742 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6743 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6744 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6745 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6746 | |
| 6747 | // Top window is also focusable but is not granted focus. |
| 6748 | windowTop->setFocusable(true); |
| 6749 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6750 | mDispatcher->onWindowInfosChanged( |
| 6751 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6752 | setFocusedWindow(windowSecond); |
| 6753 | |
| 6754 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6755 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6756 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6757 | |
| 6758 | // Focused window should receive event. |
| 6759 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6760 | windowTop->assertNoEvents(); |
| 6761 | } |
| 6762 | |
| 6763 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 6764 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6765 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6766 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6767 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6768 | |
| 6769 | window->setFocusable(true); |
| 6770 | // Release channel for window is no longer valid. |
| 6771 | window->releaseChannel(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6772 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6773 | setFocusedWindow(window); |
| 6774 | |
| 6775 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6776 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6777 | |
| 6778 | // window channel is invalid, so it should not receive any input event. |
| 6779 | window->assertNoEvents(); |
| 6780 | } |
| 6781 | |
| 6782 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 6783 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6784 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6785 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6786 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6787 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6788 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6789 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6790 | setFocusedWindow(window); |
| 6791 | |
| 6792 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6793 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6794 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6795 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6796 | window->assertNoEvents(); |
| 6797 | } |
| 6798 | |
| 6799 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 6800 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6801 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6802 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6803 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6804 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6805 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6806 | |
| 6807 | windowTop->setFocusable(true); |
| 6808 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6809 | mDispatcher->onWindowInfosChanged( |
| 6810 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6811 | setFocusedWindow(windowTop); |
| 6812 | windowTop->consumeFocusEvent(true); |
| 6813 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6814 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6815 | mDispatcher->onWindowInfosChanged( |
| 6816 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6817 | windowSecond->consumeFocusEvent(true); |
| 6818 | windowTop->consumeFocusEvent(false); |
| 6819 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6820 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6821 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6822 | |
| 6823 | // Focused window should receive event. |
| 6824 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6825 | } |
| 6826 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6827 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6828 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6829 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6830 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6831 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6832 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6833 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6834 | |
| 6835 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6836 | windowSecond->setFocusable(false); |
| 6837 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6838 | mDispatcher->onWindowInfosChanged( |
| 6839 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6840 | setFocusedWindow(windowTop); |
| 6841 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6842 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6843 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6844 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6845 | |
| 6846 | // Event should be dropped. |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6847 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6848 | windowSecond->assertNoEvents(); |
| 6849 | } |
| 6850 | |
| 6851 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 6852 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6853 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6854 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6855 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6856 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 6857 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6858 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6859 | |
| 6860 | window->setFocusable(true); |
| 6861 | previousFocusedWindow->setFocusable(true); |
| 6862 | window->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6863 | mDispatcher->onWindowInfosChanged( |
| 6864 | {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6865 | setFocusedWindow(previousFocusedWindow); |
| 6866 | previousFocusedWindow->consumeFocusEvent(true); |
| 6867 | |
| 6868 | // Requesting focus on invisible window takes focus from currently focused window. |
| 6869 | setFocusedWindow(window); |
| 6870 | previousFocusedWindow->consumeFocusEvent(false); |
| 6871 | |
| 6872 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6873 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6874 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 6875 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6876 | |
| 6877 | // Window does not get focus event or key down. |
| 6878 | window->assertNoEvents(); |
| 6879 | |
| 6880 | // Window becomes visible. |
| 6881 | window->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6882 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6883 | |
| 6884 | // Window receives focus event. |
| 6885 | window->consumeFocusEvent(true); |
| 6886 | // Focused window receives key down. |
| 6887 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6888 | } |
| 6889 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6890 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 6891 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6892 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6893 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6894 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6895 | |
| 6896 | // window is granted focus. |
| 6897 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6898 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6899 | setFocusedWindow(window); |
| 6900 | window->consumeFocusEvent(true); |
| 6901 | |
| 6902 | // When a display is removed window loses focus. |
| 6903 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 6904 | window->consumeFocusEvent(false); |
| 6905 | } |
| 6906 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6907 | /** |
| 6908 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 6909 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 6910 | * of the 'slipperyEnterWindow'. |
| 6911 | * |
| 6912 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 6913 | * a way so that the touched location is no longer covered by the top window. |
| 6914 | * |
| 6915 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 6916 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 6917 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 6918 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 6919 | * with ACTION_DOWN). |
| 6920 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 6921 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 6922 | * |
| 6923 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 6924 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 6925 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 6926 | * |
| 6927 | * In this test, we ensure that the event received by the bottom window has |
| 6928 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 6929 | */ |
| 6930 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6931 | constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6932 | constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1}; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6933 | |
| 6934 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6935 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6936 | |
| 6937 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6938 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6939 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6940 | // Make sure this one overlaps the bottom window |
| 6941 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 6942 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 6943 | // one. Windows with the same owner are not considered to be occluding each other. |
| 6944 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 6945 | |
| 6946 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6947 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6948 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6949 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6950 | mDispatcher->onWindowInfosChanged( |
| 6951 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6952 | |
| 6953 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6954 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 6955 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6956 | {{50, 50}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6957 | slipperyExitWindow->consumeMotionDown(); |
| 6958 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6959 | mDispatcher->onWindowInfosChanged( |
| 6960 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6961 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6962 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
| 6963 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6964 | {{51, 51}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6965 | |
| 6966 | slipperyExitWindow->consumeMotionCancel(); |
| 6967 | |
| 6968 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 6969 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 6970 | } |
| 6971 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 6972 | /** |
| 6973 | * Two windows, one on the left and another on the right. The left window is slippery. The right |
| 6974 | * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the |
| 6975 | * touch moves from the left window into the right window, the gesture should continue to go to the |
| 6976 | * left window. Touch shouldn't slip because the right window can't receive touches. This test |
| 6977 | * reproduces a crash. |
| 6978 | */ |
| 6979 | TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { |
| 6980 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6981 | |
| 6982 | sp<FakeWindowHandle> leftSlipperyWindow = |
| 6983 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 6984 | leftSlipperyWindow->setSlippery(true); |
| 6985 | leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6986 | |
| 6987 | sp<FakeWindowHandle> rightDropTouchesWindow = |
| 6988 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 6989 | rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100)); |
| 6990 | rightDropTouchesWindow->setDropInput(true); |
| 6991 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6992 | mDispatcher->onWindowInfosChanged( |
| 6993 | {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 6994 | |
| 6995 | // Start touch in the left window |
| 6996 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6997 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6998 | .build()); |
| 6999 | leftSlipperyWindow->consumeMotionDown(); |
| 7000 | |
| 7001 | // And move it into the right window |
| 7002 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7003 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 7004 | .build()); |
| 7005 | |
| 7006 | // Since the right window isn't eligible to receive input, touch does not slip. |
| 7007 | // The left window continues to receive the gesture. |
| 7008 | leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 7009 | rightDropTouchesWindow->assertNoEvents(); |
| 7010 | } |
| 7011 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 7012 | /** |
| 7013 | * A single window is on screen first. Touch is injected into that window. Next, a second window |
| 7014 | * appears. Since the first window is slippery, touch will move from the first window to the second. |
| 7015 | */ |
| 7016 | TEST_F(InputDispatcherTest, InjectedTouchSlips) { |
| 7017 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7018 | sp<FakeWindowHandle> originalWindow = |
| 7019 | sp<FakeWindowHandle>::make(application, mDispatcher, "Original", ADISPLAY_ID_DEFAULT); |
| 7020 | originalWindow->setFrame(Rect(0, 0, 200, 200)); |
| 7021 | originalWindow->setSlippery(true); |
| 7022 | |
| 7023 | sp<FakeWindowHandle> appearingWindow = |
| 7024 | sp<FakeWindowHandle>::make(application, mDispatcher, "Appearing", ADISPLAY_ID_DEFAULT); |
| 7025 | appearingWindow->setFrame(Rect(0, 0, 200, 200)); |
| 7026 | |
| 7027 | mDispatcher->onWindowInfosChanged({{*originalWindow->getInfo()}, {}, 0, 0}); |
| 7028 | |
| 7029 | // Touch down on the original window |
| 7030 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7031 | injectMotionEvent(*mDispatcher, |
| 7032 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7033 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 7034 | .build())); |
| 7035 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 7036 | |
| 7037 | // Now, a new window appears. This could be, for example, a notification shade that appears |
| 7038 | // after user starts to drag down on the launcher window. |
| 7039 | mDispatcher->onWindowInfosChanged( |
| 7040 | {{*appearingWindow->getInfo(), *originalWindow->getInfo()}, {}, 0, 0}); |
| 7041 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7042 | injectMotionEvent(*mDispatcher, |
| 7043 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7044 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(110)) |
| 7045 | .build())); |
| 7046 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 7047 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 7048 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7049 | injectMotionEvent(*mDispatcher, |
| 7050 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7051 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 7052 | .build())); |
| 7053 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 7054 | |
| 7055 | originalWindow->assertNoEvents(); |
| 7056 | appearingWindow->assertNoEvents(); |
| 7057 | } |
| 7058 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7059 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7060 | using Uid = gui::Uid; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7061 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7062 | |
| 7063 | sp<FakeWindowHandle> leftWindow = |
| 7064 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 7065 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 7066 | leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7067 | |
| 7068 | sp<FakeWindowHandle> rightSpy = |
| 7069 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT); |
| 7070 | rightSpy->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 7071 | rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7072 | rightSpy->setSpy(true); |
| 7073 | rightSpy->setTrustedOverlay(true); |
| 7074 | |
| 7075 | sp<FakeWindowHandle> rightWindow = |
| 7076 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 7077 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 7078 | rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7079 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7080 | mDispatcher->onWindowInfosChanged( |
| 7081 | {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7082 | |
| 7083 | // Touch in the left window |
| 7084 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7085 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7086 | .build()); |
| 7087 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown()); |
| 7088 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7089 | ASSERT_NO_FATAL_FAILURE( |
| 7090 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7091 | |
| 7092 | // Touch another finger over the right windows |
| 7093 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7094 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7095 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 7096 | .build()); |
| 7097 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown()); |
| 7098 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown()); |
| 7099 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove()); |
| 7100 | mDispatcher->waitForIdle(); |
| 7101 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7102 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, |
| 7103 | {Uid{101}, Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7104 | |
| 7105 | // Release finger over left window. The UP actions are not treated as device interaction. |
| 7106 | // The windows that did not receive the UP pointer will receive MOVE events, but since this |
| 7107 | // is part of the UP action, we do not treat this as device interaction. |
| 7108 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7109 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7110 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 7111 | .build()); |
| 7112 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp()); |
| 7113 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 7114 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 7115 | mDispatcher->waitForIdle(); |
| 7116 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 7117 | |
| 7118 | // Move remaining finger |
| 7119 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7120 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 7121 | .build()); |
| 7122 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 7123 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 7124 | mDispatcher->waitForIdle(); |
| 7125 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7126 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7127 | |
| 7128 | // Release all fingers |
| 7129 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7130 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 7131 | .build()); |
| 7132 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp()); |
| 7133 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp()); |
| 7134 | mDispatcher->waitForIdle(); |
| 7135 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 7136 | } |
| 7137 | |
| 7138 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) { |
| 7139 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7140 | |
| 7141 | sp<FakeWindowHandle> window = |
| 7142 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 7143 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 7144 | window->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7145 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7146 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7147 | setFocusedWindow(window); |
| 7148 | ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true)); |
| 7149 | |
| 7150 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build()); |
| 7151 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT)); |
| 7152 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7153 | ASSERT_NO_FATAL_FAILURE( |
| 7154 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7155 | |
| 7156 | // The UP actions are not treated as device interaction. |
| 7157 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build()); |
| 7158 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT)); |
| 7159 | mDispatcher->waitForIdle(); |
| 7160 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 7161 | } |
| 7162 | |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7163 | TEST_F(InputDispatcherTest, HoverEnterExitSynthesisUsesNewEventId) { |
| 7164 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7165 | |
| 7166 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 7167 | ADISPLAY_ID_DEFAULT); |
| 7168 | left->setFrame(Rect(0, 0, 100, 100)); |
| 7169 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7170 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 7171 | right->setFrame(Rect(100, 0, 200, 100)); |
| 7172 | sp<FakeWindowHandle> spy = |
| 7173 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT); |
| 7174 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 7175 | spy->setTrustedOverlay(true); |
| 7176 | spy->setSpy(true); |
| 7177 | |
| 7178 | mDispatcher->onWindowInfosChanged( |
| 7179 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 7180 | |
| 7181 | // Send hover move to the left window, and ensure hover enter is synthesized with a new eventId. |
| 7182 | NotifyMotionArgs notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 7183 | ADISPLAY_ID_DEFAULT, {PointF{50, 50}}); |
| 7184 | mDispatcher->notifyMotion(notifyArgs); |
| 7185 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7186 | std::unique_ptr<MotionEvent> leftEnter = left->consumeMotionEvent( |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7187 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), Not(WithEventId(notifyArgs.id)), |
| 7188 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7189 | ASSERT_NE(nullptr, leftEnter); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7190 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 7191 | Not(WithEventId(notifyArgs.id)), |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7192 | Not(WithEventId(leftEnter->getId())), |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7193 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 7194 | |
| 7195 | // Send move to the right window, and ensure hover exit and enter are synthesized with new ids. |
| 7196 | notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT, |
| 7197 | {PointF{150, 50}}); |
| 7198 | mDispatcher->notifyMotion(notifyArgs); |
| 7199 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7200 | std::unique_ptr<MotionEvent> leftExit = left->consumeMotionEvent( |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7201 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), Not(WithEventId(notifyArgs.id)), |
| 7202 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7203 | ASSERT_NE(nullptr, leftExit); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7204 | right->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 7205 | Not(WithEventId(notifyArgs.id)), |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7206 | Not(WithEventId(leftExit->getId())), |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7207 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 7208 | |
| 7209 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithEventId(notifyArgs.id))); |
| 7210 | } |
| 7211 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 7212 | class InputDispatcherFallbackKeyTest : public InputDispatcherTest { |
| 7213 | protected: |
| 7214 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7215 | sp<FakeWindowHandle> mWindow; |
| 7216 | |
| 7217 | virtual void SetUp() override { |
| 7218 | InputDispatcherTest::SetUp(); |
| 7219 | |
| 7220 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 7221 | |
| 7222 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 7223 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7224 | |
| 7225 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 7226 | setFocusedWindow(mWindow); |
| 7227 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeFocusEvent(/*hasFocus=*/true)); |
| 7228 | } |
| 7229 | |
| 7230 | void setFallback(int32_t keycode) { |
| 7231 | mFakePolicy->setUnhandledKeyHandler([keycode](const KeyEvent& event) { |
| 7232 | return KeyEventBuilder(event).keyCode(keycode).build(); |
| 7233 | }); |
| 7234 | } |
| 7235 | |
| 7236 | void consumeKey(bool handled, const ::testing::Matcher<KeyEvent>& matcher) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7237 | std::unique_ptr<KeyEvent> event = mWindow->consumeKey(handled); |
| 7238 | ASSERT_NE(nullptr, event); |
| 7239 | ASSERT_THAT(*event, matcher); |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 7240 | } |
| 7241 | }; |
| 7242 | |
| 7243 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotNotifiedForHandledKey) { |
| 7244 | mDispatcher->notifyKey( |
| 7245 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7246 | consumeKey(/*handled=*/true, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 7247 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7248 | } |
| 7249 | |
| 7250 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotifiedForUnhandledKey) { |
| 7251 | mDispatcher->notifyKey( |
| 7252 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7253 | consumeKey(/*handled=*/false, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 7254 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7255 | } |
| 7256 | |
| 7257 | TEST_F(InputDispatcherFallbackKeyTest, NoFallbackRequestedByPolicy) { |
| 7258 | mDispatcher->notifyKey( |
| 7259 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7260 | |
| 7261 | // Do not handle this key event. |
| 7262 | consumeKey(/*handled=*/false, |
| 7263 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7264 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7265 | |
| 7266 | // Since the policy did not request any fallback to be generated, ensure there are no events. |
| 7267 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7268 | } |
| 7269 | |
| 7270 | TEST_F(InputDispatcherFallbackKeyTest, FallbackDispatchForUnhandledKey) { |
| 7271 | setFallback(AKEYCODE_B); |
| 7272 | mDispatcher->notifyKey( |
| 7273 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7274 | |
| 7275 | // Do not handle this key event. |
| 7276 | consumeKey(/*handled=*/false, |
| 7277 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7278 | |
| 7279 | // Since the key was not handled, ensure the fallback event was dispatched instead. |
| 7280 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7281 | consumeKey(/*handled=*/true, |
| 7282 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7283 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7284 | |
| 7285 | // Release the original key, and ensure the fallback key is also released. |
| 7286 | mDispatcher->notifyKey( |
| 7287 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7288 | consumeKey(/*handled=*/false, |
| 7289 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7290 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7291 | consumeKey(/*handled=*/true, |
| 7292 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7293 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7294 | |
| 7295 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7296 | mWindow->assertNoEvents(); |
| 7297 | } |
| 7298 | |
| 7299 | TEST_F(InputDispatcherFallbackKeyTest, AppHandlesPreviouslyUnhandledKey) { |
| 7300 | setFallback(AKEYCODE_B); |
| 7301 | mDispatcher->notifyKey( |
| 7302 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7303 | |
| 7304 | // Do not handle this key event, but handle the fallback. |
| 7305 | consumeKey(/*handled=*/false, |
| 7306 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7307 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7308 | consumeKey(/*handled=*/true, |
| 7309 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7310 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7311 | |
| 7312 | // Release the original key, and ensure the fallback key is also released. |
| 7313 | mDispatcher->notifyKey( |
| 7314 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7315 | // But this time, the app handles the original key. |
| 7316 | consumeKey(/*handled=*/true, |
| 7317 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7318 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7319 | // Ensure the fallback key is canceled. |
| 7320 | consumeKey(/*handled=*/true, |
| 7321 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7322 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 7323 | |
| 7324 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7325 | mWindow->assertNoEvents(); |
| 7326 | } |
| 7327 | |
| 7328 | TEST_F(InputDispatcherFallbackKeyTest, AppDoesNotHandleFallback) { |
| 7329 | setFallback(AKEYCODE_B); |
| 7330 | mDispatcher->notifyKey( |
| 7331 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7332 | |
| 7333 | // Do not handle this key event. |
| 7334 | consumeKey(/*handled=*/false, |
| 7335 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7336 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7337 | // App does not handle the fallback either, so ensure another fallback is not generated. |
| 7338 | setFallback(AKEYCODE_C); |
| 7339 | consumeKey(/*handled=*/false, |
| 7340 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7341 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7342 | |
| 7343 | // Release the original key, and ensure the fallback key is also released. |
| 7344 | setFallback(AKEYCODE_B); |
| 7345 | mDispatcher->notifyKey( |
| 7346 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7347 | consumeKey(/*handled=*/false, |
| 7348 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7349 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7350 | consumeKey(/*handled=*/false, |
| 7351 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7352 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7353 | |
| 7354 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7355 | mWindow->assertNoEvents(); |
| 7356 | } |
| 7357 | |
| 7358 | TEST_F(InputDispatcherFallbackKeyTest, InconsistentPolicyCancelsFallback) { |
| 7359 | setFallback(AKEYCODE_B); |
| 7360 | mDispatcher->notifyKey( |
| 7361 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7362 | |
| 7363 | // Do not handle this key event, so fallback is generated. |
| 7364 | consumeKey(/*handled=*/false, |
| 7365 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7366 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7367 | consumeKey(/*handled=*/true, |
| 7368 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7369 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7370 | |
| 7371 | // Release the original key, but assume the policy is misbehaving and it |
| 7372 | // generates an inconsistent fallback to the one from the DOWN event. |
| 7373 | setFallback(AKEYCODE_C); |
| 7374 | mDispatcher->notifyKey( |
| 7375 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7376 | consumeKey(/*handled=*/false, |
| 7377 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7378 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7379 | // Ensure the fallback key reported before as DOWN is canceled due to the inconsistency. |
| 7380 | consumeKey(/*handled=*/true, |
| 7381 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7382 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 7383 | |
| 7384 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7385 | mWindow->assertNoEvents(); |
| 7386 | } |
| 7387 | |
| 7388 | TEST_F(InputDispatcherFallbackKeyTest, CanceledKeyCancelsFallback) { |
| 7389 | setFallback(AKEYCODE_B); |
| 7390 | mDispatcher->notifyKey( |
| 7391 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7392 | |
| 7393 | // Do not handle this key event, so fallback is generated. |
| 7394 | consumeKey(/*handled=*/false, |
| 7395 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7396 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7397 | consumeKey(/*handled=*/true, |
| 7398 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7399 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7400 | |
| 7401 | // The original key is canceled. |
| 7402 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
| 7403 | .keyCode(AKEYCODE_A) |
| 7404 | .addFlag(AKEY_EVENT_FLAG_CANCELED) |
| 7405 | .build()); |
| 7406 | consumeKey(/*handled=*/false, |
| 7407 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), |
| 7408 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 7409 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7410 | // Ensure the fallback key is also canceled due to the original key being canceled. |
| 7411 | consumeKey(/*handled=*/true, |
| 7412 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7413 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 7414 | |
| 7415 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7416 | mWindow->assertNoEvents(); |
| 7417 | } |
| 7418 | |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 7419 | TEST_F(InputDispatcherFallbackKeyTest, InputChannelRemovedDuringPolicyCall) { |
Prabir Pradhan | b13da8f | 2024-01-09 23:10:13 +0000 | [diff] [blame] | 7420 | setFallback(AKEYCODE_B); |
| 7421 | mDispatcher->notifyKey( |
| 7422 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7423 | |
| 7424 | // Do not handle this key event. |
| 7425 | consumeKey(/*handled=*/false, |
| 7426 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7427 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7428 | consumeKey(/*handled=*/true, |
| 7429 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7430 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7431 | |
| 7432 | mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) { |
| 7433 | // When the unhandled key is reported to the policy next, remove the input channel. |
| 7434 | mDispatcher->removeInputChannel(mWindow->getToken()); |
| 7435 | return KeyEventBuilder(event).keyCode(AKEYCODE_B).build(); |
| 7436 | }); |
| 7437 | // Release the original key, and let the app now handle the previously unhandled key. |
| 7438 | // This should result in the previously generated fallback key to be cancelled. |
| 7439 | // Since the policy was notified of the unhandled DOWN event earlier, it will also be notified |
| 7440 | // of the UP event for consistency. The Dispatcher calls into the policy from its own thread |
| 7441 | // without holding the lock, because it need to synchronously fetch the fallback key. While in |
| 7442 | // the policy call, we will now remove the input channel. Once the policy call returns, the |
| 7443 | // Dispatcher will no longer have a channel to send cancellation events to. Ensure this does |
| 7444 | // not cause any crashes. |
| 7445 | mDispatcher->notifyKey( |
| 7446 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7447 | consumeKey(/*handled=*/true, |
| 7448 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7449 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7450 | } |
| 7451 | |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 7452 | TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedDuringPolicyCall) { |
| 7453 | setFallback(AKEYCODE_B); |
| 7454 | mDispatcher->notifyKey( |
| 7455 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7456 | |
| 7457 | // Do not handle this key event. |
| 7458 | consumeKey(/*handled=*/false, |
| 7459 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7460 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7461 | consumeKey(/*handled=*/true, |
| 7462 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7463 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7464 | |
| 7465 | mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) { |
| 7466 | // When the unhandled key is reported to the policy next, remove the window. |
| 7467 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 7468 | return KeyEventBuilder(event).keyCode(AKEYCODE_B).build(); |
| 7469 | }); |
| 7470 | // Release the original key, which the app will not handle. When this unhandled key is reported |
| 7471 | // to the policy, the window will be removed. |
| 7472 | mDispatcher->notifyKey( |
| 7473 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7474 | consumeKey(/*handled=*/false, |
| 7475 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7476 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7477 | |
| 7478 | // Since the window was removed, it loses focus, and the channel state will be reset. |
| 7479 | consumeKey(/*handled=*/true, |
| 7480 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7481 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 7482 | mWindow->consumeFocusEvent(false); |
| 7483 | mWindow->assertNoEvents(); |
| 7484 | } |
| 7485 | |
| 7486 | TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedWhileAwaitingFinishedSignal) { |
| 7487 | setFallback(AKEYCODE_B); |
| 7488 | mDispatcher->notifyKey( |
| 7489 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7490 | |
| 7491 | // Do not handle this key event. |
| 7492 | consumeKey(/*handled=*/false, |
| 7493 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7494 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7495 | const auto [seq, event] = mWindow->receiveEvent(); |
| 7496 | ASSERT_TRUE(seq.has_value() && event != nullptr) << "Failed to receive fallback event"; |
| 7497 | ASSERT_EQ(event->getType(), InputEventType::KEY); |
| 7498 | ASSERT_THAT(static_cast<const KeyEvent&>(*event), |
| 7499 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7500 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7501 | |
| 7502 | // Remove the window now, which should generate a cancellations and make the window lose focus. |
| 7503 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 7504 | consumeKey(/*handled=*/true, |
| 7505 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), |
| 7506 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 7507 | consumeKey(/*handled=*/true, |
| 7508 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7509 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 7510 | mWindow->consumeFocusEvent(false); |
| 7511 | |
| 7512 | // Finish the event by reporting it as handled. |
| 7513 | mWindow->finishEvent(*seq); |
| 7514 | mWindow->assertNoEvents(); |
| 7515 | } |
| 7516 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7517 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 7518 | protected: |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 7519 | static constexpr std::chrono::nanoseconds KEY_REPEAT_TIMEOUT = 40ms; |
| 7520 | static constexpr std::chrono::nanoseconds KEY_REPEAT_DELAY = 40ms; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7521 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7522 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7523 | sp<FakeWindowHandle> mWindow; |
| 7524 | |
| 7525 | virtual void SetUp() override { |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 7526 | InputDispatcherTest::SetUp(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7527 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 7528 | mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7529 | setUpWindow(); |
| 7530 | } |
| 7531 | |
| 7532 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7533 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7534 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7535 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7536 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7537 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7538 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7539 | mWindow->consumeFocusEvent(true); |
| 7540 | } |
| 7541 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7542 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7543 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7544 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7545 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7546 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7547 | |
| 7548 | // Window should receive key down event. |
| 7549 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7550 | } |
| 7551 | |
| 7552 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 7553 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7554 | mWindow->consumeKeyEvent( |
| 7555 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount))); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7556 | } |
| 7557 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7558 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7559 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7560 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7561 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7562 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7563 | |
| 7564 | // Window should receive key down event. |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7565 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7566 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7567 | } |
| 7568 | }; |
| 7569 | |
| 7570 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7571 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7572 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 7573 | expectKeyRepeatOnce(repeatCount); |
| 7574 | } |
| 7575 | } |
| 7576 | |
| 7577 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7578 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7579 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 7580 | expectKeyRepeatOnce(repeatCount); |
| 7581 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7582 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7583 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7584 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 7585 | expectKeyRepeatOnce(repeatCount); |
| 7586 | } |
| 7587 | } |
| 7588 | |
| 7589 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7590 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 7591 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 7592 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7593 | mWindow->assertNoEvents(); |
| 7594 | } |
| 7595 | |
| 7596 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7597 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 7598 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 7599 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 7600 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7601 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7602 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7603 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7604 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 7605 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7606 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7607 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7608 | mWindow->assertNoEvents(); |
| 7609 | } |
| 7610 | |
| 7611 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7612 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 7613 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 7614 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 7615 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7616 | // 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] | 7617 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7618 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7619 | mWindow->assertNoEvents(); |
| 7620 | } |
| 7621 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 7622 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 7623 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7624 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7625 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 7626 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 7627 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 7628 | mWindow->assertNoEvents(); |
| 7629 | } |
| 7630 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7631 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 7632 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7633 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7634 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7635 | std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey(); |
| 7636 | ASSERT_NE(nullptr, repeatEvent); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7637 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7638 | IdGenerator::getSource(repeatEvent->getId())); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7639 | } |
| 7640 | } |
| 7641 | |
| 7642 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 7643 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7644 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7645 | |
| 7646 | std::unordered_set<int32_t> idSet; |
| 7647 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7648 | std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey(); |
| 7649 | ASSERT_NE(nullptr, repeatEvent); |
| 7650 | int32_t id = repeatEvent->getId(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7651 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 7652 | idSet.insert(id); |
| 7653 | } |
| 7654 | } |
| 7655 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7656 | /* Test InputDispatcher for MultiDisplay */ |
| 7657 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 7658 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7659 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7660 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7661 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7662 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7663 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7664 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7665 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7666 | // Set focus window for primary display, but focused display would be second one. |
| 7667 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7668 | windowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7669 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
| 7670 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7671 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7672 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7673 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7674 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7675 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7676 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7677 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7678 | // Set focus display to second one. |
| 7679 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 7680 | // Set focus window for second display. |
| 7681 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7682 | windowInSecondary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7683 | mDispatcher->onWindowInfosChanged( |
| 7684 | {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7685 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7686 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7687 | } |
| 7688 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7689 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7690 | InputDispatcherTest::TearDown(); |
| 7691 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7692 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7693 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7694 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7695 | windowInSecondary.clear(); |
| 7696 | } |
| 7697 | |
| 7698 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7699 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7700 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7701 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7702 | sp<FakeWindowHandle> windowInSecondary; |
| 7703 | }; |
| 7704 | |
| 7705 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 7706 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7707 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7708 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7709 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7710 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7711 | windowInSecondary->assertNoEvents(); |
| 7712 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7713 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7714 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7715 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7716 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7717 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7718 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7719 | } |
| 7720 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7721 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 7722 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 7723 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7724 | injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7725 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7726 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 7727 | windowInSecondary->assertNoEvents(); |
| 7728 | |
| 7729 | // Test inject a key down without display id specified. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7730 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7731 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7732 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7733 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7734 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7735 | // Remove all windows in secondary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7736 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7737 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7738 | // Old focus should receive a cancel event. |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7739 | windowInSecondary->consumeKeyUp(ADISPLAY_ID_NONE, AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7740 | |
| 7741 | // Test inject a key down, should timeout because of no target window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7742 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7743 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7744 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7745 | windowInSecondary->assertNoEvents(); |
| 7746 | } |
| 7747 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7748 | // Test per-display input monitors for motion event. |
| 7749 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7750 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7751 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7752 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7753 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7754 | |
| 7755 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7756 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7757 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7758 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7759 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7760 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7761 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7762 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7763 | |
| 7764 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7765 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7766 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7767 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7768 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7769 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7770 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7771 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7772 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 7773 | // Lift up the touch from the second display |
| 7774 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7775 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 7776 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7777 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 7778 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 7779 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7780 | // Test inject a non-pointer motion event. |
| 7781 | // If specific a display, it will dispatch to the focused window of particular display, |
| 7782 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7783 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7784 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7785 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7786 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7787 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7788 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7789 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7790 | } |
| 7791 | |
| 7792 | // Test per-display input monitors for key event. |
| 7793 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7794 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7795 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7796 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7797 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7798 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7799 | |
| 7800 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7801 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7802 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7803 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7804 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7805 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7806 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7807 | } |
| 7808 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7809 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 7810 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7811 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7812 | secondWindowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7813 | mDispatcher->onWindowInfosChanged( |
| 7814 | {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(), |
| 7815 | *windowInSecondary->getInfo()}, |
| 7816 | {}, |
| 7817 | 0, |
| 7818 | 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7819 | setFocusedWindow(secondWindowInPrimary); |
| 7820 | windowInPrimary->consumeFocusEvent(false); |
| 7821 | secondWindowInPrimary->consumeFocusEvent(true); |
| 7822 | |
| 7823 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7824 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7825 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7826 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7827 | windowInPrimary->assertNoEvents(); |
| 7828 | windowInSecondary->assertNoEvents(); |
| 7829 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7830 | } |
| 7831 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7832 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 7833 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7834 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7835 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7836 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7837 | |
| 7838 | // Test touch down on primary display. |
| 7839 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7840 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7841 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7842 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7843 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7844 | |
| 7845 | // Test touch down on second display. |
| 7846 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7847 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7848 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7849 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 7850 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 7851 | |
| 7852 | // Trigger cancel touch. |
| 7853 | mDispatcher->cancelCurrentTouch(); |
| 7854 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 7855 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 7856 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 7857 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 7858 | |
| 7859 | // Test inject a move motion event, no window/monitor should receive the event. |
| 7860 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7861 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7862 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 7863 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 7864 | windowInPrimary->assertNoEvents(); |
| 7865 | monitorInPrimary.assertNoEvents(); |
| 7866 | |
| 7867 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7868 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7869 | SECOND_DISPLAY_ID, {110, 200})) |
| 7870 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 7871 | windowInSecondary->assertNoEvents(); |
| 7872 | monitorInSecondary.assertNoEvents(); |
| 7873 | } |
| 7874 | |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 7875 | /** |
| 7876 | * Send a key to the primary display and to the secondary display. |
| 7877 | * Then cause the key on the primary display to be canceled by sending in a stale key. |
| 7878 | * Ensure that the key on the primary display is canceled, and that the key on the secondary display |
| 7879 | * does not get canceled. |
| 7880 | */ |
| 7881 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture) { |
| 7882 | // Send a key down on primary display |
| 7883 | mDispatcher->notifyKey( |
| 7884 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 7885 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7886 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 7887 | .build()); |
| 7888 | windowInPrimary->consumeKeyEvent( |
| 7889 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 7890 | windowInSecondary->assertNoEvents(); |
| 7891 | |
| 7892 | // Send a key down on second display |
| 7893 | mDispatcher->notifyKey( |
| 7894 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 7895 | .displayId(SECOND_DISPLAY_ID) |
| 7896 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 7897 | .build()); |
| 7898 | windowInSecondary->consumeKeyEvent( |
| 7899 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID))); |
| 7900 | windowInPrimary->assertNoEvents(); |
| 7901 | |
| 7902 | // Send a valid key up event on primary display that will be dropped because it is stale |
| 7903 | NotifyKeyArgs staleKeyUp = |
| 7904 | KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
| 7905 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7906 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 7907 | .build(); |
| 7908 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms; |
| 7909 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
| 7910 | std::this_thread::sleep_for(STALE_EVENT_TIMEOUT); |
| 7911 | mDispatcher->notifyKey(staleKeyUp); |
| 7912 | |
| 7913 | // Only the key gesture corresponding to the dropped event should receive the cancel event. |
| 7914 | // Therefore, windowInPrimary should get the cancel event and windowInSecondary should not |
| 7915 | // receive any events. |
| 7916 | windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), |
| 7917 | WithDisplayId(ADISPLAY_ID_DEFAULT), |
| 7918 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 7919 | windowInSecondary->assertNoEvents(); |
| 7920 | } |
| 7921 | |
| 7922 | /** |
| 7923 | * Similar to 'WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture' but for motion events. |
| 7924 | */ |
| 7925 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropMotionEvent_OnlyCancelCorrespondingGesture) { |
| 7926 | // Send touch down on primary display. |
| 7927 | mDispatcher->notifyMotion( |
| 7928 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7929 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 7930 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7931 | .build()); |
| 7932 | windowInPrimary->consumeMotionEvent( |
| 7933 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 7934 | windowInSecondary->assertNoEvents(); |
| 7935 | |
| 7936 | // Send touch down on second display. |
| 7937 | mDispatcher->notifyMotion( |
| 7938 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7939 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 7940 | .displayId(SECOND_DISPLAY_ID) |
| 7941 | .build()); |
| 7942 | windowInPrimary->assertNoEvents(); |
| 7943 | windowInSecondary->consumeMotionEvent( |
| 7944 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID))); |
| 7945 | |
| 7946 | // inject a valid MotionEvent on primary display that will be stale when it arrives. |
| 7947 | NotifyMotionArgs staleMotionUp = |
| 7948 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7949 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7950 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 7951 | .build(); |
| 7952 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms; |
| 7953 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
| 7954 | std::this_thread::sleep_for(STALE_EVENT_TIMEOUT); |
| 7955 | mDispatcher->notifyMotion(staleMotionUp); |
| 7956 | |
| 7957 | // For stale motion events, we let the gesture to complete. This behaviour is different from key |
| 7958 | // events, where we would cancel the current keys instead. |
| 7959 | windowInPrimary->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 7960 | windowInSecondary->assertNoEvents(); |
| 7961 | } |
| 7962 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7963 | class InputFilterTest : public InputDispatcherTest { |
| 7964 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7965 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 7966 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7967 | NotifyMotionArgs motionArgs; |
| 7968 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7969 | motionArgs = |
| 7970 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7971 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7972 | motionArgs = |
| 7973 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7974 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7975 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7976 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 7977 | const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue()); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7978 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7979 | } else { |
| 7980 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 7981 | } |
| 7982 | } |
| 7983 | |
| 7984 | void testNotifyKey(bool expectToBeFiltered) { |
| 7985 | NotifyKeyArgs keyArgs; |
| 7986 | |
| 7987 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7988 | mDispatcher->notifyKey(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7989 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7990 | mDispatcher->notifyKey(keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7991 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7992 | |
| 7993 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 7994 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7995 | } else { |
| 7996 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 7997 | } |
| 7998 | } |
| 7999 | }; |
| 8000 | |
| 8001 | // Test InputFilter for MotionEvent |
| 8002 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 8003 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8004 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false); |
| 8005 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8006 | |
| 8007 | // Enable InputFilter |
| 8008 | mDispatcher->setInputFilterEnabled(true); |
| 8009 | // Test touch on both primary and second display, and check if both events are filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8010 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true); |
| 8011 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8012 | |
| 8013 | // Disable InputFilter |
| 8014 | mDispatcher->setInputFilterEnabled(false); |
| 8015 | // Test touch on both primary and second display, and check if both events aren't filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8016 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false); |
| 8017 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8018 | } |
| 8019 | |
| 8020 | // Test InputFilter for KeyEvent |
| 8021 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 8022 | // Since the InputFilter is disabled by default, check if key event aren't filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8023 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8024 | |
| 8025 | // Enable InputFilter |
| 8026 | mDispatcher->setInputFilterEnabled(true); |
| 8027 | // Send a key event, and check if it is filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8028 | testNotifyKey(/*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8029 | |
| 8030 | // Disable InputFilter |
| 8031 | mDispatcher->setInputFilterEnabled(false); |
| 8032 | // Send a key event, and check if it isn't filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8033 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8034 | } |
| 8035 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 8036 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 8037 | // logical display coordinate space. |
| 8038 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 8039 | ui::Transform firstDisplayTransform; |
| 8040 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 8041 | ui::Transform secondDisplayTransform; |
| 8042 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 8043 | |
| 8044 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 8045 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 8046 | displayInfos[0].transform = firstDisplayTransform; |
| 8047 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 8048 | displayInfos[1].transform = secondDisplayTransform; |
| 8049 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 8050 | mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0}); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 8051 | |
| 8052 | // Enable InputFilter |
| 8053 | mDispatcher->setInputFilterEnabled(true); |
| 8054 | |
| 8055 | // Ensure the correct transforms are used for the displays. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8056 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true, firstDisplayTransform); |
| 8057 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 8058 | } |
| 8059 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8060 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 8061 | protected: |
| 8062 | virtual void SetUp() override { |
| 8063 | InputDispatcherTest::SetUp(); |
| 8064 | |
| 8065 | /** |
| 8066 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 8067 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 8068 | * on. |
| 8069 | */ |
| 8070 | mDispatcher->setInputFilterEnabled(true); |
| 8071 | |
| 8072 | std::shared_ptr<InputApplicationHandle> application = |
| 8073 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8074 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 8075 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8076 | |
| 8077 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8078 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8079 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8080 | setFocusedWindow(mWindow); |
| 8081 | mWindow->consumeFocusEvent(true); |
| 8082 | } |
| 8083 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 8084 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 8085 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8086 | KeyEvent event; |
| 8087 | |
| 8088 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 8089 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 8090 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8091 | KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8092 | const int32_t additionalPolicyFlags = |
| 8093 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 8094 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8095 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 8096 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8097 | policyFlags | additionalPolicyFlags)); |
| 8098 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8099 | mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags))); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 8100 | } |
| 8101 | |
| 8102 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 8103 | int32_t flags) { |
| 8104 | MotionEvent event; |
| 8105 | PointerProperties pointerProperties[1]; |
| 8106 | PointerCoords pointerCoords[1]; |
| 8107 | pointerProperties[0].clear(); |
| 8108 | pointerProperties[0].id = 0; |
| 8109 | pointerCoords[0].clear(); |
| 8110 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 8111 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 8112 | |
| 8113 | ui::Transform identityTransform; |
| 8114 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 8115 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 8116 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 8117 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 8118 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 8119 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 8120 | eventTime, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8121 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 8122 | |
| 8123 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 8124 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8125 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 8126 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 8127 | policyFlags | additionalPolicyFlags)); |
| 8128 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8129 | mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId))); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8130 | } |
| 8131 | |
| 8132 | private: |
| 8133 | sp<FakeWindowHandle> mWindow; |
| 8134 | }; |
| 8135 | |
| 8136 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 8137 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 8138 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 8139 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8140 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 8141 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8142 | } |
| 8143 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 8144 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8145 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8146 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 8147 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 8148 | } |
| 8149 | |
| 8150 | TEST_F(InputFilterInjectionPolicyTest, |
| 8151 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 8152 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8153 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 8154 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8155 | } |
| 8156 | |
| 8157 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8158 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 8159 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8160 | } |
| 8161 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 8162 | class InputDispatcherUserActivityPokeTests : public InputDispatcherTest { |
| 8163 | protected: |
| 8164 | virtual void SetUp() override { |
| 8165 | InputDispatcherTest::SetUp(); |
| 8166 | |
| 8167 | std::shared_ptr<FakeApplicationHandle> application = |
| 8168 | std::make_shared<FakeApplicationHandle>(); |
| 8169 | application->setDispatchingTimeout(100ms); |
| 8170 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
| 8171 | ADISPLAY_ID_DEFAULT); |
Yeabkal Wubshit | 222d83d | 2024-01-24 18:00:09 +0000 | [diff] [blame] | 8172 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 8173 | mWindow->setDispatchingTimeout(100ms); |
| 8174 | mWindow->setFocusable(true); |
| 8175 | |
| 8176 | // Set focused application. |
| 8177 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 8178 | |
| 8179 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 8180 | setFocusedWindow(mWindow); |
| 8181 | mWindow->consumeFocusEvent(true); |
| 8182 | } |
| 8183 | |
| 8184 | void notifyAndConsumeMotion(int32_t action, uint32_t source, int32_t displayId, |
| 8185 | nsecs_t eventTime) { |
| 8186 | mDispatcher->notifyMotion(MotionArgsBuilder(action, source) |
| 8187 | .displayId(displayId) |
| 8188 | .eventTime(eventTime) |
| 8189 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8190 | .build()); |
| 8191 | mWindow->consumeMotionEvent(WithMotionAction(action)); |
| 8192 | } |
| 8193 | |
| 8194 | private: |
| 8195 | sp<FakeWindowHandle> mWindow; |
| 8196 | }; |
| 8197 | |
| 8198 | TEST_F_WITH_FLAGS( |
| 8199 | InputDispatcherUserActivityPokeTests, MinPokeTimeObserved, |
| 8200 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 8201 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 8202 | mDispatcher->setMinTimeBetweenUserActivityPokes(50ms); |
| 8203 | |
| 8204 | // First event of type TOUCH. Should poke. |
| 8205 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8206 | milliseconds_to_nanoseconds(50)); |
| 8207 | mFakePolicy->assertUserActivityPoked( |
| 8208 | {{milliseconds_to_nanoseconds(50), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 8209 | |
| 8210 | // 80ns > 50ns has passed since previous TOUCH event. Should poke. |
| 8211 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8212 | milliseconds_to_nanoseconds(130)); |
| 8213 | mFakePolicy->assertUserActivityPoked( |
| 8214 | {{milliseconds_to_nanoseconds(130), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 8215 | |
| 8216 | // First event of type OTHER. Should poke (despite being within 50ns of previous TOUCH event). |
| 8217 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT, |
| 8218 | milliseconds_to_nanoseconds(135)); |
| 8219 | mFakePolicy->assertUserActivityPoked( |
| 8220 | {{milliseconds_to_nanoseconds(135), USER_ACTIVITY_EVENT_OTHER, ADISPLAY_ID_DEFAULT}}); |
| 8221 | |
| 8222 | // Within 50ns of previous TOUCH event. Should NOT poke. |
| 8223 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8224 | milliseconds_to_nanoseconds(140)); |
| 8225 | mFakePolicy->assertUserActivityNotPoked(); |
| 8226 | |
| 8227 | // Within 50ns of previous OTHER event. Should NOT poke. |
| 8228 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT, |
| 8229 | milliseconds_to_nanoseconds(150)); |
| 8230 | mFakePolicy->assertUserActivityNotPoked(); |
| 8231 | |
| 8232 | // Within 50ns of previous TOUCH event (which was at time 130). Should NOT poke. |
| 8233 | // Note that STYLUS is mapped to TOUCH user activity, since it's a pointer-type source. |
| 8234 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT, |
| 8235 | milliseconds_to_nanoseconds(160)); |
| 8236 | mFakePolicy->assertUserActivityNotPoked(); |
| 8237 | |
| 8238 | // 65ns > 50ns has passed since previous OTHER event. Should poke. |
| 8239 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT, |
| 8240 | milliseconds_to_nanoseconds(200)); |
| 8241 | mFakePolicy->assertUserActivityPoked( |
| 8242 | {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_OTHER, ADISPLAY_ID_DEFAULT}}); |
| 8243 | |
| 8244 | // 170ns > 50ns has passed since previous TOUCH event. Should poke. |
| 8245 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT, |
| 8246 | milliseconds_to_nanoseconds(300)); |
| 8247 | mFakePolicy->assertUserActivityPoked( |
| 8248 | {{milliseconds_to_nanoseconds(300), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 8249 | |
| 8250 | // Assert that there's no more user activity poke event. |
| 8251 | mFakePolicy->assertUserActivityNotPoked(); |
| 8252 | } |
| 8253 | |
| 8254 | TEST_F_WITH_FLAGS( |
| 8255 | InputDispatcherUserActivityPokeTests, DefaultMinPokeTimeOf100MsUsed, |
| 8256 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 8257 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 8258 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8259 | milliseconds_to_nanoseconds(200)); |
| 8260 | mFakePolicy->assertUserActivityPoked( |
| 8261 | {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 8262 | |
| 8263 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8264 | milliseconds_to_nanoseconds(280)); |
| 8265 | mFakePolicy->assertUserActivityNotPoked(); |
| 8266 | |
| 8267 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8268 | milliseconds_to_nanoseconds(340)); |
| 8269 | mFakePolicy->assertUserActivityPoked( |
| 8270 | {{milliseconds_to_nanoseconds(340), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 8271 | } |
| 8272 | |
| 8273 | TEST_F_WITH_FLAGS( |
| 8274 | InputDispatcherUserActivityPokeTests, ZeroMinPokeTimeDisablesRateLimiting, |
| 8275 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 8276 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 8277 | mDispatcher->setMinTimeBetweenUserActivityPokes(0ms); |
| 8278 | |
| 8279 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20); |
| 8280 | mFakePolicy->assertUserActivityPoked(); |
| 8281 | |
| 8282 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 30); |
| 8283 | mFakePolicy->assertUserActivityPoked(); |
| 8284 | } |
| 8285 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8286 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 8287 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8288 | InputDispatcherTest::SetUp(); |
| 8289 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8290 | std::shared_ptr<FakeApplicationHandle> application = |
| 8291 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8292 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8293 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8294 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8295 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8296 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8297 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8298 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8299 | |
| 8300 | // Set focused application. |
| 8301 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8302 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8303 | |
| 8304 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8305 | mDispatcher->onWindowInfosChanged( |
| 8306 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8307 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8308 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8309 | } |
| 8310 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 8311 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8312 | InputDispatcherTest::TearDown(); |
| 8313 | |
| 8314 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8315 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8316 | } |
| 8317 | |
| 8318 | protected: |
| 8319 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8320 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 8321 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8322 | }; |
| 8323 | |
| 8324 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 8325 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 8326 | // the onPointerDownOutsideFocus callback. |
| 8327 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8328 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8329 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 8330 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8331 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 8332 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8333 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8334 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8335 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 8336 | } |
| 8337 | |
| 8338 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 8339 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 8340 | // onPointerDownOutsideFocus callback. |
| 8341 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8342 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8343 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, |
| 8344 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8345 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 8346 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8347 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8348 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8349 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8350 | } |
| 8351 | |
| 8352 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 8353 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 8354 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 8355 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8356 | injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8357 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 8358 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8359 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8360 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8361 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8362 | } |
| 8363 | |
| 8364 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 8365 | // DOWN on the window that already has focus. Ensure no window received the |
| 8366 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8367 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8368 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8369 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 8370 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8371 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 8372 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8373 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8374 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8375 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8376 | } |
| 8377 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 8378 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 8379 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 8380 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 8381 | const MotionEvent event = |
| 8382 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 8383 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8384 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 8385 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 8386 | .build(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8387 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 8388 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8389 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 8390 | |
| 8391 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8392 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 8393 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 8394 | mUnfocusedWindow->assertNoEvents(); |
| 8395 | } |
| 8396 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8397 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 8398 | // windows that point to the same client token. |
| 8399 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 8400 | virtual void SetUp() override { |
| 8401 | InputDispatcherTest::SetUp(); |
| 8402 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8403 | std::shared_ptr<FakeApplicationHandle> application = |
| 8404 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8405 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 8406 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8407 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 8408 | |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 8409 | mWindow2 = mWindow1->clone(ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8410 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 8411 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8412 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8413 | } |
| 8414 | |
| 8415 | protected: |
| 8416 | sp<FakeWindowHandle> mWindow1; |
| 8417 | sp<FakeWindowHandle> mWindow2; |
| 8418 | |
| 8419 | // 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] | 8420 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 8421 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 8422 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8423 | } |
| 8424 | |
| 8425 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 8426 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8427 | const std::string name = window->getName(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8428 | std::unique_ptr<MotionEvent> motionEvent = |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 8429 | window->consumeMotionEvent(WithMotionAction(expectedAction)); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8430 | ASSERT_NE(nullptr, motionEvent); |
| 8431 | ASSERT_EQ(points.size(), motionEvent->getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8432 | |
| 8433 | for (size_t i = 0; i < points.size(); i++) { |
| 8434 | float expectedX = points[i].x; |
| 8435 | float expectedY = points[i].y; |
| 8436 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8437 | EXPECT_EQ(expectedX, motionEvent->getX(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8438 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8439 | << ", got " << motionEvent->getX(i); |
| 8440 | EXPECT_EQ(expectedY, motionEvent->getY(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8441 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8442 | << ", got " << motionEvent->getY(i); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8443 | } |
| 8444 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8445 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8446 | void touchAndAssertPositions(sp<FakeWindowHandle> touchedWindow, int32_t action, |
| 8447 | const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8448 | std::vector<PointF> expectedPoints) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8449 | mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 8450 | ADISPLAY_ID_DEFAULT, touchedPoints)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8451 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8452 | consumeMotionEvent(touchedWindow, action, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8453 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8454 | }; |
| 8455 | |
| 8456 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 8457 | // Touch Window 1 |
| 8458 | PointF touchedPoint = {10, 10}; |
| 8459 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8460 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8461 | |
| 8462 | // Release touch on Window 1 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8463 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8464 | |
| 8465 | // Touch Window 2 |
| 8466 | touchedPoint = {150, 150}; |
| 8467 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8468 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8469 | } |
| 8470 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8471 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 8472 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8473 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8474 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8475 | |
| 8476 | // Touch Window 1 |
| 8477 | PointF touchedPoint = {10, 10}; |
| 8478 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8479 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8480 | // Release touch on Window 1 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8481 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8482 | |
| 8483 | // Touch Window 2 |
| 8484 | touchedPoint = {150, 150}; |
| 8485 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8486 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 8487 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8488 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8489 | // Update the transform so rotation is set |
| 8490 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8491 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8492 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8493 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8494 | } |
| 8495 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8496 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8497 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8498 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8499 | |
| 8500 | // Touch Window 1 |
| 8501 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 8502 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8503 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8504 | |
| 8505 | // Touch Window 2 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8506 | // Since this is part of the same touch gesture that has already been dispatched to Window 1, |
| 8507 | // the touch stream from Window 2 will be merged with the stream in Window 1. The merged stream |
| 8508 | // will continue to be dispatched through Window 1. |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8509 | touchedPoints.push_back(PointF{150, 150}); |
| 8510 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8511 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8512 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8513 | // Release Window 2 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8514 | touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8515 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8516 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8517 | // Update the transform so rotation is set for Window 2 |
| 8518 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8519 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8520 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8521 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8522 | } |
| 8523 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8524 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8525 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8526 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8527 | |
| 8528 | // Touch Window 1 |
| 8529 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 8530 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8531 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8532 | |
| 8533 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8534 | touchedPoints.push_back(PointF{150, 150}); |
| 8535 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8536 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8537 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8538 | |
| 8539 | // Move both windows |
| 8540 | touchedPoints = {{20, 20}, {175, 175}}; |
| 8541 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 8542 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 8543 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8544 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8545 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8546 | // Release Window 2 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8547 | touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8548 | expectedPoints.pop_back(); |
| 8549 | |
| 8550 | // Touch Window 2 |
| 8551 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8552 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8553 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8554 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8555 | |
| 8556 | // Move both windows |
| 8557 | touchedPoints = {{20, 20}, {175, 175}}; |
| 8558 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 8559 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 8560 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8561 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8562 | } |
| 8563 | |
| 8564 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 8565 | mWindow1->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8566 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8567 | |
| 8568 | // Touch Window 1 |
| 8569 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 8570 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8571 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8572 | |
| 8573 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8574 | touchedPoints.push_back(PointF{150, 150}); |
| 8575 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8576 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8577 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8578 | |
| 8579 | // Move both windows |
| 8580 | touchedPoints = {{20, 20}, {175, 175}}; |
| 8581 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 8582 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 8583 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8584 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8585 | } |
| 8586 | |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 8587 | /** |
| 8588 | * When one of the windows is slippery, the touch should not slip into the other window with the |
| 8589 | * same input channel. |
| 8590 | */ |
| 8591 | TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) { |
| 8592 | mWindow1->setSlippery(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8593 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 8594 | |
| 8595 | // Touch down in window 1 |
| 8596 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8597 | ADISPLAY_ID_DEFAULT, {{50, 50}})); |
| 8598 | consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}}); |
| 8599 | |
| 8600 | // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip. |
| 8601 | // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN |
| 8602 | // getting generated. |
| 8603 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8604 | ADISPLAY_ID_DEFAULT, {{150, 150}})); |
| 8605 | |
| 8606 | consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}}); |
| 8607 | } |
| 8608 | |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8609 | /** |
| 8610 | * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and |
| 8611 | * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window |
| 8612 | * that the pointer is hovering over may have a different transform. |
| 8613 | */ |
| 8614 | TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) { |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8615 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8616 | |
| 8617 | // Start hover in window 1 |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 8618 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN) |
| 8619 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8620 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8621 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 8622 | {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8623 | // Move hover to window 2. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 8624 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8625 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 8626 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8627 | consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}}); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8628 | consumeMotionEvent(mWindow2, ACTION_HOVER_ENTER, |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8629 | {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})}); |
| 8630 | } |
| 8631 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8632 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 8633 | virtual void SetUp() override { |
| 8634 | InputDispatcherTest::SetUp(); |
| 8635 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8636 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8637 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8638 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 8639 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8640 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8641 | mWindow->setDispatchingTimeout(100ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8642 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8643 | |
| 8644 | // Set focused application. |
| 8645 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 8646 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8647 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8648 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8649 | mWindow->consumeFocusEvent(true); |
| 8650 | } |
| 8651 | |
| 8652 | virtual void TearDown() override { |
| 8653 | InputDispatcherTest::TearDown(); |
| 8654 | mWindow.clear(); |
| 8655 | } |
| 8656 | |
| 8657 | protected: |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8658 | static constexpr std::chrono::duration SPY_TIMEOUT = 200ms; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8659 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8660 | sp<FakeWindowHandle> mWindow; |
| 8661 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 8662 | |
| 8663 | void tapOnWindow() { |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 8664 | const auto touchingPointer = PointerBuilder(/*id=*/0, ToolType::FINGER) |
| 8665 | .x(WINDOW_LOCATION.x) |
| 8666 | .y(WINDOW_LOCATION.y); |
| 8667 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8668 | .pointer(touchingPointer) |
| 8669 | .build()); |
| 8670 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8671 | .pointer(touchingPointer) |
| 8672 | .build()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8673 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8674 | |
| 8675 | sp<FakeWindowHandle> addSpyWindow() { |
| 8676 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8677 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8678 | spy->setTrustedOverlay(true); |
| 8679 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8680 | spy->setSpy(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8681 | spy->setDispatchingTimeout(SPY_TIMEOUT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8682 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8683 | return spy; |
| 8684 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8685 | }; |
| 8686 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8687 | // Send a tap and respond, which should not cause an ANR. |
| 8688 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 8689 | tapOnWindow(); |
| 8690 | mWindow->consumeMotionDown(); |
| 8691 | mWindow->consumeMotionUp(); |
| 8692 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8693 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8694 | } |
| 8695 | |
| 8696 | // Send a regular key and respond, which should not cause an ANR. |
| 8697 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8698 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8699 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8700 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8701 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8702 | } |
| 8703 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 8704 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 8705 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8706 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 8707 | mWindow->consumeFocusEvent(false); |
| 8708 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8709 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8710 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 8711 | InputEventInjectionSync::NONE, CONSUME_TIMEOUT_EVENT_EXPECTED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8712 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8713 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 8714 | // Key will not go to window because we have no focused window. |
| 8715 | // The 'no focused window' ANR timer should start instead. |
| 8716 | |
| 8717 | // Now, the focused application goes away. |
| 8718 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 8719 | // The key should get dropped and there should be no ANR. |
| 8720 | |
| 8721 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8722 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8723 | } |
| 8724 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8725 | // 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] | 8726 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 8727 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8728 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8729 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8730 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8731 | WINDOW_LOCATION)); |
| 8732 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8733 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8734 | ASSERT_TRUE(sequenceNum); |
| 8735 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8736 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8737 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8738 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8739 | mWindow->consumeMotionEvent( |
| 8740 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8741 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8742 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8743 | } |
| 8744 | |
| 8745 | // Send a key to the app and have the app not respond right away. |
| 8746 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 8747 | // Inject a key, and don't respond - expect that ANR is called. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8748 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8749 | const auto [sequenceNum, _] = mWindow->receiveEvent(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8750 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8751 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8752 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8753 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8754 | } |
| 8755 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8756 | // We have a focused application, but no focused window |
| 8757 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8758 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8759 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8760 | mWindow->consumeFocusEvent(false); |
| 8761 | |
| 8762 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8763 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8764 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8765 | WINDOW_LOCATION)); |
| 8766 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 8767 | mDispatcher->waitForIdle(); |
| 8768 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8769 | |
| 8770 | // Once a focused event arrives, we get an ANR for this application |
| 8771 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 8772 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8773 | const InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8774 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8775 | InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8776 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8777 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 8778 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8779 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8780 | } |
| 8781 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8782 | /** |
| 8783 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 8784 | * there will not be an ANR. |
| 8785 | */ |
| 8786 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 8787 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8788 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8789 | mWindow->consumeFocusEvent(false); |
| 8790 | |
| 8791 | KeyEvent event; |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 8792 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 8793 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8794 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 8795 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 8796 | |
| 8797 | // Define a valid key down event that is stale (too old). |
| 8798 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8799 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /*flags=*/0, AKEYCODE_A, KEY_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8800 | AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8801 | |
| 8802 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 8803 | |
| 8804 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8805 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8806 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 8807 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 8808 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 8809 | << "Injection should fail because the event is stale"; |
| 8810 | |
| 8811 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8812 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8813 | mWindow->assertNoEvents(); |
| 8814 | } |
| 8815 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8816 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8817 | // Make sure that we don't notify policy twice about the same ANR. |
| 8818 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 8819 | const std::chrono::duration appTimeout = 400ms; |
| 8820 | mApplication->setDispatchingTimeout(appTimeout); |
| 8821 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 8822 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8823 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8824 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8825 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8826 | |
| 8827 | // Once a focused event arrives, we get an ANR for this application |
| 8828 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 8829 | // injection times out (instead of failing). |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 8830 | const std::chrono::duration eventInjectionTimeout = 100ms; |
| 8831 | ASSERT_LT(eventInjectionTimeout, appTimeout); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8832 | const InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8833 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 8834 | InputEventInjectionSync::WAIT_FOR_RESULT, eventInjectionTimeout, |
| 8835 | /*allowKeyRepeat=*/false); |
| 8836 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result) |
| 8837 | << "result=" << ftl::enum_string(result); |
| 8838 | // We already waited for 'eventInjectionTimeout`, because the countdown started when the event |
| 8839 | // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait. |
| 8840 | std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout; |
| 8841 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8842 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 8843 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8844 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 8845 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8846 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8847 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 8848 | // '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] | 8849 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8850 | } |
| 8851 | |
| 8852 | // We have a focused application, but no focused window |
| 8853 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8854 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8855 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8856 | mWindow->consumeFocusEvent(false); |
| 8857 | |
| 8858 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8859 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8860 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 8861 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 8862 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8863 | |
| 8864 | // Future focused events get dropped right away |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8865 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8866 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8867 | mWindow->assertNoEvents(); |
| 8868 | } |
| 8869 | |
| 8870 | /** |
| 8871 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 8872 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 8873 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 8874 | * the ANR mechanism should still work. |
| 8875 | * |
| 8876 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 8877 | * DOWN event, while not responding on the second one. |
| 8878 | */ |
| 8879 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 8880 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8881 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8882 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 8883 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 8884 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8885 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8886 | |
| 8887 | // Now send ACTION_UP, with identical timestamp |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8888 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8889 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 8890 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 8891 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8892 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8893 | |
| 8894 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 8895 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8896 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8897 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8898 | } |
| 8899 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8900 | // A spy window can receive an ANR |
| 8901 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 8902 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 8903 | |
| 8904 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8905 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8906 | WINDOW_LOCATION)); |
| 8907 | mWindow->consumeMotionDown(); |
| 8908 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8909 | const auto [sequenceNum, _] = spy->receiveEvent(); // ACTION_DOWN |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8910 | ASSERT_TRUE(sequenceNum); |
| 8911 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8912 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8913 | |
| 8914 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8915 | spy->consumeMotionEvent( |
| 8916 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8917 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8918 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8919 | } |
| 8920 | |
| 8921 | // 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] | 8922 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8923 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 8924 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8925 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8926 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8927 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8928 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8929 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8930 | |
| 8931 | // Stuck on the ACTION_UP |
| 8932 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8933 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8934 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8935 | // 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] | 8936 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8937 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8938 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8939 | |
| 8940 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 8941 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8942 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8943 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8944 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8945 | } |
| 8946 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8947 | // 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] | 8948 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8949 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 8950 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8951 | |
| 8952 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8953 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8954 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8955 | |
| 8956 | mWindow->consumeMotionDown(); |
| 8957 | // Stuck on the ACTION_UP |
| 8958 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8959 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8960 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8961 | // 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] | 8962 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8963 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8964 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8965 | |
| 8966 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 8967 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8968 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8969 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8970 | spy->assertNoEvents(); |
| 8971 | } |
| 8972 | |
| 8973 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8974 | mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8975 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 8976 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8977 | |
| 8978 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8979 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8980 | WINDOW_LOCATION)); |
| 8981 | |
| 8982 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8983 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 8984 | ASSERT_TRUE(consumeSeq); |
| 8985 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8986 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(), |
| 8987 | MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8988 | |
| 8989 | monitor.finishEvent(*consumeSeq); |
| 8990 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 8991 | |
| 8992 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8993 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8994 | } |
| 8995 | |
| 8996 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 8997 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 8998 | // get an ANR again. |
| 8999 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 9000 | // 2. consume all pending events (= queue becomes healthy again) |
| 9001 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 9002 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 9003 | tapOnWindow(); |
| 9004 | |
| 9005 | mWindow->consumeMotionDown(); |
| 9006 | // Block on ACTION_UP |
| 9007 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9008 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9009 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 9010 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9011 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9012 | mWindow->assertNoEvents(); |
| 9013 | |
| 9014 | tapOnWindow(); |
| 9015 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9016 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9017 | mWindow->consumeMotionUp(); |
| 9018 | |
| 9019 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9020 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9021 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9022 | mWindow->assertNoEvents(); |
| 9023 | } |
| 9024 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9025 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 9026 | // it. |
| 9027 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9028 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9029 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9030 | WINDOW_LOCATION)); |
| 9031 | |
| 9032 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9033 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9034 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9035 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 9036 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9037 | // 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] | 9038 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 9039 | mWindow->consumeMotionEvent( |
| 9040 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9041 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9042 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9043 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9044 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9045 | } |
| 9046 | |
| 9047 | /** |
| 9048 | * If a window is processing a motion event, and then a key event comes in, the key event should |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9049 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9050 | */ |
| 9051 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 9052 | // The timeouts in this test are established by relying on the fact that the "key waiting for |
| 9053 | // events timeout" is equal to 500ms. |
| 9054 | ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9055 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9056 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9057 | |
| 9058 | tapOnWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9059 | const auto& [downSequenceNum, downEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9060 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9061 | const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9062 | ASSERT_TRUE(upSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9063 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 9064 | // Don't finish the events yet, and send a key |
| 9065 | mDispatcher->notifyKey( |
| 9066 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 9067 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 9068 | .build()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9069 | // Key will not be sent to the window, yet, because the window is still processing events |
| 9070 | // and the key remains pending, waiting for the touch events to be processed |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9071 | // Make sure that `assertNoEvents` doesn't wait too long, because it could cause an ANR. |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 9072 | mWindow->assertNoEvents(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9073 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 9074 | std::this_thread::sleep_for(400ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9075 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 9076 | // to the focused window, even though we have not yet finished the motion event |
| 9077 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 9078 | mWindow->finishEvent(*downSequenceNum); |
| 9079 | mWindow->finishEvent(*upSequenceNum); |
| 9080 | } |
| 9081 | |
| 9082 | /** |
| 9083 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 9084 | * not go to the focused window until the motion is processed. |
| 9085 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 9086 | * focused window right away. |
| 9087 | */ |
| 9088 | TEST_F(InputDispatcherSingleWindowAnr, |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 9089 | PendingKey_IsDeliveredWhileMotionIsProcessingAndNewTouchComesIn) { |
| 9090 | // The timeouts in this test are established by relying on the fact that the "key waiting for |
| 9091 | // events timeout" is equal to 500ms. |
| 9092 | ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9093 | mWindow->setDispatchingTimeout(2s); // Set a long ANR timeout to prevent it from triggering |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9094 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9095 | |
| 9096 | tapOnWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9097 | const auto& [downSequenceNum, _] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9098 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9099 | const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9100 | ASSERT_TRUE(upSequenceNum); |
| 9101 | // Don't finish the events yet, and send a key |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 9102 | mDispatcher->notifyKey( |
| 9103 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 9104 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 9105 | .build()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9106 | // At this point, key is still pending, and should not be sent to the application yet. |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 9107 | mWindow->assertNoEvents(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9108 | |
| 9109 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 9110 | tapOnWindow(); |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 9111 | // Now that we tapped, we should receive the key immediately. |
| 9112 | // Since there's still room for slowness, we use 200ms, which is much less than |
| 9113 | // the "key waiting for events' timeout of 500ms minus the already waited 100ms duration. |
| 9114 | std::unique_ptr<InputEvent> keyEvent = mWindow->consume(200ms); |
| 9115 | ASSERT_NE(nullptr, keyEvent); |
| 9116 | ASSERT_EQ(InputEventType::KEY, keyEvent->getType()); |
| 9117 | ASSERT_THAT(static_cast<KeyEvent&>(*keyEvent), WithKeyAction(AKEY_EVENT_ACTION_DOWN)); |
| 9118 | // it doesn't matter that we haven't ack'd the other events yet. We can finish events in any |
| 9119 | // order. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9120 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 9121 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 9122 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 9123 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9124 | mWindow->assertNoEvents(); |
| 9125 | } |
| 9126 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 9127 | /** |
| 9128 | * Send an event to the app and have the app not respond right away. |
| 9129 | * When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 9130 | * So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 9131 | * At some point, the window becomes responsive again. |
| 9132 | * Ensure that subsequent events get dropped, and the next gesture is delivered. |
| 9133 | */ |
| 9134 | TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) { |
| 9135 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9136 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 9137 | .build()); |
| 9138 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9139 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 9140 | ASSERT_TRUE(sequenceNum); |
| 9141 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 9142 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 9143 | |
| 9144 | mWindow->finishEvent(*sequenceNum); |
| 9145 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 9146 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9147 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
| 9148 | |
| 9149 | // Now that the window is responsive, let's continue the gesture. |
| 9150 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 9151 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 9152 | .build()); |
| 9153 | |
| 9154 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9155 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 9156 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 9157 | .build()); |
| 9158 | |
| 9159 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 9160 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 9161 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 9162 | .build()); |
| 9163 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 9164 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 9165 | .build()); |
| 9166 | // We already canceled this pointer, so the window shouldn't get any new events. |
| 9167 | mWindow->assertNoEvents(); |
| 9168 | |
| 9169 | // Start another one. |
| 9170 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9171 | .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15)) |
| 9172 | .build()); |
| 9173 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 9174 | } |
| 9175 | |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 9176 | // Send an event to the app and have the app not respond right away. Then remove the app window. |
| 9177 | // When the window is removed, the dispatcher will cancel the events for that window. |
| 9178 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 9179 | TEST_F(InputDispatcherSingleWindowAnr, AnrAfterWindowRemoval) { |
| 9180 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9181 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9182 | {WINDOW_LOCATION})); |
| 9183 | |
| 9184 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
| 9185 | ASSERT_TRUE(sequenceNum); |
| 9186 | |
| 9187 | // Remove the window, but the input channel should remain alive. |
| 9188 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 9189 | |
| 9190 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 9191 | // Since the window was removed, Dispatcher does not know the PID associated with the window |
| 9192 | // anymore, so the policy is notified without the PID. |
| 9193 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken(), |
| 9194 | /*pid=*/std::nullopt); |
| 9195 | |
| 9196 | mWindow->finishEvent(*sequenceNum); |
| 9197 | // The cancellation was generated when the window was removed, along with the focus event. |
| 9198 | mWindow->consumeMotionEvent( |
| 9199 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 9200 | mWindow->consumeFocusEvent(false); |
| 9201 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9202 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt); |
| 9203 | } |
| 9204 | |
| 9205 | // Send an event to the app and have the app not respond right away. Wait for the policy to be |
| 9206 | // notified of the unresponsive window, then remove the app window. |
| 9207 | TEST_F(InputDispatcherSingleWindowAnr, AnrFollowedByWindowRemoval) { |
| 9208 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9209 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9210 | {WINDOW_LOCATION})); |
| 9211 | |
| 9212 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
| 9213 | ASSERT_TRUE(sequenceNum); |
| 9214 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 9215 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 9216 | |
| 9217 | // Remove the window, but the input channel should remain alive. |
| 9218 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 9219 | |
| 9220 | mWindow->finishEvent(*sequenceNum); |
| 9221 | // The cancellation was generated during the ANR, and the window lost focus when it was removed. |
| 9222 | mWindow->consumeMotionEvent( |
| 9223 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 9224 | mWindow->consumeFocusEvent(false); |
| 9225 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9226 | // Since the window was removed, Dispatcher does not know the PID associated with the window |
| 9227 | // becoming responsive, so the policy is notified without the PID. |
| 9228 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt); |
| 9229 | } |
| 9230 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9231 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 9232 | virtual void SetUp() override { |
| 9233 | InputDispatcherTest::SetUp(); |
| 9234 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9235 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9236 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9237 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 9238 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9239 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9240 | // 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] | 9241 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9242 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9243 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 9244 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9245 | mFocusedWindow->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9246 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9247 | |
| 9248 | // Set focused application. |
| 9249 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9250 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9251 | |
| 9252 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9253 | mDispatcher->onWindowInfosChanged( |
| 9254 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9255 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9256 | mFocusedWindow->consumeFocusEvent(true); |
| 9257 | } |
| 9258 | |
| 9259 | virtual void TearDown() override { |
| 9260 | InputDispatcherTest::TearDown(); |
| 9261 | |
| 9262 | mUnfocusedWindow.clear(); |
| 9263 | mFocusedWindow.clear(); |
| 9264 | } |
| 9265 | |
| 9266 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9267 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9268 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 9269 | sp<FakeWindowHandle> mFocusedWindow; |
| 9270 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 9271 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 9272 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 9273 | |
| 9274 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 9275 | |
| 9276 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 9277 | |
| 9278 | private: |
| 9279 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9280 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9281 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9282 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9283 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9284 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9285 | location)); |
| 9286 | } |
| 9287 | }; |
| 9288 | |
| 9289 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 9290 | // should be ANR'd first. |
| 9291 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9292 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 9293 | injectMotionEvent(*mDispatcher, |
| 9294 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 9295 | AINPUT_SOURCE_TOUCHSCREEN) |
| 9296 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 9297 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 9298 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 9299 | .build())); |
| 9300 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9301 | injectMotionEvent(*mDispatcher, |
| 9302 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 9303 | AINPUT_SOURCE_TOUCHSCREEN) |
| 9304 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 9305 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 9306 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 9307 | .build())); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9308 | mFocusedWindow->consumeMotionDown(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 9309 | mFocusedWindow->consumeMotionUp(); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 9310 | mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9311 | // We consumed all events, so no ANR |
| 9312 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9313 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9314 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9315 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 9316 | injectMotionEvent(*mDispatcher, |
| 9317 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 9318 | AINPUT_SOURCE_TOUCHSCREEN) |
| 9319 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 9320 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 9321 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 9322 | .build())); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9323 | const auto [unfocusedSequenceNum, _] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9324 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9325 | |
| 9326 | const std::chrono::duration timeout = |
| 9327 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9328 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 9329 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9330 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9331 | mFocusedWindow->consumeMotionDown(); |
| 9332 | // This cancel is generated because the connection was unresponsive |
| 9333 | mFocusedWindow->consumeMotionCancel(); |
| 9334 | mFocusedWindow->assertNoEvents(); |
| 9335 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9336 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9337 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 9338 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9339 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9340 | } |
| 9341 | |
| 9342 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 9343 | // it doesn't matter which order they should have ANR. |
| 9344 | // But we should receive ANR for both. |
| 9345 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 9346 | // Set the timeout for unfocused window to match the focused window |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9347 | mUnfocusedWindow->setDispatchingTimeout( |
| 9348 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9349 | mDispatcher->onWindowInfosChanged( |
| 9350 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9351 | |
| 9352 | tapOnFocusedWindow(); |
| 9353 | // we should have ACTION_DOWN/ACTION_UP on focused window and ACTION_OUTSIDE on unfocused window |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9354 | // We don't know which window will ANR first. But both of them should happen eventually. |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9355 | std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken( |
| 9356 | mFocusedWindow->getDispatchingTimeout( |
| 9357 | DISPATCHING_TIMEOUT)), |
| 9358 | mFakePolicy->getUnresponsiveWindowToken(0ms)}; |
| 9359 | |
| 9360 | ASSERT_THAT(anrConnectionTokens, |
| 9361 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 9362 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9363 | |
| 9364 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9365 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9366 | |
| 9367 | mFocusedWindow->consumeMotionDown(); |
| 9368 | mFocusedWindow->consumeMotionUp(); |
| 9369 | mUnfocusedWindow->consumeMotionOutside(); |
| 9370 | |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9371 | std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(), |
| 9372 | mFakePolicy->getResponsiveWindowToken()}; |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9373 | |
| 9374 | // Both applications should be marked as responsive, in any order |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9375 | ASSERT_THAT(responsiveTokens, |
| 9376 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 9377 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9378 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9379 | } |
| 9380 | |
| 9381 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 9382 | // We should also log an error to account for the dropped event (not tested here). |
| 9383 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 9384 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 9385 | tapOnFocusedWindow(); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 9386 | mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9387 | // Receive the events, but don't respond |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9388 | const auto [downEventSequenceNum, downEvent] = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9389 | ASSERT_TRUE(downEventSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9390 | const auto [upEventSequenceNum, upEvent] = mFocusedWindow->receiveEvent(); // ACTION_UP |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9391 | ASSERT_TRUE(upEventSequenceNum); |
| 9392 | const std::chrono::duration timeout = |
| 9393 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9394 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9395 | |
| 9396 | // Tap once again |
| 9397 | // 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] | 9398 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9399 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9400 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9401 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9402 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9403 | FOCUSED_WINDOW_LOCATION)); |
| 9404 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 9405 | // valid touch target |
| 9406 | mUnfocusedWindow->assertNoEvents(); |
| 9407 | |
| 9408 | // Consume the first tap |
| 9409 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 9410 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 9411 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9412 | // The second tap did not go to the focused window |
| 9413 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9414 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9415 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 9416 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9417 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9418 | } |
| 9419 | |
| 9420 | // If you tap outside of all windows, there will not be ANR |
| 9421 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9422 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9423 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9424 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 9425 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9426 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9427 | } |
| 9428 | |
| 9429 | // Since the focused window is paused, tapping on it should not produce any events |
| 9430 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 9431 | mFocusedWindow->setPaused(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9432 | mDispatcher->onWindowInfosChanged( |
| 9433 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9434 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9435 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9436 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9437 | FOCUSED_WINDOW_LOCATION)); |
| 9438 | |
| 9439 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 9440 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9441 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 9442 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9443 | |
| 9444 | mFocusedWindow->assertNoEvents(); |
| 9445 | mUnfocusedWindow->assertNoEvents(); |
| 9446 | } |
| 9447 | |
| 9448 | /** |
| 9449 | * If a window is processing a motion event, and then a key event comes in, the key event should |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9450 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9451 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 9452 | * |
| 9453 | * Warning!!! |
| 9454 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 9455 | * and the injection timeout that we specify when injecting the key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9456 | * We must have the injection timeout (100ms) be smaller than |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9457 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 9458 | * |
| 9459 | * If that value changes, this test should also change. |
| 9460 | */ |
| 9461 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 9462 | // Set a long ANR timeout to prevent it from triggering |
| 9463 | mFocusedWindow->setDispatchingTimeout(2s); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9464 | mDispatcher->onWindowInfosChanged( |
| 9465 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9466 | |
| 9467 | tapOnUnfocusedWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9468 | const auto [downSequenceNum, downEvent] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9469 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9470 | const auto [upSequenceNum, upEvent] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9471 | ASSERT_TRUE(upSequenceNum); |
| 9472 | // Don't finish the events yet, and send a key |
| 9473 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 9474 | // window even if motions are still being processed. |
| 9475 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9476 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9477 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 9478 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9479 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9480 | // Key will not be sent to the window, yet, because the window is still processing events |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9481 | // and the key remains pending, waiting for the touch events to be processed. |
| 9482 | // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED |
| 9483 | // under the hood. |
| 9484 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 9485 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9486 | |
| 9487 | // 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] | 9488 | mFocusedWindow->setFocusable(false); |
| 9489 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9490 | mDispatcher->onWindowInfosChanged( |
| 9491 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9492 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9493 | |
| 9494 | // Focus events should precede the key events |
| 9495 | mUnfocusedWindow->consumeFocusEvent(true); |
| 9496 | mFocusedWindow->consumeFocusEvent(false); |
| 9497 | |
| 9498 | // Finish the tap events, which should unblock dispatcher |
| 9499 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 9500 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 9501 | |
| 9502 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 9503 | // can finally go to the newly focused "mUnfocusedWindow". |
| 9504 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 9505 | mFocusedWindow->assertNoEvents(); |
| 9506 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9507 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9508 | } |
| 9509 | |
| 9510 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 9511 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 9512 | // The other window should not be affected by that. |
| 9513 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 9514 | // Touch Window 1 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9515 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9516 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9517 | {FOCUSED_WINDOW_LOCATION})); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 9518 | mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9519 | |
| 9520 | // Touch Window 2 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9521 | mDispatcher->notifyMotion( |
| 9522 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9523 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9524 | |
| 9525 | const std::chrono::duration timeout = |
| 9526 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9527 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9528 | |
| 9529 | mUnfocusedWindow->consumeMotionDown(); |
| 9530 | mFocusedWindow->consumeMotionDown(); |
| 9531 | // Focused window may or may not receive ACTION_MOVE |
| 9532 | // But it should definitely receive ACTION_CANCEL due to the ANR |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9533 | const auto [moveOrCancelSequenceNum, event] = mFocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9534 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 9535 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 9536 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 9537 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9538 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 9539 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 9540 | mFocusedWindow->consumeMotionCancel(); |
| 9541 | } else { |
| 9542 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 9543 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9544 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9545 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 9546 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9547 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9548 | mUnfocusedWindow->assertNoEvents(); |
| 9549 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9550 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9551 | } |
| 9552 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9553 | /** |
| 9554 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 9555 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 9556 | * |
| 9557 | * If the user touches another application during this time, the key should be dropped. |
| 9558 | * Next, if a new focused window comes in, without toggling the focused application, |
| 9559 | * then no ANR should occur. |
| 9560 | * |
| 9561 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 9562 | * but in some cases the policy may not update the focused application. |
| 9563 | */ |
| 9564 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 9565 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 9566 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | c033dfb | 2023-10-03 10:45:16 -0700 | [diff] [blame] | 9567 | focusedApplication->setDispatchingTimeout(300ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9568 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 9569 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 9570 | mFocusedWindow->setFocusable(false); |
| 9571 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9572 | mDispatcher->onWindowInfosChanged( |
| 9573 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9574 | mFocusedWindow->consumeFocusEvent(false); |
| 9575 | |
| 9576 | // Send a key. The ANR timer should start because there is no focused window. |
| 9577 | // 'focusedApplication' will get blamed if this timer completes. |
| 9578 | // 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] | 9579 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9580 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 9581 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9582 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9583 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9584 | |
| 9585 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 9586 | // then the injected touches won't cause the focused event to get dropped. |
| 9587 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 9588 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 9589 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 9590 | // For this test, it means that the key would get delivered to the window once it becomes |
| 9591 | // focused. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9592 | std::this_thread::sleep_for(100ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9593 | |
| 9594 | // Touch unfocused window. This should force the pending key to get dropped. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9595 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9596 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9597 | {UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9598 | |
| 9599 | // We do not consume the motion right away, because that would require dispatcher to first |
| 9600 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 9601 | // Set the focused window first. |
| 9602 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9603 | mDispatcher->onWindowInfosChanged( |
| 9604 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9605 | setFocusedWindow(mFocusedWindow); |
| 9606 | mFocusedWindow->consumeFocusEvent(true); |
| 9607 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 9608 | // to another application. This could be a bug / behaviour in the policy. |
| 9609 | |
| 9610 | mUnfocusedWindow->consumeMotionDown(); |
| 9611 | |
| 9612 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9613 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 9614 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 9615 | } |
| 9616 | |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 9617 | /** |
| 9618 | * If we are pruning input queue, we should never drop pointer events. Otherwise, we risk having |
| 9619 | * an inconsistent event stream inside the dispatcher. In this test, we make sure that the |
| 9620 | * dispatcher doesn't prune pointer events incorrectly. |
| 9621 | * |
| 9622 | * This test reproduces a crash in InputDispatcher. |
| 9623 | * To reproduce the crash, we need to simulate the conditions for "pruning input queue" to occur. |
| 9624 | * |
| 9625 | * Keep the currently focused application (mApplication), and have no focused window. |
| 9626 | * We set up two additional windows: |
| 9627 | * 1) The navigation bar window. This simulates the system "NavigationBar", which is used in the |
| 9628 | * 3-button navigation mode. This window injects a BACK button when it's touched. 2) The application |
| 9629 | * window. This window is not focusable, but is touchable. |
| 9630 | * |
| 9631 | * We first touch the navigation bar, which causes it to inject a key. Since there's no focused |
| 9632 | * window, the dispatcher doesn't process this key, and all other events inside dispatcher are now |
| 9633 | * blocked. The dispatcher is waiting for 'mApplication' to add a focused window. |
| 9634 | * |
| 9635 | * Now, we touch "Another window". This window is owned by a different application than |
| 9636 | * 'mApplication'. This causes the dispatcher to stop waiting for 'mApplication' to add a focused |
| 9637 | * window. Now, the "pruning input queue" behaviour should kick in, and the dispatcher should start |
| 9638 | * dropping the events from its queue. Ensure that no crash occurs. |
| 9639 | * |
| 9640 | * In this test, we are setting long timeouts to prevent ANRs and events dropped due to being stale. |
| 9641 | * This does not affect the test running time. |
| 9642 | */ |
| 9643 | TEST_F(InputDispatcherMultiWindowAnr, PruningInputQueueShouldNotDropPointerEvents) { |
| 9644 | std::shared_ptr<FakeApplicationHandle> systemUiApplication = |
| 9645 | std::make_shared<FakeApplicationHandle>(); |
| 9646 | systemUiApplication->setDispatchingTimeout(3000ms); |
| 9647 | mFakePolicy->setStaleEventTimeout(3000ms); |
| 9648 | sp<FakeWindowHandle> navigationBar = |
| 9649 | sp<FakeWindowHandle>::make(systemUiApplication, mDispatcher, "NavigationBar", |
| 9650 | ADISPLAY_ID_DEFAULT); |
| 9651 | navigationBar->setFocusable(false); |
| 9652 | navigationBar->setWatchOutsideTouch(true); |
| 9653 | navigationBar->setFrame(Rect(0, 0, 100, 100)); |
| 9654 | |
| 9655 | mApplication->setDispatchingTimeout(3000ms); |
| 9656 | // 'mApplication' is already focused, but we call it again here to make it explicit. |
| 9657 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 9658 | |
| 9659 | std::shared_ptr<FakeApplicationHandle> anotherApplication = |
| 9660 | std::make_shared<FakeApplicationHandle>(); |
| 9661 | sp<FakeWindowHandle> appWindow = |
| 9662 | sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Another window", |
| 9663 | ADISPLAY_ID_DEFAULT); |
| 9664 | appWindow->setFocusable(false); |
| 9665 | appWindow->setFrame(Rect(100, 100, 200, 200)); |
| 9666 | |
| 9667 | mDispatcher->onWindowInfosChanged( |
| 9668 | {{*navigationBar->getInfo(), *appWindow->getInfo()}, {}, 0, 0}); |
| 9669 | // 'mFocusedWindow' is no longer in the dispatcher window list, and therefore loses focus |
| 9670 | mFocusedWindow->consumeFocusEvent(false); |
| 9671 | |
| 9672 | // Touch down the navigation bar. It consumes the touch and injects a key into the dispatcher |
| 9673 | // in response. |
| 9674 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9675 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 9676 | .build()); |
| 9677 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 9678 | |
| 9679 | // Key will not be sent anywhere because we have no focused window. It will remain pending. |
| 9680 | // Pretend we are injecting KEYCODE_BACK, but it doesn't actually matter what key it is. |
| 9681 | InputEventInjectionResult result = |
| 9682 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 9683 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms, |
| 9684 | /*allowKeyRepeat=*/false); |
| 9685 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
| 9686 | |
| 9687 | // Finish the gesture - lift up finger and inject ACTION_UP key event |
| 9688 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 9689 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 9690 | .build()); |
| 9691 | result = injectKey(*mDispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 9692 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms, |
| 9693 | /*allowKeyRepeat=*/false); |
| 9694 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
| 9695 | // The key that was injected is blocking the dispatcher, so the navigation bar shouldn't be |
| 9696 | // getting any events yet. |
| 9697 | navigationBar->assertNoEvents(); |
| 9698 | |
| 9699 | // Now touch "Another window". This touch is going to a different application than the one we |
| 9700 | // are waiting for (which is 'mApplication'). |
| 9701 | // This should cause the dispatcher to drop the pending focus-dispatched events (like the key |
| 9702 | // trying to be injected) and to continue processing the rest of the events in the original |
| 9703 | // order. |
| 9704 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9705 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 9706 | .build()); |
| 9707 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 9708 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_OUTSIDE)); |
| 9709 | appWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 9710 | |
| 9711 | appWindow->assertNoEvents(); |
| 9712 | navigationBar->assertNoEvents(); |
| 9713 | } |
| 9714 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9715 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 9716 | // that has feature NO_INPUT_CHANNEL. |
| 9717 | // Layout: |
| 9718 | // Top (closest to user) |
| 9719 | // mNoInputWindow (above all windows) |
| 9720 | // mBottomWindow |
| 9721 | // Bottom (furthest from user) |
| 9722 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 9723 | virtual void SetUp() override { |
| 9724 | InputDispatcherTest::SetUp(); |
| 9725 | |
| 9726 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9727 | mNoInputWindow = |
| 9728 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 9729 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 9730 | /*createInputChannel=*/false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9731 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9732 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 9733 | // It's perfectly valid for this window to not have an associated input channel |
| 9734 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9735 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 9736 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9737 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 9738 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9739 | mDispatcher->onWindowInfosChanged( |
| 9740 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9741 | } |
| 9742 | |
| 9743 | protected: |
| 9744 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 9745 | sp<FakeWindowHandle> mNoInputWindow; |
| 9746 | sp<FakeWindowHandle> mBottomWindow; |
| 9747 | }; |
| 9748 | |
| 9749 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 9750 | PointF touchedPoint = {10, 10}; |
| 9751 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9752 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9753 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9754 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9755 | |
| 9756 | mNoInputWindow->assertNoEvents(); |
| 9757 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 9758 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 9759 | // and therefore should prevent mBottomWindow from receiving touches |
| 9760 | mBottomWindow->assertNoEvents(); |
| 9761 | } |
| 9762 | |
| 9763 | /** |
| 9764 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 9765 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 9766 | */ |
| 9767 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 9768 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9769 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 9770 | "Window with input channel and NO_INPUT_CHANNEL", |
| 9771 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9772 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9773 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9774 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9775 | mDispatcher->onWindowInfosChanged( |
| 9776 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9777 | |
| 9778 | PointF touchedPoint = {10, 10}; |
| 9779 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9780 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9781 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9782 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9783 | |
| 9784 | mNoInputWindow->assertNoEvents(); |
| 9785 | mBottomWindow->assertNoEvents(); |
| 9786 | } |
| 9787 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9788 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 9789 | protected: |
| 9790 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 9791 | sp<FakeWindowHandle> mWindow; |
| 9792 | sp<FakeWindowHandle> mMirror; |
| 9793 | |
| 9794 | virtual void SetUp() override { |
| 9795 | InputDispatcherTest::SetUp(); |
| 9796 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9797 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 9798 | mMirror = mWindow->clone(ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9799 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 9800 | mWindow->setFocusable(true); |
| 9801 | mMirror->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9802 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9803 | } |
| 9804 | }; |
| 9805 | |
| 9806 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 9807 | // Request focus on a mirrored window |
| 9808 | setFocusedWindow(mMirror); |
| 9809 | |
| 9810 | // window gets focused |
| 9811 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9812 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9813 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9814 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9815 | } |
| 9816 | |
| 9817 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 9818 | // focusable. |
| 9819 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 9820 | setFocusedWindow(mMirror); |
| 9821 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9822 | // window gets focused because it is above the mirror |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9823 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9824 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9825 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9826 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9827 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9828 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9829 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9830 | |
| 9831 | mMirror->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9832 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9833 | |
| 9834 | // window loses focus since one of the windows associated with the token in not focusable |
| 9835 | mWindow->consumeFocusEvent(false); |
| 9836 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9837 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9838 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9839 | mWindow->assertNoEvents(); |
| 9840 | } |
| 9841 | |
| 9842 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 9843 | // invisible. |
| 9844 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 9845 | setFocusedWindow(mMirror); |
| 9846 | |
| 9847 | // window gets focused |
| 9848 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9849 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9850 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9851 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9852 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9853 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9854 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9855 | |
| 9856 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9857 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9858 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9859 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9860 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9861 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9862 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9863 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9864 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9865 | |
| 9866 | mWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9867 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9868 | |
| 9869 | // window loses focus only after all windows associated with the token become invisible. |
| 9870 | mWindow->consumeFocusEvent(false); |
| 9871 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9872 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9873 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9874 | mWindow->assertNoEvents(); |
| 9875 | } |
| 9876 | |
| 9877 | // A focused & mirrored window remains focused until both windows are removed. |
| 9878 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 9879 | setFocusedWindow(mMirror); |
| 9880 | |
| 9881 | // window gets focused |
| 9882 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9883 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9884 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9885 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9886 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9887 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9888 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9889 | |
| 9890 | // single window is removed but the window token remains focused |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9891 | mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9892 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9893 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9894 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9895 | mMirror->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9896 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9897 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9898 | mMirror->consumeKeyUp(ADISPLAY_ID_NONE); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9899 | |
| 9900 | // Both windows are removed |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9901 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9902 | mWindow->consumeFocusEvent(false); |
| 9903 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9904 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9905 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9906 | mWindow->assertNoEvents(); |
| 9907 | } |
| 9908 | |
| 9909 | // Focus request can be pending until one window becomes visible. |
| 9910 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 9911 | // Request focus on an invisible mirror. |
| 9912 | mWindow->setVisible(false); |
| 9913 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9914 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9915 | setFocusedWindow(mMirror); |
| 9916 | |
| 9917 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9918 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9919 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 9920 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9921 | |
| 9922 | mMirror->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9923 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9924 | |
| 9925 | // window gets focused |
| 9926 | mWindow->consumeFocusEvent(true); |
| 9927 | // window gets the pending key event |
| 9928 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 9929 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9930 | |
| 9931 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 9932 | protected: |
| 9933 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 9934 | sp<FakeWindowHandle> mWindow; |
| 9935 | sp<FakeWindowHandle> mSecondWindow; |
| 9936 | |
| 9937 | void SetUp() override { |
| 9938 | InputDispatcherTest::SetUp(); |
| 9939 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9940 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9941 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9942 | mSecondWindow = |
| 9943 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9944 | mSecondWindow->setFocusable(true); |
| 9945 | |
| 9946 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9947 | mDispatcher->onWindowInfosChanged( |
| 9948 | {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9949 | |
| 9950 | setFocusedWindow(mWindow); |
| 9951 | mWindow->consumeFocusEvent(true); |
| 9952 | } |
| 9953 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9954 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9955 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9956 | } |
| 9957 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9958 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 9959 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9960 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9961 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 9962 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9963 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9964 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9965 | } |
| 9966 | }; |
| 9967 | |
| 9968 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 9969 | // Ensure that capture cannot be obtained for unfocused windows. |
| 9970 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 9971 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 9972 | mSecondWindow->assertNoEvents(); |
| 9973 | |
| 9974 | // Ensure that capture can be enabled from the focus window. |
| 9975 | requestAndVerifyPointerCapture(mWindow, true); |
| 9976 | |
| 9977 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 9978 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 9979 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 9980 | |
| 9981 | // Ensure that capture can be disabled from the window with capture. |
| 9982 | requestAndVerifyPointerCapture(mWindow, false); |
| 9983 | } |
| 9984 | |
| 9985 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9986 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9987 | |
| 9988 | setFocusedWindow(mSecondWindow); |
| 9989 | |
| 9990 | // Ensure that the capture disabled event was sent first. |
| 9991 | mWindow->consumeCaptureEvent(false); |
| 9992 | mWindow->consumeFocusEvent(false); |
| 9993 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9994 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9995 | |
| 9996 | // 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] | 9997 | notifyPointerCaptureChanged({}); |
| 9998 | notifyPointerCaptureChanged(request); |
| 9999 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10000 | mWindow->assertNoEvents(); |
| 10001 | mSecondWindow->assertNoEvents(); |
| 10002 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 10003 | } |
| 10004 | |
| 10005 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10006 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10007 | |
| 10008 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10009 | notifyPointerCaptureChanged({}); |
| 10010 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10011 | |
| 10012 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10013 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10014 | mWindow->consumeCaptureEvent(false); |
| 10015 | mWindow->assertNoEvents(); |
| 10016 | } |
| 10017 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 10018 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 10019 | requestAndVerifyPointerCapture(mWindow, true); |
| 10020 | |
| 10021 | // The first window loses focus. |
| 10022 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10023 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 10024 | mWindow->consumeCaptureEvent(false); |
| 10025 | |
| 10026 | // Request Pointer Capture from the second window before the notification from InputReader |
| 10027 | // arrives. |
| 10028 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10029 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 10030 | |
| 10031 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10032 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 10033 | |
| 10034 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10035 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 10036 | |
| 10037 | mSecondWindow->consumeFocusEvent(true); |
| 10038 | mSecondWindow->consumeCaptureEvent(true); |
| 10039 | } |
| 10040 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10041 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 10042 | // App repeatedly enables and disables capture. |
| 10043 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 10044 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 10045 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 10046 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 10047 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 10048 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 10049 | |
| 10050 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 10051 | // first request is now stale, this should do nothing. |
| 10052 | notifyPointerCaptureChanged(firstRequest); |
| 10053 | mWindow->assertNoEvents(); |
| 10054 | |
| 10055 | // InputReader notifies that the second request was enabled. |
| 10056 | notifyPointerCaptureChanged(secondRequest); |
| 10057 | mWindow->consumeCaptureEvent(true); |
| 10058 | } |
| 10059 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 10060 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 10061 | requestAndVerifyPointerCapture(mWindow, true); |
| 10062 | |
| 10063 | // App toggles pointer capture off and on. |
| 10064 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 10065 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 10066 | |
| 10067 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 10068 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 10069 | |
| 10070 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 10071 | // preceding "disable" request. |
| 10072 | notifyPointerCaptureChanged(enableRequest); |
| 10073 | |
| 10074 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 10075 | // any notifications. |
| 10076 | mWindow->assertNoEvents(); |
| 10077 | } |
| 10078 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 10079 | /** |
| 10080 | * One window. Hover mouse in the window, and then start capture. Make sure that the relative |
| 10081 | * mouse movements don't affect the previous mouse hovering state. |
| 10082 | * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no |
| 10083 | * HOVER_MOVE events). |
| 10084 | */ |
| 10085 | TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) { |
| 10086 | // Mouse hover on the window |
| 10087 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 10088 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 10089 | .build()); |
| 10090 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 10091 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 10092 | .build()); |
| 10093 | |
| 10094 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER))); |
| 10095 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE))); |
| 10096 | |
| 10097 | // Start pointer capture |
| 10098 | requestAndVerifyPointerCapture(mWindow, true); |
| 10099 | |
| 10100 | // Send some relative mouse movements and receive them in the window. |
| 10101 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE) |
| 10102 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11)) |
| 10103 | .build()); |
| 10104 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11), |
| 10105 | WithSource(AINPUT_SOURCE_MOUSE_RELATIVE))); |
| 10106 | |
| 10107 | // Stop pointer capture |
| 10108 | requestAndVerifyPointerCapture(mWindow, false); |
| 10109 | |
| 10110 | // Continue hovering on the window |
| 10111 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 10112 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115)) |
| 10113 | .build()); |
| 10114 | mWindow->consumeMotionEvent( |
| 10115 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 10116 | |
| 10117 | mWindow->assertNoEvents(); |
| 10118 | } |
| 10119 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10120 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 10121 | protected: |
| 10122 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10123 | |
| 10124 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 10125 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 10126 | |
| 10127 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 10128 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 10129 | |
| 10130 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 10131 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 10132 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 10133 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 10134 | MAXIMUM_OBSCURING_OPACITY); |
| 10135 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 10136 | static constexpr gui::Uid TOUCHED_APP_UID{10001}; |
| 10137 | static constexpr gui::Uid APP_B_UID{10002}; |
| 10138 | static constexpr gui::Uid APP_C_UID{10003}; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10139 | |
| 10140 | sp<FakeWindowHandle> mTouchWindow; |
| 10141 | |
| 10142 | virtual void SetUp() override { |
| 10143 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10144 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10145 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 10146 | } |
| 10147 | |
| 10148 | virtual void TearDown() override { |
| 10149 | InputDispatcherTest::TearDown(); |
| 10150 | mTouchWindow.clear(); |
| 10151 | } |
| 10152 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 10153 | sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 10154 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10155 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10156 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10157 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10158 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10159 | return window; |
| 10160 | } |
| 10161 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 10162 | sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10163 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 10164 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10165 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10166 | // Generate an arbitrary PID based on the UID |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10167 | window->setOwnerInfo(gui::Pid{static_cast<pid_t>(1777 + (uid.val() % 10000))}, uid); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10168 | return window; |
| 10169 | } |
| 10170 | |
| 10171 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10172 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10173 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10174 | points)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10175 | } |
| 10176 | }; |
| 10177 | |
| 10178 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10179 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10180 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10181 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10182 | |
| 10183 | touch(); |
| 10184 | |
| 10185 | mTouchWindow->assertNoEvents(); |
| 10186 | } |
| 10187 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 10188 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10189 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 10190 | const sp<FakeWindowHandle>& w = |
| 10191 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10192 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10193 | |
| 10194 | touch(); |
| 10195 | |
| 10196 | mTouchWindow->assertNoEvents(); |
| 10197 | } |
| 10198 | |
| 10199 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 10200 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 10201 | const sp<FakeWindowHandle>& w = |
| 10202 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10203 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 10204 | |
| 10205 | touch(); |
| 10206 | |
| 10207 | w->assertNoEvents(); |
| 10208 | } |
| 10209 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10210 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10211 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10212 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10213 | |
| 10214 | touch(); |
| 10215 | |
| 10216 | mTouchWindow->consumeAnyMotionDown(); |
| 10217 | } |
| 10218 | |
| 10219 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10220 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10221 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10222 | w->setFrame(Rect(0, 0, 50, 50)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10223 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10224 | |
| 10225 | touch({PointF{100, 100}}); |
| 10226 | |
| 10227 | mTouchWindow->consumeAnyMotionDown(); |
| 10228 | } |
| 10229 | |
| 10230 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10231 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10232 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10233 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10234 | |
| 10235 | touch(); |
| 10236 | |
| 10237 | mTouchWindow->consumeAnyMotionDown(); |
| 10238 | } |
| 10239 | |
| 10240 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 10241 | const sp<FakeWindowHandle>& w = |
| 10242 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10243 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10244 | |
| 10245 | touch(); |
| 10246 | |
| 10247 | mTouchWindow->consumeAnyMotionDown(); |
| 10248 | } |
| 10249 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 10250 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 10251 | const sp<FakeWindowHandle>& w = |
| 10252 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10253 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 10254 | |
| 10255 | touch(); |
| 10256 | |
| 10257 | w->assertNoEvents(); |
| 10258 | } |
| 10259 | |
| 10260 | /** |
| 10261 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 10262 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 10263 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 10264 | */ |
| 10265 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10266 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 10267 | const sp<FakeWindowHandle>& w = |
| 10268 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10269 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10270 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 10271 | |
| 10272 | touch(); |
| 10273 | |
| 10274 | w->consumeMotionOutside(); |
| 10275 | } |
| 10276 | |
| 10277 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 10278 | const sp<FakeWindowHandle>& w = |
| 10279 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10280 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10281 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 10282 | |
| 10283 | touch(); |
| 10284 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 10285 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 10286 | } |
| 10287 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10288 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10289 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10290 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10291 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10292 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10293 | |
| 10294 | touch(); |
| 10295 | |
| 10296 | mTouchWindow->consumeAnyMotionDown(); |
| 10297 | } |
| 10298 | |
| 10299 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 10300 | const sp<FakeWindowHandle>& w = |
| 10301 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10302 | MAXIMUM_OBSCURING_OPACITY); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10303 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10304 | |
| 10305 | touch(); |
| 10306 | |
| 10307 | mTouchWindow->consumeAnyMotionDown(); |
| 10308 | } |
| 10309 | |
| 10310 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10311 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10312 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10313 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10314 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10315 | |
| 10316 | touch(); |
| 10317 | |
| 10318 | mTouchWindow->assertNoEvents(); |
| 10319 | } |
| 10320 | |
| 10321 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 10322 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 10323 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10324 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 10325 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10326 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10327 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 10328 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10329 | mDispatcher->onWindowInfosChanged( |
| 10330 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10331 | |
| 10332 | touch(); |
| 10333 | |
| 10334 | mTouchWindow->assertNoEvents(); |
| 10335 | } |
| 10336 | |
| 10337 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 10338 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 10339 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10340 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 10341 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10342 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10343 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 10344 | OPACITY_FAR_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10345 | mDispatcher->onWindowInfosChanged( |
| 10346 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10347 | |
| 10348 | touch(); |
| 10349 | |
| 10350 | mTouchWindow->consumeAnyMotionDown(); |
| 10351 | } |
| 10352 | |
| 10353 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10354 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 10355 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10356 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10357 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10358 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10359 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 10360 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10361 | mDispatcher->onWindowInfosChanged( |
| 10362 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10363 | |
| 10364 | touch(); |
| 10365 | |
| 10366 | mTouchWindow->consumeAnyMotionDown(); |
| 10367 | } |
| 10368 | |
| 10369 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 10370 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10371 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10372 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10373 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10374 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 10375 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10376 | mDispatcher->onWindowInfosChanged( |
| 10377 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10378 | |
| 10379 | touch(); |
| 10380 | |
| 10381 | mTouchWindow->assertNoEvents(); |
| 10382 | } |
| 10383 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10384 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10385 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 10386 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10387 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 10388 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10389 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10390 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10391 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10392 | mDispatcher->onWindowInfosChanged( |
| 10393 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10394 | |
| 10395 | touch(); |
| 10396 | |
| 10397 | mTouchWindow->assertNoEvents(); |
| 10398 | } |
| 10399 | |
| 10400 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10401 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 10402 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10403 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 10404 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10405 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10406 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10407 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10408 | mDispatcher->onWindowInfosChanged( |
| 10409 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10410 | |
| 10411 | touch(); |
| 10412 | |
| 10413 | mTouchWindow->consumeAnyMotionDown(); |
| 10414 | } |
| 10415 | |
| 10416 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 10417 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10418 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 10419 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10420 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10421 | |
| 10422 | touch(); |
| 10423 | |
| 10424 | mTouchWindow->consumeAnyMotionDown(); |
| 10425 | } |
| 10426 | |
| 10427 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 10428 | const sp<FakeWindowHandle>& w = |
| 10429 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10430 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10431 | |
| 10432 | touch(); |
| 10433 | |
| 10434 | mTouchWindow->consumeAnyMotionDown(); |
| 10435 | } |
| 10436 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 10437 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10438 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 10439 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 10440 | const sp<FakeWindowHandle>& w = |
| 10441 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10442 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 10443 | |
| 10444 | touch(); |
| 10445 | |
| 10446 | mTouchWindow->assertNoEvents(); |
| 10447 | } |
| 10448 | |
| 10449 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 10450 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 10451 | const sp<FakeWindowHandle>& w = |
| 10452 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10453 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 10454 | |
| 10455 | touch(); |
| 10456 | |
| 10457 | mTouchWindow->consumeAnyMotionDown(); |
| 10458 | } |
| 10459 | |
| 10460 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10461 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 10462 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 10463 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10464 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10465 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10466 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10467 | |
| 10468 | touch(); |
| 10469 | |
| 10470 | mTouchWindow->consumeAnyMotionDown(); |
| 10471 | } |
| 10472 | |
| 10473 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10474 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 10475 | const sp<FakeWindowHandle>& w1 = |
| 10476 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 10477 | OPACITY_BELOW_THRESHOLD); |
| 10478 | const sp<FakeWindowHandle>& w2 = |
| 10479 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10480 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10481 | mDispatcher->onWindowInfosChanged( |
| 10482 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10483 | |
| 10484 | touch(); |
| 10485 | |
| 10486 | mTouchWindow->assertNoEvents(); |
| 10487 | } |
| 10488 | |
| 10489 | /** |
| 10490 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 10491 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 10492 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 10493 | */ |
| 10494 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10495 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 10496 | const sp<FakeWindowHandle>& wB = |
| 10497 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 10498 | OPACITY_BELOW_THRESHOLD); |
| 10499 | const sp<FakeWindowHandle>& wC = |
| 10500 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 10501 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10502 | mDispatcher->onWindowInfosChanged( |
| 10503 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10504 | |
| 10505 | touch(); |
| 10506 | |
| 10507 | mTouchWindow->assertNoEvents(); |
| 10508 | } |
| 10509 | |
| 10510 | /** |
| 10511 | * This test is testing that a window from a different UID but with same application token doesn't |
| 10512 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 10513 | */ |
| 10514 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10515 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 10516 | const sp<FakeWindowHandle>& w = |
| 10517 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 10518 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10519 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 10520 | |
| 10521 | touch(); |
| 10522 | |
| 10523 | mTouchWindow->consumeAnyMotionDown(); |
| 10524 | } |
| 10525 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10526 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 10527 | protected: |
| 10528 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 10529 | sp<FakeWindowHandle> mWindow; |
| 10530 | sp<FakeWindowHandle> mSecondWindow; |
| 10531 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10532 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10533 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 10534 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10535 | |
| 10536 | void SetUp() override { |
| 10537 | InputDispatcherTest::SetUp(); |
| 10538 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10539 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10540 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10541 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10542 | mSecondWindow = |
| 10543 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10544 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10545 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10546 | mSpyWindow = |
| 10547 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10548 | mSpyWindow->setSpy(true); |
| 10549 | mSpyWindow->setTrustedOverlay(true); |
| 10550 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 10551 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10552 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10553 | mDispatcher->onWindowInfosChanged( |
| 10554 | {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 10555 | {}, |
| 10556 | 0, |
| 10557 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10558 | } |
| 10559 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10560 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 10561 | switch (fromSource) { |
| 10562 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 10563 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10564 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10565 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10566 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10567 | break; |
| 10568 | case AINPUT_SOURCE_STYLUS: |
| 10569 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10570 | injectMotionEvent(*mDispatcher, |
| 10571 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10572 | AINPUT_SOURCE_STYLUS) |
| 10573 | .buttonState( |
| 10574 | AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 10575 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
| 10576 | .x(50) |
| 10577 | .y(50)) |
| 10578 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10579 | break; |
| 10580 | case AINPUT_SOURCE_MOUSE: |
| 10581 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10582 | injectMotionEvent(*mDispatcher, |
| 10583 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10584 | AINPUT_SOURCE_MOUSE) |
| 10585 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 10586 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 10587 | ToolType::MOUSE) |
| 10588 | .x(50) |
| 10589 | .y(50)) |
| 10590 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10591 | break; |
| 10592 | default: |
| 10593 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 10594 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10595 | |
| 10596 | // Window should receive motion event. |
| 10597 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10598 | // Spy window should also receive motion event |
| 10599 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10600 | } |
| 10601 | |
| 10602 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 10603 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 10604 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10605 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10606 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10607 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10608 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10609 | |
| 10610 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10611 | mDragWindow = |
| 10612 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10613 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10614 | mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), |
| 10615 | *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 10616 | {}, |
| 10617 | 0, |
| 10618 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10619 | |
| 10620 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10621 | bool transferred = |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 10622 | mDispatcher->transferTouchGesture(mWindow->getToken(), mDragWindow->getToken(), |
| 10623 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10624 | if (transferred) { |
| 10625 | mWindow->consumeMotionCancel(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10626 | mDragWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10627 | } |
| 10628 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10629 | } |
| 10630 | }; |
| 10631 | |
| 10632 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10633 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10634 | |
| 10635 | // Move on window. |
| 10636 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10637 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10638 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10639 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10640 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10641 | mWindow->consumeDragEvent(false, 50, 50); |
| 10642 | mSecondWindow->assertNoEvents(); |
| 10643 | |
| 10644 | // Move to another window. |
| 10645 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10646 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10647 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10648 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10649 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10650 | mWindow->consumeDragEvent(true, 150, 50); |
| 10651 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10652 | |
| 10653 | // Move back to original window. |
| 10654 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10655 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10656 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10657 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10658 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10659 | mWindow->consumeDragEvent(false, 50, 50); |
| 10660 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 10661 | |
| 10662 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10663 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10664 | {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10665 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10666 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10667 | mWindow->assertNoEvents(); |
| 10668 | mSecondWindow->assertNoEvents(); |
| 10669 | } |
| 10670 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10671 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10672 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10673 | |
| 10674 | // No cancel event after drag start |
| 10675 | mSpyWindow->assertNoEvents(); |
| 10676 | |
| 10677 | const MotionEvent secondFingerDownEvent = |
| 10678 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10679 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10680 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10681 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10682 | .build(); |
| 10683 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10684 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10685 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10686 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10687 | |
| 10688 | // Receives cancel for first pointer after next pointer down |
| 10689 | mSpyWindow->consumeMotionCancel(); |
| 10690 | mSpyWindow->consumeMotionDown(); |
| 10691 | |
| 10692 | mSpyWindow->assertNoEvents(); |
| 10693 | } |
| 10694 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10695 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10696 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10697 | |
| 10698 | // Move on window. |
| 10699 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10700 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10701 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10702 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10703 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10704 | mWindow->consumeDragEvent(false, 50, 50); |
| 10705 | mSecondWindow->assertNoEvents(); |
| 10706 | |
| 10707 | // Move to another window. |
| 10708 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10709 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10710 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10711 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10712 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10713 | mWindow->consumeDragEvent(true, 150, 50); |
| 10714 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10715 | |
| 10716 | // drop to another window. |
| 10717 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10718 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10719 | {150, 50})) |
| 10720 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10721 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10722 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10723 | mWindow->assertNoEvents(); |
| 10724 | mSecondWindow->assertNoEvents(); |
| 10725 | } |
| 10726 | |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10727 | TEST_F(InputDispatcherDragTests, DragAndDropNotCancelledIfSomeOtherPointerIsPilfered) { |
| 10728 | startDrag(); |
| 10729 | |
| 10730 | // No cancel event after drag start |
| 10731 | mSpyWindow->assertNoEvents(); |
| 10732 | |
| 10733 | const MotionEvent secondFingerDownEvent = |
| 10734 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10735 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 10736 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10737 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 10738 | .build(); |
| 10739 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10740 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 10741 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10742 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10743 | |
| 10744 | // Receives cancel for first pointer after next pointer down |
| 10745 | mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 10746 | mSpyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithPointerIds({1}))); |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10747 | mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 10748 | |
| 10749 | mSpyWindow->assertNoEvents(); |
| 10750 | |
| 10751 | // Spy window calls pilfer pointers |
| 10752 | EXPECT_EQ(OK, mDispatcher->pilferPointers(mSpyWindow->getToken())); |
| 10753 | mDragWindow->assertNoEvents(); |
| 10754 | |
| 10755 | const MotionEvent firstFingerMoveEvent = |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 10756 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10757 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 10758 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(60).y(60)) |
| 10759 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 10760 | .build(); |
| 10761 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 10762 | injectMotionEvent(*mDispatcher, firstFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10763 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10764 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10765 | |
| 10766 | // Drag window should still receive the new event |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10767 | mDragWindow->consumeMotionEvent( |
| 10768 | AllOf(WithMotionAction(ACTION_MOVE), WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10769 | mDragWindow->assertNoEvents(); |
| 10770 | } |
| 10771 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10772 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10773 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10774 | |
| 10775 | // Move on window and keep button pressed. |
| 10776 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10777 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10778 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 10779 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10780 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10781 | .build())) |
| 10782 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10783 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10784 | mWindow->consumeDragEvent(false, 50, 50); |
| 10785 | mSecondWindow->assertNoEvents(); |
| 10786 | |
| 10787 | // Move to another window and release button, expect to drop item. |
| 10788 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10789 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10790 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 10791 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10792 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10793 | .build())) |
| 10794 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10795 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10796 | mWindow->assertNoEvents(); |
| 10797 | mSecondWindow->assertNoEvents(); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10798 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10799 | |
| 10800 | // nothing to the window. |
| 10801 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10802 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10803 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 10804 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10805 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10806 | .build())) |
| 10807 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10808 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10809 | mWindow->assertNoEvents(); |
| 10810 | mSecondWindow->assertNoEvents(); |
| 10811 | } |
| 10812 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10813 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10814 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10815 | |
| 10816 | // Set second window invisible. |
| 10817 | mSecondWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10818 | mDispatcher->onWindowInfosChanged( |
| 10819 | {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10820 | |
| 10821 | // Move on window. |
| 10822 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10823 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10824 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10825 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10826 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10827 | mWindow->consumeDragEvent(false, 50, 50); |
| 10828 | mSecondWindow->assertNoEvents(); |
| 10829 | |
| 10830 | // Move to another window. |
| 10831 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10832 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10833 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10834 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10835 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10836 | mWindow->consumeDragEvent(true, 150, 50); |
| 10837 | mSecondWindow->assertNoEvents(); |
| 10838 | |
| 10839 | // drop to another window. |
| 10840 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10841 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10842 | {150, 50})) |
| 10843 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10844 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10845 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10846 | mWindow->assertNoEvents(); |
| 10847 | mSecondWindow->assertNoEvents(); |
| 10848 | } |
| 10849 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10850 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10851 | // Ensure window could track pointerIds if it didn't support split touch. |
| 10852 | mWindow->setPreventSplitting(true); |
| 10853 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10854 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10855 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10856 | {50, 50})) |
| 10857 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10858 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10859 | |
| 10860 | const MotionEvent secondFingerDownEvent = |
| 10861 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10862 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10863 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10864 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10865 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10866 | .build(); |
| 10867 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10868 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10869 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10870 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10871 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10872 | |
| 10873 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10874 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10875 | } |
| 10876 | |
| 10877 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 10878 | // First down on second window. |
| 10879 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10880 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10881 | {150, 50})) |
| 10882 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10883 | |
| 10884 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10885 | |
| 10886 | // Second down on first window. |
| 10887 | const MotionEvent secondFingerDownEvent = |
| 10888 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10889 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10890 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10891 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10892 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10893 | .build(); |
| 10894 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10895 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10896 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10897 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10898 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 10899 | mSecondWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10900 | |
| 10901 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10902 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10903 | |
| 10904 | // Move on window. |
| 10905 | const MotionEvent secondFingerMoveEvent = |
| 10906 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 10907 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10908 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10909 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10910 | .build(); |
| 10911 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10912 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10913 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10914 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10915 | mWindow->consumeDragEvent(false, 50, 50); |
| 10916 | mSecondWindow->consumeMotionMove(); |
| 10917 | |
| 10918 | // Release the drag pointer should perform drop. |
| 10919 | const MotionEvent secondFingerUpEvent = |
| 10920 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 10921 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10922 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10923 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10924 | .build(); |
| 10925 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10926 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10927 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10928 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10929 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken()); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10930 | mWindow->assertNoEvents(); |
| 10931 | mSecondWindow->consumeMotionMove(); |
| 10932 | } |
| 10933 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10934 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10935 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10936 | |
| 10937 | // Update window of second display. |
| 10938 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10939 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10940 | mDispatcher->onWindowInfosChanged( |
| 10941 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 10942 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 10943 | {}, |
| 10944 | 0, |
| 10945 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10946 | |
| 10947 | // Let second display has a touch state. |
| 10948 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10949 | injectMotionEvent(*mDispatcher, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10950 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10951 | AINPUT_SOURCE_TOUCHSCREEN) |
| 10952 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10953 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10954 | .build())); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 10955 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10956 | // Update window again. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10957 | mDispatcher->onWindowInfosChanged( |
| 10958 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 10959 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 10960 | {}, |
| 10961 | 0, |
| 10962 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10963 | |
| 10964 | // Move on window. |
| 10965 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10966 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10967 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10968 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10969 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10970 | mWindow->consumeDragEvent(false, 50, 50); |
| 10971 | mSecondWindow->assertNoEvents(); |
| 10972 | |
| 10973 | // Move to another window. |
| 10974 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10975 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10976 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10977 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10978 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10979 | mWindow->consumeDragEvent(true, 150, 50); |
| 10980 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10981 | |
| 10982 | // drop to another window. |
| 10983 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10984 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10985 | {150, 50})) |
| 10986 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10987 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10988 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10989 | mWindow->assertNoEvents(); |
| 10990 | mSecondWindow->assertNoEvents(); |
| 10991 | } |
| 10992 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10993 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 10994 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 10995 | // Move on window. |
| 10996 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10997 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10998 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 10999 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11000 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11001 | .x(50) |
| 11002 | .y(50)) |
| 11003 | .build())) |
| 11004 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 11005 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11006 | mWindow->consumeDragEvent(false, 50, 50); |
| 11007 | mSecondWindow->assertNoEvents(); |
| 11008 | |
| 11009 | // Move to another window. |
| 11010 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11011 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11012 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 11013 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11014 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11015 | .x(150) |
| 11016 | .y(50)) |
| 11017 | .build())) |
| 11018 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 11019 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11020 | mWindow->consumeDragEvent(true, 150, 50); |
| 11021 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 11022 | |
| 11023 | // drop to another window. |
| 11024 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11025 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11026 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 11027 | .buttonState(0) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11028 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11029 | .x(150) |
| 11030 | .y(50)) |
| 11031 | .build())) |
| 11032 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 11033 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 11034 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11035 | mWindow->assertNoEvents(); |
| 11036 | mSecondWindow->assertNoEvents(); |
| 11037 | } |
| 11038 | |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 11039 | /** |
| 11040 | * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag |
| 11041 | * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash. |
| 11042 | */ |
| 11043 | TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) { |
| 11044 | // Down on second window |
| 11045 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11046 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 11047 | {150, 50})) |
| 11048 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11049 | |
| 11050 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown()); |
| 11051 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown()); |
| 11052 | |
| 11053 | // Down on first window |
| 11054 | const MotionEvent secondFingerDownEvent = |
| 11055 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11056 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11057 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 11058 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 11059 | .build(); |
| 11060 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11061 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 11062 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11063 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11064 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 11065 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove()); |
| 11066 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1)); |
| 11067 | |
| 11068 | // Start drag on first window |
| 11069 | ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN)); |
| 11070 | |
| 11071 | // Trigger cancel |
| 11072 | mDispatcher->cancelCurrentTouch(); |
| 11073 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel()); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 11074 | ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, |
| 11075 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)); |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 11076 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel()); |
| 11077 | |
| 11078 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 11079 | // The D&D finished with nullptr |
| 11080 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
| 11081 | |
| 11082 | // Remove drag window |
| 11083 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
| 11084 | |
| 11085 | // Inject a simple gesture, ensure dispatcher not crashed |
| 11086 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11087 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 11088 | PointF{50, 50})) |
| 11089 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11090 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 11091 | |
| 11092 | const MotionEvent moveEvent = |
| 11093 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 11094 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11095 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11096 | .build(); |
| 11097 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11098 | injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT, |
| 11099 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11100 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11101 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove()); |
| 11102 | |
| 11103 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11104 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 11105 | {50, 50})) |
| 11106 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11107 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp()); |
| 11108 | } |
| 11109 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 11110 | TEST_F(InputDispatcherDragTests, NoDragAndDropWithHoveringPointer) { |
| 11111 | // Start hovering over the window. |
| 11112 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11113 | injectMotionEvent(*mDispatcher, ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE, |
| 11114 | ADISPLAY_ID_DEFAULT, {50, 50})); |
| 11115 | |
| 11116 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 11117 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 11118 | |
| 11119 | ASSERT_FALSE(startDrag(/*sendDown=*/false)) |
| 11120 | << "Drag and drop should not work with a hovering pointer"; |
| 11121 | } |
| 11122 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11123 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 11124 | |
| 11125 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 11126 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11127 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 11128 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11129 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11130 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 11131 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11132 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11133 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11134 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11135 | |
| 11136 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11137 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11138 | window->assertNoEvents(); |
| 11139 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11140 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 11141 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 11142 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 11143 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 11144 | mDispatcher->waitForIdle(); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11145 | window->assertNoEvents(); |
| 11146 | |
| 11147 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11148 | window->setDropInput(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11149 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11150 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11151 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11152 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 11153 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11154 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 11155 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11156 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11157 | window->assertNoEvents(); |
| 11158 | } |
| 11159 | |
| 11160 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 11161 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 11162 | std::make_shared<FakeApplicationHandle>(); |
| 11163 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11164 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 11165 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11166 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11167 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11168 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11169 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11170 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 11171 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11172 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11173 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11174 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 11175 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11176 | mDispatcher->onWindowInfosChanged( |
| 11177 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11178 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11179 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11180 | |
| 11181 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11182 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11183 | window->assertNoEvents(); |
| 11184 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11185 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 11186 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 11187 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 11188 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11189 | window->assertNoEvents(); |
| 11190 | |
| 11191 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11192 | window->setDropInputIfObscured(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11193 | mDispatcher->onWindowInfosChanged( |
| 11194 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11195 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11196 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11197 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 11198 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11199 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 11200 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11201 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 11202 | window->assertNoEvents(); |
| 11203 | } |
| 11204 | |
| 11205 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 11206 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 11207 | std::make_shared<FakeApplicationHandle>(); |
| 11208 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11209 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 11210 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11211 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11212 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11213 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11214 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11215 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 11216 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11217 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11218 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11219 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 11220 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11221 | mDispatcher->onWindowInfosChanged( |
| 11222 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11223 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11224 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11225 | |
| 11226 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11227 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11228 | window->assertNoEvents(); |
| 11229 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11230 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 11231 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 11232 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 11233 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11234 | window->assertNoEvents(); |
| 11235 | |
| 11236 | // When the window is no longer obscured because it went on top, it should get input |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11237 | mDispatcher->onWindowInfosChanged( |
| 11238 | {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11239 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11240 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11241 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 11242 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11243 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 11244 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11245 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11246 | window->assertNoEvents(); |
| 11247 | } |
| 11248 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11249 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 11250 | protected: |
| 11251 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11252 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11253 | sp<FakeWindowHandle> mWindow; |
| 11254 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11255 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11256 | |
| 11257 | void SetUp() override { |
| 11258 | InputDispatcherTest::SetUp(); |
| 11259 | |
| 11260 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11261 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11262 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11263 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11264 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11265 | mSecondWindow = |
| 11266 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11267 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11268 | mThirdWindow = |
| 11269 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 11270 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 11271 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11272 | |
| 11273 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11274 | mDispatcher->onWindowInfosChanged( |
| 11275 | {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()}, |
| 11276 | {}, |
| 11277 | 0, |
| 11278 | 0}); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11279 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11280 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11281 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11282 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11283 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11284 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11285 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 11286 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11287 | mThirdWindow->assertNoEvents(); |
| 11288 | } |
| 11289 | |
| 11290 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 11291 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11292 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11293 | SECOND_DISPLAY_ID)) { |
| 11294 | mWindow->assertNoEvents(); |
| 11295 | mSecondWindow->assertNoEvents(); |
| 11296 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11297 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11298 | } |
| 11299 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11300 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11301 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 11302 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 11303 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11304 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 11305 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11306 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11307 | } |
| 11308 | }; |
| 11309 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11310 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 11311 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11312 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 11313 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11314 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11315 | } |
| 11316 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11317 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 11318 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11319 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11320 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11321 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11322 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11323 | ownerUid, /*hasPermission=*/false, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 11324 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11325 | mWindow->assertNoEvents(); |
| 11326 | mSecondWindow->assertNoEvents(); |
| 11327 | } |
| 11328 | |
| 11329 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 11330 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11331 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11332 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11333 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11334 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11335 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11336 | } |
| 11337 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11338 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 11339 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11340 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 11341 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11342 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11343 | mWindow->assertNoEvents(); |
| 11344 | mSecondWindow->assertNoEvents(); |
| 11345 | } |
| 11346 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11347 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 11348 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 11349 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 11350 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11351 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11352 | mWindow->assertNoEvents(); |
| 11353 | mSecondWindow->assertNoEvents(); |
| 11354 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 11355 | } |
| 11356 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11357 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 11358 | // Interact with the window first. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11359 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11360 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11361 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 11362 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 11363 | |
| 11364 | // Then remove focus. |
| 11365 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11366 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11367 | |
| 11368 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 11369 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 11370 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 11371 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11372 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11373 | } |
| 11374 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11375 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 11376 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11377 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11378 | std::shared_ptr<FakeApplicationHandle> application = |
| 11379 | std::make_shared<FakeApplicationHandle>(); |
| 11380 | std::string name = "Fake Spy "; |
| 11381 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11382 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 11383 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11384 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 11385 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11386 | return spy; |
| 11387 | } |
| 11388 | |
| 11389 | sp<FakeWindowHandle> createForeground() { |
| 11390 | std::shared_ptr<FakeApplicationHandle> application = |
| 11391 | std::make_shared<FakeApplicationHandle>(); |
| 11392 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11393 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 11394 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11395 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11396 | return window; |
| 11397 | } |
| 11398 | |
| 11399 | private: |
| 11400 | int mSpyCount{0}; |
| 11401 | }; |
| 11402 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11403 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11404 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 11405 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 11406 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11407 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
Siarhei Vishniakou | 21f77bd | 2023-07-18 17:51:35 -0700 | [diff] [blame] | 11408 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11409 | ScopedSilentDeath _silentDeath; |
| 11410 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11411 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 11412 | spy->setTrustedOverlay(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11413 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 11414 | ".* not a trusted overlay"); |
| 11415 | } |
| 11416 | |
| 11417 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11418 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 11419 | */ |
| 11420 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11421 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11422 | mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11423 | |
| 11424 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11425 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11426 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11427 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11428 | } |
| 11429 | |
| 11430 | /** |
| 11431 | * Verify the order in which different input windows receive events. The touched foreground window |
| 11432 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 11433 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 11434 | * receive events before ones belows it. |
| 11435 | * |
| 11436 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 11437 | * spy1, spy2, window, spy3. |
| 11438 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 11439 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 11440 | * window. |
| 11441 | */ |
| 11442 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 11443 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11444 | auto spy1 = createSpy(); |
| 11445 | auto spy2 = createSpy(); |
| 11446 | auto spy3 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11447 | mDispatcher->onWindowInfosChanged( |
| 11448 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11449 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 11450 | const size_t numChannels = channels.size(); |
| 11451 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 11452 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11453 | if (!epollFd.ok()) { |
| 11454 | FAIL() << "Failed to create epoll fd"; |
| 11455 | } |
| 11456 | |
| 11457 | for (size_t i = 0; i < numChannels; i++) { |
| 11458 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 11459 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 11460 | FAIL() << "Failed to add fd to epoll"; |
| 11461 | } |
| 11462 | } |
| 11463 | |
| 11464 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11465 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11466 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11467 | |
| 11468 | std::vector<size_t> eventOrder; |
| 11469 | std::vector<struct epoll_event> events(numChannels); |
| 11470 | for (;;) { |
| 11471 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 11472 | (100ms).count()); |
| 11473 | if (nFds < 0) { |
| 11474 | FAIL() << "Failed to call epoll_wait"; |
| 11475 | } |
| 11476 | if (nFds == 0) { |
| 11477 | break; // epoll_wait timed out |
| 11478 | } |
| 11479 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 11480 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 11481 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11482 | channels[i]->consumeMotionDown(); |
| 11483 | } |
| 11484 | } |
| 11485 | |
| 11486 | // Verify the order in which the events were received. |
| 11487 | EXPECT_EQ(3u, eventOrder.size()); |
| 11488 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 11489 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 11490 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 11491 | } |
| 11492 | |
| 11493 | /** |
| 11494 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 11495 | */ |
| 11496 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 11497 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11498 | auto spy = createSpy(); |
| 11499 | spy->setTouchable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11500 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11501 | |
| 11502 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11503 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11504 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11505 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11506 | spy->assertNoEvents(); |
| 11507 | } |
| 11508 | |
| 11509 | /** |
| 11510 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 11511 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 11512 | * to the window. |
| 11513 | */ |
| 11514 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 11515 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11516 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11517 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11518 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11519 | |
| 11520 | // Inject an event outside the spy window's touchable region. |
| 11521 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11522 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11523 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11524 | window->consumeMotionDown(); |
| 11525 | spy->assertNoEvents(); |
| 11526 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11527 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11528 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11529 | window->consumeMotionUp(); |
| 11530 | spy->assertNoEvents(); |
| 11531 | |
| 11532 | // Inject an event inside the spy window's touchable region. |
| 11533 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11534 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11535 | {5, 10})) |
| 11536 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11537 | window->consumeMotionDown(); |
| 11538 | spy->consumeMotionDown(); |
| 11539 | } |
| 11540 | |
| 11541 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11542 | * 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] | 11543 | * 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] | 11544 | */ |
| 11545 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 11546 | auto window = createForeground(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11547 | window->setOwnerInfo(gui::Pid{12}, gui::Uid{34}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11548 | auto spy = createSpy(); |
| 11549 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11550 | spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11551 | spy->setFrame(Rect{0, 0, 20, 20}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11552 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11553 | |
| 11554 | // Inject an event outside the spy window's frame and touchable region. |
| 11555 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11556 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 11557 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11558 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11559 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 11560 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11561 | } |
| 11562 | |
| 11563 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11564 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 11565 | * pointers that are down within its bounds. |
| 11566 | */ |
| 11567 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 11568 | auto windowLeft = createForeground(); |
| 11569 | windowLeft->setFrame({0, 0, 100, 200}); |
| 11570 | auto windowRight = createForeground(); |
| 11571 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11572 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11573 | spy->setFrame({0, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11574 | mDispatcher->onWindowInfosChanged( |
| 11575 | {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11576 | |
| 11577 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11578 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11579 | {50, 50})) |
| 11580 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11581 | windowLeft->consumeMotionDown(); |
| 11582 | spy->consumeMotionDown(); |
| 11583 | |
| 11584 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 11585 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11586 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11587 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11588 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11589 | .build(); |
| 11590 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11591 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11592 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11593 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11594 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11595 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11596 | } |
| 11597 | |
| 11598 | /** |
| 11599 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 11600 | * the spy should receive the second pointer with ACTION_DOWN. |
| 11601 | */ |
| 11602 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 11603 | auto window = createForeground(); |
| 11604 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11605 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11606 | spyRight->setFrame({100, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11607 | mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11608 | |
| 11609 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11610 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11611 | {50, 50})) |
| 11612 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11613 | window->consumeMotionDown(); |
| 11614 | spyRight->assertNoEvents(); |
| 11615 | |
| 11616 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 11617 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11618 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11619 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11620 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11621 | .build(); |
| 11622 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11623 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11624 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11625 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11626 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11627 | spyRight->consumeMotionDown(); |
| 11628 | } |
| 11629 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11630 | /** |
| 11631 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 11632 | * windows should be allowed to control split touch. |
| 11633 | */ |
| 11634 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 11635 | // 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] | 11636 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11637 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 11638 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11639 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11640 | auto window = createForeground(); |
| 11641 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11642 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11643 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11644 | |
| 11645 | // First finger down, no window touched. |
| 11646 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11647 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11648 | {100, 200})) |
| 11649 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11650 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11651 | window->assertNoEvents(); |
| 11652 | |
| 11653 | // Second finger down on window, the window should receive touch down. |
| 11654 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 11655 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11656 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11657 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11658 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 11659 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11660 | .build(); |
| 11661 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11662 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11663 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11664 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11665 | |
| 11666 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11667 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11668 | } |
| 11669 | |
| 11670 | /** |
| 11671 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 11672 | * do not receive key events. |
| 11673 | */ |
| 11674 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11675 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11676 | spy->setFocusable(false); |
| 11677 | |
| 11678 | auto window = createForeground(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11679 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11680 | setFocusedWindow(window); |
| 11681 | window->consumeFocusEvent(true); |
| 11682 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11683 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11684 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 11685 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 11686 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11687 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11688 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 11689 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 11690 | |
| 11691 | spy->assertNoEvents(); |
| 11692 | } |
| 11693 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11694 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 11695 | |
| 11696 | /** |
| 11697 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 11698 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 11699 | */ |
| 11700 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 11701 | auto window = createForeground(); |
| 11702 | auto spy1 = createSpy(); |
| 11703 | auto spy2 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11704 | mDispatcher->onWindowInfosChanged( |
| 11705 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11706 | |
| 11707 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11708 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11709 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11710 | window->consumeMotionDown(); |
| 11711 | spy1->consumeMotionDown(); |
| 11712 | spy2->consumeMotionDown(); |
| 11713 | |
| 11714 | // Pilfer pointers from the second spy window. |
| 11715 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 11716 | spy2->assertNoEvents(); |
| 11717 | spy1->consumeMotionCancel(); |
| 11718 | window->consumeMotionCancel(); |
| 11719 | |
| 11720 | // The rest of the gesture should only be sent to the second spy window. |
| 11721 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11722 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11723 | ADISPLAY_ID_DEFAULT)) |
| 11724 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11725 | spy2->consumeMotionMove(); |
| 11726 | spy1->assertNoEvents(); |
| 11727 | window->assertNoEvents(); |
| 11728 | } |
| 11729 | |
| 11730 | /** |
| 11731 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 11732 | * in the middle of the gesture. |
| 11733 | */ |
| 11734 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 11735 | auto window = createForeground(); |
| 11736 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11737 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11738 | |
| 11739 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11740 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11741 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11742 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11743 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11744 | |
| 11745 | window->releaseChannel(); |
| 11746 | |
| 11747 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11748 | |
| 11749 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11750 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11751 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11752 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 11753 | } |
| 11754 | |
| 11755 | /** |
| 11756 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 11757 | * the spy, but not to any other windows. |
| 11758 | */ |
| 11759 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 11760 | auto spy = createSpy(); |
| 11761 | auto window = createForeground(); |
| 11762 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11763 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11764 | |
| 11765 | // First finger down on the window and the spy. |
| 11766 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11767 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11768 | {100, 200})) |
| 11769 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11770 | spy->consumeMotionDown(); |
| 11771 | window->consumeMotionDown(); |
| 11772 | |
| 11773 | // Spy window pilfers the pointers. |
| 11774 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11775 | window->consumeMotionCancel(); |
| 11776 | |
| 11777 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 11778 | const MotionEvent secondFingerDownEvent = |
| 11779 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11780 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11781 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11782 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 11783 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11784 | .build(); |
| 11785 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11786 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11787 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11788 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11789 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11790 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11791 | |
| 11792 | // Third finger goes down outside all windows, so injection should fail. |
| 11793 | const MotionEvent thirdFingerDownEvent = |
| 11794 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11795 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11796 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11797 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 11798 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 11799 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11800 | .build(); |
| 11801 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11802 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11803 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 11804 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11805 | |
| 11806 | spy->assertNoEvents(); |
| 11807 | window->assertNoEvents(); |
| 11808 | } |
| 11809 | |
| 11810 | /** |
| 11811 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 11812 | */ |
| 11813 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 11814 | auto spy = createSpy(); |
| 11815 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 11816 | auto window = createForeground(); |
| 11817 | window->setFrame(Rect(0, 0, 200, 200)); |
| 11818 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11819 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11820 | |
| 11821 | // First finger down on the window only |
| 11822 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11823 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11824 | {150, 150})) |
| 11825 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11826 | window->consumeMotionDown(); |
| 11827 | |
| 11828 | // Second finger down on the spy and window |
| 11829 | const MotionEvent secondFingerDownEvent = |
| 11830 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11831 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11832 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11833 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 11834 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11835 | .build(); |
| 11836 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11837 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11838 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11839 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11840 | spy->consumeMotionDown(); |
| 11841 | window->consumeMotionPointerDown(1); |
| 11842 | |
| 11843 | // Third finger down on the spy and window |
| 11844 | const MotionEvent thirdFingerDownEvent = |
| 11845 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11846 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11847 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11848 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 11849 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 11850 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11851 | .build(); |
| 11852 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11853 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11854 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11855 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11856 | spy->consumeMotionPointerDown(1); |
| 11857 | window->consumeMotionPointerDown(2); |
| 11858 | |
| 11859 | // Spy window pilfers the pointers. |
| 11860 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 11861 | window->consumeMotionPointerUp(/*idx=*/2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 11862 | window->consumeMotionPointerUp(/*idx=*/1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11863 | |
| 11864 | spy->assertNoEvents(); |
| 11865 | window->assertNoEvents(); |
| 11866 | } |
| 11867 | |
| 11868 | /** |
| 11869 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 11870 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 11871 | * window, then that window should receive ACTION_CANCEL. |
| 11872 | */ |
| 11873 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 11874 | auto spy = createSpy(); |
| 11875 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 11876 | auto window = createForeground(); |
| 11877 | window->setFrame(Rect(0, 0, 200, 200)); |
| 11878 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11879 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11880 | |
| 11881 | // First finger down on both spy and window |
| 11882 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11883 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11884 | {10, 10})) |
| 11885 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11886 | window->consumeMotionDown(); |
| 11887 | spy->consumeMotionDown(); |
| 11888 | |
| 11889 | // Second finger down on the spy and window |
| 11890 | const MotionEvent secondFingerDownEvent = |
| 11891 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11892 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11893 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11894 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 11895 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11896 | .build(); |
| 11897 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11898 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11899 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11900 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11901 | spy->consumeMotionPointerDown(1); |
| 11902 | window->consumeMotionPointerDown(1); |
| 11903 | |
| 11904 | // Spy window pilfers the pointers. |
| 11905 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11906 | window->consumeMotionCancel(); |
| 11907 | |
| 11908 | spy->assertNoEvents(); |
| 11909 | window->assertNoEvents(); |
| 11910 | } |
| 11911 | |
| 11912 | /** |
| 11913 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 11914 | * be sent to other windows |
| 11915 | */ |
| 11916 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 11917 | auto spy = createSpy(); |
| 11918 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 11919 | auto window = createForeground(); |
| 11920 | window->setFrame(Rect(0, 0, 200, 200)); |
| 11921 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11922 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11923 | |
| 11924 | // First finger down on both window and spy |
| 11925 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11926 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11927 | {10, 10})) |
| 11928 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11929 | window->consumeMotionDown(); |
| 11930 | spy->consumeMotionDown(); |
| 11931 | |
| 11932 | // Spy window pilfers the pointers. |
| 11933 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11934 | window->consumeMotionCancel(); |
| 11935 | |
| 11936 | // Second finger down on the window only |
| 11937 | const MotionEvent secondFingerDownEvent = |
| 11938 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11939 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11940 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11941 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 11942 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11943 | .build(); |
| 11944 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11945 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11946 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11947 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11948 | window->consumeMotionDown(); |
| 11949 | window->assertNoEvents(); |
| 11950 | |
| 11951 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 11952 | spy->consumeMotionMove(); |
| 11953 | spy->assertNoEvents(); |
| 11954 | } |
| 11955 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11956 | /** |
| 11957 | * A window on the left and a window on the right. Also, a spy window that's above all of the |
| 11958 | * windows, and spanning both left and right windows. |
| 11959 | * Send simultaneous motion streams from two different devices, one to the left window, and another |
| 11960 | * to the right window. |
| 11961 | * Pilfer from spy window. |
| 11962 | * Check that the pilfering only affects the pointers that are actually being received by the spy. |
| 11963 | */ |
| 11964 | TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) { |
| 11965 | sp<FakeWindowHandle> spy = createSpy(); |
| 11966 | spy->setFrame(Rect(0, 0, 200, 200)); |
| 11967 | sp<FakeWindowHandle> leftWindow = createForeground(); |
| 11968 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 11969 | |
| 11970 | sp<FakeWindowHandle> rightWindow = createForeground(); |
| 11971 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 11972 | |
| 11973 | constexpr int32_t stylusDeviceId = 1; |
| 11974 | constexpr int32_t touchDeviceId = 2; |
| 11975 | |
| 11976 | mDispatcher->onWindowInfosChanged( |
| 11977 | {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 11978 | |
| 11979 | // Stylus down on left window and spy |
| 11980 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 11981 | .deviceId(stylusDeviceId) |
| 11982 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 11983 | .build()); |
| 11984 | leftWindow->consumeMotionEvent( |
| 11985 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 11986 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 11987 | |
| 11988 | // Finger down on right window and spy - but spy already has stylus |
| 11989 | mDispatcher->notifyMotion( |
| 11990 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11991 | .deviceId(touchDeviceId) |
| 11992 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 11993 | .build()); |
| 11994 | rightWindow->consumeMotionEvent( |
| 11995 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11996 | spy->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11997 | |
| 11998 | // Act: pilfer from spy. Spy is currently receiving touch events. |
| 11999 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 12000 | leftWindow->consumeMotionEvent( |
| 12001 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 12002 | rightWindow->consumeMotionEvent( |
| 12003 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 12004 | |
| 12005 | // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus |
| 12006 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 12007 | .deviceId(stylusDeviceId) |
| 12008 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52)) |
| 12009 | .build()); |
| 12010 | mDispatcher->notifyMotion( |
| 12011 | MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 12012 | .deviceId(touchDeviceId) |
| 12013 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52)) |
| 12014 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 12015 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 12016 | |
| 12017 | spy->assertNoEvents(); |
| 12018 | leftWindow->assertNoEvents(); |
| 12019 | rightWindow->assertNoEvents(); |
| 12020 | } |
| 12021 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 12022 | TEST_F(InputDispatcherPilferPointersTest, NoPilferingWithHoveringPointers) { |
| 12023 | auto window = createForeground(); |
| 12024 | auto spy = createSpy(); |
| 12025 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 12026 | |
| 12027 | mDispatcher->notifyMotion( |
| 12028 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 12029 | .deviceId(1) |
| 12030 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(100).y(200)) |
| 12031 | .build()); |
| 12032 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12033 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12034 | |
| 12035 | // Pilfer pointers from the spy window should fail. |
| 12036 | EXPECT_NE(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 12037 | spy->assertNoEvents(); |
| 12038 | window->assertNoEvents(); |
| 12039 | } |
| 12040 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 12041 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 12042 | public: |
| 12043 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 12044 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 12045 | std::make_shared<FakeApplicationHandle>(); |
| 12046 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 12047 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 12048 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 12049 | overlay->setFocusable(false); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12050 | overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12051 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12052 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 12053 | overlay->setTrustedOverlay(true); |
| 12054 | |
| 12055 | std::shared_ptr<FakeApplicationHandle> application = |
| 12056 | std::make_shared<FakeApplicationHandle>(); |
| 12057 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 12058 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 12059 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 12060 | window->setFocusable(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12061 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 12062 | |
| 12063 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12064 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 12065 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12066 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 12067 | return {std::move(overlay), std::move(window)}; |
| 12068 | } |
| 12069 | |
| 12070 | void sendFingerEvent(int32_t action) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12071 | mDispatcher->notifyMotion( |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 12072 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12073 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}})); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 12074 | } |
| 12075 | |
| 12076 | void sendStylusEvent(int32_t action) { |
| 12077 | NotifyMotionArgs motionArgs = |
| 12078 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 12079 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12080 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12081 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 12082 | } |
| 12083 | }; |
| 12084 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 12085 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 12086 | |
| 12087 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
Siarhei Vishniakou | ad3b682 | 2023-06-22 14:17:35 -0700 | [diff] [blame] | 12088 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 12089 | ScopedSilentDeath _silentDeath; |
| 12090 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 12091 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 12092 | overlay->setTrustedOverlay(false); |
| 12093 | // Configuring an untrusted overlay as a stylus interceptor should cause Dispatcher to abort. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12094 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 12095 | {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 12096 | ".* not a trusted overlay"); |
| 12097 | } |
| 12098 | |
| 12099 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 12100 | auto [overlay, window] = setupStylusOverlayScenario(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12101 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 12102 | |
| 12103 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 12104 | overlay->consumeMotionDown(); |
| 12105 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 12106 | overlay->consumeMotionUp(); |
| 12107 | |
| 12108 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 12109 | window->consumeMotionDown(); |
| 12110 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 12111 | window->consumeMotionUp(); |
| 12112 | |
| 12113 | overlay->assertNoEvents(); |
| 12114 | window->assertNoEvents(); |
| 12115 | } |
| 12116 | |
| 12117 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 12118 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12119 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12120 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 12121 | |
| 12122 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 12123 | overlay->consumeMotionDown(); |
| 12124 | window->consumeMotionDown(); |
| 12125 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 12126 | overlay->consumeMotionUp(); |
| 12127 | window->consumeMotionUp(); |
| 12128 | |
| 12129 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 12130 | window->consumeMotionDown(); |
| 12131 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 12132 | window->consumeMotionUp(); |
| 12133 | |
| 12134 | overlay->assertNoEvents(); |
| 12135 | window->assertNoEvents(); |
| 12136 | } |
| 12137 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 12138 | /** |
| 12139 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 12140 | * The scenario is as follows: |
| 12141 | * - The stylus interceptor overlay is configured as a spy window. |
| 12142 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 12143 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 12144 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 12145 | */ |
| 12146 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 12147 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 12148 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12149 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 12150 | |
| 12151 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 12152 | overlay->consumeMotionDown(); |
| 12153 | window->consumeMotionDown(); |
| 12154 | |
| 12155 | // The interceptor pilfers the pointers. |
| 12156 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 12157 | window->consumeMotionCancel(); |
| 12158 | |
| 12159 | // The interceptor configures itself so that it is no longer a spy. |
| 12160 | overlay->setSpy(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12161 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 12162 | |
| 12163 | // It continues to receive the rest of the stylus gesture. |
| 12164 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 12165 | overlay->consumeMotionMove(); |
| 12166 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 12167 | overlay->consumeMotionUp(); |
| 12168 | |
| 12169 | window->assertNoEvents(); |
| 12170 | } |
| 12171 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12172 | struct User { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12173 | gui::Pid mPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 12174 | gui::Uid mUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12175 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 12176 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 12177 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12178 | User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid) |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12179 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 12180 | |
| 12181 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12182 | return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12183 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 12184 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 12185 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 12186 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 12187 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 12188 | } |
| 12189 | |
| 12190 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12191 | return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12192 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12193 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12194 | mPolicyFlags); |
| 12195 | } |
| 12196 | |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12197 | sp<FakeWindowHandle> createWindow(const char* name) const { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12198 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 12199 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12200 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 12201 | name, ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12202 | window->setOwnerInfo(mPid, mUid); |
| 12203 | return window; |
| 12204 | } |
| 12205 | }; |
| 12206 | |
| 12207 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 12208 | |
| 12209 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12210 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12211 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12212 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12213 | |
| 12214 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12215 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 12216 | window->consumeMotionDown(); |
| 12217 | |
| 12218 | setFocusedWindow(window); |
| 12219 | window->consumeFocusEvent(true); |
| 12220 | |
| 12221 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12222 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 12223 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 12224 | } |
| 12225 | |
| 12226 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12227 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12228 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12229 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12230 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12231 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12232 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 12233 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 12234 | |
| 12235 | setFocusedWindow(window); |
| 12236 | window->consumeFocusEvent(true); |
| 12237 | |
| 12238 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 12239 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 12240 | window->assertNoEvents(); |
| 12241 | } |
| 12242 | |
| 12243 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12244 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12245 | auto window = owner.createWindow("Owned window"); |
| 12246 | auto spy = owner.createWindow("Owned spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12247 | spy->setSpy(true); |
| 12248 | spy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12249 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12250 | |
| 12251 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12252 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 12253 | spy->consumeMotionDown(); |
| 12254 | window->consumeMotionDown(); |
| 12255 | } |
| 12256 | |
| 12257 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12258 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12259 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12260 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12261 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12262 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12263 | randosSpy->setSpy(true); |
| 12264 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12265 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12266 | |
| 12267 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 12268 | // not receive the event. |
| 12269 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12270 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 12271 | randosSpy->assertNoEvents(); |
| 12272 | window->consumeMotionDown(); |
| 12273 | } |
| 12274 | |
| 12275 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12276 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12277 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12278 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12279 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12280 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12281 | randosSpy->setSpy(true); |
| 12282 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12283 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12284 | |
| 12285 | // A user that has injection permission can inject into any window. |
| 12286 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12287 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12288 | ADISPLAY_ID_DEFAULT)); |
| 12289 | randosSpy->consumeMotionDown(); |
| 12290 | window->consumeMotionDown(); |
| 12291 | |
| 12292 | setFocusedWindow(randosSpy); |
| 12293 | randosSpy->consumeFocusEvent(true); |
| 12294 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12295 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12296 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 12297 | window->assertNoEvents(); |
| 12298 | } |
| 12299 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 12300 | TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12301 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12302 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12303 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12304 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12305 | auto randosWindow = rando.createWindow("Rando's window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12306 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 12307 | randosWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12308 | mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12309 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 12310 | // Do not allow generation of ACTION_OUTSIDE events into windows owned by different uids. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12311 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12312 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 12313 | window->consumeMotionDown(); |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 12314 | randosWindow->assertNoEvents(); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12315 | } |
| 12316 | |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 12317 | using InputDispatcherPointerInWindowTest = InputDispatcherTest; |
| 12318 | |
| 12319 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWhenHovering) { |
| 12320 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 12321 | |
| 12322 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 12323 | ADISPLAY_ID_DEFAULT); |
| 12324 | left->setFrame(Rect(0, 0, 100, 100)); |
| 12325 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 12326 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 12327 | right->setFrame(Rect(100, 0, 200, 100)); |
| 12328 | sp<FakeWindowHandle> spy = |
| 12329 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT); |
| 12330 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 12331 | spy->setTrustedOverlay(true); |
| 12332 | spy->setSpy(true); |
| 12333 | |
| 12334 | mDispatcher->onWindowInfosChanged( |
| 12335 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 12336 | |
| 12337 | // Hover into the left window. |
| 12338 | mDispatcher->notifyMotion( |
| 12339 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 12340 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(50).y(50)) |
| 12341 | .build()); |
| 12342 | |
| 12343 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12344 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12345 | |
| 12346 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12347 | /*pointerId=*/0)); |
| 12348 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12349 | /*pointerId=*/0)); |
| 12350 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12351 | /*pointerId=*/0)); |
| 12352 | |
| 12353 | // Hover move to the right window. |
| 12354 | mDispatcher->notifyMotion( |
| 12355 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 12356 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 12357 | .build()); |
| 12358 | |
| 12359 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12360 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12361 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 12362 | |
| 12363 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12364 | /*pointerId=*/0)); |
| 12365 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12366 | /*pointerId=*/0)); |
| 12367 | ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12368 | /*pointerId=*/0)); |
| 12369 | |
| 12370 | // Stop hovering. |
| 12371 | mDispatcher->notifyMotion( |
| 12372 | MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 12373 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 12374 | .build()); |
| 12375 | |
| 12376 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12377 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12378 | |
| 12379 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12380 | /*pointerId=*/0)); |
| 12381 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12382 | /*pointerId=*/0)); |
| 12383 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12384 | /*pointerId=*/0)); |
| 12385 | } |
| 12386 | |
| 12387 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWithSplitTouch) { |
| 12388 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 12389 | |
| 12390 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 12391 | ADISPLAY_ID_DEFAULT); |
| 12392 | left->setFrame(Rect(0, 0, 100, 100)); |
| 12393 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 12394 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 12395 | right->setFrame(Rect(100, 0, 200, 100)); |
| 12396 | sp<FakeWindowHandle> spy = |
| 12397 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT); |
| 12398 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 12399 | spy->setTrustedOverlay(true); |
| 12400 | spy->setSpy(true); |
| 12401 | |
| 12402 | mDispatcher->onWindowInfosChanged( |
| 12403 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 12404 | |
| 12405 | // First pointer down on left window. |
| 12406 | mDispatcher->notifyMotion( |
| 12407 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 12408 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12409 | .build()); |
| 12410 | |
| 12411 | left->consumeMotionDown(); |
| 12412 | spy->consumeMotionDown(); |
| 12413 | |
| 12414 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12415 | /*pointerId=*/0)); |
| 12416 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12417 | /*pointerId=*/0)); |
| 12418 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12419 | /*pointerId=*/0)); |
| 12420 | |
| 12421 | // Second pointer down on right window. |
| 12422 | mDispatcher->notifyMotion( |
| 12423 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 12424 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12425 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 12426 | .build()); |
| 12427 | |
| 12428 | left->consumeMotionMove(); |
| 12429 | right->consumeMotionDown(); |
| 12430 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 12431 | |
| 12432 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12433 | /*pointerId=*/0)); |
| 12434 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12435 | /*pointerId=*/0)); |
| 12436 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12437 | /*pointerId=*/0)); |
| 12438 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12439 | /*pointerId=*/1)); |
| 12440 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12441 | /*pointerId=*/1)); |
| 12442 | ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12443 | /*pointerId=*/1)); |
| 12444 | |
| 12445 | // Second pointer up. |
| 12446 | mDispatcher->notifyMotion( |
| 12447 | MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 12448 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12449 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 12450 | .build()); |
| 12451 | |
| 12452 | left->consumeMotionMove(); |
| 12453 | right->consumeMotionUp(); |
| 12454 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 12455 | |
| 12456 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12457 | /*pointerId=*/0)); |
| 12458 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12459 | /*pointerId=*/0)); |
| 12460 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12461 | /*pointerId=*/0)); |
| 12462 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12463 | /*pointerId=*/1)); |
| 12464 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12465 | /*pointerId=*/1)); |
| 12466 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12467 | /*pointerId=*/1)); |
| 12468 | |
| 12469 | // First pointer up. |
| 12470 | mDispatcher->notifyMotion( |
| 12471 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 12472 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12473 | .build()); |
| 12474 | |
| 12475 | left->consumeMotionUp(); |
| 12476 | spy->consumeMotionUp(); |
| 12477 | |
| 12478 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12479 | /*pointerId=*/0)); |
| 12480 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12481 | /*pointerId=*/0)); |
| 12482 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12483 | /*pointerId=*/0)); |
| 12484 | } |
| 12485 | |
| 12486 | TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse) { |
| 12487 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 12488 | |
| 12489 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 12490 | ADISPLAY_ID_DEFAULT); |
| 12491 | left->setFrame(Rect(0, 0, 100, 100)); |
| 12492 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 12493 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 12494 | right->setFrame(Rect(100, 0, 200, 100)); |
| 12495 | |
| 12496 | mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 12497 | |
| 12498 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12499 | /*pointerId=*/0)); |
| 12500 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12501 | /*pointerId=*/0)); |
| 12502 | |
| 12503 | // Hover move into the window. |
| 12504 | mDispatcher->notifyMotion( |
| 12505 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 12506 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50)) |
| 12507 | .rawXCursorPosition(50) |
| 12508 | .rawYCursorPosition(50) |
| 12509 | .deviceId(DEVICE_ID) |
| 12510 | .build()); |
| 12511 | |
| 12512 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12513 | |
| 12514 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12515 | /*pointerId=*/0)); |
| 12516 | |
| 12517 | // Move the mouse with another device. This cancels the hovering pointer from the first device. |
| 12518 | mDispatcher->notifyMotion( |
| 12519 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 12520 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50)) |
| 12521 | .rawXCursorPosition(51) |
| 12522 | .rawYCursorPosition(50) |
| 12523 | .deviceId(SECOND_DEVICE_ID) |
| 12524 | .build()); |
| 12525 | |
| 12526 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12527 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12528 | |
| 12529 | // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets |
| 12530 | // a HOVER_EXIT from the first device. |
| 12531 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12532 | /*pointerId=*/0)); |
| 12533 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, |
| 12534 | SECOND_DEVICE_ID, |
| 12535 | /*pointerId=*/0)); |
| 12536 | |
| 12537 | // Move the mouse outside the window. Document the current behavior, where the window does not |
| 12538 | // receive HOVER_EXIT even though the mouse left the window. |
| 12539 | mDispatcher->notifyMotion( |
| 12540 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 12541 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50)) |
| 12542 | .rawXCursorPosition(150) |
| 12543 | .rawYCursorPosition(50) |
| 12544 | .deviceId(SECOND_DEVICE_ID) |
| 12545 | .build()); |
| 12546 | |
| 12547 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12548 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12549 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12550 | /*pointerId=*/0)); |
| 12551 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, |
| 12552 | SECOND_DEVICE_ID, |
| 12553 | /*pointerId=*/0)); |
| 12554 | } |
| 12555 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 12556 | } // namespace android::inputdispatcher |