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" |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 18 | #include "FakeApplicationHandle.h" |
Prabir Pradhan | 81e89fe | 2024-03-20 21:17:09 +0000 | [diff] [blame] | 19 | #include "FakeInputDispatcherPolicy.h" |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 20 | #include "FakeInputTracingBackend.h" |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 21 | #include "FakeWindows.h" |
Prabir Pradhan | e3b28dd | 2023-10-06 04:19:29 +0000 | [diff] [blame] | 22 | #include "TestEventMatchers.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 23 | |
Cody Heiner | 166a5af | 2023-07-07 12:25:00 -0700 | [diff] [blame] | 24 | #include <NotifyArgsBuilders.h> |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 25 | #include <android-base/logging.h> |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 26 | #include <android-base/properties.h> |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 27 | #include <android-base/silent_death_test.h> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 28 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 29 | #include <android-base/thread_annotations.h> |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 30 | #include <binder/Binder.h> |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 31 | #include <com_android_input_flags.h> |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 32 | #include <fcntl.h> |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 33 | #include <flag_macros.h> |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 34 | #include <gmock/gmock.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 35 | #include <gtest/gtest.h> |
Siarhei Vishniakou | 3782af6 | 2024-03-07 21:56:39 -0800 | [diff] [blame] | 36 | #include <input/BlockingQueue.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 37 | #include <input/Input.h> |
Siarhei Vishniakou | 0438ca8 | 2024-03-12 14:27:25 -0700 | [diff] [blame] | 38 | #include <input/InputConsumer.h> |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 39 | #include <input/PrintTools.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 40 | #include <linux/input.h> |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 41 | #include <sys/epoll.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 42 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 43 | #include <cinttypes> |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 44 | #include <compare> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 45 | #include <thread> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 46 | #include <unordered_set> |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 47 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 48 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 49 | using android::base::StringPrintf; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 50 | using android::gui::FocusRequest; |
| 51 | using android::gui::TouchOcclusionMode; |
| 52 | using android::gui::WindowInfo; |
| 53 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 54 | using android::os::InputEventInjectionResult; |
| 55 | using android::os::InputEventInjectionSync; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 56 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 57 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 58 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 59 | using namespace ftl::flag_operators; |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 60 | using testing::AllOf; |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 61 | using testing::Not; |
Siarhei Vishniakou | 85eb580 | 2024-04-01 11:40:59 -0700 | [diff] [blame] | 62 | using testing::Pointee; |
| 63 | using testing::UnorderedElementsAre; |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 64 | |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 65 | namespace { |
| 66 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 67 | // An arbitrary time value. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 68 | static constexpr nsecs_t ARBITRARY_TIME = 1234; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 69 | |
| 70 | // An arbitrary device id. |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 71 | static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 72 | static constexpr int32_t SECOND_DEVICE_ID = 2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 73 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 74 | // An arbitrary display id. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 75 | constexpr ui::LogicalDisplayId DISPLAY_ID = ui::LogicalDisplayId::DEFAULT; |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 76 | constexpr ui::LogicalDisplayId SECOND_DISPLAY_ID = ui::LogicalDisplayId{1}; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 77 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 78 | // Ensure common actions are interchangeable between keys and motions for convenience. |
| 79 | static_assert(AMOTION_EVENT_ACTION_DOWN == AKEY_EVENT_ACTION_DOWN); |
| 80 | static_assert(AMOTION_EVENT_ACTION_UP == AKEY_EVENT_ACTION_UP); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 81 | static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN; |
| 82 | static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE; |
| 83 | static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP; |
| 84 | static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 85 | static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 86 | static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 87 | static constexpr int32_t ACTION_SCROLL = AMOTION_EVENT_ACTION_SCROLL; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 88 | static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 89 | static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 90 | /** |
| 91 | * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the |
| 92 | * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at |
| 93 | * index 0) is the new pointer going down. The same pointer could have been placed at a different |
| 94 | * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In |
| 95 | * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if |
| 96 | * pointer id=0 leaves but the pointer id=1 remains. |
| 97 | */ |
| 98 | static constexpr int32_t POINTER_0_DOWN = |
| 99 | AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 100 | static constexpr int32_t POINTER_1_DOWN = |
| 101 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 102 | static constexpr int32_t POINTER_2_DOWN = |
| 103 | AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 104 | static constexpr int32_t POINTER_3_DOWN = |
| 105 | AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 106 | static constexpr int32_t POINTER_0_UP = |
| 107 | AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 108 | static constexpr int32_t POINTER_1_UP = |
| 109 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 110 | static constexpr int32_t POINTER_2_UP = |
| 111 | AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 112 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 113 | // 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] | 114 | static constexpr gui::Pid SECONDARY_WINDOW_PID{1010}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 115 | static constexpr gui::Uid SECONDARY_WINDOW_UID{1012}; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 116 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 117 | // An arbitrary pid of the gesture monitor window |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 118 | static constexpr gui::Pid MONITOR_PID{2001}; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 119 | |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 120 | static constexpr int EXPECTED_WALLPAPER_FLAGS = |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 121 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 122 | |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 123 | using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID; |
| 124 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 125 | /** |
| 126 | * Return a DOWN key event with KEYCODE_A. |
| 127 | */ |
| 128 | static KeyEvent getTestKeyEvent() { |
| 129 | KeyEvent event; |
| 130 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 131 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 132 | ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, |
| 133 | AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 134 | return event; |
| 135 | } |
| 136 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 137 | /** |
| 138 | * Provide a local override for a flag value. The value is restored when the object of this class |
| 139 | * goes out of scope. |
| 140 | * This class is not intended to be used directly, because its usage is cumbersome. |
| 141 | * Instead, a wrapper macro SCOPED_FLAG_OVERRIDE is provided. |
| 142 | */ |
| 143 | class ScopedFlagOverride { |
| 144 | public: |
| 145 | ScopedFlagOverride(std::function<bool()> read, std::function<void(bool)> write, bool value) |
| 146 | : mInitialValue(read()), mWriteValue(write) { |
| 147 | mWriteValue(value); |
| 148 | } |
| 149 | ~ScopedFlagOverride() { mWriteValue(mInitialValue); } |
| 150 | |
| 151 | private: |
| 152 | const bool mInitialValue; |
| 153 | std::function<void(bool)> mWriteValue; |
| 154 | }; |
| 155 | |
| 156 | typedef bool (*readFlagValueFunction)(); |
| 157 | typedef void (*writeFlagValueFunction)(bool); |
| 158 | |
| 159 | /** |
| 160 | * Use this macro to locally override a flag value. |
| 161 | * Example usage: |
| 162 | * SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
| 163 | * Note: this works by creating a local variable in your current scope. Don't call this twice for |
| 164 | * the same flag, because the variable names will clash! |
| 165 | */ |
| 166 | #define SCOPED_FLAG_OVERRIDE(NAME, VALUE) \ |
| 167 | readFlagValueFunction read##NAME = com::android::input::flags::NAME; \ |
| 168 | writeFlagValueFunction write##NAME = com::android::input::flags::NAME; \ |
| 169 | ScopedFlagOverride override##NAME(read##NAME, write##NAME, (VALUE)) |
| 170 | |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 171 | } // namespace |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 172 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 173 | // --- InputDispatcherTest --- |
| 174 | |
| 175 | class InputDispatcherTest : public testing::Test { |
| 176 | protected: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 177 | std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 178 | std::unique_ptr<InputDispatcher> mDispatcher; |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 179 | std::shared_ptr<VerifyingTrace> mVerifyingTrace; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 180 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 181 | void SetUp() override { |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 182 | mVerifyingTrace = std::make_shared<VerifyingTrace>(); |
| 183 | FakeWindowHandle::sOnEventReceivedCallback = [this](const auto& _1, const auto& _2) { |
| 184 | handleEventReceivedByWindow(_1, _2); |
| 185 | }; |
| 186 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 187 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 188 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, |
| 189 | std::make_unique<FakeInputTracingBackend>( |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 190 | mVerifyingTrace)); |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 191 | |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 192 | mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 193 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 194 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 197 | void TearDown() override { |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 198 | ASSERT_NO_FATAL_FAILURE(mVerifyingTrace->verifyExpectedEventsTraced()); |
| 199 | FakeWindowHandle::sOnEventReceivedCallback = nullptr; |
| 200 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 201 | ASSERT_EQ(OK, mDispatcher->stop()); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 202 | mFakePolicy.reset(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 203 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 204 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 205 | |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 206 | void handleEventReceivedByWindow(const std::unique_ptr<InputEvent>& event, |
| 207 | const gui::WindowInfo& info) { |
| 208 | if (!event) { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | switch (event->getType()) { |
| 213 | case InputEventType::KEY: { |
| 214 | mVerifyingTrace->expectKeyDispatchTraced(static_cast<KeyEvent&>(*event), info.id); |
| 215 | break; |
| 216 | } |
| 217 | case InputEventType::MOTION: { |
| 218 | mVerifyingTrace->expectMotionDispatchTraced(static_cast<MotionEvent&>(*event), |
| 219 | info.id); |
| 220 | break; |
| 221 | } |
| 222 | default: |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 227 | /** |
| 228 | * Used for debugging when writing the test |
| 229 | */ |
| 230 | void dumpDispatcherState() { |
| 231 | std::string dump; |
| 232 | mDispatcher->dump(dump); |
| 233 | std::stringstream ss(dump); |
| 234 | std::string to; |
| 235 | |
| 236 | while (std::getline(ss, to, '\n')) { |
| 237 | ALOGE("%s", to.c_str()); |
| 238 | } |
| 239 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 240 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 241 | void setFocusedWindow(const sp<WindowInfoHandle>& window) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 242 | FocusRequest request; |
| 243 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 244 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 245 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 246 | request.displayId = window->getInfo()->displayId.val(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 247 | mDispatcher->setFocusedWindow(request); |
| 248 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 249 | }; |
| 250 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 251 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 252 | KeyEvent event; |
| 253 | |
| 254 | // Rejects undefined key actions. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 255 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 256 | ui::LogicalDisplayId::INVALID, INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 257 | /*action=*/-1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 258 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 259 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 260 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 261 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 262 | << "Should reject key events with undefined action."; |
| 263 | |
| 264 | // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 265 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 266 | ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, |
| 267 | AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 268 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 269 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 270 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 271 | << "Should reject key events with ACTION_MULTIPLE."; |
| 272 | } |
| 273 | |
| 274 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 275 | MotionEvent event; |
| 276 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 277 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 278 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 279 | pointerProperties[i].clear(); |
| 280 | pointerProperties[i].id = i; |
| 281 | pointerCoords[i].clear(); |
| 282 | } |
| 283 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 284 | // Some constants commonly used below |
| 285 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 286 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 287 | constexpr int32_t metaState = AMETA_NONE; |
| 288 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 289 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 290 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 291 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 292 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 293 | /*action=*/-1, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 294 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 295 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 296 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 297 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 298 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 299 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 300 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 301 | << "Should reject motion events with undefined action."; |
| 302 | |
| 303 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 304 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 305 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 306 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 307 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 308 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 309 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 310 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 311 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 312 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 313 | << "Should reject motion events with pointer down index too large."; |
| 314 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 315 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 316 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 317 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 318 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 319 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 320 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 321 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 322 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 323 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 324 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 325 | << "Should reject motion events with pointer down index too small."; |
| 326 | |
| 327 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 328 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 329 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 330 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 331 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 332 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 333 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 334 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 335 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 336 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 337 | << "Should reject motion events with pointer up index too large."; |
| 338 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 339 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 340 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 341 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 342 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 343 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 344 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 345 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 346 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 347 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 348 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 349 | << "Should reject motion events with pointer up index too small."; |
| 350 | |
| 351 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 352 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 353 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 354 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 355 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 356 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 357 | /*pointerCount=*/0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 358 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 359 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 360 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 361 | << "Should reject motion events with 0 pointers."; |
| 362 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 363 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 364 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 365 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 366 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 367 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 368 | /*pointerCount=*/MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 369 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 370 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 371 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 372 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 373 | |
| 374 | // Rejects motion events with invalid pointer ids. |
| 375 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 376 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 377 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 378 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 379 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 380 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 381 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 382 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 383 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 384 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 385 | << "Should reject motion events with pointer ids less than 0."; |
| 386 | |
| 387 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 388 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 389 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 390 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 391 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 392 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 393 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 394 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 395 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 396 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 397 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 398 | |
| 399 | // Rejects motion events with duplicate pointer ids. |
| 400 | pointerProperties[0].id = 1; |
| 401 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 402 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 403 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 404 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 405 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 406 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 407 | /*pointerCount=*/2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 408 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 409 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 410 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 411 | << "Should reject motion events with duplicate pointer ids."; |
| 412 | } |
| 413 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 414 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 415 | |
| 416 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 417 | constexpr nsecs_t eventTime = 20; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 418 | mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 419 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 420 | |
| 421 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 422 | } |
| 423 | |
| 424 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 425 | NotifySwitchArgs args(InputEvent::nextId(), /*eventTime=*/20, /*policyFlags=*/0, |
| 426 | /*switchValues=*/1, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 427 | /*switchMask=*/2); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 428 | mDispatcher->notifySwitch(args); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 429 | |
| 430 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 431 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 432 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 433 | } |
| 434 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 435 | namespace { |
| 436 | |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 437 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 438 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 439 | class FakeMonitorReceiver { |
| 440 | public: |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 441 | FakeMonitorReceiver(InputDispatcher& dispatcher, const std::string name, |
| 442 | ui::LogicalDisplayId displayId) |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 443 | : mInputReceiver(*dispatcher.createInputMonitor(displayId, name, MONITOR_PID), name) {} |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 444 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 445 | sp<IBinder> getToken() { return mInputReceiver.getToken(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 446 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 447 | void consumeKeyDown(ui::LogicalDisplayId expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 448 | mInputReceiver.consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 449 | expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | std::optional<int32_t> receiveEvent() { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 453 | const auto [sequenceNum, _] = mInputReceiver.receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED); |
| 454 | return sequenceNum; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 457 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver.finishEvent(consumeSeq); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 458 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 459 | void consumeMotionDown(ui::LogicalDisplayId expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 460 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 461 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 464 | void consumeMotionMove(ui::LogicalDisplayId expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 465 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 466 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 469 | void consumeMotionUp(ui::LogicalDisplayId expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 470 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, |
| 471 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 474 | void consumeMotionCancel(ui::LogicalDisplayId expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 475 | mInputReceiver.consumeMotionEvent( |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 476 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 477 | WithDisplayId(expectedDisplayId), |
| 478 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
| 479 | } |
| 480 | |
| 481 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 482 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 483 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 484 | mInputReceiver.consumeEvent(InputEventType::MOTION, action, ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 485 | /*expectedFlags=*/0); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 489 | mInputReceiver.consumeMotionEvent(matcher); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 492 | std::unique_ptr<MotionEvent> consumeMotion() { return mInputReceiver.consumeMotion(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 493 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 494 | void assertNoEvents() { mInputReceiver.assertNoEvents(CONSUME_TIMEOUT_NO_EVENT_EXPECTED); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 495 | |
| 496 | private: |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 497 | FakeInputReceiver mInputReceiver; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 498 | }; |
| 499 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 500 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 501 | InputDispatcher& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 502 | ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 503 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 504 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 505 | bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 506 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 507 | KeyEvent event; |
| 508 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 509 | |
| 510 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 511 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 512 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 513 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 514 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 515 | if (!allowKeyRepeat) { |
| 516 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 517 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 518 | // Inject event until dispatch out. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 519 | return dispatcher.injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 520 | } |
| 521 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 522 | static void assertInjectedKeyTimesOut(InputDispatcher& dispatcher) { |
| 523 | InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 524 | injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 525 | ui::LogicalDisplayId::INVALID, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 526 | CONSUME_TIMEOUT_NO_EVENT_EXPECTED); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 527 | if (result != InputEventInjectionResult::TIMED_OUT) { |
| 528 | FAIL() << "Injection should have timed out, but got " << ftl::enum_string(result); |
| 529 | } |
| 530 | } |
| 531 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 532 | static InputEventInjectionResult injectKeyDown( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 533 | InputDispatcher& dispatcher, |
| 534 | ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 535 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 538 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 539 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 540 | // has to be woken up to process the repeating key. |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 541 | static InputEventInjectionResult injectKeyDownNoRepeat( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 542 | InputDispatcher& dispatcher, |
| 543 | ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 544 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 545 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 546 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 547 | } |
| 548 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 549 | static InputEventInjectionResult injectKeyUp( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 550 | InputDispatcher& dispatcher, |
| 551 | ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 552 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 555 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 556 | InputDispatcher& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 557 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 558 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 559 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 560 | return dispatcher.injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 561 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 564 | static InputEventInjectionResult injectMotionEvent( |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 565 | InputDispatcher& dispatcher, int32_t action, int32_t source, ui::LogicalDisplayId displayId, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 566 | const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 567 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 568 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 569 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 570 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 571 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 572 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 573 | MotionEventBuilder motionBuilder = |
| 574 | MotionEventBuilder(action, source) |
| 575 | .displayId(displayId) |
| 576 | .eventTime(eventTime) |
| 577 | .rawXCursorPosition(cursorPosition.x) |
| 578 | .rawYCursorPosition(cursorPosition.y) |
| 579 | .pointer( |
| 580 | PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y)); |
| 581 | if (MotionEvent::getActionMasked(action) == ACTION_DOWN) { |
| 582 | motionBuilder.downTime(eventTime); |
| 583 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 584 | |
| 585 | // Inject event until dispatch out. |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 586 | return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode, |
| 587 | targetUid, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 588 | } |
| 589 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 590 | static InputEventInjectionResult injectMotionDown(InputDispatcher& dispatcher, int32_t source, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 591 | ui::LogicalDisplayId displayId, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 592 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 593 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 596 | static InputEventInjectionResult injectMotionUp(InputDispatcher& dispatcher, int32_t source, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 597 | ui::LogicalDisplayId displayId, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 598 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 599 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 602 | static NotifyKeyArgs generateKeyArgs( |
| 603 | int32_t action, ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 604 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 605 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 606 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 607 | AINPUT_SOURCE_KEYBOARD, displayId, POLICY_FLAG_PASS_TO_USER, action, |
| 608 | /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 609 | |
| 610 | return args; |
| 611 | } |
| 612 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 613 | [[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 614 | ui::LogicalDisplayId displayId, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 615 | const std::vector<PointF>& points) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 616 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 617 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 618 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 619 | } |
| 620 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 621 | PointerProperties pointerProperties[pointerCount]; |
| 622 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 623 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 624 | for (size_t i = 0; i < pointerCount; i++) { |
| 625 | pointerProperties[i].clear(); |
| 626 | pointerProperties[i].id = i; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 627 | pointerProperties[i].toolType = ToolType::FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 628 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 629 | pointerCoords[i].clear(); |
| 630 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 631 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 632 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 633 | |
| 634 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 635 | // Define a valid motion event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 636 | NotifyMotionArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, source, |
| 637 | displayId, POLICY_FLAG_PASS_TO_USER, action, /*actionButton=*/0, |
| 638 | /*flags=*/0, AMETA_NONE, /*buttonState=*/0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 639 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 640 | pointerCoords, /*xPrecision=*/0, /*yPrecision=*/0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 641 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 642 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /*videoFrames=*/{}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 643 | |
| 644 | return args; |
| 645 | } |
| 646 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 647 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 648 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 649 | } |
| 650 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 651 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
| 652 | ui::LogicalDisplayId displayId) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 653 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 654 | } |
| 655 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 656 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 657 | const PointerCaptureRequest& request) { |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 658 | return NotifyPointerCaptureChangedArgs(InputEvent::nextId(), systemTime(SYSTEM_TIME_MONOTONIC), |
| 659 | request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 660 | } |
| 661 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 662 | } // namespace |
| 663 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 664 | /** |
| 665 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 666 | * broken channel. |
| 667 | */ |
| 668 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 669 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 670 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 671 | "Window that breaks its input channel", |
| 672 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 673 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 674 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 675 | |
| 676 | // Window closes its channel, but the window remains. |
| 677 | window->destroyReceiver(); |
| 678 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 679 | } |
| 680 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 681 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 682 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 683 | sp<FakeWindowHandle> window = |
| 684 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 685 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 686 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 687 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 688 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 689 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 690 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 691 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 692 | |
| 693 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 694 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 695 | } |
| 696 | |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 697 | using InputDispatcherDeathTest = InputDispatcherTest; |
| 698 | |
| 699 | /** |
| 700 | * When 'onWindowInfosChanged' arguments contain a duplicate entry for the same window, dispatcher |
| 701 | * should crash. |
| 702 | */ |
| 703 | TEST_F(InputDispatcherDeathTest, DuplicateWindowInfosAbortDispatcher) { |
| 704 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
| 705 | ScopedSilentDeath _silentDeath; |
| 706 | |
| 707 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 708 | sp<FakeWindowHandle> window = |
| 709 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 710 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 711 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 712 | {{*window->getInfo(), *window->getInfo()}, {}, 0, 0}), |
| 713 | "Incorrect WindowInfosUpdate provided"); |
| 714 | } |
| 715 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 716 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 717 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 718 | sp<FakeWindowHandle> window = |
| 719 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 720 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 721 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 722 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 723 | // Inject a MotionEvent to an unknown display. |
| 724 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 725 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 726 | ui::LogicalDisplayId::INVALID)) |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 727 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 728 | |
| 729 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 730 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 733 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 734 | * Calling onWindowInfosChanged once should not cause any issues. |
| 735 | * 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] | 736 | * called twice. |
| 737 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 738 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 739 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 740 | sp<FakeWindowHandle> window = |
| 741 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 742 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 743 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 744 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 745 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 746 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 747 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 748 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 749 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 750 | |
| 751 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 752 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 756 | * Calling onWindowInfosChanged twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 757 | */ |
| 758 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 759 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 760 | sp<FakeWindowHandle> window = |
| 761 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 762 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 763 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 764 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 765 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 766 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 767 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 768 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 769 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 770 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 771 | |
| 772 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 773 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 776 | // The foreground window should receive the first touch down event. |
| 777 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 778 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 779 | sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 780 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 781 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 782 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 783 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 784 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 785 | mDispatcher->onWindowInfosChanged( |
| 786 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 787 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 788 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 789 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 790 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 791 | |
| 792 | // Top window should receive the touch down event. Second window should not receive anything. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 793 | windowTop->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 794 | windowSecond->assertNoEvents(); |
| 795 | } |
| 796 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 797 | /** |
| 798 | * Two windows: A top window, and a wallpaper behind the window. |
| 799 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 800 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 801 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 802 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 803 | */ |
| 804 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 805 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 806 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 807 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 808 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 809 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 810 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 811 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 812 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 813 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 814 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 815 | mDispatcher->onWindowInfosChanged( |
| 816 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 817 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 818 | injectMotionEvent(*mDispatcher, |
| 819 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 820 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(200)) |
| 821 | .build())) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 822 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 823 | |
| 824 | // Both foreground window and its wallpaper should receive the touch down |
| 825 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 826 | wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 827 | |
| 828 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 829 | injectMotionEvent(*mDispatcher, |
| 830 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 831 | .pointer(PointerBuilder(0, ToolType::FINGER).x(110).y(200)) |
| 832 | .build())) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 833 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 834 | |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 835 | foregroundWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 836 | wallpaperWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 837 | |
| 838 | // Now the foreground window goes away, but the wallpaper stays |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 839 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 840 | foregroundWindow->consumeMotionCancel(); |
| 841 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 842 | wallpaperWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | /** |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 846 | * Two fingers down on the window, and lift off the first finger. |
| 847 | * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event |
| 848 | * contains a single pointer. |
| 849 | */ |
| 850 | TEST_F(InputDispatcherTest, CancelAfterPointer0Up) { |
| 851 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 852 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 853 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 854 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 855 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 856 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 857 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 858 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 859 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 860 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 861 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 862 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 863 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 864 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 865 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 866 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 867 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 868 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 869 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 870 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 871 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 872 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 873 | |
| 874 | // Remove the window. The gesture should be canceled |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 875 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 876 | const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}}; |
| 877 | window->consumeMotionEvent( |
| 878 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers))); |
| 879 | } |
| 880 | |
| 881 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 882 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 883 | * with the following differences: |
| 884 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 885 | * clean up the connection. |
| 886 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 887 | * Ensure that there's no crash in the dispatcher. |
| 888 | */ |
| 889 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 890 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 891 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 892 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 893 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 894 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 895 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 896 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 897 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 898 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 899 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 900 | mDispatcher->onWindowInfosChanged( |
| 901 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 902 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 903 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 904 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 905 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 906 | |
| 907 | // Both foreground window and its wallpaper should receive the touch down |
| 908 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 909 | wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 910 | |
| 911 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 912 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 913 | ui::LogicalDisplayId::DEFAULT, {110, 200})) |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 914 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 915 | |
| 916 | foregroundWindow->consumeMotionMove(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 917 | wallpaperWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 918 | |
| 919 | // Wallpaper closes its channel, but the window remains. |
| 920 | wallpaperWindow->destroyReceiver(); |
| 921 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 922 | |
| 923 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 924 | // is no longer valid. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 925 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 926 | foregroundWindow->consumeMotionCancel(); |
| 927 | } |
| 928 | |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 929 | /** |
| 930 | * Two windows: left and right, and a separate wallpaper window underneath each. Device A sends a |
| 931 | * down event to the left window. Device B sends a down event to the right window. Next, the right |
| 932 | * window disappears. Both the right window and its wallpaper window should receive cancel event. |
| 933 | * The left window and its wallpaper window should not receive any events. |
| 934 | */ |
| 935 | TEST_F(InputDispatcherTest, MultiDeviceDisappearingWindowWithWallpaperWindows) { |
| 936 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 937 | sp<FakeWindowHandle> leftForegroundWindow = |
| 938 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 939 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 940 | leftForegroundWindow->setFrame(Rect(0, 0, 100, 100)); |
| 941 | leftForegroundWindow->setDupTouchToWallpaper(true); |
| 942 | sp<FakeWindowHandle> leftWallpaperWindow = |
| 943 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 944 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 945 | leftWallpaperWindow->setFrame(Rect(0, 0, 100, 100)); |
| 946 | leftWallpaperWindow->setIsWallpaper(true); |
| 947 | |
| 948 | sp<FakeWindowHandle> rightForegroundWindow = |
| 949 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 950 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 951 | rightForegroundWindow->setFrame(Rect(100, 0, 200, 100)); |
| 952 | rightForegroundWindow->setDupTouchToWallpaper(true); |
| 953 | sp<FakeWindowHandle> rightWallpaperWindow = |
| 954 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 955 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 956 | rightWallpaperWindow->setFrame(Rect(100, 0, 200, 100)); |
| 957 | rightWallpaperWindow->setIsWallpaper(true); |
| 958 | |
| 959 | mDispatcher->onWindowInfosChanged( |
| 960 | {{*leftForegroundWindow->getInfo(), *leftWallpaperWindow->getInfo(), |
| 961 | *rightForegroundWindow->getInfo(), *rightWallpaperWindow->getInfo()}, |
| 962 | {}, |
| 963 | 0, |
| 964 | 0}); |
| 965 | |
| 966 | const DeviceId deviceA = 9; |
| 967 | const DeviceId deviceB = 3; |
| 968 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 969 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 970 | .deviceId(deviceA) |
| 971 | .build()); |
| 972 | leftForegroundWindow->consumeMotionEvent( |
| 973 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 974 | leftWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 975 | WithDeviceId(deviceA), |
| 976 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 977 | |
| 978 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 979 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 980 | .deviceId(deviceB) |
| 981 | .build()); |
| 982 | rightForegroundWindow->consumeMotionEvent( |
| 983 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 984 | rightWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 985 | WithDeviceId(deviceB), |
| 986 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 987 | |
| 988 | // Now right foreground window disappears, but right wallpaper window remains. |
| 989 | mDispatcher->onWindowInfosChanged( |
| 990 | {{*leftForegroundWindow->getInfo(), *leftWallpaperWindow->getInfo(), |
| 991 | *rightWallpaperWindow->getInfo()}, |
| 992 | {}, |
| 993 | 0, |
| 994 | 0}); |
| 995 | |
| 996 | // Left foreground window and left wallpaper window still exist, and should not receive any |
| 997 | // events. |
| 998 | leftForegroundWindow->assertNoEvents(); |
| 999 | leftWallpaperWindow->assertNoEvents(); |
| 1000 | // Since right foreground window disappeared, right wallpaper window and right foreground window |
| 1001 | // should receive cancel events. |
| 1002 | rightForegroundWindow->consumeMotionEvent( |
| 1003 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB))); |
| 1004 | rightWallpaperWindow->consumeMotionEvent( |
| 1005 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB), |
| 1006 | WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_CANCELED))); |
| 1007 | } |
| 1008 | |
| 1009 | /** |
| 1010 | * Three windows arranged horizontally and without any overlap. Every window has a |
| 1011 | * wallpaper window underneath. The middle window also has SLIPPERY flag. |
| 1012 | * Device A sends a down event to the left window. Device B sends a down event to the middle window. |
| 1013 | * Next, device B sends move event to the right window. Touch for device B should slip from the |
| 1014 | * middle window to the right window. Also, the right wallpaper window should receive a down event. |
| 1015 | * The middle window and its wallpaper window should receive a cancel event. The left window should |
| 1016 | * not receive any events. If device B continues to report events, the right window and its |
| 1017 | * wallpaper window should receive remaining events. |
| 1018 | */ |
| 1019 | TEST_F(InputDispatcherTest, MultiDeviceSlipperyTouchWithWallpaperWindow) { |
| 1020 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1021 | sp<FakeWindowHandle> leftForegroundWindow = |
| 1022 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1023 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1024 | leftForegroundWindow->setFrame(Rect(0, 0, 100, 100)); |
| 1025 | leftForegroundWindow->setDupTouchToWallpaper(true); |
| 1026 | sp<FakeWindowHandle> leftWallpaperWindow = |
| 1027 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1028 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1029 | leftWallpaperWindow->setFrame(Rect(0, 0, 100, 100)); |
| 1030 | leftWallpaperWindow->setIsWallpaper(true); |
| 1031 | |
| 1032 | sp<FakeWindowHandle> middleForegroundWindow = |
| 1033 | sp<FakeWindowHandle>::make(application, mDispatcher, "Middle foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1034 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1035 | middleForegroundWindow->setFrame(Rect(100, 0, 200, 100)); |
| 1036 | middleForegroundWindow->setDupTouchToWallpaper(true); |
| 1037 | middleForegroundWindow->setSlippery(true); |
| 1038 | sp<FakeWindowHandle> middleWallpaperWindow = |
| 1039 | sp<FakeWindowHandle>::make(application, mDispatcher, "Middle wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1040 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1041 | middleWallpaperWindow->setFrame(Rect(100, 0, 200, 100)); |
| 1042 | middleWallpaperWindow->setIsWallpaper(true); |
| 1043 | |
| 1044 | sp<FakeWindowHandle> rightForegroundWindow = |
| 1045 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1046 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1047 | rightForegroundWindow->setFrame(Rect(200, 0, 300, 100)); |
| 1048 | rightForegroundWindow->setDupTouchToWallpaper(true); |
| 1049 | sp<FakeWindowHandle> rightWallpaperWindow = |
| 1050 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1051 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1052 | rightWallpaperWindow->setFrame(Rect(200, 0, 300, 100)); |
| 1053 | rightWallpaperWindow->setIsWallpaper(true); |
| 1054 | |
| 1055 | mDispatcher->onWindowInfosChanged( |
| 1056 | {{*leftForegroundWindow->getInfo(), *leftWallpaperWindow->getInfo(), |
| 1057 | *middleForegroundWindow->getInfo(), *middleWallpaperWindow->getInfo(), |
| 1058 | *rightForegroundWindow->getInfo(), *rightWallpaperWindow->getInfo()}, |
| 1059 | {}, |
| 1060 | 0, |
| 1061 | 0}); |
| 1062 | |
| 1063 | const DeviceId deviceA = 9; |
| 1064 | const DeviceId deviceB = 3; |
| 1065 | // Device A sends a DOWN event to the left window |
| 1066 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1067 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 1068 | .deviceId(deviceA) |
| 1069 | .build()); |
| 1070 | leftForegroundWindow->consumeMotionEvent( |
| 1071 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 1072 | leftWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 1073 | WithDeviceId(deviceA), |
| 1074 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 1075 | // Device B sends a DOWN event to the middle window |
| 1076 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1077 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 1078 | .deviceId(deviceB) |
| 1079 | .build()); |
| 1080 | middleForegroundWindow->consumeMotionEvent( |
| 1081 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 1082 | middleWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 1083 | WithDeviceId(deviceB), |
| 1084 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 1085 | // Move the events of device B to the top of the right window. |
| 1086 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1087 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(50)) |
| 1088 | .deviceId(deviceB) |
| 1089 | .build()); |
| 1090 | middleForegroundWindow->consumeMotionEvent( |
| 1091 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB))); |
| 1092 | middleWallpaperWindow->consumeMotionEvent( |
| 1093 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB), |
| 1094 | WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_CANCELED))); |
| 1095 | rightForegroundWindow->consumeMotionEvent( |
| 1096 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 1097 | rightWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 1098 | WithDeviceId(deviceB), |
| 1099 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 1100 | // Make sure the window on the right can receive the remaining events. |
| 1101 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1102 | .pointer(PointerBuilder(0, ToolType::FINGER).x(251).y(51)) |
| 1103 | .deviceId(deviceB) |
| 1104 | .build()); |
| 1105 | leftForegroundWindow->assertNoEvents(); |
| 1106 | leftWallpaperWindow->assertNoEvents(); |
| 1107 | middleForegroundWindow->assertNoEvents(); |
| 1108 | middleWallpaperWindow->assertNoEvents(); |
| 1109 | rightForegroundWindow->consumeMotionEvent( |
| 1110 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB))); |
| 1111 | rightWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), |
| 1112 | WithDeviceId(deviceB), |
| 1113 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 1114 | } |
| 1115 | |
| 1116 | /** |
| 1117 | * Similar to the test above, we have three windows, they are arranged horizontally and without any |
| 1118 | * overlap, and every window has a wallpaper window. The middle window is a simple window, without |
| 1119 | * any special flags. Device A reports a down event that lands in left window. Device B sends a down |
| 1120 | * event to the middle window and then touch is transferred from the middle window to the right |
| 1121 | * window. The right window and its wallpaper window should receive a down event. The middle window |
| 1122 | * and its wallpaper window should receive a cancel event. The left window should not receive any |
| 1123 | * events. Subsequent events reported by device B should go to the right window and its wallpaper. |
| 1124 | */ |
| 1125 | TEST_F(InputDispatcherTest, MultiDeviceTouchTransferWithWallpaperWindows) { |
| 1126 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1127 | sp<FakeWindowHandle> leftForegroundWindow = |
| 1128 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1129 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1130 | leftForegroundWindow->setFrame(Rect(0, 0, 100, 100)); |
| 1131 | leftForegroundWindow->setDupTouchToWallpaper(true); |
| 1132 | sp<FakeWindowHandle> leftWallpaperWindow = |
| 1133 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1134 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1135 | leftWallpaperWindow->setFrame(Rect(0, 0, 100, 100)); |
| 1136 | leftWallpaperWindow->setIsWallpaper(true); |
| 1137 | |
| 1138 | sp<FakeWindowHandle> middleForegroundWindow = |
| 1139 | sp<FakeWindowHandle>::make(application, mDispatcher, "Middle foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1140 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1141 | middleForegroundWindow->setFrame(Rect(100, 0, 200, 100)); |
| 1142 | middleForegroundWindow->setDupTouchToWallpaper(true); |
| 1143 | sp<FakeWindowHandle> middleWallpaperWindow = |
| 1144 | sp<FakeWindowHandle>::make(application, mDispatcher, "Middle wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1145 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1146 | middleWallpaperWindow->setFrame(Rect(100, 0, 200, 100)); |
| 1147 | middleWallpaperWindow->setIsWallpaper(true); |
| 1148 | |
| 1149 | sp<FakeWindowHandle> rightForegroundWindow = |
| 1150 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1151 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1152 | rightForegroundWindow->setFrame(Rect(200, 0, 300, 100)); |
| 1153 | rightForegroundWindow->setDupTouchToWallpaper(true); |
| 1154 | sp<FakeWindowHandle> rightWallpaperWindow = |
| 1155 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1156 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1157 | rightWallpaperWindow->setFrame(Rect(200, 0, 300, 100)); |
| 1158 | rightWallpaperWindow->setIsWallpaper(true); |
| 1159 | |
| 1160 | mDispatcher->onWindowInfosChanged( |
| 1161 | {{*leftForegroundWindow->getInfo(), *leftWallpaperWindow->getInfo(), |
| 1162 | *middleForegroundWindow->getInfo(), *middleWallpaperWindow->getInfo(), |
| 1163 | *rightForegroundWindow->getInfo(), *rightWallpaperWindow->getInfo()}, |
| 1164 | {}, |
| 1165 | 0, |
| 1166 | 0}); |
| 1167 | |
| 1168 | const DeviceId deviceA = 9; |
| 1169 | const DeviceId deviceB = 3; |
| 1170 | // Device A touch down on the left window |
| 1171 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1172 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 1173 | .deviceId(deviceA) |
| 1174 | .build()); |
| 1175 | leftForegroundWindow->consumeMotionEvent( |
| 1176 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 1177 | leftWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 1178 | WithDeviceId(deviceA), |
| 1179 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 1180 | // Device B touch down on the middle window |
| 1181 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1182 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 1183 | .deviceId(deviceB) |
| 1184 | .build()); |
| 1185 | middleForegroundWindow->consumeMotionEvent( |
| 1186 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 1187 | middleWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 1188 | WithDeviceId(deviceB), |
| 1189 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 1190 | |
| 1191 | // Transfer touch from the middle window to the right window. |
| 1192 | ASSERT_TRUE(mDispatcher->transferTouchGesture(middleForegroundWindow->getToken(), |
| 1193 | rightForegroundWindow->getToken())); |
| 1194 | |
| 1195 | middleForegroundWindow->consumeMotionEvent( |
| 1196 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB))); |
| 1197 | middleWallpaperWindow->consumeMotionEvent( |
| 1198 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB), |
| 1199 | WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_CANCELED))); |
| 1200 | rightForegroundWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 1201 | WithDeviceId(deviceB), |
| 1202 | WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
| 1203 | rightWallpaperWindow->consumeMotionEvent( |
| 1204 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB), |
| 1205 | WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
| 1206 | |
| 1207 | // Make sure the right window can receive the remaining events. |
| 1208 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1209 | .pointer(PointerBuilder(0, ToolType::FINGER).x(251).y(51)) |
| 1210 | .deviceId(deviceB) |
| 1211 | .build()); |
| 1212 | leftForegroundWindow->assertNoEvents(); |
| 1213 | leftWallpaperWindow->assertNoEvents(); |
| 1214 | middleForegroundWindow->assertNoEvents(); |
| 1215 | middleWallpaperWindow->assertNoEvents(); |
| 1216 | rightForegroundWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), |
| 1217 | WithDeviceId(deviceB), |
| 1218 | WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
| 1219 | rightWallpaperWindow->consumeMotionEvent( |
| 1220 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB), |
| 1221 | WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
| 1222 | } |
| 1223 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1224 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 1225 | public ::testing::WithParamInterface<bool> {}; |
| 1226 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 1227 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1228 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1229 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1230 | * The top window gets a multitouch gesture. |
| 1231 | * Ensure that wallpaper gets the same gesture. |
| 1232 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1233 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1234 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1235 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1236 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 1237 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1238 | foregroundWindow->setDupTouchToWallpaper(true); |
| 1239 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1240 | |
| 1241 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1242 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 1243 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1244 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1245 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1246 | mDispatcher->onWindowInfosChanged( |
| 1247 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1248 | |
| 1249 | // Touch down on top window |
| 1250 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1251 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 1252 | ui::LogicalDisplayId::DEFAULT, {100, 100})) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1253 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1254 | |
| 1255 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1256 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1257 | wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1258 | |
| 1259 | // Second finger down on the top window |
| 1260 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1261 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1262 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1263 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1264 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1265 | .build(); |
| 1266 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1267 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1268 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1269 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1270 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1271 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1272 | EXPECTED_WALLPAPER_FLAGS); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1273 | |
| 1274 | const MotionEvent secondFingerUpEvent = |
| 1275 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1276 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1277 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1278 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1279 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1280 | .build(); |
| 1281 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1282 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1283 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1284 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1285 | foregroundWindow->consumeMotionPointerUp(0); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1286 | wallpaperWindow->consumeMotionPointerUp(0, ui::LogicalDisplayId::DEFAULT, |
| 1287 | EXPECTED_WALLPAPER_FLAGS); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1288 | |
| 1289 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1290 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1291 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1292 | AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1293 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1294 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1295 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1296 | .x(100) |
| 1297 | .y(100)) |
| 1298 | .build(), |
| 1299 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1300 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1301 | foregroundWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
| 1302 | wallpaperWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | /** |
| 1306 | * Two windows: a window on the left and window on the right. |
| 1307 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1308 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1309 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1310 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1311 | * ACTION_POINTER_DOWN(1). |
| 1312 | */ |
| 1313 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1314 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1315 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 1316 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1317 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1318 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1319 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1320 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 1321 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1322 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1323 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1324 | |
| 1325 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1326 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 1327 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1328 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1329 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1330 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1331 | mDispatcher->onWindowInfosChanged( |
| 1332 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 1333 | {}, |
| 1334 | 0, |
| 1335 | 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1336 | |
| 1337 | // Touch down on left window |
| 1338 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1339 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 1340 | ui::LogicalDisplayId::DEFAULT, {100, 100})) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1341 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1342 | |
| 1343 | // Both foreground window and its wallpaper should receive the touch down |
| 1344 | leftWindow->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1345 | wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1346 | |
| 1347 | // Second finger down on the right window |
| 1348 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1349 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1350 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1351 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1352 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1353 | .build(); |
| 1354 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1355 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1356 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1357 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1358 | |
| 1359 | leftWindow->consumeMotionMove(); |
| 1360 | // Since the touch is split, right window gets ACTION_DOWN |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1361 | rightWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 1362 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1363 | EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1364 | |
| 1365 | // Now, leftWindow, which received the first finger, disappears. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1366 | mDispatcher->onWindowInfosChanged( |
| 1367 | {{*rightWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1368 | leftWindow->consumeMotionCancel(); |
| 1369 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1370 | wallpaperWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1371 | |
| 1372 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 1373 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 1374 | const MotionEvent secondFingerMoveEvent = |
| 1375 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1376 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1377 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1378 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1379 | .build(); |
| 1380 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1381 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1382 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 1383 | rightWindow->consumeMotionMove(); |
| 1384 | |
| 1385 | leftWindow->assertNoEvents(); |
| 1386 | rightWindow->assertNoEvents(); |
| 1387 | wallpaperWindow->assertNoEvents(); |
| 1388 | } |
| 1389 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1390 | /** |
| 1391 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 1392 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 1393 | * The right window should receive ACTION_DOWN. |
| 1394 | */ |
| 1395 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1396 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1397 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 1398 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1399 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1400 | leftWindow->setDupTouchToWallpaper(true); |
| 1401 | leftWindow->setSlippery(true); |
| 1402 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1403 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 1404 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1405 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1406 | |
| 1407 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1408 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 1409 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1410 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1411 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1412 | mDispatcher->onWindowInfosChanged( |
| 1413 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 1414 | {}, |
| 1415 | 0, |
| 1416 | 0}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1417 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1418 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1419 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1420 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 1421 | ui::LogicalDisplayId::DEFAULT, {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1422 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1423 | |
| 1424 | // Both foreground window and its wallpaper should receive the touch down |
| 1425 | leftWindow->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1426 | wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1427 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1428 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1429 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1430 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1431 | ui::LogicalDisplayId::DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1432 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1433 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1434 | leftWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1435 | rightWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 1436 | wallpaperWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1439 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1440 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 1441 | * interactive, it might stop sending this flag. |
| 1442 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 1443 | * to have a consistent input stream. |
| 1444 | * |
| 1445 | * Test procedure: |
| 1446 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 1447 | * DOWN (new gesture). |
| 1448 | * |
| 1449 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 1450 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 1451 | * |
| 1452 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 1453 | * regular window below) to emulate the actual situation where it happens on the device. |
| 1454 | */ |
| 1455 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 1456 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1457 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 1458 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1459 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1460 | spyWindow->setTrustedOverlay(true); |
| 1461 | spyWindow->setSpy(true); |
| 1462 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1463 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1464 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1465 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1466 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1467 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1468 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1469 | |
| 1470 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1471 | mDispatcher->notifyMotion( |
| 1472 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1473 | .deviceId(touchDeviceId) |
| 1474 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1475 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1476 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1477 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1478 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1479 | .deviceId(touchDeviceId) |
| 1480 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1481 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1482 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 1483 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1484 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1485 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1486 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1487 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1488 | |
| 1489 | // Cancel the current gesture. Send the cancel without the default policy flags. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1490 | mDispatcher->notifyMotion( |
| 1491 | MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 1492 | .deviceId(touchDeviceId) |
| 1493 | .policyFlags(0) |
| 1494 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1495 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 1496 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1497 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 1498 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 1499 | |
| 1500 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 1501 | // 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] | 1502 | mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1503 | |
| 1504 | // Start new gesture |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1505 | mDispatcher->notifyMotion( |
| 1506 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1507 | .deviceId(touchDeviceId) |
| 1508 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1509 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1510 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1511 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1512 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1513 | |
| 1514 | // No more events |
| 1515 | spyWindow->assertNoEvents(); |
| 1516 | window->assertNoEvents(); |
| 1517 | } |
| 1518 | |
| 1519 | /** |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 1520 | * Same as the above 'TwoPointerCancelInconsistentPolicy' test, but for hovers. |
| 1521 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 1522 | * interactive, it might stop sending this flag. |
| 1523 | * We've already ensured the consistency of the touch event in this case, and we should also ensure |
| 1524 | * the consistency of the hover event in this case. |
| 1525 | * |
| 1526 | * Test procedure: |
| 1527 | * HOVER_ENTER -> HOVER_MOVE -> (stop sending POLICY_FLAG_PASS_TO_USER) -> HOVER_EXIT |
| 1528 | * HOVER_ENTER -> HOVER_MOVE -> HOVER_EXIT |
| 1529 | * |
| 1530 | * We expect to receive two full streams of hover events. |
| 1531 | */ |
| 1532 | TEST_F(InputDispatcherTest, HoverEventInconsistentPolicy) { |
| 1533 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1534 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1535 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1536 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 1537 | window->setFrame(Rect(0, 0, 300, 300)); |
| 1538 | |
| 1539 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1540 | |
| 1541 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1542 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1543 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 1544 | .build()); |
| 1545 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1546 | |
| 1547 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1548 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1549 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 1550 | .build()); |
| 1551 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 1552 | |
| 1553 | // Send hover exit without the default policy flags. |
| 1554 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 1555 | .policyFlags(0) |
| 1556 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 1557 | .build()); |
| 1558 | |
| 1559 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1560 | |
| 1561 | // Send a simple hover event stream, ensure dispatcher not crashed and window can receive |
| 1562 | // right event. |
| 1563 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1564 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1565 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(200).y(201)) |
| 1566 | .build()); |
| 1567 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1568 | |
| 1569 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1570 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1571 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 1572 | .build()); |
| 1573 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 1574 | |
| 1575 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 1576 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1577 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 1578 | .build()); |
| 1579 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1580 | } |
| 1581 | |
| 1582 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1583 | * Two windows: a window on the left and a window on the right. |
| 1584 | * Mouse is hovered from the right window into the left window. |
| 1585 | * Next, we tap on the left window, where the cursor was last seen. |
| 1586 | * The second tap is done onto the right window. |
| 1587 | * The mouse and tap are from two different devices. |
| 1588 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 1589 | * explicitly helps during debugging. |
| 1590 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 1591 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 1592 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1593 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap_legacy) { |
| 1594 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
| 1595 | |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1596 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1597 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 1598 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1599 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1600 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1601 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 1602 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1603 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 1604 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1605 | mDispatcher->onWindowInfosChanged( |
| 1606 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1607 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 1608 | // stale. |
| 1609 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1610 | const int32_t mouseDeviceId = 6; |
| 1611 | const int32_t touchDeviceId = 4; |
| 1612 | // Move the cursor from right |
| 1613 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1614 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1615 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1616 | AINPUT_SOURCE_MOUSE) |
| 1617 | .deviceId(mouseDeviceId) |
| 1618 | .downTime(baseTime + 10) |
| 1619 | .eventTime(baseTime + 20) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1620 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1621 | .build())); |
| 1622 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 1623 | |
| 1624 | // .. to the left window |
| 1625 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1626 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1627 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1628 | AINPUT_SOURCE_MOUSE) |
| 1629 | .deviceId(mouseDeviceId) |
| 1630 | .downTime(baseTime + 10) |
| 1631 | .eventTime(baseTime + 30) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1632 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1633 | .build())); |
| 1634 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 1635 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 1636 | // Now tap the left window |
| 1637 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1638 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1639 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 1640 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1641 | .deviceId(touchDeviceId) |
| 1642 | .downTime(baseTime + 40) |
| 1643 | .eventTime(baseTime + 40) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1644 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1645 | .build())); |
| 1646 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 1647 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1648 | |
| 1649 | // release tap |
| 1650 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1651 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1652 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1653 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1654 | .deviceId(touchDeviceId) |
| 1655 | .downTime(baseTime + 40) |
| 1656 | .eventTime(baseTime + 50) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1657 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1658 | .build())); |
| 1659 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 1660 | |
| 1661 | // Tap the window on the right |
| 1662 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1663 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1664 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 1665 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1666 | .deviceId(touchDeviceId) |
| 1667 | .downTime(baseTime + 60) |
| 1668 | .eventTime(baseTime + 60) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1669 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1670 | .build())); |
| 1671 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1672 | |
| 1673 | // release tap |
| 1674 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1675 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1676 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1677 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1678 | .deviceId(touchDeviceId) |
| 1679 | .downTime(baseTime + 60) |
| 1680 | .eventTime(baseTime + 70) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1681 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1682 | .build())); |
| 1683 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 1684 | |
| 1685 | // No more events |
| 1686 | leftWindow->assertNoEvents(); |
| 1687 | rightWindow->assertNoEvents(); |
| 1688 | } |
| 1689 | |
| 1690 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1691 | * Two windows: a window on the left and a window on the right. |
| 1692 | * Mouse is hovered from the right window into the left window. |
| 1693 | * Next, we tap on the left window, where the cursor was last seen. |
| 1694 | * The second tap is done onto the right window. |
| 1695 | * The mouse and tap are from two different devices. |
| 1696 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 1697 | * explicitly helps during debugging. |
| 1698 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 1699 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 1700 | */ |
| 1701 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 1702 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 1703 | |
| 1704 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1705 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 1706 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1707 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1708 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1709 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 1710 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1711 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 1712 | |
| 1713 | mDispatcher->onWindowInfosChanged( |
| 1714 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 1715 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 1716 | // stale. |
| 1717 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1718 | const int32_t mouseDeviceId = 6; |
| 1719 | const int32_t touchDeviceId = 4; |
| 1720 | // Move the cursor from right |
| 1721 | mDispatcher->notifyMotion( |
| 1722 | MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 1723 | .deviceId(mouseDeviceId) |
| 1724 | .downTime(baseTime + 10) |
| 1725 | .eventTime(baseTime + 20) |
| 1726 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100)) |
| 1727 | .build()); |
| 1728 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 1729 | |
| 1730 | // .. to the left window |
| 1731 | mDispatcher->notifyMotion( |
| 1732 | MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 1733 | .deviceId(mouseDeviceId) |
| 1734 | .downTime(baseTime + 10) |
| 1735 | .eventTime(baseTime + 30) |
| 1736 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100)) |
| 1737 | .build()); |
| 1738 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 1739 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 1740 | // Now tap the left window |
| 1741 | mDispatcher->notifyMotion( |
| 1742 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1743 | .deviceId(touchDeviceId) |
| 1744 | .downTime(baseTime + 40) |
| 1745 | .eventTime(baseTime + 40) |
| 1746 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1747 | .build()); |
| 1748 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1749 | |
| 1750 | // release tap |
| 1751 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1752 | .deviceId(touchDeviceId) |
| 1753 | .downTime(baseTime + 40) |
| 1754 | .eventTime(baseTime + 50) |
| 1755 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1756 | .build()); |
| 1757 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 1758 | |
| 1759 | // Tap the window on the right |
| 1760 | mDispatcher->notifyMotion( |
| 1761 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1762 | .deviceId(touchDeviceId) |
| 1763 | .downTime(baseTime + 60) |
| 1764 | .eventTime(baseTime + 60) |
| 1765 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 1766 | .build()); |
| 1767 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1768 | |
| 1769 | // release tap |
| 1770 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1771 | .deviceId(touchDeviceId) |
| 1772 | .downTime(baseTime + 60) |
| 1773 | .eventTime(baseTime + 70) |
| 1774 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 1775 | .build()); |
| 1776 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 1777 | |
| 1778 | // No more events |
| 1779 | leftWindow->assertNoEvents(); |
| 1780 | rightWindow->assertNoEvents(); |
| 1781 | } |
| 1782 | |
| 1783 | /** |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1784 | * Start hovering in a window. While this hover is still active, make another window appear on top. |
| 1785 | * The top, obstructing window has no input channel, so it's not supposed to receive input. |
| 1786 | * While the top window is present, the hovering is stopped. |
| 1787 | * Later, hovering gets resumed again. |
| 1788 | * Ensure that new hover gesture is handled correctly. |
| 1789 | * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly |
| 1790 | * to the window that's currently being hovered over. |
| 1791 | */ |
| 1792 | TEST_F(InputDispatcherTest, HoverWhileWindowAppears) { |
| 1793 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1794 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1795 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1796 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1797 | |
| 1798 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1799 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1800 | |
| 1801 | // Start hovering in the window |
| 1802 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1803 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1804 | .build()); |
| 1805 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1806 | |
| 1807 | // Now, an obscuring window appears! |
| 1808 | sp<FakeWindowHandle> obscuringWindow = |
| 1809 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1810 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1811 | /*createInputChannel=*/false); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1812 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1813 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 1814 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 1815 | obscuringWindow->setNoInputChannel(true); |
| 1816 | obscuringWindow->setFocusable(false); |
| 1817 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1818 | mDispatcher->onWindowInfosChanged( |
| 1819 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1820 | |
| 1821 | // While this new obscuring window is present, the hovering is stopped |
| 1822 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 1823 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1824 | .build()); |
| 1825 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1826 | |
| 1827 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1828 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1829 | |
| 1830 | // And a new hover gesture starts. |
| 1831 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1832 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1833 | .build()); |
| 1834 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1835 | } |
| 1836 | |
| 1837 | /** |
| 1838 | * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to |
| 1839 | * the obscuring window. |
| 1840 | */ |
| 1841 | TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) { |
| 1842 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1843 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1844 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1845 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1846 | |
| 1847 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1848 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1849 | |
| 1850 | // Start hovering in the window |
| 1851 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1852 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1853 | .build()); |
| 1854 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1855 | |
| 1856 | // Now, an obscuring window appears! |
| 1857 | sp<FakeWindowHandle> obscuringWindow = |
| 1858 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1859 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1860 | /*createInputChannel=*/false); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1861 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1862 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 1863 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 1864 | obscuringWindow->setNoInputChannel(true); |
| 1865 | obscuringWindow->setFocusable(false); |
| 1866 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1867 | mDispatcher->onWindowInfosChanged( |
| 1868 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1869 | |
| 1870 | // While this new obscuring window is present, the hovering continues. The event can't go to the |
| 1871 | // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window. |
| 1872 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1873 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1874 | .build()); |
| 1875 | obscuringWindow->assertNoEvents(); |
| 1876 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1877 | |
| 1878 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1879 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1880 | |
| 1881 | // Hovering continues in the same position. The hovering pointer re-enters the bottom window, |
| 1882 | // so it should generate a HOVER_ENTER |
| 1883 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1884 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1885 | .build()); |
| 1886 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1887 | |
| 1888 | // Now the MOVE should be getting dispatched normally |
| 1889 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1890 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 1891 | .build()); |
| 1892 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 1893 | } |
| 1894 | |
| 1895 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1896 | * Hover mouse over a window, and then send ACTION_SCROLL. Ensure both the hover and the scroll |
| 1897 | * events are delivered to the window. |
| 1898 | */ |
| 1899 | TEST_F(InputDispatcherTest, HoverMoveAndScroll) { |
| 1900 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1901 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1902 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1903 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1904 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1905 | |
| 1906 | // Start hovering in the window |
| 1907 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 1908 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 1909 | .build()); |
| 1910 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1911 | |
| 1912 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 1913 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 1914 | .build()); |
| 1915 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 1916 | |
| 1917 | // Scroll with the mouse |
| 1918 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_SCROLL, AINPUT_SOURCE_MOUSE) |
| 1919 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 1920 | .build()); |
| 1921 | window->consumeMotionEvent(WithMotionAction(ACTION_SCROLL)); |
| 1922 | } |
| 1923 | |
| 1924 | using InputDispatcherMultiDeviceTest = InputDispatcherTest; |
| 1925 | |
| 1926 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1927 | * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that |
| 1928 | * touch is dropped, because stylus should be preferred over touch. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1929 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1930 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownBlocksTouchDown) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1931 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1932 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1933 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1934 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1935 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1936 | |
| 1937 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1938 | |
| 1939 | constexpr int32_t touchDeviceId = 4; |
| 1940 | constexpr int32_t stylusDeviceId = 2; |
| 1941 | |
| 1942 | // Stylus down |
| 1943 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 1944 | .deviceId(stylusDeviceId) |
| 1945 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 1946 | .build()); |
| 1947 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 1948 | |
| 1949 | // Touch down |
| 1950 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1951 | .deviceId(touchDeviceId) |
| 1952 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 1953 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1954 | |
| 1955 | // Touch move |
| 1956 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1957 | .deviceId(touchDeviceId) |
| 1958 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 1959 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1960 | // Touch is ignored because stylus is already down |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1961 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1962 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1963 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 1964 | .deviceId(stylusDeviceId) |
| 1965 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 1966 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1967 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 1968 | WithCoords(101, 111))); |
| 1969 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1970 | window->assertNoEvents(); |
| 1971 | } |
| 1972 | |
| 1973 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1974 | * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that |
| 1975 | * touch is not dropped, because multiple devices are allowed to be active in the same window. |
| 1976 | */ |
| 1977 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownDoesNotBlockTouchDown) { |
| 1978 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 1979 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1980 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1981 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1982 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1983 | |
| 1984 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1985 | |
| 1986 | constexpr int32_t touchDeviceId = 4; |
| 1987 | constexpr int32_t stylusDeviceId = 2; |
| 1988 | |
| 1989 | // Stylus down |
| 1990 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 1991 | .deviceId(stylusDeviceId) |
| 1992 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 1993 | .build()); |
| 1994 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 1995 | |
| 1996 | // Touch down |
| 1997 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1998 | .deviceId(touchDeviceId) |
| 1999 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2000 | .build()); |
| 2001 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2002 | |
| 2003 | // Touch move |
| 2004 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2005 | .deviceId(touchDeviceId) |
| 2006 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2007 | .build()); |
| 2008 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2009 | |
| 2010 | // Stylus move |
| 2011 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2012 | .deviceId(stylusDeviceId) |
| 2013 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2014 | .build()); |
| 2015 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2016 | WithCoords(101, 111))); |
| 2017 | |
| 2018 | window->assertNoEvents(); |
| 2019 | } |
| 2020 | |
| 2021 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2022 | * 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] | 2023 | * 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] | 2024 | * Similar test as above, but with added SPY window. |
| 2025 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2026 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyBlocksTouchDown) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2027 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2028 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2029 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2030 | ui::LogicalDisplayId::DEFAULT); |
| 2031 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 2032 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2033 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2034 | spyWindow->setTrustedOverlay(true); |
| 2035 | spyWindow->setSpy(true); |
| 2036 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2037 | |
| 2038 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 2039 | |
| 2040 | constexpr int32_t touchDeviceId = 4; |
| 2041 | constexpr int32_t stylusDeviceId = 2; |
| 2042 | |
| 2043 | // Stylus down |
| 2044 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2045 | .deviceId(stylusDeviceId) |
| 2046 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2047 | .build()); |
| 2048 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2049 | spyWindow->consumeMotionEvent( |
| 2050 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2051 | |
| 2052 | // Touch down |
| 2053 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2054 | .deviceId(touchDeviceId) |
| 2055 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2056 | .build()); |
| 2057 | |
| 2058 | // Touch move |
| 2059 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2060 | .deviceId(touchDeviceId) |
| 2061 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2062 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2063 | |
| 2064 | // Touch is ignored because stylus is already down |
| 2065 | |
| 2066 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2067 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2068 | .deviceId(stylusDeviceId) |
| 2069 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2070 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2071 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2072 | WithCoords(101, 111))); |
| 2073 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2074 | WithCoords(101, 111))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2075 | |
| 2076 | window->assertNoEvents(); |
| 2077 | spyWindow->assertNoEvents(); |
| 2078 | } |
| 2079 | |
| 2080 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2081 | * One window and one spy window. Stylus down on the window. Next, touch from another device goes |
| 2082 | * down. Ensure that touch is not dropped, because multiple devices can be active at the same time. |
| 2083 | * Similar test as above, but with added SPY window. |
| 2084 | */ |
| 2085 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyDoesNotBlockTouchDown) { |
| 2086 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2087 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2088 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2089 | ui::LogicalDisplayId::DEFAULT); |
| 2090 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 2091 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2092 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2093 | spyWindow->setTrustedOverlay(true); |
| 2094 | spyWindow->setSpy(true); |
| 2095 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2096 | |
| 2097 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 2098 | |
| 2099 | constexpr int32_t touchDeviceId = 4; |
| 2100 | constexpr int32_t stylusDeviceId = 2; |
| 2101 | |
| 2102 | // Stylus down |
| 2103 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2104 | .deviceId(stylusDeviceId) |
| 2105 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2106 | .build()); |
| 2107 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2108 | spyWindow->consumeMotionEvent( |
| 2109 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2110 | |
| 2111 | // Touch down |
| 2112 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2113 | .deviceId(touchDeviceId) |
| 2114 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2115 | .build()); |
| 2116 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2117 | spyWindow->consumeMotionEvent( |
| 2118 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2119 | |
| 2120 | // Touch move |
| 2121 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2122 | .deviceId(touchDeviceId) |
| 2123 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2124 | .build()); |
| 2125 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2126 | spyWindow->consumeMotionEvent( |
| 2127 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2128 | |
| 2129 | // Subsequent stylus movements are delivered correctly |
| 2130 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2131 | .deviceId(stylusDeviceId) |
| 2132 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2133 | .build()); |
| 2134 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2135 | WithCoords(101, 111))); |
| 2136 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2137 | WithCoords(101, 111))); |
| 2138 | |
| 2139 | window->assertNoEvents(); |
| 2140 | spyWindow->assertNoEvents(); |
| 2141 | } |
| 2142 | |
| 2143 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2144 | * 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] | 2145 | * touch is dropped, because stylus hover takes precedence. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2146 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2147 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverBlocksTouchDown) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2148 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2149 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2150 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2151 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2152 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2153 | |
| 2154 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2155 | |
| 2156 | constexpr int32_t touchDeviceId = 4; |
| 2157 | constexpr int32_t stylusDeviceId = 2; |
| 2158 | |
| 2159 | // Stylus down on the window |
| 2160 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2161 | .deviceId(stylusDeviceId) |
| 2162 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2163 | .build()); |
| 2164 | window->consumeMotionEvent( |
| 2165 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2166 | |
| 2167 | // Touch down on window |
| 2168 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2169 | .deviceId(touchDeviceId) |
| 2170 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2171 | .build()); |
| 2172 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2173 | .deviceId(touchDeviceId) |
| 2174 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2175 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2176 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2177 | // Touch is ignored because stylus is hovering |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2178 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2179 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2180 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2181 | .deviceId(stylusDeviceId) |
| 2182 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2183 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2184 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2185 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2186 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2187 | // and subsequent touches continue to be ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2188 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2189 | .deviceId(touchDeviceId) |
| 2190 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2191 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2192 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2193 | } |
| 2194 | |
| 2195 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2196 | * One window. Stylus hover on the window. Next, touch from another device goes down. Ensure that |
| 2197 | * touch is not dropped, because stylus hover and touch can be both active at the same time. |
| 2198 | */ |
| 2199 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverDoesNotBlockTouchDown) { |
| 2200 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2201 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2202 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2203 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2204 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2205 | |
| 2206 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2207 | |
| 2208 | constexpr int32_t touchDeviceId = 4; |
| 2209 | constexpr int32_t stylusDeviceId = 2; |
| 2210 | |
| 2211 | // Stylus down on the window |
| 2212 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2213 | .deviceId(stylusDeviceId) |
| 2214 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2215 | .build()); |
| 2216 | window->consumeMotionEvent( |
| 2217 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2218 | |
| 2219 | // Touch down on window |
| 2220 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2221 | .deviceId(touchDeviceId) |
| 2222 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2223 | .build()); |
| 2224 | // Touch move on window |
| 2225 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2226 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2227 | .deviceId(touchDeviceId) |
| 2228 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2229 | .build()); |
| 2230 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2231 | |
| 2232 | // Subsequent stylus movements are delivered correctly |
| 2233 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2234 | .deviceId(stylusDeviceId) |
| 2235 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2236 | .build()); |
| 2237 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2238 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
| 2239 | |
| 2240 | // and subsequent touches continue to work |
| 2241 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2242 | .deviceId(touchDeviceId) |
| 2243 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2244 | .build()); |
| 2245 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2246 | window->assertNoEvents(); |
| 2247 | } |
| 2248 | |
| 2249 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2250 | * 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] | 2251 | * Ensure that touch is canceled, because stylus hover should take precedence. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2252 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2253 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusHover) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2254 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2255 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2256 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2257 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2258 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2259 | |
| 2260 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2261 | |
| 2262 | constexpr int32_t touchDeviceId = 4; |
| 2263 | constexpr int32_t stylusDeviceId = 2; |
| 2264 | |
| 2265 | // Touch down on window |
| 2266 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2267 | .deviceId(touchDeviceId) |
| 2268 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2269 | .build()); |
| 2270 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2271 | .deviceId(touchDeviceId) |
| 2272 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2273 | .build()); |
| 2274 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2275 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2276 | |
| 2277 | // Stylus hover on the window |
| 2278 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2279 | .deviceId(stylusDeviceId) |
| 2280 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2281 | .build()); |
| 2282 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2283 | .deviceId(stylusDeviceId) |
| 2284 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2285 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2286 | // Stylus hover movement causes touch to be canceled |
| 2287 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 2288 | WithCoords(141, 146))); |
| 2289 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 2290 | WithDeviceId(stylusDeviceId), WithCoords(100, 110))); |
| 2291 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2292 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2293 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2294 | // Subsequent touch movements are ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2295 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2296 | .deviceId(touchDeviceId) |
| 2297 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2298 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2299 | |
| 2300 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2304 | * One window. Touch down on the window. Then, stylus hover on the window from another device. |
| 2305 | * Ensure that touch is not canceled, because stylus hover can be active at the same time as touch. |
| 2306 | */ |
| 2307 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsNotCanceledByStylusHover) { |
| 2308 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2309 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2310 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2311 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2312 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2313 | |
| 2314 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2315 | |
| 2316 | constexpr int32_t touchDeviceId = 4; |
| 2317 | constexpr int32_t stylusDeviceId = 2; |
| 2318 | |
| 2319 | // Touch down on window |
| 2320 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2321 | .deviceId(touchDeviceId) |
| 2322 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2323 | .build()); |
| 2324 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2325 | .deviceId(touchDeviceId) |
| 2326 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2327 | .build()); |
| 2328 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2329 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2330 | |
| 2331 | // Stylus hover on the window |
| 2332 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2333 | .deviceId(stylusDeviceId) |
| 2334 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2335 | .build()); |
| 2336 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2337 | .deviceId(stylusDeviceId) |
| 2338 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2339 | .build()); |
| 2340 | // Stylus hover movement is received normally |
| 2341 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 2342 | WithDeviceId(stylusDeviceId), WithCoords(100, 110))); |
| 2343 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2344 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
| 2345 | |
| 2346 | // Subsequent touch movements also work |
| 2347 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2348 | .deviceId(touchDeviceId) |
| 2349 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2350 | .build()); |
| 2351 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId), |
| 2352 | WithCoords(142, 147))); |
| 2353 | |
| 2354 | window->assertNoEvents(); |
| 2355 | } |
| 2356 | |
| 2357 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2358 | * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that |
| 2359 | * the latest stylus takes over. That is, old stylus should be canceled and the new stylus should |
| 2360 | * become active. |
| 2361 | */ |
| 2362 | TEST_F(InputDispatcherMultiDeviceTest, LatestStylusWins) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2363 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2364 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2365 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2366 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2367 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2368 | |
| 2369 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2370 | |
| 2371 | constexpr int32_t stylusDeviceId1 = 3; |
| 2372 | constexpr int32_t stylusDeviceId2 = 5; |
| 2373 | |
| 2374 | // Touch down on window |
| 2375 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2376 | .deviceId(stylusDeviceId1) |
| 2377 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100)) |
| 2378 | .build()); |
| 2379 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2380 | .deviceId(stylusDeviceId1) |
| 2381 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2382 | .build()); |
| 2383 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1))); |
| 2384 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2385 | |
| 2386 | // Second stylus down |
| 2387 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2388 | .deviceId(stylusDeviceId2) |
| 2389 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10)) |
| 2390 | .build()); |
| 2391 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2392 | .deviceId(stylusDeviceId2) |
| 2393 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11)) |
| 2394 | .build()); |
| 2395 | |
| 2396 | // First stylus is canceled, second one takes over. |
| 2397 | window->consumeMotionEvent( |
| 2398 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId1))); |
| 2399 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2))); |
| 2400 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2))); |
| 2401 | |
| 2402 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2403 | .deviceId(stylusDeviceId1) |
| 2404 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2405 | .build()); |
| 2406 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2407 | window->assertNoEvents(); |
| 2408 | } |
| 2409 | |
| 2410 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2411 | * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that |
| 2412 | * both stylus devices can function simultaneously. |
| 2413 | */ |
| 2414 | TEST_F(InputDispatcherMultiDeviceTest, TwoStylusDevicesActiveAtTheSameTime) { |
| 2415 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2416 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2417 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2418 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2419 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2420 | |
| 2421 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2422 | |
| 2423 | constexpr int32_t stylusDeviceId1 = 3; |
| 2424 | constexpr int32_t stylusDeviceId2 = 5; |
| 2425 | |
| 2426 | // Touch down on window |
| 2427 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2428 | .deviceId(stylusDeviceId1) |
| 2429 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100)) |
| 2430 | .build()); |
| 2431 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2432 | .deviceId(stylusDeviceId1) |
| 2433 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2434 | .build()); |
| 2435 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1))); |
| 2436 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2437 | |
| 2438 | // Second stylus down |
| 2439 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2440 | .deviceId(stylusDeviceId2) |
| 2441 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10)) |
| 2442 | .build()); |
| 2443 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2444 | .deviceId(stylusDeviceId2) |
| 2445 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11)) |
| 2446 | .build()); |
| 2447 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2))); |
| 2448 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2))); |
| 2449 | |
| 2450 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2451 | .deviceId(stylusDeviceId1) |
| 2452 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2453 | .build()); |
| 2454 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2455 | window->assertNoEvents(); |
| 2456 | } |
| 2457 | |
| 2458 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2459 | * One window. Touch down on the window. Then, stylus down on the window from another device. |
| 2460 | * Ensure that is canceled, because stylus down should be preferred over touch. |
| 2461 | */ |
| 2462 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusDown) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2463 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2464 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2465 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2466 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2467 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2468 | |
| 2469 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2470 | |
| 2471 | constexpr int32_t touchDeviceId = 4; |
| 2472 | constexpr int32_t stylusDeviceId = 2; |
| 2473 | |
| 2474 | // Touch down on window |
| 2475 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2476 | .deviceId(touchDeviceId) |
| 2477 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2478 | .build()); |
| 2479 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2480 | .deviceId(touchDeviceId) |
| 2481 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2482 | .build()); |
| 2483 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2484 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2485 | |
| 2486 | // Stylus down on the window |
| 2487 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2488 | .deviceId(stylusDeviceId) |
| 2489 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2490 | .build()); |
| 2491 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 2492 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2493 | |
| 2494 | // Subsequent stylus movements are delivered correctly |
| 2495 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2496 | .deviceId(stylusDeviceId) |
| 2497 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2498 | .build()); |
| 2499 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2500 | WithCoords(101, 111))); |
| 2501 | } |
| 2502 | |
| 2503 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2504 | * One window. Touch down on the window. Then, stylus down on the window from another device. |
| 2505 | * Ensure that both touch and stylus are functioning independently. |
| 2506 | */ |
| 2507 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsNotCanceledByStylusDown) { |
| 2508 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2509 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2510 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2511 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2512 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2513 | |
| 2514 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2515 | |
| 2516 | constexpr int32_t touchDeviceId = 4; |
| 2517 | constexpr int32_t stylusDeviceId = 2; |
| 2518 | |
| 2519 | // Touch down on window |
| 2520 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2521 | .deviceId(touchDeviceId) |
| 2522 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2523 | .build()); |
| 2524 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2525 | .deviceId(touchDeviceId) |
| 2526 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2527 | .build()); |
| 2528 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2529 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2530 | |
| 2531 | // Stylus down on the window |
| 2532 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2533 | .deviceId(stylusDeviceId) |
| 2534 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2535 | .build()); |
| 2536 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2537 | |
| 2538 | // Subsequent stylus movements are delivered correctly |
| 2539 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2540 | .deviceId(stylusDeviceId) |
| 2541 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2542 | .build()); |
| 2543 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2544 | WithCoords(101, 111))); |
| 2545 | |
| 2546 | // Touch continues to work too |
| 2547 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2548 | .deviceId(touchDeviceId) |
| 2549 | .pointer(PointerBuilder(0, ToolType::FINGER).x(148).y(149)) |
| 2550 | .build()); |
| 2551 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2552 | } |
| 2553 | |
| 2554 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2555 | * Two windows: a window on the left and a window on the right. |
| 2556 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2557 | * down. Then, on the left window, also place second touch pointer down. |
| 2558 | * This test tries to reproduce a crash. |
| 2559 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2560 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2561 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch_legacy) { |
| 2562 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2563 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2564 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2565 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2566 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2567 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2568 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2569 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2570 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2571 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2572 | mDispatcher->onWindowInfosChanged( |
| 2573 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2574 | |
| 2575 | const int32_t touchDeviceId = 4; |
| 2576 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2577 | |
| 2578 | // Start hovering over the left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2579 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2580 | .deviceId(mouseDeviceId) |
| 2581 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2582 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2583 | leftWindow->consumeMotionEvent( |
| 2584 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2585 | |
| 2586 | // Mouse down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2587 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2588 | .deviceId(mouseDeviceId) |
| 2589 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2590 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2591 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2592 | |
| 2593 | leftWindow->consumeMotionEvent( |
| 2594 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2595 | leftWindow->consumeMotionEvent( |
| 2596 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2597 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2598 | mDispatcher->notifyMotion( |
| 2599 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2600 | .deviceId(mouseDeviceId) |
| 2601 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2602 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2603 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2604 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2605 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2606 | |
| 2607 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2608 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2609 | .deviceId(touchDeviceId) |
| 2610 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2611 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2612 | leftWindow->assertNoEvents(); |
| 2613 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2614 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2615 | |
| 2616 | // Second touch pointer down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2617 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2618 | .deviceId(touchDeviceId) |
| 2619 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2620 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2621 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2622 | // Since this is now a new splittable pointer going down on the left window, and it's coming |
| 2623 | // from a different device, the current gesture in the left window (pointer down) should first |
| 2624 | // be canceled. |
| 2625 | leftWindow->consumeMotionEvent( |
| 2626 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2627 | leftWindow->consumeMotionEvent( |
| 2628 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2629 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2630 | // current implementation. |
| 2631 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2632 | rightWindow->consumeMotionEvent( |
| 2633 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2634 | |
| 2635 | leftWindow->assertNoEvents(); |
| 2636 | rightWindow->assertNoEvents(); |
| 2637 | } |
| 2638 | |
| 2639 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2640 | * Two windows: a window on the left and a window on the right. |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2641 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2642 | * down. Then, on the left window, also place second touch pointer down. |
| 2643 | * This test tries to reproduce a crash. |
| 2644 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2645 | */ |
| 2646 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch) { |
| 2647 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2648 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2649 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2650 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2651 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2652 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2653 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2654 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2655 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2656 | |
| 2657 | mDispatcher->onWindowInfosChanged( |
| 2658 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2659 | |
| 2660 | const int32_t touchDeviceId = 4; |
| 2661 | const int32_t mouseDeviceId = 6; |
| 2662 | |
| 2663 | // Start hovering over the left window |
| 2664 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2665 | .deviceId(mouseDeviceId) |
| 2666 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2667 | .build()); |
| 2668 | leftWindow->consumeMotionEvent( |
| 2669 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2670 | |
| 2671 | // Mouse down on left window |
| 2672 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2673 | .deviceId(mouseDeviceId) |
| 2674 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2675 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2676 | .build()); |
| 2677 | |
| 2678 | leftWindow->consumeMotionEvent( |
| 2679 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2680 | leftWindow->consumeMotionEvent( |
| 2681 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2682 | |
| 2683 | mDispatcher->notifyMotion( |
| 2684 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2685 | .deviceId(mouseDeviceId) |
| 2686 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2687 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2688 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2689 | .build()); |
| 2690 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2691 | |
| 2692 | // First touch pointer down on right window |
| 2693 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2694 | .deviceId(touchDeviceId) |
| 2695 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2696 | .build()); |
| 2697 | leftWindow->assertNoEvents(); |
| 2698 | |
| 2699 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2700 | |
| 2701 | // Second touch pointer down on left window |
| 2702 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2703 | .deviceId(touchDeviceId) |
| 2704 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2705 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2706 | .build()); |
| 2707 | // Since this is now a new splittable pointer going down on the left window, and it's coming |
| 2708 | // from a different device, it will be split and delivered to left window separately. |
| 2709 | leftWindow->consumeMotionEvent( |
| 2710 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2711 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2712 | // current implementation. |
| 2713 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2714 | rightWindow->consumeMotionEvent( |
| 2715 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2716 | |
| 2717 | leftWindow->assertNoEvents(); |
| 2718 | rightWindow->assertNoEvents(); |
| 2719 | } |
| 2720 | |
| 2721 | /** |
| 2722 | * Two windows: a window on the left and a window on the right. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2723 | * Mouse is hovered on the left window and stylus is hovered on the right window. |
| 2724 | */ |
| 2725 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHover) { |
| 2726 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2727 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2728 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2729 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2730 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2731 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2732 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2733 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2734 | |
| 2735 | mDispatcher->onWindowInfosChanged( |
| 2736 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2737 | |
| 2738 | const int32_t stylusDeviceId = 3; |
| 2739 | const int32_t mouseDeviceId = 6; |
| 2740 | |
| 2741 | // Start hovering over the left window |
| 2742 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2743 | .deviceId(mouseDeviceId) |
| 2744 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 2745 | .build()); |
| 2746 | leftWindow->consumeMotionEvent( |
| 2747 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2748 | |
| 2749 | // Stylus hovered on right window |
| 2750 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2751 | .deviceId(stylusDeviceId) |
| 2752 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(300).y(100)) |
| 2753 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2754 | rightWindow->consumeMotionEvent( |
| 2755 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2756 | |
| 2757 | // Subsequent HOVER_MOVE events are dispatched correctly. |
| 2758 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 2759 | .deviceId(mouseDeviceId) |
| 2760 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2761 | .build()); |
| 2762 | leftWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2763 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2764 | |
| 2765 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2766 | .deviceId(stylusDeviceId) |
| 2767 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(310).y(110)) |
| 2768 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2769 | rightWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2770 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2771 | |
| 2772 | leftWindow->assertNoEvents(); |
| 2773 | rightWindow->assertNoEvents(); |
| 2774 | } |
| 2775 | |
| 2776 | /** |
| 2777 | * Three windows: a window on the left and a window on the right. |
| 2778 | * And a spy window that's positioned above all of them. |
| 2779 | * Stylus down on the left window and remains down. Touch goes down on the right and remains down. |
| 2780 | * Check the stream that's received by the spy. |
| 2781 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2782 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy_legacy) { |
| 2783 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2784 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2785 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2786 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 2787 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2788 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 2789 | spyWindow->setTrustedOverlay(true); |
| 2790 | spyWindow->setSpy(true); |
| 2791 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2792 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2793 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2794 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2795 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2796 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2797 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2798 | |
| 2799 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2800 | |
| 2801 | mDispatcher->onWindowInfosChanged( |
| 2802 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2803 | |
| 2804 | const int32_t stylusDeviceId = 1; |
| 2805 | const int32_t touchDeviceId = 2; |
| 2806 | |
| 2807 | // Stylus down on the left window |
| 2808 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2809 | .deviceId(stylusDeviceId) |
| 2810 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2811 | .build()); |
| 2812 | leftWindow->consumeMotionEvent( |
| 2813 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2814 | spyWindow->consumeMotionEvent( |
| 2815 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2816 | |
| 2817 | // Touch down on the right window |
| 2818 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2819 | .deviceId(touchDeviceId) |
| 2820 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2821 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2822 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2823 | rightWindow->consumeMotionEvent( |
| 2824 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2825 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2826 | // Spy window does not receive touch events, because stylus events take precedence, and it |
| 2827 | // already has an active stylus gesture. |
| 2828 | |
| 2829 | // 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] | 2830 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2831 | .deviceId(stylusDeviceId) |
| 2832 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2833 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2834 | leftWindow->consumeMotionEvent( |
| 2835 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 2836 | spyWindow->consumeMotionEvent( |
| 2837 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2838 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2839 | // Further MOVE events keep going to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2840 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2841 | .deviceId(touchDeviceId) |
| 2842 | .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110)) |
| 2843 | .build()); |
| 2844 | rightWindow->consumeMotionEvent( |
| 2845 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2846 | |
| 2847 | spyWindow->assertNoEvents(); |
| 2848 | leftWindow->assertNoEvents(); |
| 2849 | rightWindow->assertNoEvents(); |
| 2850 | } |
| 2851 | |
| 2852 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2853 | * Three windows: a window on the left and a window on the right. |
| 2854 | * And a spy window that's positioned above all of them. |
| 2855 | * Stylus down on the left window and remains down. Touch goes down on the right and remains down. |
| 2856 | * Check the stream that's received by the spy. |
| 2857 | */ |
| 2858 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy) { |
| 2859 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2860 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2861 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2862 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 2863 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2864 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 2865 | spyWindow->setTrustedOverlay(true); |
| 2866 | spyWindow->setSpy(true); |
| 2867 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2868 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2869 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2870 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2871 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2872 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2873 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2874 | |
| 2875 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2876 | |
| 2877 | mDispatcher->onWindowInfosChanged( |
| 2878 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2879 | |
| 2880 | const int32_t stylusDeviceId = 1; |
| 2881 | const int32_t touchDeviceId = 2; |
| 2882 | |
| 2883 | // Stylus down on the left window |
| 2884 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2885 | .deviceId(stylusDeviceId) |
| 2886 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2887 | .build()); |
| 2888 | leftWindow->consumeMotionEvent( |
| 2889 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2890 | spyWindow->consumeMotionEvent( |
| 2891 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2892 | |
| 2893 | // Touch down on the right window |
| 2894 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2895 | .deviceId(touchDeviceId) |
| 2896 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2897 | .build()); |
| 2898 | leftWindow->assertNoEvents(); |
| 2899 | rightWindow->consumeMotionEvent( |
| 2900 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2901 | spyWindow->consumeMotionEvent( |
| 2902 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2903 | |
| 2904 | // Stylus movements continue. They should be delivered to the left window and to the spy window |
| 2905 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2906 | .deviceId(stylusDeviceId) |
| 2907 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2908 | .build()); |
| 2909 | leftWindow->consumeMotionEvent( |
| 2910 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 2911 | spyWindow->consumeMotionEvent( |
| 2912 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 2913 | |
| 2914 | // Further touch MOVE events keep going to the right window and to the spy |
| 2915 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2916 | .deviceId(touchDeviceId) |
| 2917 | .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110)) |
| 2918 | .build()); |
| 2919 | rightWindow->consumeMotionEvent( |
| 2920 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2921 | spyWindow->consumeMotionEvent( |
| 2922 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2923 | |
| 2924 | spyWindow->assertNoEvents(); |
| 2925 | leftWindow->assertNoEvents(); |
| 2926 | rightWindow->assertNoEvents(); |
| 2927 | } |
| 2928 | |
| 2929 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2930 | * Three windows: a window on the left, a window on the right, and a spy window positioned above |
| 2931 | * both. |
| 2932 | * Check hover in left window and touch down in the right window. |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2933 | * 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] | 2934 | * At the same time, left and right should be getting independent streams of hovering and touch, |
| 2935 | * respectively. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2936 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2937 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverBlocksTouchWithSpy) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2938 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2939 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2940 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2941 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 2942 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2943 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 2944 | spyWindow->setTrustedOverlay(true); |
| 2945 | spyWindow->setSpy(true); |
| 2946 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2947 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2948 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2949 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2950 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2951 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2952 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2953 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2954 | |
| 2955 | mDispatcher->onWindowInfosChanged( |
| 2956 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2957 | |
| 2958 | const int32_t stylusDeviceId = 1; |
| 2959 | const int32_t touchDeviceId = 2; |
| 2960 | |
| 2961 | // Stylus hover on the left window |
| 2962 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2963 | .deviceId(stylusDeviceId) |
| 2964 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2965 | .build()); |
| 2966 | leftWindow->consumeMotionEvent( |
| 2967 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2968 | spyWindow->consumeMotionEvent( |
| 2969 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2970 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2971 | // Touch down on the right window. Spy doesn't receive this touch because it already has |
| 2972 | // stylus hovering there. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2973 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2974 | .deviceId(touchDeviceId) |
| 2975 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2976 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2977 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2978 | spyWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2979 | rightWindow->consumeMotionEvent( |
| 2980 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2981 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2982 | // 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] | 2983 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2984 | .deviceId(stylusDeviceId) |
| 2985 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2986 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2987 | leftWindow->consumeMotionEvent( |
| 2988 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2989 | spyWindow->consumeMotionEvent( |
| 2990 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2991 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2992 | // Touch movements continue. They should be delivered to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2993 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2994 | .deviceId(touchDeviceId) |
| 2995 | .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101)) |
| 2996 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2997 | rightWindow->consumeMotionEvent( |
| 2998 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2999 | |
| 3000 | spyWindow->assertNoEvents(); |
| 3001 | leftWindow->assertNoEvents(); |
| 3002 | rightWindow->assertNoEvents(); |
| 3003 | } |
| 3004 | |
| 3005 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3006 | * Three windows: a window on the left, a window on the right, and a spy window positioned above |
| 3007 | * both. |
| 3008 | * Check hover in left window and touch down in the right window. |
| 3009 | * At first, spy should receive hover. Next, spy should receive touch. |
| 3010 | * At the same time, left and right should be getting independent streams of hovering and touch, |
| 3011 | * respectively. |
| 3012 | */ |
| 3013 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverDoesNotBlockTouchWithSpy) { |
| 3014 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3015 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3016 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3017 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 3018 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3019 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 3020 | spyWindow->setTrustedOverlay(true); |
| 3021 | spyWindow->setSpy(true); |
| 3022 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3023 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 3024 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3025 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3026 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3027 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 3028 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3029 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3030 | |
| 3031 | mDispatcher->onWindowInfosChanged( |
| 3032 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3033 | |
| 3034 | const int32_t stylusDeviceId = 1; |
| 3035 | const int32_t touchDeviceId = 2; |
| 3036 | |
| 3037 | // Stylus hover on the left window |
| 3038 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3039 | .deviceId(stylusDeviceId) |
| 3040 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 3041 | .build()); |
| 3042 | leftWindow->consumeMotionEvent( |
| 3043 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3044 | spyWindow->consumeMotionEvent( |
| 3045 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3046 | |
| 3047 | // Touch down on the right window. |
| 3048 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3049 | .deviceId(touchDeviceId) |
| 3050 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3051 | .build()); |
| 3052 | leftWindow->assertNoEvents(); |
| 3053 | spyWindow->consumeMotionEvent( |
| 3054 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3055 | rightWindow->consumeMotionEvent( |
| 3056 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3057 | |
| 3058 | // Stylus movements continue. They should be delivered to the left window and the spy. |
| 3059 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3060 | .deviceId(stylusDeviceId) |
| 3061 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 3062 | .build()); |
| 3063 | leftWindow->consumeMotionEvent( |
| 3064 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3065 | spyWindow->consumeMotionEvent( |
| 3066 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3067 | |
| 3068 | // Touch movements continue. They should be delivered to the right window and the spy |
| 3069 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3070 | .deviceId(touchDeviceId) |
| 3071 | .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101)) |
| 3072 | .build()); |
| 3073 | rightWindow->consumeMotionEvent( |
| 3074 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3075 | spyWindow->consumeMotionEvent( |
| 3076 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3077 | |
| 3078 | spyWindow->assertNoEvents(); |
| 3079 | leftWindow->assertNoEvents(); |
| 3080 | rightWindow->assertNoEvents(); |
| 3081 | } |
| 3082 | |
| 3083 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3084 | * On a single window, use two different devices: mouse and touch. |
| 3085 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 3086 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 3087 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 3088 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 3089 | * represent a new gesture. |
| 3090 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3091 | TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown_legacy) { |
| 3092 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3093 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3094 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3095 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3096 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3097 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3098 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3099 | |
| 3100 | const int32_t touchDeviceId = 4; |
| 3101 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3102 | |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3103 | // First touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3104 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3105 | .deviceId(touchDeviceId) |
| 3106 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3107 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3108 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3109 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3110 | .deviceId(touchDeviceId) |
| 3111 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3112 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3113 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3114 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3115 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3116 | .deviceId(touchDeviceId) |
| 3117 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3118 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3119 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3120 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3121 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3122 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 3123 | |
| 3124 | // Mouse down. The touch should be canceled |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3125 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3126 | .deviceId(mouseDeviceId) |
| 3127 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3128 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3129 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3130 | |
| 3131 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 3132 | WithPointerCount(1u))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3133 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3134 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3135 | mDispatcher->notifyMotion( |
| 3136 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3137 | .deviceId(mouseDeviceId) |
| 3138 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3139 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3140 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3141 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3142 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3143 | |
| 3144 | // Second touch pointer down. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3145 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3146 | .deviceId(touchDeviceId) |
| 3147 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3148 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3149 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3150 | // Since we already canceled this touch gesture, it will be ignored until a completely new |
| 3151 | // gesture is started. This is easier to implement than trying to keep track of the new pointer |
| 3152 | // and generating an ACTION_DOWN instead of ACTION_POINTER_DOWN. |
| 3153 | // However, mouse movements should continue to work. |
| 3154 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3155 | .deviceId(mouseDeviceId) |
| 3156 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3157 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110)) |
| 3158 | .build()); |
| 3159 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 3160 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3161 | window->assertNoEvents(); |
| 3162 | } |
| 3163 | |
| 3164 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3165 | * On a single window, use two different devices: mouse and touch. |
| 3166 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 3167 | * Mouse is clicked next, which should not interfere with the touch stream. |
| 3168 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is also |
| 3169 | * delivered correctly. |
| 3170 | */ |
| 3171 | TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown) { |
| 3172 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3173 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3174 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3175 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3176 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3177 | |
| 3178 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3179 | |
| 3180 | const int32_t touchDeviceId = 4; |
| 3181 | const int32_t mouseDeviceId = 6; |
| 3182 | |
| 3183 | // First touch pointer down |
| 3184 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3185 | .deviceId(touchDeviceId) |
| 3186 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3187 | .build()); |
| 3188 | // Second touch pointer down |
| 3189 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3190 | .deviceId(touchDeviceId) |
| 3191 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3192 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3193 | .build()); |
| 3194 | // First touch pointer lifts. The second one remains down |
| 3195 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3196 | .deviceId(touchDeviceId) |
| 3197 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3198 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3199 | .build()); |
| 3200 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3201 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3202 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 3203 | |
| 3204 | // Mouse down |
| 3205 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3206 | .deviceId(mouseDeviceId) |
| 3207 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3208 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3209 | .build()); |
| 3210 | |
| 3211 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3212 | |
| 3213 | mDispatcher->notifyMotion( |
| 3214 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3215 | .deviceId(mouseDeviceId) |
| 3216 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3217 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3218 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3219 | .build()); |
| 3220 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3221 | |
| 3222 | // Second touch pointer down. |
| 3223 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3224 | .deviceId(touchDeviceId) |
| 3225 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3226 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3227 | .build()); |
| 3228 | window->consumeMotionEvent(AllOf(WithMotionAction(POINTER_0_DOWN), WithDeviceId(touchDeviceId), |
| 3229 | WithPointerCount(2u))); |
| 3230 | |
| 3231 | // Mouse movements should continue to work |
| 3232 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3233 | .deviceId(mouseDeviceId) |
| 3234 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3235 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110)) |
| 3236 | .build()); |
| 3237 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 3238 | |
| 3239 | window->assertNoEvents(); |
| 3240 | } |
| 3241 | |
| 3242 | /** |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3243 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels |
| 3244 | * the injected event. |
| 3245 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3246 | TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent_legacy) { |
| 3247 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3248 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3249 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3250 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3251 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3252 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3253 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3254 | |
| 3255 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3256 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 3257 | // completion. |
| 3258 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3259 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3260 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3261 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3262 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3263 | .build())); |
| 3264 | window->consumeMotionEvent( |
| 3265 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3266 | |
| 3267 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer |
| 3268 | // should be canceled and the new gesture should take over. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3269 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3270 | .deviceId(touchDeviceId) |
| 3271 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3272 | .build()); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3273 | |
| 3274 | window->consumeMotionEvent( |
| 3275 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3276 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3277 | } |
| 3278 | |
| 3279 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3280 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event runs |
| 3281 | * parallel to the injected event. |
| 3282 | */ |
| 3283 | TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent) { |
| 3284 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3285 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3286 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3287 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3288 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3289 | |
| 3290 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3291 | |
| 3292 | const int32_t touchDeviceId = 4; |
| 3293 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 3294 | // completion. |
| 3295 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3296 | injectMotionEvent(*mDispatcher, |
| 3297 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3298 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
| 3299 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 3300 | .build())); |
| 3301 | window->consumeMotionEvent( |
| 3302 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3303 | |
| 3304 | // Now a real touch comes. The injected pointer will remain, and the new gesture will also be |
| 3305 | // allowed through. |
| 3306 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3307 | .deviceId(touchDeviceId) |
| 3308 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3309 | .build()); |
| 3310 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3311 | } |
| 3312 | |
| 3313 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3314 | * This test is similar to the test above, but the sequence of injected events is different. |
| 3315 | * |
| 3316 | * Two windows: a window on the left and a window on the right. |
| 3317 | * Mouse is hovered over the left window. |
| 3318 | * Next, we tap on the left window, where the cursor was last seen. |
| 3319 | * |
| 3320 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 3321 | * the left window. |
| 3322 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 3323 | * window (first), and then another on the left window (second). |
| 3324 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 3325 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 3326 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3327 | TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch_legacy) { |
| 3328 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3329 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3330 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 3331 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3332 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3333 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3334 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 3335 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3336 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3337 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3338 | mDispatcher->onWindowInfosChanged( |
| 3339 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3340 | |
| 3341 | const int32_t mouseDeviceId = 6; |
| 3342 | const int32_t touchDeviceId = 4; |
| 3343 | // Hover over the left window. Keep the cursor there. |
| 3344 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3345 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3346 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3347 | AINPUT_SOURCE_MOUSE) |
| 3348 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3349 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3350 | .build())); |
| 3351 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3352 | |
| 3353 | // Tap on left window |
| 3354 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3355 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3356 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3357 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3358 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3359 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3360 | .build())); |
| 3361 | |
| 3362 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3363 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3364 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3365 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3366 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3367 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3368 | .build())); |
| 3369 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3370 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3371 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 3372 | |
| 3373 | // First finger down on right window |
| 3374 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3375 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3376 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3377 | 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)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3380 | .build())); |
| 3381 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3382 | |
| 3383 | // Second finger down on the left window |
| 3384 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3385 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3386 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3387 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3388 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3389 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3390 | .build())); |
| 3391 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3392 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 3393 | |
| 3394 | // No more events |
| 3395 | leftWindow->assertNoEvents(); |
| 3396 | rightWindow->assertNoEvents(); |
| 3397 | } |
| 3398 | |
| 3399 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3400 | * This test is similar to the test above, but the sequence of injected events is different. |
| 3401 | * |
| 3402 | * Two windows: a window on the left and a window on the right. |
| 3403 | * Mouse is hovered over the left window. |
| 3404 | * Next, we tap on the left window, where the cursor was last seen. |
| 3405 | * |
| 3406 | * After that, we send one finger down onto the right window, and then a second finger down onto |
| 3407 | * the left window. |
| 3408 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 3409 | * window (first), and then another on the left window (second). |
| 3410 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 3411 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 3412 | */ |
| 3413 | TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch) { |
| 3414 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3415 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3416 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 3417 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3418 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3419 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3420 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 3421 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3422 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3423 | |
| 3424 | mDispatcher->onWindowInfosChanged( |
| 3425 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3426 | |
| 3427 | const int32_t mouseDeviceId = 6; |
| 3428 | const int32_t touchDeviceId = 4; |
| 3429 | // Hover over the left window. Keep the cursor there. |
| 3430 | mDispatcher->notifyMotion( |
| 3431 | MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3432 | .deviceId(mouseDeviceId) |
| 3433 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 3434 | .build()); |
| 3435 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3436 | |
| 3437 | // Tap on left window |
| 3438 | mDispatcher->notifyMotion( |
| 3439 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3440 | .deviceId(touchDeviceId) |
| 3441 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3442 | .build()); |
| 3443 | |
| 3444 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3445 | .deviceId(touchDeviceId) |
| 3446 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3447 | .build()); |
| 3448 | leftWindow->consumeMotionEvent( |
| 3449 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3450 | leftWindow->consumeMotionEvent( |
| 3451 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithDeviceId(touchDeviceId))); |
| 3452 | |
| 3453 | // First finger down on right window |
| 3454 | mDispatcher->notifyMotion( |
| 3455 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3456 | .deviceId(touchDeviceId) |
| 3457 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3458 | .build()); |
| 3459 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3460 | |
| 3461 | // Second finger down on the left window |
| 3462 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3463 | .deviceId(touchDeviceId) |
| 3464 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3465 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 3466 | .build()); |
| 3467 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3468 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 3469 | |
| 3470 | // No more events |
| 3471 | leftWindow->assertNoEvents(); |
| 3472 | rightWindow->assertNoEvents(); |
| 3473 | } |
| 3474 | |
| 3475 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3476 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 3477 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 3478 | * touch is gone, stylus hovering should start working again. |
| 3479 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3480 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverIgnoresTouchTap) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3481 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3482 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3483 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3484 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3485 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3486 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3487 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3488 | |
| 3489 | const int32_t stylusDeviceId = 5; |
| 3490 | const int32_t touchDeviceId = 4; |
| 3491 | // Start hovering with stylus |
| 3492 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3493 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3494 | MotionEventBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3495 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3496 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3497 | .build())); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3498 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3499 | |
| 3500 | // Finger down on the window |
| 3501 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3502 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3503 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3504 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3505 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3506 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3507 | // The touch device should be ignored! |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3508 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3509 | // Continue hovering with stylus. |
| 3510 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3511 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3512 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3513 | AINPUT_SOURCE_STYLUS) |
| 3514 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3515 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3516 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3517 | // Hovers continue to work |
| 3518 | window->consumeMotionEvent( |
| 3519 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3520 | |
| 3521 | // Lift up the finger |
| 3522 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3523 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3524 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3525 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3526 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3527 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3528 | .build())); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3529 | |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3530 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3531 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3532 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3533 | AINPUT_SOURCE_STYLUS) |
| 3534 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3535 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3536 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3537 | window->consumeMotionEvent( |
| 3538 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3539 | window->assertNoEvents(); |
| 3540 | } |
| 3541 | |
| 3542 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3543 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 3544 | * While the touch is down, hovering from the stylus is not affected. After the touch is gone, |
| 3545 | * check that the stylus hovering continues to work. |
| 3546 | */ |
| 3547 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverWithTouchTap) { |
| 3548 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3549 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3550 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3551 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3552 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3553 | |
| 3554 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3555 | |
| 3556 | const int32_t stylusDeviceId = 5; |
| 3557 | const int32_t touchDeviceId = 4; |
| 3558 | // Start hovering with stylus |
| 3559 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3560 | .deviceId(stylusDeviceId) |
| 3561 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3562 | .build()); |
| 3563 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3564 | |
| 3565 | // Finger down on the window |
| 3566 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3567 | .deviceId(touchDeviceId) |
| 3568 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3569 | .build()); |
| 3570 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3571 | |
| 3572 | // Continue hovering with stylus. |
| 3573 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3574 | .deviceId(stylusDeviceId) |
| 3575 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60)) |
| 3576 | .build()); |
| 3577 | // Hovers continue to work |
| 3578 | window->consumeMotionEvent( |
| 3579 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3580 | |
| 3581 | // Lift up the finger |
| 3582 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3583 | .deviceId(touchDeviceId) |
| 3584 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3585 | .build()); |
| 3586 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDeviceId(touchDeviceId))); |
| 3587 | |
| 3588 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3589 | .deviceId(stylusDeviceId) |
| 3590 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70)) |
| 3591 | .build()); |
| 3592 | window->consumeMotionEvent( |
| 3593 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3594 | window->assertNoEvents(); |
| 3595 | } |
| 3596 | |
| 3597 | /** |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3598 | * If stylus is down anywhere on the screen, then touches should not be delivered to windows that |
| 3599 | * have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 3600 | * |
| 3601 | * Two windows: one on the left and one on the right. |
| 3602 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 3603 | * Stylus down on the left window, and then touch down on the right window. |
| 3604 | * Check that the right window doesn't get touches while the stylus is down on the left window. |
| 3605 | */ |
| 3606 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusDownBlocksTouch) { |
| 3607 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3608 | sp<FakeWindowHandle> leftWindow = |
| 3609 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3610 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3611 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3612 | |
| 3613 | sp<FakeWindowHandle> sbtRightWindow = |
| 3614 | sp<FakeWindowHandle>::make(application, mDispatcher, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3615 | "Stylus blocks touch (right) window", |
| 3616 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3617 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3618 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 3619 | |
| 3620 | mDispatcher->onWindowInfosChanged( |
| 3621 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 3622 | |
| 3623 | const int32_t stylusDeviceId = 5; |
| 3624 | const int32_t touchDeviceId = 4; |
| 3625 | |
| 3626 | // Stylus down in the left window |
| 3627 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3628 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 3629 | .deviceId(stylusDeviceId) |
| 3630 | .build()); |
| 3631 | leftWindow->consumeMotionEvent( |
| 3632 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 3633 | |
| 3634 | // Finger tap on the right window |
| 3635 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3636 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3637 | .deviceId(touchDeviceId) |
| 3638 | .build()); |
| 3639 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3640 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3641 | .deviceId(touchDeviceId) |
| 3642 | .build()); |
| 3643 | |
| 3644 | // The touch should be blocked, because stylus is down somewhere else on screen! |
| 3645 | sbtRightWindow->assertNoEvents(); |
| 3646 | |
| 3647 | // Continue stylus motion, and ensure it's not impacted. |
| 3648 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 3649 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3650 | .deviceId(stylusDeviceId) |
| 3651 | .build()); |
| 3652 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3653 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3654 | .deviceId(stylusDeviceId) |
| 3655 | .build()); |
| 3656 | leftWindow->consumeMotionEvent( |
| 3657 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 3658 | leftWindow->consumeMotionEvent( |
| 3659 | AllOf(WithMotionAction(ACTION_UP), WithDeviceId(stylusDeviceId))); |
| 3660 | |
| 3661 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3662 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3663 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3664 | .deviceId(touchDeviceId) |
| 3665 | .build()); |
| 3666 | sbtRightWindow->consumeMotionEvent( |
| 3667 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3668 | } |
| 3669 | |
| 3670 | /** |
| 3671 | * If stylus is hovering anywhere on the screen, then touches should not be delivered to windows |
| 3672 | * that have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 3673 | * |
| 3674 | * Two windows: one on the left and one on the right. |
| 3675 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 3676 | * Stylus hover on the left window, and then touch down on the right window. |
| 3677 | * Check that the right window doesn't get touches while the stylus is hovering on the left window. |
| 3678 | */ |
| 3679 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusHoverBlocksTouch) { |
| 3680 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3681 | sp<FakeWindowHandle> leftWindow = |
| 3682 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3683 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3684 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3685 | |
| 3686 | sp<FakeWindowHandle> sbtRightWindow = |
| 3687 | sp<FakeWindowHandle>::make(application, mDispatcher, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3688 | "Stylus blocks touch (right) window", |
| 3689 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3690 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3691 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 3692 | |
| 3693 | mDispatcher->onWindowInfosChanged( |
| 3694 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 3695 | |
| 3696 | const int32_t stylusDeviceId = 5; |
| 3697 | const int32_t touchDeviceId = 4; |
| 3698 | |
| 3699 | // Stylus hover in the left window |
| 3700 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3701 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 3702 | .deviceId(stylusDeviceId) |
| 3703 | .build()); |
| 3704 | leftWindow->consumeMotionEvent( |
| 3705 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3706 | |
| 3707 | // Finger tap on the right window |
| 3708 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3709 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3710 | .deviceId(touchDeviceId) |
| 3711 | .build()); |
| 3712 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3713 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3714 | .deviceId(touchDeviceId) |
| 3715 | .build()); |
| 3716 | |
| 3717 | // The touch should be blocked, because stylus is hovering somewhere else on screen! |
| 3718 | sbtRightWindow->assertNoEvents(); |
| 3719 | |
| 3720 | // Continue stylus motion, and ensure it's not impacted. |
| 3721 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3722 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3723 | .deviceId(stylusDeviceId) |
| 3724 | .build()); |
| 3725 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3726 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3727 | .deviceId(stylusDeviceId) |
| 3728 | .build()); |
| 3729 | leftWindow->consumeMotionEvent( |
| 3730 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3731 | leftWindow->consumeMotionEvent( |
| 3732 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId))); |
| 3733 | |
| 3734 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3735 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3736 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3737 | .deviceId(touchDeviceId) |
| 3738 | .build()); |
| 3739 | sbtRightWindow->consumeMotionEvent( |
| 3740 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3741 | } |
| 3742 | |
| 3743 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3744 | * A spy window above a window with no input channel. |
| 3745 | * Start hovering with a stylus device, and then tap with it. |
| 3746 | * Ensure spy window receives the entire sequence. |
| 3747 | */ |
| 3748 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 3749 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3750 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 3751 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3752 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3753 | spyWindow->setTrustedOverlay(true); |
| 3754 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3755 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3756 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3757 | window->setNoInputChannel(true); |
| 3758 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3759 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3760 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3761 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3762 | // Start hovering with stylus |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3763 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3764 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3765 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3766 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3767 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3768 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3769 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3770 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3771 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3772 | |
| 3773 | // Stylus touches down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3774 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3775 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3776 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3777 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3778 | |
| 3779 | // Stylus goes up |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3780 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3781 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3782 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3783 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 3784 | |
| 3785 | // Again hover |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3786 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3787 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3788 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3789 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3790 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3791 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3792 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3793 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3794 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3795 | |
| 3796 | // No more events |
| 3797 | spyWindow->assertNoEvents(); |
| 3798 | window->assertNoEvents(); |
| 3799 | } |
| 3800 | |
| 3801 | /** |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 3802 | * A stale stylus HOVER_EXIT event is injected. Since it's a stale event, it should generally be |
| 3803 | * rejected. But since we already have an ongoing gesture, this event should be processed. |
| 3804 | * This prevents inconsistent events being handled inside the dispatcher. |
| 3805 | */ |
| 3806 | TEST_F(InputDispatcherTest, StaleStylusHoverGestureIsComplete) { |
| 3807 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3808 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3809 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3810 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 3811 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3812 | |
| 3813 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3814 | |
| 3815 | // Start hovering with stylus |
| 3816 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3817 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3818 | .build()); |
| 3819 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3820 | |
| 3821 | NotifyMotionArgs hoverExit = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3822 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3823 | .build(); |
| 3824 | // Make this 'hoverExit' event stale |
| 3825 | mFakePolicy->setStaleEventTimeout(100ms); |
| 3826 | std::this_thread::sleep_for(100ms); |
| 3827 | |
| 3828 | // It shouldn't be dropped by the dispatcher, even though it's stale. |
| 3829 | mDispatcher->notifyMotion(hoverExit); |
| 3830 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3831 | |
| 3832 | // Stylus starts hovering again! There should be no crash. |
| 3833 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3834 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(51)) |
| 3835 | .build()); |
| 3836 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3837 | } |
| 3838 | |
| 3839 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3840 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 3841 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 3842 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 3843 | * While the mouse is down, new move events from the touch device should be ignored. |
| 3844 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3845 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove_legacy) { |
| 3846 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3847 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3848 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 3849 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3850 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3851 | spyWindow->setTrustedOverlay(true); |
| 3852 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3853 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3854 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3855 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3856 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3857 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3858 | |
| 3859 | const int32_t mouseDeviceId = 7; |
| 3860 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3861 | |
| 3862 | // Hover a bit with mouse first |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3863 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3864 | .deviceId(mouseDeviceId) |
| 3865 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3866 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3867 | spyWindow->consumeMotionEvent( |
| 3868 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3869 | window->consumeMotionEvent( |
| 3870 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3871 | |
| 3872 | // Start touching |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3873 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3874 | .deviceId(touchDeviceId) |
| 3875 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3876 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3877 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3878 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3879 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3880 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3881 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3882 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3883 | .deviceId(touchDeviceId) |
| 3884 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 3885 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3886 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3887 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3888 | |
| 3889 | // Pilfer the stream |
| 3890 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3891 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 3892 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3893 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3894 | .deviceId(touchDeviceId) |
| 3895 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 3896 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3897 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3898 | |
| 3899 | // Mouse down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3900 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3901 | .deviceId(mouseDeviceId) |
| 3902 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3903 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3904 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3905 | |
| 3906 | spyWindow->consumeMotionEvent( |
| 3907 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 3908 | spyWindow->consumeMotionEvent( |
| 3909 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3910 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3911 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3912 | mDispatcher->notifyMotion( |
| 3913 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3914 | .deviceId(mouseDeviceId) |
| 3915 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3916 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3917 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3918 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3919 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3920 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3921 | |
| 3922 | // Mouse move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3923 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3924 | .deviceId(mouseDeviceId) |
| 3925 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3926 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 3927 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3928 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3929 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3930 | |
| 3931 | // Touch move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3932 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3933 | .deviceId(touchDeviceId) |
| 3934 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 3935 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3936 | |
| 3937 | // No more events |
| 3938 | spyWindow->assertNoEvents(); |
| 3939 | window->assertNoEvents(); |
| 3940 | } |
| 3941 | |
| 3942 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3943 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 3944 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 3945 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 3946 | * While the mouse is down, new move events from the touch device should continue to work. |
| 3947 | */ |
| 3948 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 3949 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3950 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3951 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 3952 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3953 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3954 | spyWindow->setTrustedOverlay(true); |
| 3955 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3956 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3957 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3958 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3959 | |
| 3960 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 3961 | |
| 3962 | const int32_t mouseDeviceId = 7; |
| 3963 | const int32_t touchDeviceId = 4; |
| 3964 | |
| 3965 | // Hover a bit with mouse first |
| 3966 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3967 | .deviceId(mouseDeviceId) |
| 3968 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3969 | .build()); |
| 3970 | spyWindow->consumeMotionEvent( |
| 3971 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3972 | window->consumeMotionEvent( |
| 3973 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3974 | |
| 3975 | // Start touching |
| 3976 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3977 | .deviceId(touchDeviceId) |
| 3978 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3979 | .build()); |
| 3980 | |
| 3981 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3982 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3983 | |
| 3984 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3985 | .deviceId(touchDeviceId) |
| 3986 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 3987 | .build()); |
| 3988 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3989 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3990 | |
| 3991 | // Pilfer the stream |
| 3992 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3993 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 3994 | // Hover is not pilfered! Only touch. |
| 3995 | |
| 3996 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3997 | .deviceId(touchDeviceId) |
| 3998 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 3999 | .build()); |
| 4000 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 4001 | |
| 4002 | // Mouse down |
| 4003 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4004 | .deviceId(mouseDeviceId) |
| 4005 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4006 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 4007 | .build()); |
| 4008 | |
| 4009 | spyWindow->consumeMotionEvent( |
| 4010 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 4011 | spyWindow->consumeMotionEvent( |
| 4012 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 4013 | window->consumeMotionEvent( |
| 4014 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 4015 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 4016 | |
| 4017 | mDispatcher->notifyMotion( |
| 4018 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 4019 | .deviceId(mouseDeviceId) |
| 4020 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4021 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4022 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 4023 | .build()); |
| 4024 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4025 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4026 | |
| 4027 | // Mouse move! |
| 4028 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4029 | .deviceId(mouseDeviceId) |
| 4030 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4031 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 4032 | .build()); |
| 4033 | spyWindow->consumeMotionEvent( |
| 4034 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 4035 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 4036 | |
| 4037 | // Touch move! |
| 4038 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 4039 | .deviceId(touchDeviceId) |
| 4040 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 4041 | .build()); |
| 4042 | spyWindow->consumeMotionEvent( |
| 4043 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 4044 | |
| 4045 | // No more events |
| 4046 | spyWindow->assertNoEvents(); |
| 4047 | window->assertNoEvents(); |
| 4048 | } |
| 4049 | |
| 4050 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4051 | * On the display, have a single window, and also an area where there's no window. |
| 4052 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 4053 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 4054 | */ |
| 4055 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 4056 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4057 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4058 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4059 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4060 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4061 | |
| 4062 | // Touch down on the empty space |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4063 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4064 | |
| 4065 | mDispatcher->waitForIdle(); |
| 4066 | window->assertNoEvents(); |
| 4067 | |
| 4068 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4069 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4070 | mDispatcher->waitForIdle(); |
| 4071 | window->consumeMotionDown(); |
| 4072 | } |
| 4073 | |
| 4074 | /** |
| 4075 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 4076 | * non-touchable window. |
| 4077 | */ |
| 4078 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 4079 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4080 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4081 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4082 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 4083 | window1->setTouchable(false); |
| 4084 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4085 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4086 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 4087 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4088 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4089 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4090 | // Touch down on the non-touchable window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4091 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4092 | |
| 4093 | mDispatcher->waitForIdle(); |
| 4094 | window1->assertNoEvents(); |
| 4095 | window2->assertNoEvents(); |
| 4096 | |
| 4097 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4098 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4099 | mDispatcher->waitForIdle(); |
| 4100 | window2->consumeMotionDown(); |
| 4101 | } |
| 4102 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4103 | /** |
| 4104 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 4105 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 4106 | */ |
| 4107 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 4108 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4109 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4110 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4111 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 4112 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4113 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4114 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 4115 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4116 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4117 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4118 | // Touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4119 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4120 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4121 | |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4122 | const std::unique_ptr<MotionEvent> firstDown = |
| 4123 | window1->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 4124 | ASSERT_EQ(firstDown->getDownTime(), firstDown->getEventTime()); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4125 | window2->assertNoEvents(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4126 | |
| 4127 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4128 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4129 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4130 | |
| 4131 | const std::unique_ptr<MotionEvent> secondDown = |
| 4132 | window2->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 4133 | ASSERT_EQ(secondDown->getDownTime(), secondDown->getEventTime()); |
| 4134 | ASSERT_NE(firstDown->getDownTime(), secondDown->getDownTime()); |
| 4135 | // We currently send MOVE events to all windows receiving a split touch when there is any change |
| 4136 | // in the touch state, even when none of the pointers in the split window actually moved. |
| 4137 | // Document this behavior in the test. |
| 4138 | window1->consumeMotionMove(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4139 | |
| 4140 | // Now move the pointer on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4141 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4142 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4143 | |
| 4144 | window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime())); |
| 4145 | window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime())); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4146 | |
| 4147 | // Now add new touch down on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4148 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4149 | mDispatcher->waitForIdle(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4150 | |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4151 | window2->consumeMotionEvent( |
| 4152 | AllOf(WithMotionAction(POINTER_1_DOWN), WithDownTime(secondDown->getDownTime()))); |
| 4153 | window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime())); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4154 | |
| 4155 | // Now move the pointer on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4156 | mDispatcher->notifyMotion( |
| 4157 | generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4158 | mDispatcher->waitForIdle(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4159 | |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4160 | window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime())); |
| 4161 | window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime())); |
| 4162 | |
| 4163 | // Now add new touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4164 | mDispatcher->notifyMotion( |
| 4165 | generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4166 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4167 | |
| 4168 | window1->consumeMotionEvent( |
| 4169 | AllOf(WithMotionAction(POINTER_1_DOWN), WithDownTime(firstDown->getDownTime()))); |
| 4170 | window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime())); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4171 | } |
| 4172 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4173 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4174 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4175 | sp<FakeWindowHandle> windowLeft = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 4176 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4177 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4178 | sp<FakeWindowHandle> windowRight = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 4179 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4180 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4181 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4182 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4183 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4184 | mDispatcher->onWindowInfosChanged( |
| 4185 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4186 | |
| 4187 | // 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] | 4188 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4189 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4190 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4191 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4192 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4193 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4194 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4195 | |
| 4196 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4197 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4198 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4199 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4200 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4201 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4202 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4203 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 4204 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4205 | |
| 4206 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4207 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4208 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4209 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4210 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4211 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4212 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4213 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4214 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4215 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4216 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4217 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4218 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 4219 | AINPUT_SOURCE_MOUSE) |
| 4220 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4221 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4222 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4223 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4224 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4225 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4226 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4227 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4228 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 4229 | AINPUT_SOURCE_MOUSE) |
| 4230 | .buttonState(0) |
| 4231 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4232 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4233 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4234 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4235 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4236 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4237 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4238 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4239 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4240 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4241 | .build())); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4242 | windowLeft->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4243 | |
| 4244 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4245 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4246 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4247 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4248 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4249 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4250 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4251 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4252 | |
| 4253 | // No more events |
| 4254 | windowLeft->assertNoEvents(); |
| 4255 | windowRight->assertNoEvents(); |
| 4256 | } |
| 4257 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4258 | /** |
| 4259 | * Put two fingers down (and don't release them) and click the mouse button. |
| 4260 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 4261 | * currently active gesture should be canceled, and the new one should proceed. |
| 4262 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4263 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick_legacy) { |
| 4264 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4265 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4266 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4267 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4268 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4269 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4270 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4271 | |
| 4272 | const int32_t touchDeviceId = 4; |
| 4273 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4274 | |
| 4275 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4276 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4277 | .deviceId(touchDeviceId) |
| 4278 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4279 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4280 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4281 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4282 | .deviceId(touchDeviceId) |
| 4283 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4284 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 4285 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4286 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4287 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4288 | |
| 4289 | // Inject a series of mouse events for a mouse click |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4290 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4291 | .deviceId(mouseDeviceId) |
| 4292 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4293 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4294 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4295 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 4296 | WithPointerCount(2u))); |
| 4297 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 4298 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4299 | mDispatcher->notifyMotion( |
| 4300 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 4301 | .deviceId(mouseDeviceId) |
| 4302 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4303 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4304 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4305 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4306 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4307 | |
| 4308 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 4309 | // already canceled gesture, it should be ignored. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4310 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 4311 | .deviceId(touchDeviceId) |
| 4312 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 4313 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 4314 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4315 | window->assertNoEvents(); |
| 4316 | } |
| 4317 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4318 | /** |
| 4319 | * Put two fingers down (and don't release them) and click the mouse button. |
| 4320 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 4321 | * currently active gesture should not be canceled, and the new one should proceed in parallel. |
| 4322 | */ |
| 4323 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 4324 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 4325 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4326 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4327 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4328 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4329 | |
| 4330 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 4331 | |
| 4332 | const int32_t touchDeviceId = 4; |
| 4333 | const int32_t mouseDeviceId = 6; |
| 4334 | |
| 4335 | // Two pointers down |
| 4336 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4337 | .deviceId(touchDeviceId) |
| 4338 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4339 | .build()); |
| 4340 | |
| 4341 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4342 | .deviceId(touchDeviceId) |
| 4343 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4344 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 4345 | .build()); |
| 4346 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4347 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4348 | |
| 4349 | // Send a series of mouse events for a mouse click |
| 4350 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4351 | .deviceId(mouseDeviceId) |
| 4352 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4353 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4354 | .build()); |
| 4355 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 4356 | |
| 4357 | mDispatcher->notifyMotion( |
| 4358 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 4359 | .deviceId(mouseDeviceId) |
| 4360 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4361 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4362 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4363 | .build()); |
| 4364 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4365 | |
| 4366 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 4367 | // already active gesture, it should be sent normally. |
| 4368 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 4369 | .deviceId(touchDeviceId) |
| 4370 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 4371 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 4372 | .build()); |
| 4373 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 4374 | window->assertNoEvents(); |
| 4375 | } |
| 4376 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4377 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 4378 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4379 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4380 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 4381 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4382 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4383 | spyWindow->setTrustedOverlay(true); |
| 4384 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4385 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4386 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4387 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4388 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4389 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4390 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4391 | |
| 4392 | // Send mouse cursor to the window |
| 4393 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4394 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4395 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4396 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4397 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4398 | .build())); |
| 4399 | |
| 4400 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4401 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4402 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4403 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4404 | |
| 4405 | window->assertNoEvents(); |
| 4406 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4407 | } |
| 4408 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4409 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows_legacy) { |
| 4410 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4411 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4412 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4413 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 4414 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4415 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4416 | spyWindow->setTrustedOverlay(true); |
| 4417 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4418 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4419 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4420 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4421 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4422 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4423 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4424 | |
| 4425 | // Send mouse cursor to the window |
| 4426 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4427 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4428 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4429 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4430 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4431 | .build())); |
| 4432 | |
| 4433 | // Move mouse cursor |
| 4434 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4435 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4436 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4437 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4438 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4439 | .build())); |
| 4440 | |
| 4441 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4442 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4443 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4444 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4445 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4446 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4447 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4448 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4449 | // Touch down on the window |
| 4450 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4451 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4452 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 4453 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4454 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4455 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4456 | .build())); |
| 4457 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4458 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4459 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4460 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4461 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4462 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4463 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4464 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4465 | |
| 4466 | // pilfer the motion, retaining the gesture on the spy window. |
| 4467 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 4468 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 4469 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4470 | |
| 4471 | // Touch UP on the window |
| 4472 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4473 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4474 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 4475 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4476 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4477 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4478 | .build())); |
| 4479 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4480 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4481 | |
| 4482 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 4483 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 4484 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 4485 | |
| 4486 | // One more tap - DOWN |
| 4487 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4488 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4489 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 4490 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4491 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4492 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4493 | .build())); |
| 4494 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4495 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4496 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4497 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4498 | |
| 4499 | // Touch UP on the window |
| 4500 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4501 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4502 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 4503 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4504 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4505 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4506 | .build())); |
| 4507 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4508 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4509 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4510 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4511 | |
| 4512 | window->assertNoEvents(); |
| 4513 | spyWindow->assertNoEvents(); |
| 4514 | } |
| 4515 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4516 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 4517 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 4518 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4519 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4520 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 4521 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4522 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4523 | spyWindow->setTrustedOverlay(true); |
| 4524 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4525 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4526 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4527 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4528 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4529 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4530 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 4531 | |
| 4532 | // Send mouse cursor to the window |
| 4533 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4534 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 4535 | .build()); |
| 4536 | |
| 4537 | // Move mouse cursor |
| 4538 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4539 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 4540 | .build()); |
| 4541 | |
| 4542 | window->consumeMotionEvent( |
| 4543 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4544 | spyWindow->consumeMotionEvent( |
| 4545 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4546 | window->consumeMotionEvent( |
| 4547 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4548 | spyWindow->consumeMotionEvent( |
| 4549 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4550 | // Touch down on the window |
| 4551 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4552 | .deviceId(SECOND_DEVICE_ID) |
| 4553 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
| 4554 | .build()); |
| 4555 | window->consumeMotionEvent( |
| 4556 | AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4557 | spyWindow->consumeMotionEvent( |
| 4558 | AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4559 | |
| 4560 | // pilfer the motion, retaining the gesture on the spy window. |
| 4561 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 4562 | window->consumeMotionEvent( |
| 4563 | AllOf(WithMotionAction(ACTION_CANCEL), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4564 | // Mouse hover is not pilfered |
| 4565 | |
| 4566 | // Touch UP on the window |
| 4567 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4568 | .deviceId(SECOND_DEVICE_ID) |
| 4569 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
| 4570 | .build()); |
| 4571 | spyWindow->consumeMotionEvent( |
| 4572 | AllOf(WithMotionAction(ACTION_UP), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4573 | |
| 4574 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 4575 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 4576 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 4577 | |
| 4578 | // One more tap - DOWN |
| 4579 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4580 | .deviceId(SECOND_DEVICE_ID) |
| 4581 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
| 4582 | .build()); |
| 4583 | window->consumeMotionEvent( |
| 4584 | AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4585 | spyWindow->consumeMotionEvent( |
| 4586 | AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4587 | |
| 4588 | // Touch UP on the window |
| 4589 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4590 | .deviceId(SECOND_DEVICE_ID) |
| 4591 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
| 4592 | .build()); |
| 4593 | window->consumeMotionEvent( |
| 4594 | AllOf(WithMotionAction(ACTION_UP), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4595 | spyWindow->consumeMotionEvent( |
| 4596 | AllOf(WithMotionAction(ACTION_UP), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4597 | |
| 4598 | // Mouse movement continues normally as well |
| 4599 | // Move mouse cursor |
| 4600 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4601 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(120).y(130)) |
| 4602 | .build()); |
| 4603 | window->consumeMotionEvent( |
| 4604 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4605 | spyWindow->consumeMotionEvent( |
| 4606 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4607 | |
| 4608 | window->assertNoEvents(); |
| 4609 | spyWindow->assertNoEvents(); |
| 4610 | } |
| 4611 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4612 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 4613 | // directly in this test. |
| 4614 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4615 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4616 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4617 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4618 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4619 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4620 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4621 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4622 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4623 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4624 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4625 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4626 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4627 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4628 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4629 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4630 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4631 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4632 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4633 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4634 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4635 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4636 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4637 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4638 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4639 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4640 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4641 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4642 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4643 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 4644 | AINPUT_SOURCE_MOUSE) |
| 4645 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4646 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4647 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4648 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4649 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4650 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4651 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4652 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4653 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 4654 | AINPUT_SOURCE_MOUSE) |
| 4655 | .buttonState(0) |
| 4656 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4657 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4658 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4659 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4660 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4661 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4662 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4663 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4664 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4665 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4666 | .build())); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4667 | window->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4668 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 4669 | // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the |
| 4670 | // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail. |
| 4671 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4672 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4673 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 4674 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4675 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4676 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4677 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4678 | } |
| 4679 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4680 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4681 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 4682 | * is generated. |
| 4683 | */ |
| 4684 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 4685 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4686 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4687 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4688 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 4689 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4690 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4691 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4692 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4693 | |
| 4694 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4695 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4696 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4697 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4698 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4699 | .build())); |
| 4700 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4701 | |
| 4702 | // Remove the window, but keep the channel. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4703 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4704 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 4705 | } |
| 4706 | |
| 4707 | /** |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4708 | * Test that invalid HOVER events sent by accessibility do not cause a fatal crash. |
| 4709 | */ |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 4710 | TEST_F_WITH_FLAGS(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash, |
| 4711 | REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(com::android::input::flags, |
| 4712 | a11y_crash_on_inconsistent_event_stream))) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4713 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4714 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4715 | ui::LogicalDisplayId::DEFAULT); |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4716 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 4717 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4718 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4719 | |
| 4720 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 4721 | |
| 4722 | MotionEventBuilder hoverEnterBuilder = |
| 4723 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4724 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4725 | .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 4726 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4727 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 4728 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4729 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 4730 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4731 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4732 | } |
| 4733 | |
| 4734 | /** |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4735 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. |
| 4736 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4737 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover_legacy) { |
| 4738 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4739 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4740 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4741 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4742 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4743 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4744 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4745 | |
| 4746 | const int32_t mouseDeviceId = 7; |
| 4747 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4748 | |
| 4749 | // Start hovering with the mouse |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4750 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4751 | .deviceId(mouseDeviceId) |
| 4752 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 4753 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4754 | window->consumeMotionEvent( |
| 4755 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 4756 | |
| 4757 | // Touch goes down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4758 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4759 | .deviceId(touchDeviceId) |
| 4760 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4761 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4762 | |
| 4763 | window->consumeMotionEvent( |
| 4764 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 4765 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 4766 | } |
| 4767 | |
| 4768 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4769 | * If mouse is hovering when the touch goes down, the hovering should not be stopped. |
| 4770 | */ |
| 4771 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { |
| 4772 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 4773 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4774 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4775 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4776 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4777 | |
| 4778 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 4779 | |
| 4780 | const int32_t mouseDeviceId = 7; |
| 4781 | const int32_t touchDeviceId = 4; |
| 4782 | |
| 4783 | // Start hovering with the mouse |
| 4784 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4785 | .deviceId(mouseDeviceId) |
| 4786 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 4787 | .build()); |
| 4788 | window->consumeMotionEvent( |
| 4789 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 4790 | |
| 4791 | // Touch goes down |
| 4792 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4793 | .deviceId(touchDeviceId) |
| 4794 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4795 | .build()); |
| 4796 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 4797 | } |
| 4798 | |
| 4799 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4800 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4801 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 4802 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4803 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4804 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap_legacy) { |
| 4805 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4806 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4807 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4808 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4809 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4810 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4811 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4812 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4813 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 4814 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4815 | ASSERT_NO_FATAL_FAILURE( |
| 4816 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4817 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4818 | |
| 4819 | // Tap on the window |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4820 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4821 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4822 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4823 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4824 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4825 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 4826 | |
| 4827 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4828 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4829 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4830 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4831 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4832 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4833 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4834 | ASSERT_NO_FATAL_FAILURE( |
| 4835 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4836 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4837 | } |
| 4838 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4839 | /** |
| 4840 | * Send a mouse hover event followed by a tap from touchscreen. |
| 4841 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 4842 | * stream's source has been switched. |
| 4843 | */ |
| 4844 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 4845 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 4846 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4847 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4848 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4849 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4850 | |
| 4851 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 4852 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4853 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 4854 | .build()); |
| 4855 | |
| 4856 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4857 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4858 | |
| 4859 | // Tap on the window |
| 4860 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4861 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4862 | .build()); |
| 4863 | |
| 4864 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4865 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4866 | |
| 4867 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4868 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4869 | |
| 4870 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4871 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4872 | .build()); |
| 4873 | |
| 4874 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4875 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4876 | } |
| 4877 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4878 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 4879 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4880 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 4881 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4882 | ui::LogicalDisplayId::DEFAULT); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4883 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 4884 | sp<FakeWindowHandle> windowSecondDisplay = |
| 4885 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 4886 | SECOND_DISPLAY_ID); |
| 4887 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 4888 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4889 | mDispatcher->onWindowInfosChanged( |
| 4890 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4891 | |
| 4892 | // Set cursor position in window in default display and check that hover enter and move |
| 4893 | // events are generated. |
| 4894 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4895 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4896 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4897 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4898 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4899 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4900 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4901 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4902 | |
| 4903 | // Remove all windows in secondary display and check that no event happens on window in |
| 4904 | // primary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4905 | mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0}); |
| 4906 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4907 | windowDefaultDisplay->assertNoEvents(); |
| 4908 | |
| 4909 | // Move cursor position in window in default display and check that only hover move |
| 4910 | // event is generated and not hover enter event. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4911 | mDispatcher->onWindowInfosChanged( |
| 4912 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4913 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4914 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4915 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4916 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4917 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4918 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4919 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4920 | windowDefaultDisplay->consumeMotionEvent( |
| 4921 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4922 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4923 | windowDefaultDisplay->assertNoEvents(); |
| 4924 | } |
| 4925 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4926 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4927 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4928 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4929 | sp<FakeWindowHandle> windowLeft = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 4930 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4931 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4932 | sp<FakeWindowHandle> windowRight = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 4933 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4934 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4935 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4936 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4937 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4938 | mDispatcher->onWindowInfosChanged( |
| 4939 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4940 | |
| 4941 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 4942 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4943 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4944 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4945 | ui::LogicalDisplayId::DEFAULT, {610, 400}, {599, 400})); |
| 4946 | windowLeft->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4947 | windowRight->assertNoEvents(); |
| 4948 | } |
| 4949 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4950 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4951 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4952 | sp<FakeWindowHandle> window = |
| 4953 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 4954 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4955 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4956 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4957 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4958 | setFocusedWindow(window); |
| 4959 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4960 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4961 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4962 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4963 | |
| 4964 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4965 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4966 | |
| 4967 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 4968 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4969 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4970 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT, AKEY_EVENT_FLAG_CANCELED); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4971 | } |
| 4972 | |
| 4973 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4974 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4975 | sp<FakeWindowHandle> window = |
| 4976 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 4977 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4978 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4979 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4980 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4981 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4982 | AINPUT_SOURCE_TOUCHSCREEN, |
| 4983 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4984 | |
| 4985 | // Window should receive motion down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4986 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4987 | |
| 4988 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 4989 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4990 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 4991 | window->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4992 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4993 | } |
| 4994 | |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 4995 | TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) { |
| 4996 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4997 | sp<FakeWindowHandle> window = |
| 4998 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 4999 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 5000 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5001 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 5002 | |
| 5003 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 5004 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10)) |
| 5005 | .build()); |
| 5006 | |
| 5007 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 5008 | |
| 5009 | // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT |
| 5010 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
| 5011 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 5012 | |
| 5013 | // After the device has been reset, a new hovering stream can be sent to the window |
| 5014 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 5015 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15)) |
| 5016 | .build()); |
| 5017 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 5018 | } |
| 5019 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5020 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 5021 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5022 | sp<FakeWindowHandle> window = |
| 5023 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5024 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5025 | window->setFocusable(true); |
| 5026 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5027 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5028 | setFocusedWindow(window); |
| 5029 | |
| 5030 | window->consumeFocusEvent(true); |
| 5031 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5032 | const NotifyKeyArgs keyArgs = |
| 5033 | generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5034 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 5035 | const nsecs_t injectTime = keyArgs.eventTime; |
| 5036 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5037 | mDispatcher->notifyKey(keyArgs); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5038 | // The dispatching time should be always greater than or equal to intercept key timeout. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5039 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5040 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 5041 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 5042 | } |
| 5043 | |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 5044 | /** |
| 5045 | * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set. |
| 5046 | */ |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5047 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 5048 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5049 | sp<FakeWindowHandle> window = |
| 5050 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5051 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5052 | window->setFocusable(true); |
| 5053 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5054 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5055 | setFocusedWindow(window); |
| 5056 | |
| 5057 | window->consumeFocusEvent(true); |
| 5058 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5059 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
| 5060 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 5061 | |
| 5062 | // Set a value that's significantly larger than the default consumption timeout. If the |
| 5063 | // implementation is correct, the actual value doesn't matter; it won't slow down the test. |
| 5064 | mFakePolicy->setInterceptKeyTimeout(600ms); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5065 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 5066 | // Window should receive key event immediately when same key up. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5067 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5068 | } |
| 5069 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5070 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5071 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 5072 | * WATCH_OUTSIDE_TOUCH. |
| 5073 | * Both windows are owned by the same UID. |
| 5074 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 5075 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 5076 | */ |
| 5077 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 5078 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5079 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5080 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5081 | window->setFrame(Rect{0, 0, 100, 100}); |
| 5082 | |
| 5083 | sp<FakeWindowHandle> outsideWindow = |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 5084 | sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5085 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5086 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 5087 | outsideWindow->setWatchOutsideTouch(true); |
| 5088 | // 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] | 5089 | mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5090 | |
| 5091 | // Tap on first window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5092 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5093 | AINPUT_SOURCE_TOUCHSCREEN, |
| 5094 | ui::LogicalDisplayId::DEFAULT, {PointF{50, 50}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5095 | window->consumeMotionDown(); |
| 5096 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 5097 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 5098 | outsideWindow->consumeMotionEvent( |
| 5099 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 5100 | |
| 5101 | // Ensure outsideWindow doesn't get any more events for the gesture. |
| 5102 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5103 | ui::LogicalDisplayId::DEFAULT, {PointF{51, 51}})); |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 5104 | window->consumeMotionMove(); |
| 5105 | outsideWindow->assertNoEvents(); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5106 | } |
| 5107 | |
| 5108 | /** |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 5109 | * Three windows: |
| 5110 | * - Left window |
| 5111 | * - Right window |
| 5112 | * - Outside window(watch for ACTION_OUTSIDE events) |
| 5113 | * The windows "left" and "outside" share the same owner, the window "right" has a different owner, |
| 5114 | * In order to allow the outside window can receive the ACTION_OUTSIDE events, the outside window is |
| 5115 | * positioned above the "left" and "right" windows, and it doesn't overlap with them. |
| 5116 | * |
| 5117 | * First, device A report a down event landed in the right window, the outside window can receive |
| 5118 | * an ACTION_OUTSIDE event that with zeroed coordinates, the device B report a down event landed |
| 5119 | * in the left window, the outside window can receive an ACTION_OUTSIDE event the with valid |
| 5120 | * coordinates, after these, device A and device B continue report MOVE event, the right and left |
| 5121 | * window can receive it, but outside window event can't receive it. |
| 5122 | */ |
| 5123 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinatesWhenMultiDevice) { |
| 5124 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5125 | sp<FakeWindowHandle> leftWindow = |
| 5126 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5127 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 5128 | leftWindow->setFrame(Rect{0, 0, 100, 100}); |
| 5129 | leftWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
| 5130 | |
| 5131 | sp<FakeWindowHandle> outsideWindow = |
| 5132 | sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5133 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 5134 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 5135 | outsideWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
| 5136 | outsideWindow->setWatchOutsideTouch(true); |
| 5137 | |
| 5138 | std::shared_ptr<FakeApplicationHandle> anotherApplication = |
| 5139 | std::make_shared<FakeApplicationHandle>(); |
| 5140 | sp<FakeWindowHandle> rightWindow = |
| 5141 | sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Right Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5142 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 5143 | rightWindow->setFrame(Rect{100, 0, 200, 100}); |
| 5144 | rightWindow->setOwnerInfo(gui::Pid{2}, gui::Uid{202}); |
| 5145 | |
| 5146 | // OutsideWindow must be above left window and right window to receive ACTION_OUTSIDE events |
| 5147 | // when left window or right window is tapped |
| 5148 | mDispatcher->onWindowInfosChanged( |
| 5149 | {{*outsideWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, |
| 5150 | {}, |
| 5151 | 0, |
| 5152 | 0}); |
| 5153 | |
| 5154 | const DeviceId deviceA = 9; |
| 5155 | const DeviceId deviceB = 3; |
| 5156 | |
| 5157 | // Tap on right window use device A |
| 5158 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5159 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 5160 | .deviceId(deviceA) |
| 5161 | .build()); |
| 5162 | leftWindow->assertNoEvents(); |
| 5163 | rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 5164 | // Right window is belonged to another owner, so outsideWindow should receive ACTION_OUTSIDE |
| 5165 | // with zeroed coords. |
| 5166 | outsideWindow->consumeMotionEvent( |
| 5167 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithDeviceId(deviceA), WithCoords(0, 0))); |
| 5168 | |
| 5169 | // Tap on left window use device B |
| 5170 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5171 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5172 | .deviceId(deviceB) |
| 5173 | .build()); |
| 5174 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 5175 | rightWindow->assertNoEvents(); |
| 5176 | // Because new gesture down on the left window that has the same owner with outside Window, the |
| 5177 | // outside Window should receive the ACTION_OUTSIDE with coords. |
| 5178 | outsideWindow->consumeMotionEvent( |
| 5179 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithDeviceId(deviceB), WithCoords(-50, -50))); |
| 5180 | |
| 5181 | // Ensure that windows that can only accept outside do not receive remaining gestures |
| 5182 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5183 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(51)) |
| 5184 | .deviceId(deviceA) |
| 5185 | .build()); |
| 5186 | leftWindow->assertNoEvents(); |
| 5187 | rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceA))); |
| 5188 | |
| 5189 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5190 | .pointer(PointerBuilder(0, ToolType::FINGER).x(51).y(51)) |
| 5191 | .deviceId(deviceB) |
| 5192 | .build()); |
| 5193 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB))); |
| 5194 | rightWindow->assertNoEvents(); |
| 5195 | outsideWindow->assertNoEvents(); |
| 5196 | } |
| 5197 | |
| 5198 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5199 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 5200 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 5201 | * ACTION_OUTSIDE event is sent per gesture. |
| 5202 | */ |
| 5203 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 5204 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 5205 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5206 | sp<FakeWindowHandle> window = |
| 5207 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5208 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5209 | window->setWatchOutsideTouch(true); |
| 5210 | window->setFrame(Rect{0, 0, 100, 100}); |
| 5211 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5212 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5213 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5214 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 5215 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5216 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5217 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5218 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5219 | mDispatcher->onWindowInfosChanged( |
| 5220 | {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5221 | |
| 5222 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5223 | mDispatcher->notifyMotion( |
| 5224 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5225 | ui::LogicalDisplayId::DEFAULT, {PointF{-10, -10}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5226 | window->assertNoEvents(); |
| 5227 | secondWindow->assertNoEvents(); |
| 5228 | |
| 5229 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 5230 | // Now, `window` should get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5231 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5232 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5233 | {PointF{-10, -10}, PointF{105, 105}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5234 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 5235 | window->consumeMotionEvent( |
| 5236 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5237 | secondWindow->consumeMotionDown(); |
| 5238 | thirdWindow->assertNoEvents(); |
| 5239 | |
| 5240 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 5241 | // 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] | 5242 | mDispatcher->notifyMotion( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5243 | generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5244 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5245 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5246 | window->assertNoEvents(); |
| 5247 | secondWindow->consumeMotionMove(); |
| 5248 | thirdWindow->consumeMotionDown(); |
| 5249 | } |
| 5250 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5251 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 5252 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5253 | sp<FakeWindowHandle> window = |
| 5254 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5255 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5256 | window->setFocusable(true); |
| 5257 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5258 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5259 | setFocusedWindow(window); |
| 5260 | |
| 5261 | window->consumeFocusEvent(true); |
| 5262 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5263 | const NotifyKeyArgs keyDown = |
| 5264 | generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT); |
| 5265 | const NotifyKeyArgs keyUp = |
| 5266 | generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5267 | mDispatcher->notifyKey(keyDown); |
| 5268 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5269 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5270 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
| 5271 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5272 | |
| 5273 | // 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] | 5274 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5275 | |
| 5276 | window->consumeFocusEvent(false); |
| 5277 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5278 | mDispatcher->notifyKey(keyDown); |
| 5279 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5280 | window->assertNoEvents(); |
| 5281 | } |
| 5282 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5283 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 5284 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5285 | sp<FakeWindowHandle> window = |
| 5286 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5287 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5288 | // Ensure window is non-split and have some transform. |
| 5289 | window->setPreventSplitting(true); |
| 5290 | window->setWindowOffset(20, 40); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5291 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5292 | |
| 5293 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5294 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 5295 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5296 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5297 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5298 | |
| 5299 | const MotionEvent secondFingerDownEvent = |
| 5300 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5301 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5302 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 5303 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 5304 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5305 | .build(); |
| 5306 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5307 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5308 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 5309 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5310 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5311 | std::unique_ptr<MotionEvent> event = window->consumeMotionEvent(); |
| 5312 | ASSERT_NE(nullptr, event); |
| 5313 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 5314 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 5315 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 5316 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 5317 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5318 | } |
| 5319 | |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 5320 | /** |
| 5321 | * Two windows: a splittable and a non-splittable. |
| 5322 | * The non-splittable window shouldn't receive any "incomplete" gestures. |
| 5323 | * Send the first pointer to the splittable window, and then touch the non-splittable window. |
| 5324 | * The second pointer should be dropped because the initial window is splittable, so it won't get |
| 5325 | * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any |
| 5326 | * "incomplete" gestures. |
| 5327 | */ |
| 5328 | TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) { |
| 5329 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5330 | sp<FakeWindowHandle> leftWindow = |
| 5331 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5332 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 5333 | leftWindow->setPreventSplitting(false); |
| 5334 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5335 | sp<FakeWindowHandle> rightWindow = |
| 5336 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5337 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 5338 | rightWindow->setPreventSplitting(true); |
| 5339 | rightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 5340 | mDispatcher->onWindowInfosChanged( |
| 5341 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 5342 | |
| 5343 | // Touch down on left, splittable window |
| 5344 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5345 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5346 | .build()); |
| 5347 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 5348 | |
| 5349 | mDispatcher->notifyMotion( |
| 5350 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5351 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 5352 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
| 5353 | .build()); |
| 5354 | leftWindow->assertNoEvents(); |
| 5355 | rightWindow->assertNoEvents(); |
| 5356 | } |
| 5357 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 5358 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) { |
| 5359 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5360 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5361 | ui::LogicalDisplayId::DEFAULT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 5362 | window->setFrame(Rect(0, 0, 400, 400)); |
| 5363 | sp<FakeWindowHandle> trustedOverlay = |
| 5364 | sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5365 | ui::LogicalDisplayId::DEFAULT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 5366 | trustedOverlay->setSpy(true); |
| 5367 | trustedOverlay->setTrustedOverlay(true); |
| 5368 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5369 | mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 5370 | |
| 5371 | // Start a three-finger touchpad swipe |
| 5372 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 5373 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 5374 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5375 | .build()); |
| 5376 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 5377 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 5378 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 5379 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5380 | .build()); |
| 5381 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 5382 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 5383 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 5384 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 5385 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5386 | .build()); |
| 5387 | |
| 5388 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 5389 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 5390 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN)); |
| 5391 | |
| 5392 | // Move the swipe a bit |
| 5393 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 5394 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5395 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 5396 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 5397 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5398 | .build()); |
| 5399 | |
| 5400 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 5401 | |
| 5402 | // End the swipe |
| 5403 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 5404 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5405 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 5406 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 5407 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5408 | .build()); |
| 5409 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 5410 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5411 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 5412 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5413 | .build()); |
| 5414 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 5415 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5416 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5417 | .build()); |
| 5418 | |
| 5419 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP)); |
| 5420 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 5421 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 5422 | |
| 5423 | window->assertNoEvents(); |
| 5424 | } |
| 5425 | |
| 5426 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) { |
| 5427 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5428 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5429 | ui::LogicalDisplayId::DEFAULT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 5430 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5431 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 5432 | |
| 5433 | // Start a three-finger touchpad swipe |
| 5434 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 5435 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 5436 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5437 | .build()); |
| 5438 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 5439 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 5440 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 5441 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5442 | .build()); |
| 5443 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 5444 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 5445 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 5446 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 5447 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5448 | .build()); |
| 5449 | |
| 5450 | // Move the swipe a bit |
| 5451 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 5452 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5453 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 5454 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 5455 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5456 | .build()); |
| 5457 | |
| 5458 | // End the swipe |
| 5459 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 5460 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5461 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 5462 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 5463 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5464 | .build()); |
| 5465 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 5466 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5467 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 5468 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5469 | .build()); |
| 5470 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 5471 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5472 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5473 | .build()); |
| 5474 | |
| 5475 | window->assertNoEvents(); |
| 5476 | } |
| 5477 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5478 | /** |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5479 | * Send a two-pointer gesture to a single window. The window's orientation changes in response to |
| 5480 | * the first pointer. |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 5481 | * 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] | 5482 | */ |
| 5483 | TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) { |
| 5484 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5485 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5486 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5487 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 5488 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5489 | |
| 5490 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5491 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5492 | .downTime(baseTime + 10) |
| 5493 | .eventTime(baseTime + 10) |
| 5494 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 5495 | .build()); |
| 5496 | |
| 5497 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 5498 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5499 | // Change the transform so that the orientation is now different from original. |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 5500 | window->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5501 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 5502 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5503 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5504 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5505 | .downTime(baseTime + 10) |
| 5506 | .eventTime(baseTime + 30) |
| 5507 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 5508 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 5509 | .build()); |
| 5510 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 5511 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 5512 | |
| 5513 | // 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] | 5514 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 5515 | .downTime(baseTime + 10) |
| 5516 | .eventTime(baseTime + 40) |
| 5517 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 5518 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 5519 | .build()); |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 5520 | |
| 5521 | window->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 5522 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5523 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 5524 | .downTime(baseTime + 10) |
| 5525 | .eventTime(baseTime + 50) |
| 5526 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 5527 | .build()); |
| 5528 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 5529 | window->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 5530 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5531 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5532 | .downTime(baseTime + 60) |
| 5533 | .eventTime(baseTime + 60) |
| 5534 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40)) |
| 5535 | .build()); |
| 5536 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 5537 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5538 | } |
| 5539 | |
| 5540 | /** |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5541 | * When there are multiple screens, such as screen projection to TV or screen recording, if the |
| 5542 | * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and |
| 5543 | * its coordinates should be converted by the transform of the windows of target screen. |
| 5544 | */ |
| 5545 | TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) { |
| 5546 | // This case will create a window and a spy window on the default display and mirror |
| 5547 | // window on the second display. cancel event is sent through spy window pilferPointers |
| 5548 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5549 | |
| 5550 | sp<FakeWindowHandle> spyWindowDefaultDisplay = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5551 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 5552 | ui::LogicalDisplayId::DEFAULT); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5553 | spyWindowDefaultDisplay->setTrustedOverlay(true); |
| 5554 | spyWindowDefaultDisplay->setSpy(true); |
| 5555 | |
| 5556 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 5557 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5558 | ui::LogicalDisplayId::DEFAULT); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5559 | windowDefaultDisplay->setWindowTransform(1, 0, 0, 1); |
| 5560 | |
| 5561 | sp<FakeWindowHandle> windowSecondDisplay = windowDefaultDisplay->clone(SECOND_DISPLAY_ID); |
| 5562 | windowSecondDisplay->setWindowTransform(2, 0, 0, 2); |
| 5563 | |
| 5564 | // Add the windows to the dispatcher |
| 5565 | mDispatcher->onWindowInfosChanged( |
| 5566 | {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(), |
| 5567 | *windowSecondDisplay->getInfo()}, |
| 5568 | {}, |
| 5569 | 0, |
| 5570 | 0}); |
| 5571 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5572 | // Send down to ui::LogicalDisplayId::DEFAULT |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5573 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5574 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 5575 | ui::LogicalDisplayId::DEFAULT, {100, 100})) |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5576 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5577 | |
| 5578 | spyWindowDefaultDisplay->consumeMotionDown(); |
| 5579 | windowDefaultDisplay->consumeMotionDown(); |
| 5580 | |
| 5581 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken())); |
| 5582 | |
| 5583 | // windowDefaultDisplay gets cancel |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5584 | std::unique_ptr<MotionEvent> event = windowDefaultDisplay->consumeMotionEvent(); |
| 5585 | ASSERT_NE(nullptr, event); |
| 5586 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event->getAction()); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5587 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5588 | // The cancel event is sent to windowDefaultDisplay of the ui::LogicalDisplayId::DEFAULT |
| 5589 | // display, so the coordinates of the cancel are converted by windowDefaultDisplay's transform, |
| 5590 | // the x and y coordinates are both 100, otherwise if the cancel event is sent to |
| 5591 | // windowSecondDisplay of SECOND_DISPLAY_ID, the x and y coordinates are 200 |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5592 | EXPECT_EQ(100, event->getX(0)); |
| 5593 | EXPECT_EQ(100, event->getY(0)); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5594 | } |
| 5595 | |
| 5596 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5597 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 5598 | * |
| 5599 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 5600 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 5601 | * space. |
| 5602 | */ |
| 5603 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 5604 | public: |
| 5605 | void SetUp() override { |
| 5606 | InputDispatcherTest::SetUp(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5607 | removeAllWindowsAndDisplays(); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5608 | } |
| 5609 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 5610 | void addDisplayInfo(ui::LogicalDisplayId displayId, const ui::Transform& transform) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5611 | gui::DisplayInfo info; |
| 5612 | info.displayId = displayId; |
| 5613 | info.transform = transform; |
| 5614 | mDisplayInfos.push_back(std::move(info)); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5615 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5616 | } |
| 5617 | |
| 5618 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 5619 | mWindowInfos.push_back(*windowHandle->getInfo()); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5620 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5621 | } |
| 5622 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5623 | void removeAllWindowsAndDisplays() { |
| 5624 | mDisplayInfos.clear(); |
| 5625 | mWindowInfos.clear(); |
| 5626 | } |
| 5627 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5628 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 5629 | // on the display. |
| 5630 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 5631 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 5632 | // respectively. |
| 5633 | ui::Transform displayTransform; |
| 5634 | displayTransform.set(2, 0, 0, 4); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5635 | addDisplayInfo(ui::LogicalDisplayId::DEFAULT, displayTransform); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5636 | |
| 5637 | std::shared_ptr<FakeApplicationHandle> application = |
| 5638 | std::make_shared<FakeApplicationHandle>(); |
| 5639 | |
| 5640 | // Add two windows to the display. Their frames are represented in the display space. |
| 5641 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5642 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5643 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5644 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 5645 | addWindow(firstWindow); |
| 5646 | |
| 5647 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5648 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5649 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5650 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 5651 | addWindow(secondWindow); |
| 5652 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 5653 | } |
| 5654 | |
| 5655 | private: |
| 5656 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 5657 | std::vector<gui::WindowInfo> mWindowInfos; |
| 5658 | }; |
| 5659 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5660 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5661 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5662 | // 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] | 5663 | // selected so that if the hit test was performed with the point and the bounds being in |
| 5664 | // different coordinate spaces, the event would end up in the incorrect window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5665 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5666 | AINPUT_SOURCE_TOUCHSCREEN, |
| 5667 | ui::LogicalDisplayId::DEFAULT, {PointF{75, 55}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5668 | |
| 5669 | firstWindow->consumeMotionDown(); |
| 5670 | secondWindow->assertNoEvents(); |
| 5671 | } |
| 5672 | |
| 5673 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 5674 | // the event should be treated as being in the logical display space. |
| 5675 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 5676 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5677 | // Send down to the first window. The point is represented in the logical display space. The |
| 5678 | // point is selected so that if the hit test was done in logical display space, then it would |
| 5679 | // end up in the incorrect window. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5680 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5681 | PointF{75 * 2, 55 * 4}); |
| 5682 | |
| 5683 | firstWindow->consumeMotionDown(); |
| 5684 | secondWindow->assertNoEvents(); |
| 5685 | } |
| 5686 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5687 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 5688 | // event should be treated as being in the logical display space. |
| 5689 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 5690 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5691 | |
| 5692 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5693 | ui::Transform injectedEventTransform; |
| 5694 | injectedEventTransform.set(matrix); |
| 5695 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 5696 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 5697 | |
| 5698 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5699 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5700 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 5701 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5702 | .x(untransformedPoint.x) |
| 5703 | .y(untransformedPoint.y)) |
| 5704 | .build(); |
| 5705 | event.transform(matrix); |
| 5706 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5707 | injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5708 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 5709 | |
| 5710 | firstWindow->consumeMotionDown(); |
| 5711 | secondWindow->assertNoEvents(); |
| 5712 | } |
| 5713 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5714 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 5715 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5716 | |
| 5717 | // Send down to the second window. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5718 | mDispatcher->notifyMotion( |
| 5719 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5720 | ui::LogicalDisplayId::DEFAULT, {PointF{150, 220}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5721 | |
| 5722 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5723 | std::unique_ptr<MotionEvent> event = secondWindow->consumeMotionEvent(); |
| 5724 | ASSERT_NE(nullptr, event); |
| 5725 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5726 | |
| 5727 | // 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] | 5728 | EXPECT_EQ(300, event->getRawX(0)); |
| 5729 | EXPECT_EQ(880, event->getRawY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5730 | |
| 5731 | // Ensure that the x and y values are in the window's coordinate space. |
| 5732 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 5733 | // 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] | 5734 | EXPECT_EQ(100, event->getX(0)); |
| 5735 | EXPECT_EQ(80, event->getY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5736 | } |
| 5737 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5738 | TEST_F(InputDispatcherDisplayProjectionTest, CancelMotionWithCorrectCoordinates) { |
| 5739 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5740 | // The monitor will always receive events in the logical display's coordinate space, because |
| 5741 | // it does not have a window. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5742 | FakeMonitorReceiver monitor{*mDispatcher, "Monitor", ui::LogicalDisplayId::DEFAULT}; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5743 | |
| 5744 | // Send down to the first window. |
| 5745 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5746 | ui::LogicalDisplayId::DEFAULT, {PointF{50, 100}})); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5747 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5748 | monitor.consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5749 | |
| 5750 | // Second pointer goes down on second window. |
| 5751 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5752 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5753 | {PointF{50, 100}, PointF{150, 220}})); |
| 5754 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80))); |
| 5755 | const std::map<int32_t, PointF> expectedMonitorPointers{{0, PointF{100, 400}}, |
| 5756 | {1, PointF{300, 880}}}; |
| 5757 | monitor.consumeMotionEvent( |
| 5758 | AllOf(WithMotionAction(POINTER_1_DOWN), WithPointers(expectedMonitorPointers))); |
| 5759 | |
| 5760 | mDispatcher->cancelCurrentTouch(); |
| 5761 | |
| 5762 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 5763 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 80))); |
| 5764 | monitor.consumeMotionEvent( |
| 5765 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedMonitorPointers))); |
| 5766 | } |
| 5767 | |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 5768 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeDownWithCorrectCoordinates) { |
| 5769 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5770 | |
| 5771 | // Send down to the first window. |
| 5772 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5773 | ui::LogicalDisplayId::DEFAULT, {PointF{50, 100}})); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 5774 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5775 | |
| 5776 | // The pointer is transferred to the second window, and the second window receives it in the |
| 5777 | // correct coordinate space. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5778 | mDispatcher->transferTouchGesture(firstWindow->getToken(), secondWindow->getToken()); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 5779 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 5780 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(-100, -400))); |
| 5781 | } |
| 5782 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5783 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverEnterExitWithCorrectCoordinates) { |
| 5784 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5785 | |
| 5786 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 5787 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5788 | ui::LogicalDisplayId::DEFAULT, |
| 5789 | {PointF{150, 220}})); |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5790 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5791 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5792 | |
| 5793 | // Touch down at the same location and ensure a hover exit is synthesized. |
| 5794 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5795 | ui::LogicalDisplayId::DEFAULT, |
| 5796 | {PointF{150, 220}})); |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5797 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5798 | WithRawCoords(300, 880))); |
| 5799 | secondWindow->consumeMotionEvent( |
| 5800 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5801 | secondWindow->assertNoEvents(); |
| 5802 | firstWindow->assertNoEvents(); |
| 5803 | } |
| 5804 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5805 | // Same as above, but while the window is being mirrored. |
| 5806 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 5807 | SynthesizeHoverEnterExitWithCorrectCoordinatesWhenMirrored) { |
| 5808 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5809 | |
| 5810 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5811 | ui::Transform secondDisplayTransform; |
| 5812 | secondDisplayTransform.set(matrix); |
| 5813 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 5814 | |
| 5815 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 5816 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 5817 | addWindow(secondWindowClone); |
| 5818 | |
| 5819 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 5820 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5821 | ui::LogicalDisplayId::DEFAULT, |
| 5822 | {PointF{150, 220}})); |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5823 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5824 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5825 | |
| 5826 | // Touch down at the same location and ensure a hover exit is synthesized for the correct |
| 5827 | // display. |
| 5828 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5829 | ui::LogicalDisplayId::DEFAULT, |
| 5830 | {PointF{150, 220}})); |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5831 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5832 | WithRawCoords(300, 880))); |
| 5833 | secondWindow->consumeMotionEvent( |
| 5834 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5835 | secondWindow->assertNoEvents(); |
| 5836 | firstWindow->assertNoEvents(); |
| 5837 | } |
| 5838 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5839 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverCancelationWithCorrectCoordinates) { |
| 5840 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5841 | |
| 5842 | // Send hover enter to second window |
| 5843 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5844 | ui::LogicalDisplayId::DEFAULT, |
| 5845 | {PointF{150, 220}})); |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5846 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5847 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5848 | |
| 5849 | mDispatcher->cancelCurrentTouch(); |
| 5850 | |
| 5851 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5852 | WithRawCoords(300, 880))); |
| 5853 | secondWindow->assertNoEvents(); |
| 5854 | firstWindow->assertNoEvents(); |
| 5855 | } |
| 5856 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5857 | // Same as above, but while the window is being mirrored. |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5858 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 5859 | SynthesizeHoverCancelationWithCorrectCoordinatesWhenMirrored) { |
| 5860 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5861 | |
| 5862 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5863 | ui::Transform secondDisplayTransform; |
| 5864 | secondDisplayTransform.set(matrix); |
| 5865 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 5866 | |
| 5867 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 5868 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 5869 | addWindow(secondWindowClone); |
| 5870 | |
| 5871 | // Send hover enter to second window |
| 5872 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5873 | ui::LogicalDisplayId::DEFAULT, |
| 5874 | {PointF{150, 220}})); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5875 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5876 | WithCoords(100, 80), WithRawCoords(300, 880), |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5877 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5878 | |
| 5879 | mDispatcher->cancelCurrentTouch(); |
| 5880 | |
| 5881 | // Ensure the cancelation happens with the correct displayId and the correct coordinates. |
| 5882 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5883 | WithRawCoords(300, 880), |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5884 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5885 | secondWindow->assertNoEvents(); |
| 5886 | firstWindow->assertNoEvents(); |
| 5887 | } |
| 5888 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5889 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ |
| 5890 | class InputDispatcherDisplayOrientationFixture |
| 5891 | : public InputDispatcherDisplayProjectionTest, |
| 5892 | public ::testing::WithParamInterface<ui::Rotation> {}; |
| 5893 | |
| 5894 | // This test verifies the touchable region of a window for all rotations of the display by tapping |
| 5895 | // in different locations on the display, specifically points close to the four corners of a |
| 5896 | // window. |
| 5897 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { |
| 5898 | constexpr static int32_t displayWidth = 400; |
| 5899 | constexpr static int32_t displayHeight = 800; |
| 5900 | |
| 5901 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5902 | |
| 5903 | const auto rotation = GetParam(); |
| 5904 | |
| 5905 | // Set up the display with the specified rotation. |
| 5906 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 5907 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 5908 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 5909 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 5910 | logicalDisplayWidth, logicalDisplayHeight); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5911 | addDisplayInfo(ui::LogicalDisplayId::DEFAULT, displayTransform); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5912 | |
| 5913 | // Create a window with its bounds determined in the logical display. |
| 5914 | const Rect frameInLogicalDisplay(100, 100, 200, 300); |
| 5915 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5916 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5917 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5918 | window->setFrame(frameInDisplay, displayTransform); |
| 5919 | addWindow(window); |
| 5920 | |
| 5921 | // The following points in logical display space should be inside the window. |
| 5922 | static const std::array<vec2, 4> insidePoints{ |
| 5923 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 5924 | for (const auto pointInsideWindow : insidePoints) { |
| 5925 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); |
| 5926 | const PointF pointInDisplaySpace{p.x, p.y}; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5927 | mDispatcher->notifyMotion( |
| 5928 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5929 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5930 | window->consumeMotionDown(); |
| 5931 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5932 | mDispatcher->notifyMotion( |
| 5933 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5934 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5935 | window->consumeMotionUp(); |
| 5936 | } |
| 5937 | |
| 5938 | // The following points in logical display space should be outside the window. |
| 5939 | static const std::array<vec2, 5> outsidePoints{ |
| 5940 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 5941 | for (const auto pointOutsideWindow : outsidePoints) { |
| 5942 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); |
| 5943 | const PointF pointInDisplaySpace{p.x, p.y}; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5944 | mDispatcher->notifyMotion( |
| 5945 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5946 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5947 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5948 | mDispatcher->notifyMotion( |
| 5949 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5950 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5951 | } |
| 5952 | window->assertNoEvents(); |
| 5953 | } |
| 5954 | |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 5955 | // This test verifies the occlusion detection for all rotations of the display by tapping |
| 5956 | // in different locations on the display, specifically points close to the four corners of a |
| 5957 | // window. |
| 5958 | TEST_P(InputDispatcherDisplayOrientationFixture, BlockUntrustClickInDifferentOrientations) { |
| 5959 | constexpr static int32_t displayWidth = 400; |
| 5960 | constexpr static int32_t displayHeight = 800; |
| 5961 | |
| 5962 | std::shared_ptr<FakeApplicationHandle> untrustedWindowApplication = |
| 5963 | std::make_shared<FakeApplicationHandle>(); |
| 5964 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5965 | |
| 5966 | const auto rotation = GetParam(); |
| 5967 | |
| 5968 | // Set up the display with the specified rotation. |
| 5969 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 5970 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 5971 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 5972 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 5973 | logicalDisplayWidth, logicalDisplayHeight); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5974 | addDisplayInfo(ui::LogicalDisplayId::DEFAULT, displayTransform); |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 5975 | |
| 5976 | // Create a window that not trusted. |
| 5977 | const Rect untrustedWindowFrameInLogicalDisplay(100, 100, 200, 300); |
| 5978 | |
| 5979 | const Rect untrustedWindowFrameInDisplay = |
| 5980 | displayTransform.inverse().transform(untrustedWindowFrameInLogicalDisplay); |
| 5981 | |
| 5982 | sp<FakeWindowHandle> untrustedWindow = |
| 5983 | sp<FakeWindowHandle>::make(untrustedWindowApplication, mDispatcher, "UntrustedWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5984 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 5985 | untrustedWindow->setFrame(untrustedWindowFrameInDisplay, displayTransform); |
| 5986 | untrustedWindow->setTrustedOverlay(false); |
| 5987 | untrustedWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 5988 | untrustedWindow->setTouchable(false); |
| 5989 | untrustedWindow->setAlpha(1.0f); |
| 5990 | untrustedWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
| 5991 | addWindow(untrustedWindow); |
| 5992 | |
| 5993 | // Create a simple app window below the untrusted window. |
| 5994 | const Rect simpleAppWindowFrameInLogicalDisplay(0, 0, 300, 600); |
| 5995 | const Rect simpleAppWindowFrameInDisplay = |
| 5996 | displayTransform.inverse().transform(simpleAppWindowFrameInLogicalDisplay); |
| 5997 | |
| 5998 | sp<FakeWindowHandle> simpleAppWindow = |
| 5999 | sp<FakeWindowHandle>::make(application, mDispatcher, "SimpleAppWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6000 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6001 | simpleAppWindow->setFrame(simpleAppWindowFrameInDisplay, displayTransform); |
| 6002 | simpleAppWindow->setOwnerInfo(gui::Pid{2}, gui::Uid{202}); |
| 6003 | addWindow(simpleAppWindow); |
| 6004 | |
| 6005 | // The following points in logical display space should be inside the untrusted window, so |
| 6006 | // the simple window could not receive events that coordinate is these point. |
| 6007 | static const std::array<vec2, 4> untrustedPoints{ |
| 6008 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 6009 | |
| 6010 | for (const auto untrustedPoint : untrustedPoints) { |
| 6011 | const vec2 p = displayTransform.inverse().transform(untrustedPoint); |
| 6012 | const PointF pointInDisplaySpace{p.x, p.y}; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6013 | mDispatcher->notifyMotion( |
| 6014 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6015 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
| 6016 | mDispatcher->notifyMotion( |
| 6017 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6018 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6019 | } |
| 6020 | untrustedWindow->assertNoEvents(); |
| 6021 | simpleAppWindow->assertNoEvents(); |
| 6022 | // The following points in logical display space should be outside the untrusted window, so |
| 6023 | // the simple window should receive events that coordinate is these point. |
| 6024 | static const std::array<vec2, 5> trustedPoints{ |
| 6025 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 6026 | for (const auto trustedPoint : trustedPoints) { |
| 6027 | const vec2 p = displayTransform.inverse().transform(trustedPoint); |
| 6028 | const PointF pointInDisplaySpace{p.x, p.y}; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6029 | mDispatcher->notifyMotion( |
| 6030 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6031 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
| 6032 | simpleAppWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6033 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6034 | mDispatcher->notifyMotion( |
| 6035 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6036 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
| 6037 | simpleAppWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6038 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 6039 | } |
| 6040 | untrustedWindow->assertNoEvents(); |
| 6041 | } |
| 6042 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6043 | // Run the precision tests for all rotations. |
| 6044 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, |
| 6045 | InputDispatcherDisplayOrientationFixture, |
| 6046 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, |
| 6047 | ui::ROTATION_270), |
| 6048 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { |
| 6049 | return ftl::enum_string(testParamInfo.param); |
| 6050 | }); |
| 6051 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6052 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 6053 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6054 | |
| 6055 | class TransferTouchFixture : public InputDispatcherTest, |
| 6056 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 6057 | |
| 6058 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6059 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6060 | |
| 6061 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6062 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6063 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6064 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6065 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6066 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6067 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6068 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6069 | sp<FakeWindowHandle> wallpaper = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6070 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 6071 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6072 | wallpaper->setIsWallpaper(true); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6073 | // 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] | 6074 | mDispatcher->onWindowInfosChanged( |
| 6075 | {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6076 | setFocusedWindow(firstWindow); |
| 6077 | firstWindow->consumeFocusEvent(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6078 | |
| 6079 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6080 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6081 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6082 | ui::LogicalDisplayId::DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6083 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6084 | // Only the first window should get the down event |
| 6085 | firstWindow->consumeMotionDown(); |
| 6086 | secondWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6087 | wallpaper->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6088 | // Dispatcher reports pointer down outside focus for the wallpaper |
| 6089 | mFakePolicy->assertOnPointerDownEquals(wallpaper->getToken()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6090 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6091 | // Transfer touch to the second window |
| 6092 | TransferFunction f = GetParam(); |
| 6093 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 6094 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6095 | // The first window gets cancel and the second gets down |
| 6096 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6097 | secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 6098 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 6099 | wallpaper->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6100 | // There should not be any changes to the focused window when transferring touch |
| 6101 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertOnPointerDownWasNotCalled()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6102 | |
| 6103 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6104 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6105 | ui::LogicalDisplayId::DEFAULT)); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6106 | // The first window gets no events and the second gets up |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6107 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6108 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6109 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6110 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6111 | } |
| 6112 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6113 | /** |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6114 | * When 'transferTouchGesture' API is invoked, dispatcher needs to find the "best" window to take |
| 6115 | * touch from. When we have spy windows, there are several windows to choose from: either spy, or |
| 6116 | * 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] | 6117 | * natural to the user. |
| 6118 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 6119 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 6120 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 6121 | * the first window to the second. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6122 | * The main goal here is to test the behaviour of 'transferTouchGesture' API, but it's still valid |
| 6123 | * to test the other API, as well. |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6124 | */ |
| 6125 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 6126 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6127 | |
| 6128 | // Create a couple of windows + a spy window |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6129 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 6130 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6131 | spyWindow->setTrustedOverlay(true); |
| 6132 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6133 | sp<FakeWindowHandle> firstWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "First", |
| 6134 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6135 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6136 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 6137 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6138 | |
| 6139 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6140 | mDispatcher->onWindowInfosChanged( |
| 6141 | {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6142 | |
| 6143 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6144 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6145 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6146 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6147 | // Only the first window and spy should get the down event |
| 6148 | spyWindow->consumeMotionDown(); |
| 6149 | firstWindow->consumeMotionDown(); |
| 6150 | |
| 6151 | // 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] | 6152 | // if f === 'transferTouchGesture'. |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6153 | TransferFunction f = GetParam(); |
| 6154 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 6155 | ASSERT_TRUE(success); |
| 6156 | // The first window gets cancel and the second gets down |
| 6157 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6158 | secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 6159 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6160 | |
| 6161 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6162 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6163 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6164 | // The first window gets no events and the second+spy get up |
| 6165 | firstWindow->assertNoEvents(); |
| 6166 | spyWindow->consumeMotionUp(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6167 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6168 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6169 | } |
| 6170 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6171 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6172 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6173 | |
| 6174 | PointF touchPoint = {10, 10}; |
| 6175 | |
| 6176 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6177 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6178 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6179 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6180 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6181 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6182 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6183 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6184 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6185 | |
| 6186 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6187 | mDispatcher->onWindowInfosChanged( |
| 6188 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6189 | |
| 6190 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6191 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6192 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6193 | ui::LogicalDisplayId::DEFAULT, {touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6194 | // Only the first window should get the down event |
| 6195 | firstWindow->consumeMotionDown(); |
| 6196 | secondWindow->assertNoEvents(); |
| 6197 | |
| 6198 | // Send pointer down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6199 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6200 | ui::LogicalDisplayId::DEFAULT, |
| 6201 | {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6202 | // Only the first window should get the pointer down event |
| 6203 | firstWindow->consumeMotionPointerDown(1); |
| 6204 | secondWindow->assertNoEvents(); |
| 6205 | |
| 6206 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6207 | TransferFunction f = GetParam(); |
| 6208 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 6209 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6210 | // The first window gets cancel and the second gets down and pointer down |
| 6211 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6212 | secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 6213 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 6214 | secondWindow->consumeMotionPointerDown(1, ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6215 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6216 | |
| 6217 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6218 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6219 | ui::LogicalDisplayId::DEFAULT, |
| 6220 | {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6221 | // The first window gets nothing and the second gets pointer up |
| 6222 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6223 | secondWindow->consumeMotionPointerUp(1, ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6224 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6225 | |
| 6226 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6227 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6228 | ui::LogicalDisplayId::DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6229 | // The first window gets nothing and the second gets up |
| 6230 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6231 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6232 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6233 | } |
| 6234 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6235 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 6236 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6237 | |
| 6238 | // Create a couple of windows |
| 6239 | sp<FakeWindowHandle> firstWindow = |
| 6240 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6241 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6242 | firstWindow->setDupTouchToWallpaper(true); |
| 6243 | sp<FakeWindowHandle> secondWindow = |
| 6244 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6245 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6246 | secondWindow->setDupTouchToWallpaper(true); |
| 6247 | |
| 6248 | sp<FakeWindowHandle> wallpaper1 = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6249 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", |
| 6250 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6251 | wallpaper1->setIsWallpaper(true); |
| 6252 | |
| 6253 | sp<FakeWindowHandle> wallpaper2 = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6254 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", |
| 6255 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6256 | wallpaper2->setIsWallpaper(true); |
| 6257 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6258 | mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(), |
| 6259 | *secondWindow->getInfo(), *wallpaper2->getInfo()}, |
| 6260 | {}, |
| 6261 | 0, |
| 6262 | 0}); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6263 | |
| 6264 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6265 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6266 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6267 | ui::LogicalDisplayId::DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6268 | |
| 6269 | // Only the first window should get the down event |
| 6270 | firstWindow->consumeMotionDown(); |
| 6271 | secondWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6272 | wallpaper1->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6273 | wallpaper2->assertNoEvents(); |
| 6274 | |
| 6275 | // Transfer touch focus to the second window |
| 6276 | TransferFunction f = GetParam(); |
| 6277 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 6278 | ASSERT_TRUE(success); |
| 6279 | |
| 6280 | // The first window gets cancel and the second gets down |
| 6281 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6282 | secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 6283 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 6284 | wallpaper1->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
| 6285 | wallpaper2->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 6286 | EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6287 | |
| 6288 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6289 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6290 | ui::LogicalDisplayId::DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6291 | // The first window gets no events and the second gets up |
| 6292 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6293 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6294 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6295 | wallpaper1->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6296 | wallpaper2->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 6297 | EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6298 | } |
| 6299 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6300 | // 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] | 6301 | // 'transferTouchGesture' and 'transferTouchOnDisplay' are equivalent in behaviour. They only differ |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6302 | // for the case where there are multiple pointers split across several windows. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6303 | INSTANTIATE_TEST_SUITE_P( |
| 6304 | InputDispatcherTransferFunctionTests, TransferTouchFixture, |
| 6305 | ::testing::Values( |
| 6306 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, sp<IBinder> /*ignored*/, |
| 6307 | sp<IBinder> destChannelToken) { |
| 6308 | return dispatcher->transferTouchOnDisplay(destChannelToken, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6309 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6310 | }, |
| 6311 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, sp<IBinder> from, |
| 6312 | sp<IBinder> to) { |
| 6313 | return dispatcher->transferTouchGesture(from, to, |
| 6314 | /*isDragAndDrop=*/false); |
| 6315 | })); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6316 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6317 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6318 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6319 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6320 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6321 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6322 | ui::LogicalDisplayId::DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6323 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6324 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6325 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6326 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6327 | ui::LogicalDisplayId::DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6328 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6329 | |
| 6330 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6331 | mDispatcher->onWindowInfosChanged( |
| 6332 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6333 | |
| 6334 | PointF pointInFirst = {300, 200}; |
| 6335 | PointF pointInSecond = {300, 600}; |
| 6336 | |
| 6337 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6338 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6339 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6340 | ui::LogicalDisplayId::DEFAULT, {pointInFirst})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6341 | // Only the first window should get the down event |
| 6342 | firstWindow->consumeMotionDown(); |
| 6343 | secondWindow->assertNoEvents(); |
| 6344 | |
| 6345 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6346 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6347 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6348 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6349 | // The first window gets a move and the second a down |
| 6350 | firstWindow->consumeMotionMove(); |
| 6351 | secondWindow->consumeMotionDown(); |
| 6352 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6353 | // Transfer touch to the second window |
| 6354 | mDispatcher->transferTouchGesture(firstWindow->getToken(), secondWindow->getToken()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6355 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 6356 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6357 | secondWindow->consumeMotionPointerDown(1, ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6358 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6359 | |
| 6360 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6361 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6362 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6363 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6364 | // The first window gets nothing and the second gets pointer up |
| 6365 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6366 | secondWindow->consumeMotionPointerUp(1, ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6367 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6368 | |
| 6369 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6370 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6371 | ui::LogicalDisplayId::DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6372 | // The first window gets nothing and the second gets up |
| 6373 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6374 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6375 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6376 | } |
| 6377 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6378 | // Same as TransferTouch_TwoPointersSplitTouch, but using 'transferTouchOnDisplay' api. |
| 6379 | // Unlike 'transferTouchGesture', calling 'transferTouchOnDisplay' when there are two windows |
| 6380 | // receiving touch is not supported, so the touch should continue on those windows and the |
| 6381 | // transferred-to window should get nothing. |
| 6382 | TEST_F(InputDispatcherTest, TransferTouchOnDisplay_TwoPointersSplitTouch) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6383 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6384 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6385 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6386 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6387 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6388 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6389 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6390 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6391 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6392 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6393 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6394 | |
| 6395 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6396 | mDispatcher->onWindowInfosChanged( |
| 6397 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6398 | |
| 6399 | PointF pointInFirst = {300, 200}; |
| 6400 | PointF pointInSecond = {300, 600}; |
| 6401 | |
| 6402 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6403 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6404 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6405 | ui::LogicalDisplayId::DEFAULT, {pointInFirst})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6406 | // Only the first window should get the down event |
| 6407 | firstWindow->consumeMotionDown(); |
| 6408 | secondWindow->assertNoEvents(); |
| 6409 | |
| 6410 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6411 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6412 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6413 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6414 | // The first window gets a move and the second a down |
| 6415 | firstWindow->consumeMotionMove(); |
| 6416 | secondWindow->consumeMotionDown(); |
| 6417 | |
| 6418 | // Transfer touch focus to the second window |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6419 | const bool transferred = mDispatcher->transferTouchOnDisplay(secondWindow->getToken(), |
| 6420 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6421 | // The 'transferTouchOnDisplay' call should not succeed, because there are 2 touched windows |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6422 | ASSERT_FALSE(transferred); |
| 6423 | firstWindow->assertNoEvents(); |
| 6424 | secondWindow->assertNoEvents(); |
| 6425 | |
| 6426 | // The rest of the dispatch should proceed as normal |
| 6427 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6428 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6429 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6430 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6431 | // The first window gets MOVE and the second gets pointer up |
| 6432 | firstWindow->consumeMotionMove(); |
| 6433 | secondWindow->consumeMotionUp(); |
| 6434 | |
| 6435 | // Send up event to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6436 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6437 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6438 | // The first window gets nothing and the second gets up |
| 6439 | firstWindow->consumeMotionUp(); |
| 6440 | secondWindow->assertNoEvents(); |
| 6441 | } |
| 6442 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6443 | // 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] | 6444 | // 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] | 6445 | // the windows info of second display before default display. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6446 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6447 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6448 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6449 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", |
| 6450 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6451 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6452 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6453 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", |
| 6454 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6455 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6456 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6457 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 6458 | firstWindowInPrimary->clone(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6459 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6460 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6461 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6462 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6463 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6464 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6465 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6466 | |
| 6467 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6468 | mDispatcher->onWindowInfosChanged( |
| 6469 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 6470 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 6471 | *secondWindowInPrimary->getInfo()}, |
| 6472 | {}, |
| 6473 | 0, |
| 6474 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6475 | |
| 6476 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6477 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6478 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6479 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6480 | |
| 6481 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6482 | firstWindowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6483 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6484 | // Transfer touch |
| 6485 | ASSERT_TRUE(mDispatcher->transferTouchGesture(firstWindowInPrimary->getToken(), |
| 6486 | secondWindowInPrimary->getToken())); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6487 | // The first window gets cancel. |
| 6488 | firstWindowInPrimary->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6489 | secondWindowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6490 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6491 | |
| 6492 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6493 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6494 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6495 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6496 | firstWindowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6497 | secondWindowInPrimary->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6498 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6499 | |
| 6500 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6501 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6502 | {150, 50})) |
| 6503 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6504 | firstWindowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6505 | secondWindowInPrimary->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6506 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6507 | } |
| 6508 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6509 | // Same as TransferTouch_CloneSurface, but this touch on the secondary display and use |
| 6510 | // 'transferTouchOnDisplay' api. |
| 6511 | TEST_F(InputDispatcherTest, TransferTouchOnDisplay_CloneSurface) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6512 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6513 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6514 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", |
| 6515 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6516 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6517 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6518 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", |
| 6519 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6520 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6521 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6522 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 6523 | firstWindowInPrimary->clone(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6524 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6525 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6526 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6527 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6528 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6529 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6530 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6531 | |
| 6532 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6533 | mDispatcher->onWindowInfosChanged( |
| 6534 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 6535 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 6536 | *secondWindowInPrimary->getInfo()}, |
| 6537 | {}, |
| 6538 | 0, |
| 6539 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6540 | |
| 6541 | // Touch on second display. |
| 6542 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6543 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 6544 | {50, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6545 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6546 | |
| 6547 | // Window should receive motion event. |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6548 | firstWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6549 | |
| 6550 | // Transfer touch focus |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6551 | ASSERT_TRUE(mDispatcher->transferTouchOnDisplay(secondWindowInSecondary->getToken(), |
| 6552 | SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6553 | |
| 6554 | // The first window gets cancel. |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6555 | firstWindowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6556 | secondWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, |
| 6557 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6558 | |
| 6559 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6560 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6561 | SECOND_DISPLAY_ID, {150, 50})) |
| 6562 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6563 | firstWindowInSecondary->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6564 | secondWindowInSecondary->consumeMotionMove(SECOND_DISPLAY_ID, |
| 6565 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6566 | |
| 6567 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6568 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6569 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6570 | firstWindowInSecondary->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6571 | secondWindowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6572 | } |
| 6573 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6574 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6575 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6576 | sp<FakeWindowHandle> window = |
| 6577 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6578 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6579 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6580 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6581 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6582 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6583 | |
| 6584 | window->consumeFocusEvent(true); |
| 6585 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6586 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6587 | |
| 6588 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6589 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6590 | |
| 6591 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 6592 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6593 | mFakePolicy->assertUserActivityPoked(); |
| 6594 | } |
| 6595 | |
| 6596 | TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) { |
| 6597 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6598 | sp<FakeWindowHandle> window = |
| 6599 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6600 | ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6601 | |
| 6602 | window->setDisableUserActivity(true); |
| 6603 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6604 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6605 | setFocusedWindow(window); |
| 6606 | |
| 6607 | window->consumeFocusEvent(true); |
| 6608 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6609 | mDispatcher->notifyKey( |
| 6610 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6611 | |
| 6612 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6613 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6614 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6615 | // Should have not poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 6616 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6617 | mFakePolicy->assertUserActivityNotPoked(); |
| 6618 | } |
| 6619 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6620 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceivePolicyConsumedKey) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6621 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6622 | sp<FakeWindowHandle> window = |
| 6623 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6624 | ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6625 | |
| 6626 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6627 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6628 | setFocusedWindow(window); |
| 6629 | |
| 6630 | window->consumeFocusEvent(true); |
| 6631 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6632 | mFakePolicy->setConsumeKeyBeforeDispatching(true); |
| 6633 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6634 | mDispatcher->notifyKey( |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6635 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6636 | mDispatcher->waitForIdle(); |
| 6637 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6638 | // Key is not passed down |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6639 | window->assertNoEvents(); |
| 6640 | |
| 6641 | // Should have poked user activity |
| 6642 | mFakePolicy->assertUserActivityPoked(); |
| 6643 | } |
| 6644 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6645 | TEST_F(InputDispatcherTest, FocusedWindow_PolicyConsumedKeyIgnoresDisableUserActivity) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6646 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6647 | sp<FakeWindowHandle> window = |
| 6648 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6649 | ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6650 | |
| 6651 | window->setDisableUserActivity(true); |
| 6652 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6653 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6654 | setFocusedWindow(window); |
| 6655 | |
| 6656 | window->consumeFocusEvent(true); |
| 6657 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6658 | mFakePolicy->setConsumeKeyBeforeDispatching(true); |
| 6659 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6660 | mDispatcher->notifyKey( |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6661 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6662 | mDispatcher->waitForIdle(); |
| 6663 | |
| 6664 | // System key is not passed down |
| 6665 | window->assertNoEvents(); |
| 6666 | |
| 6667 | // Should have poked user activity |
| 6668 | mFakePolicy->assertUserActivityPoked(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6669 | } |
| 6670 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6671 | class DisableUserActivityInputDispatcherTest : public InputDispatcherTest, |
| 6672 | public ::testing::WithParamInterface<bool> {}; |
| 6673 | |
| 6674 | TEST_P(DisableUserActivityInputDispatcherTest, NotPassedToUserUserActivity) { |
| 6675 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6676 | sp<FakeWindowHandle> window = |
| 6677 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6678 | ui::LogicalDisplayId::DEFAULT); |
| 6679 | |
| 6680 | window->setDisableUserActivity(GetParam()); |
| 6681 | |
| 6682 | window->setFocusable(true); |
| 6683 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 6684 | setFocusedWindow(window); |
| 6685 | |
| 6686 | window->consumeFocusEvent(true); |
| 6687 | |
| 6688 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 6689 | .keyCode(AKEYCODE_A) |
| 6690 | .policyFlags(0) |
| 6691 | .build()); |
| 6692 | mDispatcher->waitForIdle(); |
| 6693 | |
| 6694 | // Key is not passed down |
| 6695 | window->assertNoEvents(); |
| 6696 | |
| 6697 | // Should not have poked user activity |
| 6698 | mFakePolicy->assertUserActivityNotPoked(); |
| 6699 | } |
| 6700 | |
| 6701 | INSTANTIATE_TEST_CASE_P(DisableUserActivity, DisableUserActivityInputDispatcherTest, |
| 6702 | ::testing::Bool()); |
| 6703 | |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6704 | TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) { |
| 6705 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6706 | sp<FakeWindowHandle> window = |
| 6707 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6708 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6709 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6710 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6711 | |
| 6712 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6713 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6714 | ui::LogicalDisplayId::DEFAULT, {100, 100})) |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6715 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6716 | |
| 6717 | window->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6718 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6719 | |
| 6720 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 6721 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6722 | mFakePolicy->assertUserActivityPoked(); |
| 6723 | } |
| 6724 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6725 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6726 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6727 | sp<FakeWindowHandle> window = |
| 6728 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6729 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6730 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6731 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6732 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6733 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6734 | mDispatcher->waitForIdle(); |
| 6735 | |
| 6736 | window->assertNoEvents(); |
| 6737 | } |
| 6738 | |
| 6739 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 6740 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6741 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6742 | sp<FakeWindowHandle> window = |
| 6743 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6744 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6745 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6746 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6747 | |
| 6748 | // Send key |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6749 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6750 | // Send motion |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6751 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6752 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6753 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6754 | |
| 6755 | // Window should receive only the motion event |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6756 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6757 | window->assertNoEvents(); // Key event or focus event will not be received |
| 6758 | } |
| 6759 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6760 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 6761 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6762 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6763 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6764 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6765 | ui::LogicalDisplayId::DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6766 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6767 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6768 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6769 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6770 | ui::LogicalDisplayId::DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6771 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6772 | |
| 6773 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6774 | mDispatcher->onWindowInfosChanged( |
| 6775 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6776 | |
| 6777 | PointF pointInFirst = {300, 200}; |
| 6778 | PointF pointInSecond = {300, 600}; |
| 6779 | |
| 6780 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6781 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6782 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6783 | ui::LogicalDisplayId::DEFAULT, {pointInFirst})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6784 | // Only the first window should get the down event |
| 6785 | firstWindow->consumeMotionDown(); |
| 6786 | secondWindow->assertNoEvents(); |
| 6787 | |
| 6788 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6789 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6790 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6791 | {pointInFirst, pointInSecond})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6792 | // The first window gets a move and the second a down |
| 6793 | firstWindow->consumeMotionMove(); |
| 6794 | secondWindow->consumeMotionDown(); |
| 6795 | |
| 6796 | // Send pointer cancel to the second window |
| 6797 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6798 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6799 | ui::LogicalDisplayId::DEFAULT, {pointInFirst, pointInSecond}); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6800 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6801 | mDispatcher->notifyMotion(pointerUpMotionArgs); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6802 | // The first window gets move and the second gets cancel. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6803 | firstWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 6804 | secondWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6805 | |
| 6806 | // Send up event. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6807 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6808 | ui::LogicalDisplayId::DEFAULT)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6809 | // The first window gets up and the second gets nothing. |
| 6810 | firstWindow->consumeMotionUp(); |
| 6811 | secondWindow->assertNoEvents(); |
| 6812 | } |
| 6813 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6814 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 6815 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6816 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6817 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 6818 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6819 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6820 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 6821 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 6822 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 6823 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6824 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6825 | window->assertNoEvents(); |
| 6826 | mDispatcher->waitForIdle(); |
| 6827 | } |
| 6828 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6829 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 6830 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6831 | /** |
| 6832 | * Two entities that receive touch: A window, and a global monitor. |
| 6833 | * The touch goes to the window, and then the window disappears. |
| 6834 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 6835 | * for the monitor, as well. |
| 6836 | * 1. foregroundWindow |
| 6837 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 6838 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6839 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6840 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6841 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 6842 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6843 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6844 | FakeMonitorReceiver monitor = |
| 6845 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6846 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6847 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6848 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6849 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6850 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6851 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6852 | |
| 6853 | // Both the foreground window and the global monitor should receive the touch down |
| 6854 | window->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6855 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6856 | |
| 6857 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6858 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6859 | ui::LogicalDisplayId::DEFAULT, {110, 200})) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6860 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6861 | |
| 6862 | window->consumeMotionMove(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6863 | monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6864 | |
| 6865 | // Now the foreground window goes away |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6866 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6867 | window->consumeMotionCancel(); |
| 6868 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 6869 | |
| 6870 | // If more events come in, there will be no more foreground window to send them to. This will |
| 6871 | // cause a cancel for the monitor, as well. |
| 6872 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6873 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6874 | ui::LogicalDisplayId::DEFAULT, {120, 200})) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6875 | << "Injection should fail because the window was removed"; |
| 6876 | window->assertNoEvents(); |
| 6877 | // Global monitor now gets the cancel |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6878 | monitor.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6879 | } |
| 6880 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6881 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6882 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6883 | sp<FakeWindowHandle> window = |
| 6884 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6885 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6886 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6887 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6888 | FakeMonitorReceiver monitor = |
| 6889 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6890 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6891 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6892 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6893 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6894 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6895 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 6896 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6897 | } |
| 6898 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6899 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6900 | FakeMonitorReceiver monitor = |
| 6901 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6902 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6903 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6904 | sp<FakeWindowHandle> window = |
| 6905 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6906 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6907 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6908 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6909 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6910 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6911 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6912 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6913 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 6914 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6915 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6916 | // Pilfer pointers from the monitor. |
| 6917 | // This should not do anything and the window should continue to receive events. |
| 6918 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6919 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6920 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6921 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6922 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6923 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6924 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6925 | monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
| 6926 | window->consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6927 | } |
| 6928 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6929 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6930 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6931 | sp<FakeWindowHandle> window = |
| 6932 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6933 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6934 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6935 | window->setWindowOffset(20, 40); |
| 6936 | window->setWindowTransform(0, 1, -1, 0); |
| 6937 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6938 | FakeMonitorReceiver monitor = |
| 6939 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6940 | |
| 6941 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6942 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6943 | ui::LogicalDisplayId::DEFAULT)) |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6944 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6945 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6946 | std::unique_ptr<MotionEvent> event = monitor.consumeMotion(); |
| 6947 | ASSERT_NE(nullptr, event); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6948 | // Even though window has transform, gesture monitor must not. |
| 6949 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 6950 | } |
| 6951 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6952 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6953 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6954 | FakeMonitorReceiver monitor = |
| 6955 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6956 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6957 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6958 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6959 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6960 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 6961 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6962 | } |
| 6963 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6964 | /** |
| 6965 | * Two displays |
| 6966 | * The first monitor has a foreground window, a monitor |
| 6967 | * The second window has only one monitor. |
| 6968 | * We first inject a Down event into the first display, this injection should succeed and both |
| 6969 | * the foreground window and monitor should receive a down event, then inject a Down event into |
| 6970 | * the second display as well, this injection should fail, at this point, the first display |
| 6971 | * window and monitor should not receive a cancel or any other event. |
| 6972 | * Continue to inject Move and UP events to the first display, the events should be received |
| 6973 | * normally by the foreground window and monitor. |
| 6974 | */ |
| 6975 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCanceledWhenAnotherEmptyDisplayReceiveEvents) { |
| 6976 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6977 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 6978 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6979 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6980 | FakeMonitorReceiver monitor = |
| 6981 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6982 | FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 6983 | |
| 6984 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 6985 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6986 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6987 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6988 | << "The down event injected into the first display should succeed"; |
| 6989 | |
| 6990 | window->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6991 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6992 | |
| 6993 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6994 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 6995 | {100, 200})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6996 | << "The down event injected into the second display should fail since there's no " |
| 6997 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6998 | |
| 6999 | // Continue to inject event to first display. |
| 7000 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7001 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7002 | ui::LogicalDisplayId::DEFAULT, {110, 220})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7003 | << "The move event injected into the first display should succeed"; |
| 7004 | |
| 7005 | window->consumeMotionMove(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7006 | monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7007 | |
| 7008 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7009 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7010 | {110, 220})) |
| 7011 | << "The up event injected into the first display should succeed"; |
| 7012 | |
| 7013 | window->consumeMotionUp(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7014 | monitor.consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7015 | |
| 7016 | window->assertNoEvents(); |
| 7017 | monitor.assertNoEvents(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7018 | secondMonitor.assertNoEvents(); |
| 7019 | } |
| 7020 | |
| 7021 | /** |
| 7022 | * Two displays |
| 7023 | * There is a monitor and foreground window on each display. |
| 7024 | * First, we inject down events into each of the two displays, at this point, the foreground windows |
| 7025 | * and monitors on both displays should receive down events. |
| 7026 | * At this point, the foreground window of the second display goes away, the gone window should |
| 7027 | * receive the cancel event, and the other windows and monitors should not receive any events. |
| 7028 | * Inject a move event into the second display. At this point, the injection should fail because |
| 7029 | * the second display no longer has a foreground window. At this point, the monitor on the second |
| 7030 | * display should receive a cancel event, and any windows or monitors on the first display should |
| 7031 | * not receive any events, and any subsequent injection of events into the second display should |
| 7032 | * also fail. |
| 7033 | * Continue to inject events into the first display, and the events should all be injected |
| 7034 | * successfully and received normally. |
| 7035 | */ |
| 7036 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCancelWhenAnotherDisplayMonitorTouchCanceled) { |
| 7037 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7038 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 7039 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7040 | sp<FakeWindowHandle> secondWindow = |
| 7041 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondForeground", |
| 7042 | SECOND_DISPLAY_ID); |
| 7043 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7044 | FakeMonitorReceiver monitor = |
| 7045 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7046 | FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 7047 | |
| 7048 | // There is a foreground window on both displays. |
| 7049 | mDispatcher->onWindowInfosChanged({{*window->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
| 7050 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7051 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7052 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7053 | << "The down event injected into the first display should succeed"; |
| 7054 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7055 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 7056 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7057 | |
| 7058 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7059 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 7060 | {100, 200})) |
| 7061 | << "The down event injected into the second display should succeed"; |
| 7062 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7063 | secondWindow->consumeMotionDown(SECOND_DISPLAY_ID); |
| 7064 | secondMonitor.consumeMotionDown(SECOND_DISPLAY_ID); |
| 7065 | |
| 7066 | // Now second window is gone away. |
| 7067 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 7068 | |
| 7069 | // The gone window should receive a cancel, and the monitor on the second display should not |
| 7070 | // receive any events. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7071 | secondWindow->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 7072 | secondMonitor.assertNoEvents(); |
| 7073 | |
| 7074 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 7075 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7076 | SECOND_DISPLAY_ID, {110, 220})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7077 | << "The move event injected into the second display should fail because there's no " |
| 7078 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7079 | // Now the monitor on the second display should receive a cancel event. |
| 7080 | secondMonitor.consumeMotionCancel(SECOND_DISPLAY_ID); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7081 | |
| 7082 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7083 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7084 | ui::LogicalDisplayId::DEFAULT, {110, 200})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7085 | << "The move event injected into the first display should succeed"; |
| 7086 | |
| 7087 | window->consumeMotionMove(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7088 | monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7089 | |
| 7090 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7091 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 7092 | {110, 220})) |
| 7093 | << "The up event injected into the second display should fail because there's no " |
| 7094 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7095 | |
| 7096 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7097 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7098 | {110, 220})) |
| 7099 | << "The up event injected into the first display should succeed"; |
| 7100 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7101 | window->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
| 7102 | monitor.consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7103 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7104 | window->assertNoEvents(); |
| 7105 | monitor.assertNoEvents(); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7106 | secondWindow->assertNoEvents(); |
| 7107 | secondMonitor.assertNoEvents(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7108 | } |
| 7109 | |
| 7110 | /** |
| 7111 | * One display with transform |
| 7112 | * There is a foreground window and a monitor on the display |
| 7113 | * Inject down event and move event sequentially, the foreground window and monitor can receive down |
| 7114 | * event and move event, then let the foreground window go away, the foreground window receives |
| 7115 | * cancel event, inject move event again, the monitor receives cancel event, all the events received |
| 7116 | * by the monitor should be with the same transform as the display |
| 7117 | */ |
| 7118 | TEST_F(InputDispatcherMonitorTest, MonitorTouchCancelEventWithDisplayTransform) { |
| 7119 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7120 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 7121 | ui::LogicalDisplayId::DEFAULT); |
| 7122 | FakeMonitorReceiver monitor = |
| 7123 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7124 | |
| 7125 | ui::Transform transform; |
| 7126 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 7127 | |
| 7128 | gui::DisplayInfo displayInfo; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7129 | displayInfo.displayId = ui::LogicalDisplayId::DEFAULT; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7130 | displayInfo.transform = transform; |
| 7131 | |
| 7132 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
| 7133 | |
| 7134 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7135 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7136 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7137 | << "The down event injected should succeed"; |
| 7138 | |
| 7139 | window->consumeMotionDown(); |
| 7140 | std::unique_ptr<MotionEvent> downMotionEvent = monitor.consumeMotion(); |
| 7141 | EXPECT_EQ(transform, downMotionEvent->getTransform()); |
| 7142 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, downMotionEvent->getAction()); |
| 7143 | |
| 7144 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7145 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7146 | ui::LogicalDisplayId::DEFAULT, {110, 220})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7147 | << "The move event injected should succeed"; |
| 7148 | |
| 7149 | window->consumeMotionMove(); |
| 7150 | std::unique_ptr<MotionEvent> moveMotionEvent = monitor.consumeMotion(); |
| 7151 | EXPECT_EQ(transform, moveMotionEvent->getTransform()); |
| 7152 | EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, moveMotionEvent->getAction()); |
| 7153 | |
| 7154 | // Let foreground window gone |
| 7155 | mDispatcher->onWindowInfosChanged({{}, {displayInfo}, 0, 0}); |
| 7156 | |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7157 | // Foreground window should receive a cancel event, but not the monitor. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7158 | window->consumeMotionCancel(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7159 | |
| 7160 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 7161 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7162 | ui::LogicalDisplayId::DEFAULT, {110, 220})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7163 | << "The move event injected should failed"; |
| 7164 | // Now foreground should not receive any events, but monitor should receive a cancel event |
| 7165 | // with transform that same as display's display. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7166 | std::unique_ptr<MotionEvent> cancelMotionEvent = monitor.consumeMotion(); |
| 7167 | EXPECT_EQ(transform, cancelMotionEvent->getTransform()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7168 | EXPECT_EQ(ui::LogicalDisplayId::DEFAULT, cancelMotionEvent->getDisplayId()); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7169 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, cancelMotionEvent->getAction()); |
| 7170 | |
| 7171 | // Other event inject to this display should fail. |
| 7172 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 7173 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7174 | ui::LogicalDisplayId::DEFAULT, {110, 220})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7175 | << "The up event injected should fail because the touched window was removed"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7176 | window->assertNoEvents(); |
| 7177 | monitor.assertNoEvents(); |
| 7178 | } |
| 7179 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7180 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7181 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7182 | sp<FakeWindowHandle> window = |
| 7183 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7184 | ui::LogicalDisplayId::DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7185 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7186 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7187 | |
| 7188 | NotifyMotionArgs motionArgs = |
| 7189 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7190 | ui::LogicalDisplayId::DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7191 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7192 | mDispatcher->notifyMotion(motionArgs); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7193 | // Window should receive motion down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7194 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7195 | |
| 7196 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7197 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7198 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 7199 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 7200 | motionArgs.pointerCoords[0].getX() - 10); |
| 7201 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7202 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7203 | window->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7204 | } |
| 7205 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7206 | /** |
| 7207 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 7208 | * the device default right away. In the test scenario, we check both the default value, |
| 7209 | * and the action of enabling / disabling. |
| 7210 | */ |
| 7211 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7212 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7213 | sp<FakeWindowHandle> window = |
| 7214 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 7215 | ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7216 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7217 | |
| 7218 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7219 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7220 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7221 | |
| 7222 | SCOPED_TRACE("Check default value of touch mode"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7223 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7224 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7225 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7226 | |
| 7227 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7228 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7229 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7230 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7231 | |
| 7232 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7233 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7234 | /*hasPermission=*/true, ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7235 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7236 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7237 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7238 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7239 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7240 | |
| 7241 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7242 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7243 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7244 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7245 | |
| 7246 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7247 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7248 | /*hasPermission=*/true, ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7249 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7250 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7251 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7252 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7253 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7254 | |
| 7255 | window->assertNoEvents(); |
| 7256 | } |
| 7257 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7258 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7259 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7260 | sp<FakeWindowHandle> window = |
| 7261 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 7262 | ui::LogicalDisplayId::DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7263 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7264 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7265 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7266 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7267 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7268 | setFocusedWindow(window); |
| 7269 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7270 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7271 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7272 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 7273 | mDispatcher->notifyKey(keyArgs); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7274 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7275 | std::unique_ptr<KeyEvent> event = window->consumeKey(); |
| 7276 | ASSERT_NE(event, nullptr); |
| 7277 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7278 | ASSERT_NE(verified, nullptr); |
| 7279 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 7280 | |
| 7281 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 7282 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 7283 | ASSERT_EQ(keyArgs.source, verified->source); |
| 7284 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 7285 | |
| 7286 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 7287 | |
| 7288 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7289 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 7290 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7291 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 7292 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 7293 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 7294 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 7295 | } |
| 7296 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7297 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7298 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7299 | sp<FakeWindowHandle> window = |
| 7300 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 7301 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7302 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7303 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7304 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 7305 | ui::Transform transform; |
| 7306 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 7307 | |
| 7308 | gui::DisplayInfo displayInfo; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7309 | displayInfo.displayId = ui::LogicalDisplayId::DEFAULT; |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 7310 | displayInfo.transform = transform; |
| 7311 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 7312 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7313 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7314 | const NotifyMotionArgs motionArgs = |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7315 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7316 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7317 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7318 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7319 | std::unique_ptr<MotionEvent> event = window->consumeMotionEvent(); |
| 7320 | ASSERT_NE(nullptr, event); |
| 7321 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7322 | ASSERT_NE(verified, nullptr); |
| 7323 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 7324 | |
| 7325 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 7326 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 7327 | EXPECT_EQ(motionArgs.source, verified->source); |
| 7328 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 7329 | |
| 7330 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 7331 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 7332 | const vec2 rawXY = |
| 7333 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 7334 | motionArgs.pointerCoords[0].getXYValue()); |
| 7335 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 7336 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7337 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7338 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 7339 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7340 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 7341 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 7342 | } |
| 7343 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 7344 | /** |
| 7345 | * Ensure that separate calls to sign the same data are generating the same key. |
| 7346 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 7347 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 7348 | * tests. |
| 7349 | */ |
| 7350 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 7351 | KeyEvent event = getTestKeyEvent(); |
| 7352 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 7353 | |
| 7354 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 7355 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 7356 | ASSERT_EQ(hmac1, hmac2); |
| 7357 | } |
| 7358 | |
| 7359 | /** |
| 7360 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 7361 | */ |
| 7362 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 7363 | KeyEvent event = getTestKeyEvent(); |
| 7364 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 7365 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 7366 | |
| 7367 | verifiedEvent.deviceId += 1; |
| 7368 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7369 | |
| 7370 | verifiedEvent.source += 1; |
| 7371 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7372 | |
| 7373 | verifiedEvent.eventTimeNanos += 1; |
| 7374 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7375 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 7376 | verifiedEvent.displayId = ui::LogicalDisplayId{verifiedEvent.displayId.val() + 1}; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 7377 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7378 | |
| 7379 | verifiedEvent.action += 1; |
| 7380 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7381 | |
| 7382 | verifiedEvent.downTimeNanos += 1; |
| 7383 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7384 | |
| 7385 | verifiedEvent.flags += 1; |
| 7386 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7387 | |
| 7388 | verifiedEvent.keyCode += 1; |
| 7389 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7390 | |
| 7391 | verifiedEvent.scanCode += 1; |
| 7392 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7393 | |
| 7394 | verifiedEvent.metaState += 1; |
| 7395 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7396 | |
| 7397 | verifiedEvent.repeatCount += 1; |
| 7398 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7399 | } |
| 7400 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7401 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 7402 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7403 | sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 7404 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7405 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7406 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 7407 | ui::LogicalDisplayId::DEFAULT); |
| 7408 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7409 | |
| 7410 | // Top window is also focusable but is not granted focus. |
| 7411 | windowTop->setFocusable(true); |
| 7412 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7413 | mDispatcher->onWindowInfosChanged( |
| 7414 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7415 | setFocusedWindow(windowSecond); |
| 7416 | |
| 7417 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7418 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7419 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7420 | |
| 7421 | // Focused window should receive event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7422 | windowSecond->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7423 | windowTop->assertNoEvents(); |
| 7424 | } |
| 7425 | |
| 7426 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 7427 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7428 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
| 7429 | ui::LogicalDisplayId::DEFAULT); |
| 7430 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7431 | |
| 7432 | window->setFocusable(true); |
| 7433 | // Release channel for window is no longer valid. |
| 7434 | window->releaseChannel(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7435 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7436 | setFocusedWindow(window); |
| 7437 | |
| 7438 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7439 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7440 | |
| 7441 | // window channel is invalid, so it should not receive any input event. |
| 7442 | window->assertNoEvents(); |
| 7443 | } |
| 7444 | |
| 7445 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 7446 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7447 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
| 7448 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7449 | window->setFocusable(false); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7450 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7451 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7452 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7453 | setFocusedWindow(window); |
| 7454 | |
| 7455 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7456 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7457 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7458 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7459 | window->assertNoEvents(); |
| 7460 | } |
| 7461 | |
| 7462 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 7463 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7464 | sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 7465 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7466 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7467 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 7468 | ui::LogicalDisplayId::DEFAULT); |
| 7469 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7470 | |
| 7471 | windowTop->setFocusable(true); |
| 7472 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7473 | mDispatcher->onWindowInfosChanged( |
| 7474 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7475 | setFocusedWindow(windowTop); |
| 7476 | windowTop->consumeFocusEvent(true); |
| 7477 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 7478 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7479 | mDispatcher->onWindowInfosChanged( |
| 7480 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7481 | windowSecond->consumeFocusEvent(true); |
| 7482 | windowTop->consumeFocusEvent(false); |
| 7483 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7484 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7485 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7486 | |
| 7487 | // Focused window should receive event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7488 | windowSecond->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7489 | } |
| 7490 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 7491 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7492 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7493 | sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 7494 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7495 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7496 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 7497 | ui::LogicalDisplayId::DEFAULT); |
| 7498 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7499 | |
| 7500 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 7501 | windowSecond->setFocusable(false); |
| 7502 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7503 | mDispatcher->onWindowInfosChanged( |
| 7504 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 7505 | setFocusedWindow(windowTop); |
| 7506 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7507 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7508 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 7509 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7510 | |
| 7511 | // Event should be dropped. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7512 | windowTop->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7513 | windowSecond->assertNoEvents(); |
| 7514 | } |
| 7515 | |
| 7516 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 7517 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7518 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
| 7519 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7520 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7521 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7522 | ui::LogicalDisplayId::DEFAULT); |
| 7523 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7524 | |
| 7525 | window->setFocusable(true); |
| 7526 | previousFocusedWindow->setFocusable(true); |
| 7527 | window->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7528 | mDispatcher->onWindowInfosChanged( |
| 7529 | {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7530 | setFocusedWindow(previousFocusedWindow); |
| 7531 | previousFocusedWindow->consumeFocusEvent(true); |
| 7532 | |
| 7533 | // Requesting focus on invisible window takes focus from currently focused window. |
| 7534 | setFocusedWindow(window); |
| 7535 | previousFocusedWindow->consumeFocusEvent(false); |
| 7536 | |
| 7537 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7538 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7539 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7540 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7541 | |
| 7542 | // Window does not get focus event or key down. |
| 7543 | window->assertNoEvents(); |
| 7544 | |
| 7545 | // Window becomes visible. |
| 7546 | window->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7547 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7548 | |
| 7549 | // Window receives focus event. |
| 7550 | window->consumeFocusEvent(true); |
| 7551 | // Focused window receives key down. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7552 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7553 | } |
| 7554 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 7555 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 7556 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7557 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "window", |
| 7558 | ui::LogicalDisplayId::DEFAULT); |
| 7559 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 7560 | |
| 7561 | // window is granted focus. |
| 7562 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7563 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 7564 | setFocusedWindow(window); |
| 7565 | window->consumeFocusEvent(true); |
| 7566 | |
| 7567 | // When a display is removed window loses focus. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7568 | mDispatcher->displayRemoved(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 7569 | window->consumeFocusEvent(false); |
| 7570 | } |
| 7571 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7572 | /** |
| 7573 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 7574 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 7575 | * of the 'slipperyEnterWindow'. |
| 7576 | * |
| 7577 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 7578 | * a way so that the touched location is no longer covered by the top window. |
| 7579 | * |
| 7580 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 7581 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 7582 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 7583 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 7584 | * with ACTION_DOWN). |
| 7585 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 7586 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 7587 | * |
| 7588 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 7589 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 7590 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 7591 | * |
| 7592 | * In this test, we ensure that the event received by the bottom window has |
| 7593 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 7594 | */ |
| 7595 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 7596 | constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7597 | constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1}; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7598 | |
| 7599 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7600 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7601 | |
| 7602 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7603 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 7604 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7605 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7606 | // Make sure this one overlaps the bottom window |
| 7607 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 7608 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 7609 | // one. Windows with the same owner are not considered to be occluding each other. |
| 7610 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 7611 | |
| 7612 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7613 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 7614 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7615 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7616 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7617 | mDispatcher->onWindowInfosChanged( |
| 7618 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7619 | |
| 7620 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7621 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7622 | AINPUT_SOURCE_TOUCHSCREEN, |
| 7623 | ui::LogicalDisplayId::DEFAULT, {{50, 50}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7624 | slipperyExitWindow->consumeMotionDown(); |
| 7625 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7626 | mDispatcher->onWindowInfosChanged( |
| 7627 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7628 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7629 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7630 | AINPUT_SOURCE_TOUCHSCREEN, |
| 7631 | ui::LogicalDisplayId::DEFAULT, {{51, 51}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7632 | |
| 7633 | slipperyExitWindow->consumeMotionCancel(); |
| 7634 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7635 | slipperyEnterWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7636 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 7637 | } |
| 7638 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 7639 | /** |
| 7640 | * Two windows, one on the left and another on the right. The left window is slippery. The right |
| 7641 | * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the |
| 7642 | * touch moves from the left window into the right window, the gesture should continue to go to the |
| 7643 | * left window. Touch shouldn't slip because the right window can't receive touches. This test |
| 7644 | * reproduces a crash. |
| 7645 | */ |
| 7646 | TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { |
| 7647 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7648 | |
| 7649 | sp<FakeWindowHandle> leftSlipperyWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7650 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 7651 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 7652 | leftSlipperyWindow->setSlippery(true); |
| 7653 | leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7654 | |
| 7655 | sp<FakeWindowHandle> rightDropTouchesWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7656 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 7657 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 7658 | rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100)); |
| 7659 | rightDropTouchesWindow->setDropInput(true); |
| 7660 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7661 | mDispatcher->onWindowInfosChanged( |
| 7662 | {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 7663 | |
| 7664 | // Start touch in the left window |
| 7665 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7666 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7667 | .build()); |
| 7668 | leftSlipperyWindow->consumeMotionDown(); |
| 7669 | |
| 7670 | // And move it into the right window |
| 7671 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7672 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 7673 | .build()); |
| 7674 | |
| 7675 | // Since the right window isn't eligible to receive input, touch does not slip. |
| 7676 | // The left window continues to receive the gesture. |
| 7677 | leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 7678 | rightDropTouchesWindow->assertNoEvents(); |
| 7679 | } |
| 7680 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 7681 | /** |
| 7682 | * A single window is on screen first. Touch is injected into that window. Next, a second window |
| 7683 | * appears. Since the first window is slippery, touch will move from the first window to the second. |
| 7684 | */ |
| 7685 | TEST_F(InputDispatcherTest, InjectedTouchSlips) { |
| 7686 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7687 | sp<FakeWindowHandle> originalWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7688 | sp<FakeWindowHandle>::make(application, mDispatcher, "Original", |
| 7689 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 7690 | originalWindow->setFrame(Rect(0, 0, 200, 200)); |
| 7691 | originalWindow->setSlippery(true); |
| 7692 | |
| 7693 | sp<FakeWindowHandle> appearingWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7694 | sp<FakeWindowHandle>::make(application, mDispatcher, "Appearing", |
| 7695 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 7696 | appearingWindow->setFrame(Rect(0, 0, 200, 200)); |
| 7697 | |
| 7698 | mDispatcher->onWindowInfosChanged({{*originalWindow->getInfo()}, {}, 0, 0}); |
| 7699 | |
| 7700 | // Touch down on the original window |
| 7701 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7702 | injectMotionEvent(*mDispatcher, |
| 7703 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7704 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 7705 | .build())); |
| 7706 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 7707 | |
| 7708 | // Now, a new window appears. This could be, for example, a notification shade that appears |
| 7709 | // after user starts to drag down on the launcher window. |
| 7710 | mDispatcher->onWindowInfosChanged( |
| 7711 | {{*appearingWindow->getInfo(), *originalWindow->getInfo()}, {}, 0, 0}); |
| 7712 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7713 | injectMotionEvent(*mDispatcher, |
| 7714 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7715 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(110)) |
| 7716 | .build())); |
| 7717 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 7718 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 7719 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7720 | injectMotionEvent(*mDispatcher, |
| 7721 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7722 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 7723 | .build())); |
| 7724 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 7725 | |
| 7726 | originalWindow->assertNoEvents(); |
| 7727 | appearingWindow->assertNoEvents(); |
| 7728 | } |
| 7729 | |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7730 | /** |
| 7731 | * Three windows: |
| 7732 | * - left window, which has FLAG_SLIPPERY, so it supports slippery exit |
| 7733 | * - right window |
| 7734 | * - spy window |
| 7735 | * The three windows do not overlap. |
| 7736 | * |
| 7737 | * We have two devices reporting events: |
| 7738 | * - Device A reports ACTION_DOWN, which lands in the left window |
| 7739 | * - Device B reports ACTION_DOWN, which lands in the spy window. |
| 7740 | * - Now, device B reports ACTION_MOVE events which move to the right window. |
| 7741 | * |
| 7742 | * The right window should not receive any events because the spy window is not a foreground window, |
| 7743 | * and also it does not support slippery touches. |
| 7744 | */ |
| 7745 | TEST_F(InputDispatcherTest, MultiDeviceSpyWindowSlipTest) { |
| 7746 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7747 | sp<FakeWindowHandle> leftWindow = |
| 7748 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7749 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7750 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7751 | leftWindow->setSlippery(true); |
| 7752 | |
| 7753 | sp<FakeWindowHandle> rightWindow = |
| 7754 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7755 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7756 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 7757 | |
| 7758 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7759 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", |
| 7760 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7761 | spyWindow->setFrame(Rect(200, 0, 300, 100)); |
| 7762 | spyWindow->setSpy(true); |
| 7763 | spyWindow->setTrustedOverlay(true); |
| 7764 | |
| 7765 | mDispatcher->onWindowInfosChanged( |
| 7766 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *spyWindow->getInfo()}, {}, 0, 0}); |
| 7767 | |
| 7768 | const DeviceId deviceA = 9; |
| 7769 | const DeviceId deviceB = 3; |
| 7770 | |
| 7771 | // Tap on left window with device A |
| 7772 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7773 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7774 | .deviceId(deviceA) |
| 7775 | .build()); |
| 7776 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 7777 | |
| 7778 | // Tap on spy window with device B |
| 7779 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7780 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(50)) |
| 7781 | .deviceId(deviceB) |
| 7782 | .build()); |
| 7783 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 7784 | |
| 7785 | // Move to right window with device B. Touches should not slip to the right window, because spy |
| 7786 | // window is not a foreground window, and it does not have FLAG_SLIPPERY |
| 7787 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7788 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 7789 | .deviceId(deviceB) |
| 7790 | .build()); |
| 7791 | leftWindow->assertNoEvents(); |
| 7792 | rightWindow->assertNoEvents(); |
| 7793 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB))); |
| 7794 | } |
| 7795 | |
| 7796 | /** |
| 7797 | * Three windows arranged horizontally and without any overlap. |
| 7798 | * The left and right windows have FLAG_SLIPPERY. The middle window does not have any special flags. |
| 7799 | * |
| 7800 | * We have two devices reporting events: |
| 7801 | * - Device A reports ACTION_DOWN which lands in the left window |
| 7802 | * - Device B reports ACTION_DOWN which lands in the right window |
| 7803 | * - Device B reports ACTION_MOVE that shifts to the middle window. |
| 7804 | * This should cause touches for Device B to slip from the right window to the middle window. |
| 7805 | * The right window should receive ACTION_CANCEL for device B and the |
| 7806 | * middle window should receive down event for Device B. |
| 7807 | * If device B reports more ACTION_MOVE events, the middle window should receive remaining events. |
| 7808 | */ |
| 7809 | TEST_F(InputDispatcherTest, MultiDeviceSlipperyWindowTest) { |
Siarhei Vishniakou | dd56df1 | 2024-05-20 14:56:38 -0700 | [diff] [blame] | 7810 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7811 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7812 | sp<FakeWindowHandle> leftWindow = |
| 7813 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7814 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7815 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7816 | leftWindow->setSlippery(true); |
| 7817 | |
| 7818 | sp<FakeWindowHandle> middleWindow = |
| 7819 | sp<FakeWindowHandle>::make(application, mDispatcher, "middle window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7820 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7821 | middleWindow->setFrame(Rect(100, 0, 200, 100)); |
| 7822 | |
| 7823 | sp<FakeWindowHandle> rightWindow = |
| 7824 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7825 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7826 | rightWindow->setFrame(Rect(200, 0, 300, 100)); |
| 7827 | rightWindow->setSlippery(true); |
| 7828 | |
| 7829 | mDispatcher->onWindowInfosChanged( |
| 7830 | {{*leftWindow->getInfo(), *middleWindow->getInfo(), *rightWindow->getInfo()}, |
| 7831 | {}, |
| 7832 | 0, |
| 7833 | 0}); |
| 7834 | |
| 7835 | const DeviceId deviceA = 9; |
| 7836 | const DeviceId deviceB = 3; |
| 7837 | |
| 7838 | // Tap on left window with device A |
| 7839 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7840 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7841 | .deviceId(deviceA) |
| 7842 | .build()); |
| 7843 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 7844 | |
| 7845 | // Tap on right window with device B |
| 7846 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7847 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(50)) |
| 7848 | .deviceId(deviceB) |
| 7849 | .build()); |
| 7850 | rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 7851 | |
| 7852 | // Move to middle window with device B. Touches should slip to middle window, because right |
| 7853 | // window is a foreground window that's associated with device B and has FLAG_SLIPPERY. |
| 7854 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7855 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 7856 | .deviceId(deviceB) |
| 7857 | .build()); |
| 7858 | rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB))); |
| 7859 | middleWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 7860 | |
| 7861 | // Move to middle window with device A. Touches should slip to middle window, because left |
| 7862 | // window is a foreground window that's associated with device A and has FLAG_SLIPPERY. |
| 7863 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7864 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 7865 | .deviceId(deviceA) |
| 7866 | .build()); |
| 7867 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceA))); |
| 7868 | middleWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 7869 | |
| 7870 | // Ensure that middle window can receive the remaining move events. |
| 7871 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7872 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(51)) |
| 7873 | .deviceId(deviceB) |
| 7874 | .build()); |
| 7875 | leftWindow->assertNoEvents(); |
| 7876 | middleWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB))); |
| 7877 | rightWindow->assertNoEvents(); |
| 7878 | } |
| 7879 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7880 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7881 | using Uid = gui::Uid; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7882 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7883 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7884 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 7885 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7886 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 7887 | leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7888 | |
| 7889 | sp<FakeWindowHandle> rightSpy = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7890 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", |
| 7891 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7892 | rightSpy->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 7893 | rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7894 | rightSpy->setSpy(true); |
| 7895 | rightSpy->setTrustedOverlay(true); |
| 7896 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7897 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 7898 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7899 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 7900 | rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7901 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7902 | mDispatcher->onWindowInfosChanged( |
| 7903 | {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7904 | |
| 7905 | // Touch in the left window |
| 7906 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7907 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7908 | .build()); |
| 7909 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown()); |
| 7910 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7911 | ASSERT_NO_FATAL_FAILURE( |
| 7912 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7913 | |
| 7914 | // Touch another finger over the right windows |
| 7915 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7916 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7917 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 7918 | .build()); |
| 7919 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown()); |
| 7920 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown()); |
| 7921 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove()); |
| 7922 | mDispatcher->waitForIdle(); |
| 7923 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7924 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, |
| 7925 | {Uid{101}, Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7926 | |
| 7927 | // Release finger over left window. The UP actions are not treated as device interaction. |
| 7928 | // The windows that did not receive the UP pointer will receive MOVE events, but since this |
| 7929 | // is part of the UP action, we do not treat this as device interaction. |
| 7930 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7931 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7932 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 7933 | .build()); |
| 7934 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp()); |
| 7935 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 7936 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 7937 | mDispatcher->waitForIdle(); |
| 7938 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 7939 | |
| 7940 | // Move remaining finger |
| 7941 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7942 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 7943 | .build()); |
| 7944 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 7945 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 7946 | mDispatcher->waitForIdle(); |
| 7947 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7948 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7949 | |
| 7950 | // Release all fingers |
| 7951 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7952 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 7953 | .build()); |
| 7954 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp()); |
| 7955 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp()); |
| 7956 | mDispatcher->waitForIdle(); |
| 7957 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 7958 | } |
| 7959 | |
| 7960 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) { |
| 7961 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7962 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7963 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 7964 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7965 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 7966 | window->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7967 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7968 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7969 | setFocusedWindow(window); |
| 7970 | ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true)); |
| 7971 | |
| 7972 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7973 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT)); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7974 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7975 | ASSERT_NO_FATAL_FAILURE( |
| 7976 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7977 | |
| 7978 | // The UP actions are not treated as device interaction. |
| 7979 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7980 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT)); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7981 | mDispatcher->waitForIdle(); |
| 7982 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 7983 | } |
| 7984 | |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7985 | TEST_F(InputDispatcherTest, HoverEnterExitSynthesisUsesNewEventId) { |
| 7986 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7987 | |
| 7988 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7989 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7990 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7991 | sp<FakeWindowHandle> right = |
| 7992 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 7993 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7994 | right->setFrame(Rect(100, 0, 200, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7995 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", |
| 7996 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7997 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 7998 | spy->setTrustedOverlay(true); |
| 7999 | spy->setSpy(true); |
| 8000 | |
| 8001 | mDispatcher->onWindowInfosChanged( |
| 8002 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 8003 | |
| 8004 | // Send hover move to the left window, and ensure hover enter is synthesized with a new eventId. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8005 | NotifyMotionArgs notifyArgs = |
| 8006 | generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 8007 | ui::LogicalDisplayId::DEFAULT, {PointF{50, 50}}); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8008 | mDispatcher->notifyMotion(notifyArgs); |
| 8009 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8010 | std::unique_ptr<MotionEvent> leftEnter = left->consumeMotionEvent( |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8011 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), Not(WithEventId(notifyArgs.id)), |
| 8012 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8013 | ASSERT_NE(nullptr, leftEnter); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8014 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 8015 | Not(WithEventId(notifyArgs.id)), |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8016 | Not(WithEventId(leftEnter->getId())), |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8017 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 8018 | |
| 8019 | // Send move to the right window, and ensure hover exit and enter are synthesized with new ids. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8020 | notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 8021 | ui::LogicalDisplayId::DEFAULT, {PointF{150, 50}}); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8022 | mDispatcher->notifyMotion(notifyArgs); |
| 8023 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8024 | std::unique_ptr<MotionEvent> leftExit = left->consumeMotionEvent( |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8025 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), Not(WithEventId(notifyArgs.id)), |
| 8026 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8027 | ASSERT_NE(nullptr, leftExit); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8028 | right->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 8029 | Not(WithEventId(notifyArgs.id)), |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8030 | Not(WithEventId(leftExit->getId())), |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8031 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 8032 | |
| 8033 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithEventId(notifyArgs.id))); |
| 8034 | } |
| 8035 | |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 8036 | /** |
| 8037 | * When a device reports a DOWN event, which lands in a window that supports splits, and then the |
| 8038 | * device then reports a POINTER_DOWN, which lands in the location of a non-existing window, then |
| 8039 | * the previous window should receive this event and not be dropped. |
| 8040 | */ |
| 8041 | TEST_F(InputDispatcherMultiDeviceTest, SingleDevicePointerDownEventRetentionWithoutWindowTarget) { |
| 8042 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8043 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 8044 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 8045 | window->setFrame(Rect(0, 0, 100, 100)); |
| 8046 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 8047 | |
| 8048 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8049 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8050 | .build()); |
| 8051 | |
| 8052 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 8053 | |
| 8054 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8055 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8056 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 8057 | .build()); |
| 8058 | |
| 8059 | window->consumeMotionEvent(AllOf(WithMotionAction(POINTER_1_DOWN))); |
| 8060 | } |
| 8061 | |
| 8062 | /** |
| 8063 | * When deviceA reports a DOWN event, which lands in a window that supports splits, and then deviceB |
| 8064 | * also reports a DOWN event, which lands in the location of a non-existing window, then the |
| 8065 | * previous window should receive deviceB's event and it should be dropped. |
| 8066 | */ |
| 8067 | TEST_F(InputDispatcherMultiDeviceTest, SecondDeviceDownEventDroppedWithoutWindowTarget) { |
| 8068 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8069 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 8070 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 8071 | window->setFrame(Rect(0, 0, 100, 100)); |
| 8072 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 8073 | |
| 8074 | const DeviceId deviceA = 9; |
| 8075 | const DeviceId deviceB = 3; |
| 8076 | |
| 8077 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8078 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8079 | .deviceId(deviceA) |
| 8080 | .build()); |
| 8081 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 8082 | |
| 8083 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8084 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
| 8085 | .deviceId(deviceB) |
| 8086 | .build()); |
| 8087 | window->assertNoEvents(); |
| 8088 | } |
| 8089 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 8090 | class InputDispatcherFallbackKeyTest : public InputDispatcherTest { |
| 8091 | protected: |
| 8092 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 8093 | sp<FakeWindowHandle> mWindow; |
| 8094 | |
| 8095 | virtual void SetUp() override { |
| 8096 | InputDispatcherTest::SetUp(); |
| 8097 | |
| 8098 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 8099 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8100 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Window", |
| 8101 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 8102 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
| 8103 | |
| 8104 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 8105 | setFocusedWindow(mWindow); |
| 8106 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeFocusEvent(/*hasFocus=*/true)); |
| 8107 | } |
| 8108 | |
| 8109 | void setFallback(int32_t keycode) { |
| 8110 | mFakePolicy->setUnhandledKeyHandler([keycode](const KeyEvent& event) { |
| 8111 | return KeyEventBuilder(event).keyCode(keycode).build(); |
| 8112 | }); |
| 8113 | } |
| 8114 | |
| 8115 | void consumeKey(bool handled, const ::testing::Matcher<KeyEvent>& matcher) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8116 | std::unique_ptr<KeyEvent> event = mWindow->consumeKey(handled); |
| 8117 | ASSERT_NE(nullptr, event); |
| 8118 | ASSERT_THAT(*event, matcher); |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 8119 | } |
| 8120 | }; |
| 8121 | |
| 8122 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotNotifiedForHandledKey) { |
| 8123 | mDispatcher->notifyKey( |
| 8124 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8125 | consumeKey(/*handled=*/true, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 8126 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8127 | } |
| 8128 | |
| 8129 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotifiedForUnhandledKey) { |
| 8130 | mDispatcher->notifyKey( |
| 8131 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8132 | consumeKey(/*handled=*/false, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 8133 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8134 | } |
| 8135 | |
| 8136 | TEST_F(InputDispatcherFallbackKeyTest, NoFallbackRequestedByPolicy) { |
| 8137 | mDispatcher->notifyKey( |
| 8138 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8139 | |
| 8140 | // Do not handle this key event. |
| 8141 | consumeKey(/*handled=*/false, |
| 8142 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8143 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8144 | |
| 8145 | // Since the policy did not request any fallback to be generated, ensure there are no events. |
| 8146 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8147 | } |
| 8148 | |
| 8149 | TEST_F(InputDispatcherFallbackKeyTest, FallbackDispatchForUnhandledKey) { |
| 8150 | setFallback(AKEYCODE_B); |
| 8151 | mDispatcher->notifyKey( |
| 8152 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8153 | |
| 8154 | // Do not handle this key event. |
| 8155 | consumeKey(/*handled=*/false, |
| 8156 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8157 | |
| 8158 | // Since the key was not handled, ensure the fallback event was dispatched instead. |
| 8159 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8160 | consumeKey(/*handled=*/true, |
| 8161 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8162 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8163 | |
| 8164 | // Release the original key, and ensure the fallback key is also released. |
| 8165 | mDispatcher->notifyKey( |
| 8166 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8167 | consumeKey(/*handled=*/false, |
| 8168 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8169 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8170 | consumeKey(/*handled=*/true, |
| 8171 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8172 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8173 | |
| 8174 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8175 | mWindow->assertNoEvents(); |
| 8176 | } |
| 8177 | |
| 8178 | TEST_F(InputDispatcherFallbackKeyTest, AppHandlesPreviouslyUnhandledKey) { |
| 8179 | setFallback(AKEYCODE_B); |
| 8180 | mDispatcher->notifyKey( |
| 8181 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8182 | |
| 8183 | // Do not handle this key event, but handle the fallback. |
| 8184 | consumeKey(/*handled=*/false, |
| 8185 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8186 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8187 | consumeKey(/*handled=*/true, |
| 8188 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8189 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8190 | |
| 8191 | // Release the original key, and ensure the fallback key is also released. |
| 8192 | mDispatcher->notifyKey( |
| 8193 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8194 | // But this time, the app handles the original key. |
| 8195 | consumeKey(/*handled=*/true, |
| 8196 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8197 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8198 | // Ensure the fallback key is canceled. |
| 8199 | consumeKey(/*handled=*/true, |
| 8200 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8201 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 8202 | |
| 8203 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8204 | mWindow->assertNoEvents(); |
| 8205 | } |
| 8206 | |
| 8207 | TEST_F(InputDispatcherFallbackKeyTest, AppDoesNotHandleFallback) { |
| 8208 | setFallback(AKEYCODE_B); |
| 8209 | mDispatcher->notifyKey( |
| 8210 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8211 | |
| 8212 | // Do not handle this key event. |
| 8213 | consumeKey(/*handled=*/false, |
| 8214 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8215 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8216 | // App does not handle the fallback either, so ensure another fallback is not generated. |
| 8217 | setFallback(AKEYCODE_C); |
| 8218 | consumeKey(/*handled=*/false, |
| 8219 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8220 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8221 | |
| 8222 | // Release the original key, and ensure the fallback key is also released. |
| 8223 | setFallback(AKEYCODE_B); |
| 8224 | mDispatcher->notifyKey( |
| 8225 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8226 | consumeKey(/*handled=*/false, |
| 8227 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8228 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8229 | consumeKey(/*handled=*/false, |
| 8230 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8231 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8232 | |
| 8233 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8234 | mWindow->assertNoEvents(); |
| 8235 | } |
| 8236 | |
| 8237 | TEST_F(InputDispatcherFallbackKeyTest, InconsistentPolicyCancelsFallback) { |
| 8238 | setFallback(AKEYCODE_B); |
| 8239 | mDispatcher->notifyKey( |
| 8240 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8241 | |
| 8242 | // Do not handle this key event, so fallback is generated. |
| 8243 | consumeKey(/*handled=*/false, |
| 8244 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8245 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8246 | consumeKey(/*handled=*/true, |
| 8247 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8248 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8249 | |
| 8250 | // Release the original key, but assume the policy is misbehaving and it |
| 8251 | // generates an inconsistent fallback to the one from the DOWN event. |
| 8252 | setFallback(AKEYCODE_C); |
| 8253 | mDispatcher->notifyKey( |
| 8254 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8255 | consumeKey(/*handled=*/false, |
| 8256 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8257 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8258 | // Ensure the fallback key reported before as DOWN is canceled due to the inconsistency. |
| 8259 | consumeKey(/*handled=*/true, |
| 8260 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8261 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 8262 | |
| 8263 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8264 | mWindow->assertNoEvents(); |
| 8265 | } |
| 8266 | |
| 8267 | TEST_F(InputDispatcherFallbackKeyTest, CanceledKeyCancelsFallback) { |
| 8268 | setFallback(AKEYCODE_B); |
| 8269 | mDispatcher->notifyKey( |
| 8270 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8271 | |
| 8272 | // Do not handle this key event, so fallback is generated. |
| 8273 | consumeKey(/*handled=*/false, |
| 8274 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8275 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8276 | consumeKey(/*handled=*/true, |
| 8277 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8278 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8279 | |
| 8280 | // The original key is canceled. |
| 8281 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
| 8282 | .keyCode(AKEYCODE_A) |
| 8283 | .addFlag(AKEY_EVENT_FLAG_CANCELED) |
| 8284 | .build()); |
| 8285 | consumeKey(/*handled=*/false, |
| 8286 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), |
| 8287 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 8288 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8289 | // Ensure the fallback key is also canceled due to the original key being canceled. |
| 8290 | consumeKey(/*handled=*/true, |
| 8291 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8292 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 8293 | |
| 8294 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8295 | mWindow->assertNoEvents(); |
| 8296 | } |
| 8297 | |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 8298 | TEST_F(InputDispatcherFallbackKeyTest, InputChannelRemovedDuringPolicyCall) { |
Prabir Pradhan | b13da8f | 2024-01-09 23:10:13 +0000 | [diff] [blame] | 8299 | setFallback(AKEYCODE_B); |
| 8300 | mDispatcher->notifyKey( |
| 8301 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8302 | |
| 8303 | // Do not handle this key event. |
| 8304 | consumeKey(/*handled=*/false, |
| 8305 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8306 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8307 | consumeKey(/*handled=*/true, |
| 8308 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8309 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8310 | |
| 8311 | mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) { |
| 8312 | // When the unhandled key is reported to the policy next, remove the input channel. |
| 8313 | mDispatcher->removeInputChannel(mWindow->getToken()); |
| 8314 | return KeyEventBuilder(event).keyCode(AKEYCODE_B).build(); |
| 8315 | }); |
| 8316 | // Release the original key, and let the app now handle the previously unhandled key. |
| 8317 | // This should result in the previously generated fallback key to be cancelled. |
| 8318 | // Since the policy was notified of the unhandled DOWN event earlier, it will also be notified |
| 8319 | // of the UP event for consistency. The Dispatcher calls into the policy from its own thread |
| 8320 | // without holding the lock, because it need to synchronously fetch the fallback key. While in |
| 8321 | // the policy call, we will now remove the input channel. Once the policy call returns, the |
| 8322 | // Dispatcher will no longer have a channel to send cancellation events to. Ensure this does |
| 8323 | // not cause any crashes. |
| 8324 | mDispatcher->notifyKey( |
| 8325 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8326 | consumeKey(/*handled=*/true, |
| 8327 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8328 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8329 | } |
| 8330 | |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 8331 | TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedDuringPolicyCall) { |
| 8332 | setFallback(AKEYCODE_B); |
| 8333 | mDispatcher->notifyKey( |
| 8334 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8335 | |
| 8336 | // Do not handle this key event. |
| 8337 | consumeKey(/*handled=*/false, |
| 8338 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8339 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8340 | consumeKey(/*handled=*/true, |
| 8341 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8342 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8343 | |
| 8344 | mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) { |
| 8345 | // When the unhandled key is reported to the policy next, remove the window. |
| 8346 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 8347 | return KeyEventBuilder(event).keyCode(AKEYCODE_B).build(); |
| 8348 | }); |
| 8349 | // Release the original key, which the app will not handle. When this unhandled key is reported |
| 8350 | // to the policy, the window will be removed. |
| 8351 | mDispatcher->notifyKey( |
| 8352 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8353 | consumeKey(/*handled=*/false, |
| 8354 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8355 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8356 | |
| 8357 | // Since the window was removed, it loses focus, and the channel state will be reset. |
| 8358 | consumeKey(/*handled=*/true, |
| 8359 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8360 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 8361 | mWindow->consumeFocusEvent(false); |
| 8362 | mWindow->assertNoEvents(); |
| 8363 | } |
| 8364 | |
| 8365 | TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedWhileAwaitingFinishedSignal) { |
| 8366 | setFallback(AKEYCODE_B); |
| 8367 | mDispatcher->notifyKey( |
| 8368 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8369 | |
| 8370 | // Do not handle this key event. |
| 8371 | consumeKey(/*handled=*/false, |
| 8372 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8373 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8374 | const auto [seq, event] = mWindow->receiveEvent(); |
| 8375 | ASSERT_TRUE(seq.has_value() && event != nullptr) << "Failed to receive fallback event"; |
| 8376 | ASSERT_EQ(event->getType(), InputEventType::KEY); |
| 8377 | ASSERT_THAT(static_cast<const KeyEvent&>(*event), |
| 8378 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8379 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8380 | |
| 8381 | // Remove the window now, which should generate a cancellations and make the window lose focus. |
| 8382 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 8383 | consumeKey(/*handled=*/true, |
| 8384 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), |
| 8385 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 8386 | consumeKey(/*handled=*/true, |
| 8387 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8388 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 8389 | mWindow->consumeFocusEvent(false); |
| 8390 | |
| 8391 | // Finish the event by reporting it as handled. |
| 8392 | mWindow->finishEvent(*seq); |
| 8393 | mWindow->assertNoEvents(); |
| 8394 | } |
| 8395 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8396 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 8397 | protected: |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 8398 | static constexpr std::chrono::nanoseconds KEY_REPEAT_TIMEOUT = 40ms; |
| 8399 | static constexpr std::chrono::nanoseconds KEY_REPEAT_DELAY = 40ms; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8400 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8401 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8402 | sp<FakeWindowHandle> mWindow; |
| 8403 | |
| 8404 | virtual void SetUp() override { |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 8405 | InputDispatcherTest::SetUp(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8406 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 8407 | mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8408 | setUpWindow(); |
| 8409 | } |
| 8410 | |
| 8411 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8412 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8413 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", |
| 8414 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8415 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8416 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8417 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8418 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8419 | mWindow->consumeFocusEvent(true); |
| 8420 | } |
| 8421 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8422 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8423 | NotifyKeyArgs keyArgs = |
| 8424 | generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8425 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8426 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8427 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8428 | |
| 8429 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8430 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8431 | } |
| 8432 | |
| 8433 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 8434 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8435 | mWindow->consumeKeyEvent( |
| 8436 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount))); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8437 | } |
| 8438 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8439 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8440 | NotifyKeyArgs keyArgs = |
| 8441 | generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8442 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8443 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8444 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8445 | |
| 8446 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8447 | mWindow->consumeKeyUp(ui::LogicalDisplayId::DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8448 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8449 | } |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 8450 | |
| 8451 | void injectKeyRepeat(int32_t repeatCount) { |
| 8452 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8453 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, repeatCount, |
| 8454 | ui::LogicalDisplayId::DEFAULT)) |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 8455 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8456 | } |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8457 | }; |
| 8458 | |
| 8459 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8460 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8461 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 8462 | expectKeyRepeatOnce(repeatCount); |
| 8463 | } |
| 8464 | } |
| 8465 | |
| 8466 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8467 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8468 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 8469 | expectKeyRepeatOnce(repeatCount); |
| 8470 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8471 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8472 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8473 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 8474 | expectKeyRepeatOnce(repeatCount); |
| 8475 | } |
| 8476 | } |
| 8477 | |
| 8478 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8479 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 8480 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 8481 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8482 | mWindow->assertNoEvents(); |
| 8483 | } |
| 8484 | |
| 8485 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8486 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 8487 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 8488 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 8489 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8490 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8491 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8492 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8493 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 8494 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8495 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8496 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8497 | mWindow->assertNoEvents(); |
| 8498 | } |
| 8499 | |
| 8500 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8501 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 8502 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 8503 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 8504 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8505 | // 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] | 8506 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8507 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8508 | mWindow->assertNoEvents(); |
| 8509 | } |
| 8510 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 8511 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 8512 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8513 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8514 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8515 | mWindow->consumeKeyUp(ui::LogicalDisplayId::DEFAULT, |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 8516 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 8517 | mWindow->assertNoEvents(); |
| 8518 | } |
| 8519 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8520 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 8521 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8522 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8523 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8524 | std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey(); |
| 8525 | ASSERT_NE(nullptr, repeatEvent); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8526 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8527 | IdGenerator::getSource(repeatEvent->getId())); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8528 | } |
| 8529 | } |
| 8530 | |
| 8531 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 8532 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8533 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8534 | |
| 8535 | std::unordered_set<int32_t> idSet; |
| 8536 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8537 | std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey(); |
| 8538 | ASSERT_NE(nullptr, repeatEvent); |
| 8539 | int32_t id = repeatEvent->getId(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8540 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 8541 | idSet.insert(id); |
| 8542 | } |
| 8543 | } |
| 8544 | |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 8545 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_CorrectRepeatCountWhenInjectKeyRepeat) { |
| 8546 | injectKeyRepeat(0); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8547 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 8548 | for (int32_t repeatCount = 1; repeatCount <= 2; ++repeatCount) { |
| 8549 | expectKeyRepeatOnce(repeatCount); |
| 8550 | } |
| 8551 | injectKeyRepeat(1); |
| 8552 | // Expect repeatCount to be 3 instead of 1 |
| 8553 | expectKeyRepeatOnce(3); |
| 8554 | } |
| 8555 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8556 | /* Test InputDispatcher for MultiDisplay */ |
| 8557 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 8558 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 8559 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8560 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8561 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8562 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8563 | windowInPrimary = sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", |
| 8564 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8565 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8566 | // Set focus window for primary display, but focused display would be second one. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8567 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8568 | windowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8569 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
| 8570 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8571 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8572 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8573 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8574 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8575 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8576 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8577 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8578 | // Set focus display to second one. |
| 8579 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
Arpit Singh | b65e2bd | 2024-06-03 09:48:16 +0000 | [diff] [blame^] | 8580 | mFakePolicy->assertFocusedDisplayNotified(SECOND_DISPLAY_ID); |
| 8581 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8582 | // Set focus window for second display. |
| 8583 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8584 | windowInSecondary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8585 | mDispatcher->onWindowInfosChanged( |
| 8586 | {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8587 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8588 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8589 | } |
| 8590 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 8591 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8592 | InputDispatcherTest::TearDown(); |
| 8593 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8594 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8595 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8596 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8597 | windowInSecondary.clear(); |
| 8598 | } |
| 8599 | |
| 8600 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8601 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8602 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8603 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8604 | sp<FakeWindowHandle> windowInSecondary; |
| 8605 | }; |
| 8606 | |
| 8607 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 8608 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8609 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8610 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 8611 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8612 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8613 | windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8614 | windowInSecondary->assertNoEvents(); |
| 8615 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8616 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8617 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8618 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8619 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8620 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 8621 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8622 | } |
| 8623 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8624 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 8625 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 8626 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8627 | injectKeyDownNoRepeat(*mDispatcher, ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8628 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8629 | windowInPrimary->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 8630 | windowInSecondary->assertNoEvents(); |
| 8631 | |
| 8632 | // Test inject a key down without display id specified. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8633 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8634 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8635 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8636 | windowInSecondary->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8637 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8638 | // Remove all windows in secondary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8639 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8640 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8641 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8642 | windowInSecondary->consumeKeyUp(ui::LogicalDisplayId::INVALID, AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8643 | |
| 8644 | // Test inject a key down, should timeout because of no target window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8645 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8646 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8647 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8648 | windowInSecondary->assertNoEvents(); |
| 8649 | } |
| 8650 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8651 | // Test per-display input monitors for motion event. |
| 8652 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8653 | FakeMonitorReceiver monitorInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8654 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8655 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 8656 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8657 | |
| 8658 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8659 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8660 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 8661 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8662 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8663 | windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 8664 | monitorInPrimary.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8665 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8666 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8667 | |
| 8668 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8669 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8670 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8671 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8672 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8673 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 8674 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8675 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8676 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 8677 | // Lift up the touch from the second display |
| 8678 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8679 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 8680 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8681 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 8682 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 8683 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8684 | // Test inject a non-pointer motion event. |
| 8685 | // If specific a display, it will dispatch to the focused window of particular display, |
| 8686 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8687 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8688 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, |
| 8689 | ui::LogicalDisplayId::INVALID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8690 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8691 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8692 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8693 | windowInSecondary->consumeMotionDown(ui::LogicalDisplayId::INVALID); |
| 8694 | monitorInSecondary.consumeMotionDown(ui::LogicalDisplayId::INVALID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8695 | } |
| 8696 | |
| 8697 | // Test per-display input monitors for key event. |
| 8698 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8699 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8700 | FakeMonitorReceiver monitorInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8701 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8702 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 8703 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8704 | |
| 8705 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8706 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8707 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8708 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8709 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8710 | windowInSecondary->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
| 8711 | monitorInSecondary.consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8712 | } |
| 8713 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8714 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 8715 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8716 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", |
| 8717 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8718 | secondWindowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8719 | mDispatcher->onWindowInfosChanged( |
| 8720 | {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(), |
| 8721 | *windowInSecondary->getInfo()}, |
| 8722 | {}, |
| 8723 | 0, |
| 8724 | 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8725 | setFocusedWindow(secondWindowInPrimary); |
| 8726 | windowInPrimary->consumeFocusEvent(false); |
| 8727 | secondWindowInPrimary->consumeFocusEvent(true); |
| 8728 | |
| 8729 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8730 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8731 | injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8732 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8733 | windowInPrimary->assertNoEvents(); |
| 8734 | windowInSecondary->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8735 | secondWindowInPrimary->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8736 | } |
| 8737 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8738 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 8739 | FakeMonitorReceiver monitorInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8740 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8741 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 8742 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8743 | |
| 8744 | // Test touch down on primary display. |
| 8745 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8746 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 8747 | ui::LogicalDisplayId::DEFAULT)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8748 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8749 | windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 8750 | monitorInPrimary.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8751 | |
| 8752 | // Test touch down on second display. |
| 8753 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8754 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8755 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8756 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 8757 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 8758 | |
| 8759 | // Trigger cancel touch. |
| 8760 | mDispatcher->cancelCurrentTouch(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8761 | windowInPrimary->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT); |
| 8762 | monitorInPrimary.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8763 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 8764 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 8765 | |
| 8766 | // Test inject a move motion event, no window/monitor should receive the event. |
| 8767 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8768 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8769 | ui::LogicalDisplayId::DEFAULT, {110, 200})) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8770 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 8771 | windowInPrimary->assertNoEvents(); |
| 8772 | monitorInPrimary.assertNoEvents(); |
| 8773 | |
| 8774 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8775 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8776 | SECOND_DISPLAY_ID, {110, 200})) |
| 8777 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 8778 | windowInSecondary->assertNoEvents(); |
| 8779 | monitorInSecondary.assertNoEvents(); |
| 8780 | } |
| 8781 | |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8782 | /** |
| 8783 | * Send a key to the primary display and to the secondary display. |
| 8784 | * Then cause the key on the primary display to be canceled by sending in a stale key. |
| 8785 | * Ensure that the key on the primary display is canceled, and that the key on the secondary display |
| 8786 | * does not get canceled. |
| 8787 | */ |
| 8788 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture) { |
| 8789 | // Send a key down on primary display |
| 8790 | mDispatcher->notifyKey( |
| 8791 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8792 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8793 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 8794 | .build()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8795 | windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), |
| 8796 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8797 | windowInSecondary->assertNoEvents(); |
| 8798 | |
| 8799 | // Send a key down on second display |
| 8800 | mDispatcher->notifyKey( |
| 8801 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 8802 | .displayId(SECOND_DISPLAY_ID) |
| 8803 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 8804 | .build()); |
| 8805 | windowInSecondary->consumeKeyEvent( |
| 8806 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID))); |
| 8807 | windowInPrimary->assertNoEvents(); |
| 8808 | |
| 8809 | // Send a valid key up event on primary display that will be dropped because it is stale |
| 8810 | NotifyKeyArgs staleKeyUp = |
| 8811 | KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8812 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8813 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 8814 | .build(); |
| 8815 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms; |
| 8816 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
| 8817 | std::this_thread::sleep_for(STALE_EVENT_TIMEOUT); |
| 8818 | mDispatcher->notifyKey(staleKeyUp); |
| 8819 | |
| 8820 | // Only the key gesture corresponding to the dropped event should receive the cancel event. |
| 8821 | // Therefore, windowInPrimary should get the cancel event and windowInSecondary should not |
| 8822 | // receive any events. |
| 8823 | windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8824 | WithDisplayId(ui::LogicalDisplayId::DEFAULT), |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8825 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 8826 | windowInSecondary->assertNoEvents(); |
| 8827 | } |
| 8828 | |
| 8829 | /** |
| 8830 | * Similar to 'WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture' but for motion events. |
| 8831 | */ |
| 8832 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropMotionEvent_OnlyCancelCorrespondingGesture) { |
| 8833 | // Send touch down on primary display. |
| 8834 | mDispatcher->notifyMotion( |
| 8835 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8836 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8837 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8838 | .build()); |
| 8839 | windowInPrimary->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8840 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8841 | windowInSecondary->assertNoEvents(); |
| 8842 | |
| 8843 | // Send touch down on second display. |
| 8844 | mDispatcher->notifyMotion( |
| 8845 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8846 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 8847 | .displayId(SECOND_DISPLAY_ID) |
| 8848 | .build()); |
| 8849 | windowInPrimary->assertNoEvents(); |
| 8850 | windowInSecondary->consumeMotionEvent( |
| 8851 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID))); |
| 8852 | |
| 8853 | // inject a valid MotionEvent on primary display that will be stale when it arrives. |
| 8854 | NotifyMotionArgs staleMotionUp = |
| 8855 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8856 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8857 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 8858 | .build(); |
| 8859 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms; |
| 8860 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
| 8861 | std::this_thread::sleep_for(STALE_EVENT_TIMEOUT); |
| 8862 | mDispatcher->notifyMotion(staleMotionUp); |
| 8863 | |
| 8864 | // For stale motion events, we let the gesture to complete. This behaviour is different from key |
| 8865 | // events, where we would cancel the current keys instead. |
| 8866 | windowInPrimary->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 8867 | windowInSecondary->assertNoEvents(); |
| 8868 | } |
| 8869 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8870 | class InputFilterTest : public InputDispatcherTest { |
| 8871 | protected: |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 8872 | void testNotifyMotion(ui::LogicalDisplayId displayId, bool expectToBeFiltered, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 8873 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8874 | NotifyMotionArgs motionArgs; |
| 8875 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8876 | motionArgs = |
| 8877 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8878 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8879 | motionArgs = |
| 8880 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8881 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8882 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8883 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 8884 | const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue()); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 8885 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8886 | } else { |
| 8887 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 8888 | } |
| 8889 | } |
| 8890 | |
| 8891 | void testNotifyKey(bool expectToBeFiltered) { |
| 8892 | NotifyKeyArgs keyArgs; |
| 8893 | |
| 8894 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8895 | mDispatcher->notifyKey(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8896 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8897 | mDispatcher->notifyKey(keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8898 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8899 | |
| 8900 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 8901 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8902 | } else { |
| 8903 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 8904 | } |
| 8905 | } |
| 8906 | }; |
| 8907 | |
| 8908 | // Test InputFilter for MotionEvent |
| 8909 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 8910 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8911 | testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/false); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8912 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8913 | |
| 8914 | // Enable InputFilter |
| 8915 | mDispatcher->setInputFilterEnabled(true); |
| 8916 | // Test touch on both primary and second display, and check if both events are filtered. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8917 | testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/true); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8918 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8919 | |
| 8920 | // Disable InputFilter |
| 8921 | mDispatcher->setInputFilterEnabled(false); |
| 8922 | // Test touch on both primary and second display, and check if both events aren't filtered. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8923 | testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/false); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8924 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8925 | } |
| 8926 | |
| 8927 | // Test InputFilter for KeyEvent |
| 8928 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 8929 | // 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] | 8930 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8931 | |
| 8932 | // Enable InputFilter |
| 8933 | mDispatcher->setInputFilterEnabled(true); |
| 8934 | // Send a key event, and check if it is filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8935 | testNotifyKey(/*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8936 | |
| 8937 | // Disable InputFilter |
| 8938 | mDispatcher->setInputFilterEnabled(false); |
| 8939 | // Send a key event, and check if it isn't filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8940 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8941 | } |
| 8942 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 8943 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 8944 | // logical display coordinate space. |
| 8945 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 8946 | ui::Transform firstDisplayTransform; |
| 8947 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 8948 | ui::Transform secondDisplayTransform; |
| 8949 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 8950 | |
| 8951 | std::vector<gui::DisplayInfo> displayInfos(2); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8952 | displayInfos[0].displayId = ui::LogicalDisplayId::DEFAULT; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 8953 | displayInfos[0].transform = firstDisplayTransform; |
| 8954 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 8955 | displayInfos[1].transform = secondDisplayTransform; |
| 8956 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 8957 | mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0}); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 8958 | |
| 8959 | // Enable InputFilter |
| 8960 | mDispatcher->setInputFilterEnabled(true); |
| 8961 | |
| 8962 | // Ensure the correct transforms are used for the displays. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8963 | testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/true, |
| 8964 | firstDisplayTransform); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8965 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 8966 | } |
| 8967 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8968 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 8969 | protected: |
| 8970 | virtual void SetUp() override { |
| 8971 | InputDispatcherTest::SetUp(); |
| 8972 | |
| 8973 | /** |
| 8974 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 8975 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 8976 | * on. |
| 8977 | */ |
| 8978 | mDispatcher->setInputFilterEnabled(true); |
| 8979 | |
| 8980 | std::shared_ptr<InputApplicationHandle> application = |
| 8981 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8982 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8983 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8984 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8985 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8986 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8987 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8988 | setFocusedWindow(mWindow); |
| 8989 | mWindow->consumeFocusEvent(true); |
| 8990 | } |
| 8991 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 8992 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 8993 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8994 | KeyEvent event; |
| 8995 | |
| 8996 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 8997 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8998 | ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, |
| 8999 | AKEYCODE_A, KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9000 | const int32_t additionalPolicyFlags = |
| 9001 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 9002 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9003 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 9004 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9005 | policyFlags | additionalPolicyFlags)); |
| 9006 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9007 | mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags))); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9008 | } |
| 9009 | |
| 9010 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 9011 | int32_t flags) { |
| 9012 | MotionEvent event; |
| 9013 | PointerProperties pointerProperties[1]; |
| 9014 | PointerCoords pointerCoords[1]; |
| 9015 | pointerProperties[0].clear(); |
| 9016 | pointerProperties[0].id = 0; |
| 9017 | pointerCoords[0].clear(); |
| 9018 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 9019 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 9020 | |
| 9021 | ui::Transform identityTransform; |
| 9022 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 9023 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 9024 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 9025 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 9026 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 9027 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 9028 | eventTime, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 9029 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9030 | |
| 9031 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 9032 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9033 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 9034 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9035 | policyFlags | additionalPolicyFlags)); |
| 9036 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9037 | mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId))); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9038 | } |
| 9039 | |
| 9040 | private: |
| 9041 | sp<FakeWindowHandle> mWindow; |
| 9042 | }; |
| 9043 | |
| 9044 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9045 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 9046 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 9047 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9048 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 9049 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9050 | } |
| 9051 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9052 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9053 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9054 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9055 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 9056 | } |
| 9057 | |
| 9058 | TEST_F(InputFilterInjectionPolicyTest, |
| 9059 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 9060 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9061 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9062 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9063 | } |
| 9064 | |
| 9065 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9066 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 9067 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9068 | } |
| 9069 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9070 | class InputDispatcherUserActivityPokeTests : public InputDispatcherTest { |
| 9071 | protected: |
| 9072 | virtual void SetUp() override { |
| 9073 | InputDispatcherTest::SetUp(); |
| 9074 | |
| 9075 | std::shared_ptr<FakeApplicationHandle> application = |
| 9076 | std::make_shared<FakeApplicationHandle>(); |
| 9077 | application->setDispatchingTimeout(100ms); |
| 9078 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9079 | ui::LogicalDisplayId::DEFAULT); |
Yeabkal Wubshit | 222d83d | 2024-01-24 18:00:09 +0000 | [diff] [blame] | 9080 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9081 | mWindow->setDispatchingTimeout(100ms); |
| 9082 | mWindow->setFocusable(true); |
| 9083 | |
| 9084 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9085 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9086 | |
| 9087 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 9088 | setFocusedWindow(mWindow); |
| 9089 | mWindow->consumeFocusEvent(true); |
| 9090 | } |
| 9091 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 9092 | void notifyAndConsumeMotion(int32_t action, uint32_t source, ui::LogicalDisplayId displayId, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9093 | nsecs_t eventTime) { |
| 9094 | mDispatcher->notifyMotion(MotionArgsBuilder(action, source) |
| 9095 | .displayId(displayId) |
| 9096 | .eventTime(eventTime) |
| 9097 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 9098 | .build()); |
| 9099 | mWindow->consumeMotionEvent(WithMotionAction(action)); |
| 9100 | } |
| 9101 | |
| 9102 | private: |
| 9103 | sp<FakeWindowHandle> mWindow; |
| 9104 | }; |
| 9105 | |
| 9106 | TEST_F_WITH_FLAGS( |
| 9107 | InputDispatcherUserActivityPokeTests, MinPokeTimeObserved, |
| 9108 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 9109 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 9110 | mDispatcher->setMinTimeBetweenUserActivityPokes(50ms); |
| 9111 | |
| 9112 | // First event of type TOUCH. Should poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9113 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9114 | milliseconds_to_nanoseconds(50)); |
| 9115 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9116 | {{milliseconds_to_nanoseconds(50), USER_ACTIVITY_EVENT_TOUCH, |
| 9117 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9118 | |
| 9119 | // 80ns > 50ns has passed since previous TOUCH event. Should poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9120 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9121 | milliseconds_to_nanoseconds(130)); |
| 9122 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9123 | {{milliseconds_to_nanoseconds(130), USER_ACTIVITY_EVENT_TOUCH, |
| 9124 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9125 | |
| 9126 | // First event of type OTHER. Should poke (despite being within 50ns of previous TOUCH event). |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9127 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, |
| 9128 | ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(135)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9129 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9130 | {{milliseconds_to_nanoseconds(135), USER_ACTIVITY_EVENT_OTHER, |
| 9131 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9132 | |
| 9133 | // Within 50ns of previous TOUCH event. Should NOT poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9134 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9135 | milliseconds_to_nanoseconds(140)); |
| 9136 | mFakePolicy->assertUserActivityNotPoked(); |
| 9137 | |
| 9138 | // Within 50ns of previous OTHER event. Should NOT poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9139 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, |
| 9140 | ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(150)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9141 | mFakePolicy->assertUserActivityNotPoked(); |
| 9142 | |
| 9143 | // Within 50ns of previous TOUCH event (which was at time 130). Should NOT poke. |
| 9144 | // Note that STYLUS is mapped to TOUCH user activity, since it's a pointer-type source. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9145 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_STYLUS, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9146 | milliseconds_to_nanoseconds(160)); |
| 9147 | mFakePolicy->assertUserActivityNotPoked(); |
| 9148 | |
| 9149 | // 65ns > 50ns has passed since previous OTHER event. Should poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9150 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, |
| 9151 | ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(200)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9152 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9153 | {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_OTHER, |
| 9154 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9155 | |
| 9156 | // 170ns > 50ns has passed since previous TOUCH event. Should poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9157 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_STYLUS, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9158 | milliseconds_to_nanoseconds(300)); |
| 9159 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9160 | {{milliseconds_to_nanoseconds(300), USER_ACTIVITY_EVENT_TOUCH, |
| 9161 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9162 | |
| 9163 | // Assert that there's no more user activity poke event. |
| 9164 | mFakePolicy->assertUserActivityNotPoked(); |
| 9165 | } |
| 9166 | |
| 9167 | TEST_F_WITH_FLAGS( |
| 9168 | InputDispatcherUserActivityPokeTests, DefaultMinPokeTimeOf100MsUsed, |
| 9169 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 9170 | rate_limit_user_activity_poke_in_dispatcher))) { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9171 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9172 | milliseconds_to_nanoseconds(200)); |
| 9173 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9174 | {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_TOUCH, |
| 9175 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9176 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9177 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9178 | milliseconds_to_nanoseconds(280)); |
| 9179 | mFakePolicy->assertUserActivityNotPoked(); |
| 9180 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9181 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9182 | milliseconds_to_nanoseconds(340)); |
| 9183 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9184 | {{milliseconds_to_nanoseconds(340), USER_ACTIVITY_EVENT_TOUCH, |
| 9185 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9186 | } |
| 9187 | |
| 9188 | TEST_F_WITH_FLAGS( |
| 9189 | InputDispatcherUserActivityPokeTests, ZeroMinPokeTimeDisablesRateLimiting, |
| 9190 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 9191 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 9192 | mDispatcher->setMinTimeBetweenUserActivityPokes(0ms); |
| 9193 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9194 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
| 9195 | 20); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9196 | mFakePolicy->assertUserActivityPoked(); |
| 9197 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9198 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
| 9199 | 30); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9200 | mFakePolicy->assertUserActivityPoked(); |
| 9201 | } |
| 9202 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9203 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 9204 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9205 | InputDispatcherTest::SetUp(); |
| 9206 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9207 | std::shared_ptr<FakeApplicationHandle> application = |
| 9208 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9209 | mUnfocusedWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 9210 | ui::LogicalDisplayId::DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9211 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9212 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9213 | mFocusedWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 9214 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 9215 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9216 | |
| 9217 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9218 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9219 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9220 | |
| 9221 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9222 | mDispatcher->onWindowInfosChanged( |
| 9223 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9224 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 9225 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9226 | } |
| 9227 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 9228 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9229 | InputDispatcherTest::TearDown(); |
| 9230 | |
| 9231 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 9232 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9233 | } |
| 9234 | |
| 9235 | protected: |
| 9236 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 9237 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 9238 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9239 | }; |
| 9240 | |
| 9241 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 9242 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 9243 | // the onPointerDownOutsideFocus callback. |
| 9244 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9245 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9246 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9247 | ui::LogicalDisplayId::DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9248 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 9249 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9250 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 9251 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9252 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 9253 | } |
| 9254 | |
| 9255 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 9256 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 9257 | // onPointerDownOutsideFocus callback. |
| 9258 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9259 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9260 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9261 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9262 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 9263 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9264 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 9265 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9266 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9267 | } |
| 9268 | |
| 9269 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 9270 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 9271 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 9272 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9273 | injectKeyDownNoRepeat(*mDispatcher, ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9274 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9275 | mFocusedWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9276 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 9277 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9278 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9279 | } |
| 9280 | |
| 9281 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 9282 | // DOWN on the window that already has focus. Ensure no window received the |
| 9283 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 9284 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9285 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9286 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9287 | ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9288 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 9289 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9290 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 9291 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9292 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9293 | } |
| 9294 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 9295 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 9296 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 9297 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 9298 | const MotionEvent event = |
| 9299 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 9300 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9301 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 9302 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 9303 | .build(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9304 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 9305 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9306 | mUnfocusedWindow->consumeAnyMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 9307 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 9308 | |
| 9309 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9310 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 9311 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 9312 | mUnfocusedWindow->assertNoEvents(); |
| 9313 | } |
| 9314 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9315 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 9316 | // windows that point to the same client token. |
| 9317 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 9318 | virtual void SetUp() override { |
| 9319 | InputDispatcherTest::SetUp(); |
| 9320 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9321 | std::shared_ptr<FakeApplicationHandle> application = |
| 9322 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9323 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9324 | ui::LogicalDisplayId::DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9325 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 9326 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9327 | mWindow2 = mWindow1->clone(ui::LogicalDisplayId::DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9328 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 9329 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9330 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9331 | } |
| 9332 | |
| 9333 | protected: |
| 9334 | sp<FakeWindowHandle> mWindow1; |
| 9335 | sp<FakeWindowHandle> mWindow2; |
| 9336 | |
| 9337 | // 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] | 9338 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 9339 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 9340 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9341 | } |
| 9342 | |
| 9343 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 9344 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 9345 | const std::string name = window->getName(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9346 | std::unique_ptr<MotionEvent> motionEvent = |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 9347 | window->consumeMotionEvent(WithMotionAction(expectedAction)); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9348 | ASSERT_NE(nullptr, motionEvent); |
| 9349 | ASSERT_EQ(points.size(), motionEvent->getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9350 | |
| 9351 | for (size_t i = 0; i < points.size(); i++) { |
| 9352 | float expectedX = points[i].x; |
| 9353 | float expectedY = points[i].y; |
| 9354 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9355 | EXPECT_EQ(expectedX, motionEvent->getX(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9356 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9357 | << ", got " << motionEvent->getX(i); |
| 9358 | EXPECT_EQ(expectedY, motionEvent->getY(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9359 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9360 | << ", got " << motionEvent->getY(i); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9361 | } |
| 9362 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9363 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9364 | void touchAndAssertPositions(sp<FakeWindowHandle> touchedWindow, int32_t action, |
| 9365 | const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9366 | std::vector<PointF> expectedPoints) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9367 | mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9368 | ui::LogicalDisplayId::DEFAULT, touchedPoints)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9369 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9370 | consumeMotionEvent(touchedWindow, action, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9371 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9372 | }; |
| 9373 | |
| 9374 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 9375 | // Touch Window 1 |
| 9376 | PointF touchedPoint = {10, 10}; |
| 9377 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9378 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9379 | |
| 9380 | // Release touch on Window 1 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9381 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9382 | |
| 9383 | // Touch Window 2 |
| 9384 | touchedPoint = {150, 150}; |
| 9385 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9386 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9387 | } |
| 9388 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9389 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 9390 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9391 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9392 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9393 | |
| 9394 | // Touch Window 1 |
| 9395 | PointF touchedPoint = {10, 10}; |
| 9396 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9397 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9398 | // Release touch on Window 1 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9399 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9400 | |
| 9401 | // Touch Window 2 |
| 9402 | touchedPoint = {150, 150}; |
| 9403 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9404 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 9405 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9406 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9407 | // Update the transform so rotation is set |
| 9408 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9409 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9410 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9411 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9412 | } |
| 9413 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9414 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9415 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9416 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9417 | |
| 9418 | // Touch Window 1 |
| 9419 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 9420 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9421 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9422 | |
| 9423 | // Touch Window 2 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9424 | // Since this is part of the same touch gesture that has already been dispatched to Window 1, |
| 9425 | // the touch stream from Window 2 will be merged with the stream in Window 1. The merged stream |
| 9426 | // will continue to be dispatched through Window 1. |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9427 | touchedPoints.push_back(PointF{150, 150}); |
| 9428 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9429 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9430 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9431 | // Release Window 2 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9432 | touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9433 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9434 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9435 | // Update the transform so rotation is set for Window 2 |
| 9436 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9437 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9438 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9439 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9440 | } |
| 9441 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9442 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9443 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9444 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9445 | |
| 9446 | // Touch Window 1 |
| 9447 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 9448 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9449 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9450 | |
| 9451 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9452 | touchedPoints.push_back(PointF{150, 150}); |
| 9453 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9454 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9455 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9456 | |
| 9457 | // Move both windows |
| 9458 | touchedPoints = {{20, 20}, {175, 175}}; |
| 9459 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 9460 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 9461 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9462 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9463 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9464 | // Release Window 2 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9465 | touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9466 | expectedPoints.pop_back(); |
| 9467 | |
| 9468 | // Touch Window 2 |
| 9469 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9470 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9471 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9472 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9473 | |
| 9474 | // Move both windows |
| 9475 | touchedPoints = {{20, 20}, {175, 175}}; |
| 9476 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 9477 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 9478 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9479 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9480 | } |
| 9481 | |
| 9482 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 9483 | mWindow1->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9484 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9485 | |
| 9486 | // Touch Window 1 |
| 9487 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 9488 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9489 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9490 | |
| 9491 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9492 | touchedPoints.push_back(PointF{150, 150}); |
| 9493 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9494 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9495 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9496 | |
| 9497 | // Move both windows |
| 9498 | touchedPoints = {{20, 20}, {175, 175}}; |
| 9499 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 9500 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 9501 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9502 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9503 | } |
| 9504 | |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 9505 | /** |
| 9506 | * When one of the windows is slippery, the touch should not slip into the other window with the |
| 9507 | * same input channel. |
| 9508 | */ |
| 9509 | TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) { |
| 9510 | mWindow1->setSlippery(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9511 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 9512 | |
| 9513 | // Touch down in window 1 |
| 9514 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9515 | ui::LogicalDisplayId::DEFAULT, {{50, 50}})); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 9516 | consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}}); |
| 9517 | |
| 9518 | // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip. |
| 9519 | // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN |
| 9520 | // getting generated. |
| 9521 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9522 | ui::LogicalDisplayId::DEFAULT, {{150, 150}})); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 9523 | |
| 9524 | consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}}); |
| 9525 | } |
| 9526 | |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 9527 | /** |
| 9528 | * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and |
| 9529 | * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window |
| 9530 | * that the pointer is hovering over may have a different transform. |
| 9531 | */ |
| 9532 | TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) { |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9533 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 9534 | |
| 9535 | // Start hover in window 1 |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 9536 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN) |
| 9537 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 9538 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 9539 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 9540 | {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 9541 | // Move hover to window 2. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 9542 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 9543 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 9544 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 9545 | consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}}); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9546 | consumeMotionEvent(mWindow2, ACTION_HOVER_ENTER, |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 9547 | {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})}); |
| 9548 | } |
| 9549 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9550 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 9551 | virtual void SetUp() override { |
| 9552 | InputDispatcherTest::SetUp(); |
| 9553 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9554 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9555 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9556 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9557 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9558 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9559 | mWindow->setDispatchingTimeout(100ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9560 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9561 | |
| 9562 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9563 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9564 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9565 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9566 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9567 | mWindow->consumeFocusEvent(true); |
| 9568 | } |
| 9569 | |
| 9570 | virtual void TearDown() override { |
| 9571 | InputDispatcherTest::TearDown(); |
| 9572 | mWindow.clear(); |
| 9573 | } |
| 9574 | |
| 9575 | protected: |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9576 | static constexpr std::chrono::duration SPY_TIMEOUT = 200ms; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9577 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9578 | sp<FakeWindowHandle> mWindow; |
| 9579 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 9580 | |
| 9581 | void tapOnWindow() { |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 9582 | const auto touchingPointer = PointerBuilder(/*id=*/0, ToolType::FINGER) |
| 9583 | .x(WINDOW_LOCATION.x) |
| 9584 | .y(WINDOW_LOCATION.y); |
| 9585 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9586 | .pointer(touchingPointer) |
| 9587 | .build()); |
| 9588 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 9589 | .pointer(touchingPointer) |
| 9590 | .build()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9591 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9592 | |
| 9593 | sp<FakeWindowHandle> addSpyWindow() { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9594 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", |
| 9595 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9596 | spy->setTrustedOverlay(true); |
| 9597 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9598 | spy->setSpy(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9599 | spy->setDispatchingTimeout(SPY_TIMEOUT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9600 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9601 | return spy; |
| 9602 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9603 | }; |
| 9604 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9605 | // Send a tap and respond, which should not cause an ANR. |
| 9606 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 9607 | tapOnWindow(); |
| 9608 | mWindow->consumeMotionDown(); |
| 9609 | mWindow->consumeMotionUp(); |
| 9610 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9611 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9612 | } |
| 9613 | |
| 9614 | // Send a regular key and respond, which should not cause an ANR. |
| 9615 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9616 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9617 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9618 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9619 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9620 | } |
| 9621 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 9622 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 9623 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9624 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 9625 | mWindow->consumeFocusEvent(false); |
| 9626 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9627 | InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9628 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 9629 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
| 9630 | CONSUME_TIMEOUT_EVENT_EXPECTED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9631 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9632 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 9633 | // Key will not go to window because we have no focused window. |
| 9634 | // The 'no focused window' ANR timer should start instead. |
| 9635 | |
| 9636 | // Now, the focused application goes away. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9637 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, nullptr); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 9638 | // The key should get dropped and there should be no ANR. |
| 9639 | |
| 9640 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9641 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9642 | } |
| 9643 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9644 | // 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] | 9645 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 9646 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9647 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9648 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9649 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9650 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9651 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9652 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9653 | ASSERT_TRUE(sequenceNum); |
| 9654 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9655 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9656 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9657 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 9658 | mWindow->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9659 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9660 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9661 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9662 | } |
| 9663 | |
| 9664 | // Send a key to the app and have the app not respond right away. |
| 9665 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 9666 | // Inject a key, and don't respond - expect that ANR is called. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9667 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9668 | const auto [sequenceNum, _] = mWindow->receiveEvent(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9669 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9670 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9671 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9672 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9673 | } |
| 9674 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9675 | // We have a focused application, but no focused window |
| 9676 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9677 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9678 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9679 | mWindow->consumeFocusEvent(false); |
| 9680 | |
| 9681 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9682 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9683 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9684 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9685 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 9686 | mDispatcher->waitForIdle(); |
| 9687 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9688 | |
| 9689 | // Once a focused event arrives, we get an ANR for this application |
| 9690 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 9691 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9692 | const InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9693 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 9694 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 9695 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9696 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9697 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 9698 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9699 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9700 | } |
| 9701 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 9702 | /** |
| 9703 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 9704 | * there will not be an ANR. |
| 9705 | */ |
| 9706 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 9707 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9708 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 9709 | mWindow->consumeFocusEvent(false); |
| 9710 | |
| 9711 | KeyEvent event; |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 9712 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 9713 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 9714 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 9715 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 9716 | |
| 9717 | // Define a valid key down event that is stale (too old). |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9718 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 9719 | ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, |
| 9720 | /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, |
| 9721 | eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 9722 | |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 9723 | const int32_t policyFlags = |
| 9724 | POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 9725 | |
| 9726 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9727 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 9728 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 9729 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 9730 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 9731 | << "Injection should fail because the event is stale"; |
| 9732 | |
| 9733 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9734 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9735 | mWindow->assertNoEvents(); |
| 9736 | } |
| 9737 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9738 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9739 | // Make sure that we don't notify policy twice about the same ANR. |
| 9740 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 9741 | const std::chrono::duration appTimeout = 400ms; |
| 9742 | mApplication->setDispatchingTimeout(appTimeout); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9743 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication); |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 9744 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9745 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9746 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9747 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9748 | |
| 9749 | // Once a focused event arrives, we get an ANR for this application |
| 9750 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 9751 | // injection times out (instead of failing). |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 9752 | const std::chrono::duration eventInjectionTimeout = 100ms; |
| 9753 | ASSERT_LT(eventInjectionTimeout, appTimeout); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9754 | const InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9755 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 9756 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 9757 | eventInjectionTimeout, |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 9758 | /*allowKeyRepeat=*/false); |
| 9759 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result) |
| 9760 | << "result=" << ftl::enum_string(result); |
| 9761 | // We already waited for 'eventInjectionTimeout`, because the countdown started when the event |
| 9762 | // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait. |
| 9763 | std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout; |
| 9764 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9765 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 9766 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9767 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 9768 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9769 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9770 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 9771 | // '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] | 9772 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9773 | } |
| 9774 | |
| 9775 | // We have a focused application, but no focused window |
| 9776 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9777 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9778 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9779 | mWindow->consumeFocusEvent(false); |
| 9780 | |
| 9781 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9782 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9783 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 9784 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 9785 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9786 | |
| 9787 | // Future focused events get dropped right away |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9788 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9789 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9790 | mWindow->assertNoEvents(); |
| 9791 | } |
| 9792 | |
| 9793 | /** |
| 9794 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 9795 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 9796 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 9797 | * the ANR mechanism should still work. |
| 9798 | * |
| 9799 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 9800 | * DOWN event, while not responding on the second one. |
| 9801 | */ |
| 9802 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 9803 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9804 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9805 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9806 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 9807 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9808 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9809 | |
| 9810 | // Now send ACTION_UP, with identical timestamp |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9811 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9812 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9813 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 9814 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9815 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9816 | |
| 9817 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9818 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9819 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9820 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9821 | } |
| 9822 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9823 | // A spy window can receive an ANR |
| 9824 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 9825 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 9826 | |
| 9827 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9828 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9829 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9830 | mWindow->consumeMotionDown(); |
| 9831 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9832 | const auto [sequenceNum, _] = spy->receiveEvent(); // ACTION_DOWN |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9833 | ASSERT_TRUE(sequenceNum); |
| 9834 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9835 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9836 | |
| 9837 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 9838 | spy->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9839 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9840 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9841 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9842 | } |
| 9843 | |
| 9844 | // 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] | 9845 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9846 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 9847 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9848 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9849 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9850 | injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT)); |
| 9851 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
| 9852 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9853 | injectKeyUp(*mDispatcher, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9854 | |
| 9855 | // Stuck on the ACTION_UP |
| 9856 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9857 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9858 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9859 | // 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] | 9860 | tapOnWindow(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9861 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 9862 | spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9863 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9864 | mWindow->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); // still the previous motion |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9865 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9866 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9867 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9868 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9869 | } |
| 9870 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9871 | // 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] | 9872 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9873 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 9874 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9875 | |
| 9876 | tapOnWindow(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9877 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 9878 | spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9879 | |
| 9880 | mWindow->consumeMotionDown(); |
| 9881 | // Stuck on the ACTION_UP |
| 9882 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9883 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9884 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9885 | // 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] | 9886 | tapOnWindow(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9887 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 9888 | spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9889 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9890 | mWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); // still the previous motion |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9891 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9892 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9893 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9894 | spy->assertNoEvents(); |
| 9895 | } |
| 9896 | |
| 9897 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9898 | mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9899 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9900 | FakeMonitorReceiver monitor = |
| 9901 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9902 | |
| 9903 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9904 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9905 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9906 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9907 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9908 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 9909 | ASSERT_TRUE(consumeSeq); |
| 9910 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9911 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(), |
| 9912 | MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9913 | |
| 9914 | monitor.finishEvent(*consumeSeq); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9915 | monitor.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9916 | |
| 9917 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9918 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9919 | } |
| 9920 | |
| 9921 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 9922 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 9923 | // get an ANR again. |
| 9924 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 9925 | // 2. consume all pending events (= queue becomes healthy again) |
| 9926 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 9927 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 9928 | tapOnWindow(); |
| 9929 | |
| 9930 | mWindow->consumeMotionDown(); |
| 9931 | // Block on ACTION_UP |
| 9932 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9933 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9934 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 9935 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9936 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9937 | mWindow->assertNoEvents(); |
| 9938 | |
| 9939 | tapOnWindow(); |
| 9940 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9941 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9942 | mWindow->consumeMotionUp(); |
| 9943 | |
| 9944 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9945 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9946 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9947 | mWindow->assertNoEvents(); |
| 9948 | } |
| 9949 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9950 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 9951 | // it. |
| 9952 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9953 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9954 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9955 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9956 | |
| 9957 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9958 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9959 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9960 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 9961 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9962 | // 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] | 9963 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 9964 | mWindow->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9965 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9966 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9967 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9968 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9969 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9970 | } |
| 9971 | |
| 9972 | /** |
| 9973 | * 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] | 9974 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9975 | */ |
| 9976 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 9977 | // The timeouts in this test are established by relying on the fact that the "key waiting for |
| 9978 | // events timeout" is equal to 500ms. |
| 9979 | ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9980 | 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] | 9981 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9982 | |
| 9983 | tapOnWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9984 | const auto& [downSequenceNum, downEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9985 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9986 | const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9987 | ASSERT_TRUE(upSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9988 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 9989 | // Don't finish the events yet, and send a key |
| 9990 | mDispatcher->notifyKey( |
| 9991 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 9992 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 9993 | .build()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9994 | // Key will not be sent to the window, yet, because the window is still processing events |
| 9995 | // 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] | 9996 | // 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] | 9997 | mWindow->assertNoEvents(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9998 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 9999 | std::this_thread::sleep_for(400ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10000 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 10001 | // to the focused window, even though we have not yet finished the motion event |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10002 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10003 | mWindow->finishEvent(*downSequenceNum); |
| 10004 | mWindow->finishEvent(*upSequenceNum); |
| 10005 | } |
| 10006 | |
| 10007 | /** |
| 10008 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 10009 | * not go to the focused window until the motion is processed. |
| 10010 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 10011 | * focused window right away. |
| 10012 | */ |
| 10013 | TEST_F(InputDispatcherSingleWindowAnr, |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 10014 | PendingKey_IsDeliveredWhileMotionIsProcessingAndNewTouchComesIn) { |
| 10015 | // The timeouts in this test are established by relying on the fact that the "key waiting for |
| 10016 | // events timeout" is equal to 500ms. |
| 10017 | ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10018 | 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] | 10019 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10020 | |
| 10021 | tapOnWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10022 | const auto& [downSequenceNum, _] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10023 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10024 | const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10025 | ASSERT_TRUE(upSequenceNum); |
| 10026 | // Don't finish the events yet, and send a key |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 10027 | mDispatcher->notifyKey( |
| 10028 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 10029 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 10030 | .build()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10031 | // 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] | 10032 | mWindow->assertNoEvents(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10033 | |
| 10034 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 10035 | tapOnWindow(); |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 10036 | // Now that we tapped, we should receive the key immediately. |
| 10037 | // Since there's still room for slowness, we use 200ms, which is much less than |
| 10038 | // the "key waiting for events' timeout of 500ms minus the already waited 100ms duration. |
| 10039 | std::unique_ptr<InputEvent> keyEvent = mWindow->consume(200ms); |
| 10040 | ASSERT_NE(nullptr, keyEvent); |
| 10041 | ASSERT_EQ(InputEventType::KEY, keyEvent->getType()); |
| 10042 | ASSERT_THAT(static_cast<KeyEvent&>(*keyEvent), WithKeyAction(AKEY_EVENT_ACTION_DOWN)); |
| 10043 | // it doesn't matter that we haven't ack'd the other events yet. We can finish events in any |
| 10044 | // order. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10045 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 10046 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 10047 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 10048 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10049 | mWindow->assertNoEvents(); |
| 10050 | } |
| 10051 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 10052 | /** |
| 10053 | * Send an event to the app and have the app not respond right away. |
| 10054 | * When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 10055 | * So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 10056 | * At some point, the window becomes responsive again. |
| 10057 | * Ensure that subsequent events get dropped, and the next gesture is delivered. |
| 10058 | */ |
| 10059 | TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) { |
| 10060 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10061 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 10062 | .build()); |
| 10063 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10064 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 10065 | ASSERT_TRUE(sequenceNum); |
| 10066 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 10067 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 10068 | |
| 10069 | mWindow->finishEvent(*sequenceNum); |
| 10070 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 10071 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10072 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
| 10073 | |
| 10074 | // Now that the window is responsive, let's continue the gesture. |
| 10075 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 10076 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 10077 | .build()); |
| 10078 | |
| 10079 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10080 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 10081 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 10082 | .build()); |
| 10083 | |
| 10084 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 10085 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 10086 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 10087 | .build()); |
| 10088 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 10089 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 10090 | .build()); |
| 10091 | // We already canceled this pointer, so the window shouldn't get any new events. |
| 10092 | mWindow->assertNoEvents(); |
| 10093 | |
| 10094 | // Start another one. |
| 10095 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10096 | .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15)) |
| 10097 | .build()); |
| 10098 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 10099 | } |
| 10100 | |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10101 | // Send an event to the app and have the app not respond right away. Then remove the app window. |
| 10102 | // When the window is removed, the dispatcher will cancel the events for that window. |
| 10103 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 10104 | TEST_F(InputDispatcherSingleWindowAnr, AnrAfterWindowRemoval) { |
| 10105 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10106 | AINPUT_SOURCE_TOUCHSCREEN, |
| 10107 | ui::LogicalDisplayId::DEFAULT, {WINDOW_LOCATION})); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10108 | |
| 10109 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
| 10110 | ASSERT_TRUE(sequenceNum); |
| 10111 | |
| 10112 | // Remove the window, but the input channel should remain alive. |
| 10113 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 10114 | |
| 10115 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 10116 | // Since the window was removed, Dispatcher does not know the PID associated with the window |
| 10117 | // anymore, so the policy is notified without the PID. |
| 10118 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken(), |
| 10119 | /*pid=*/std::nullopt); |
| 10120 | |
| 10121 | mWindow->finishEvent(*sequenceNum); |
| 10122 | // The cancellation was generated when the window was removed, along with the focus event. |
| 10123 | mWindow->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10124 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10125 | mWindow->consumeFocusEvent(false); |
| 10126 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10127 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt); |
| 10128 | } |
| 10129 | |
| 10130 | // Send an event to the app and have the app not respond right away. Wait for the policy to be |
| 10131 | // notified of the unresponsive window, then remove the app window. |
| 10132 | TEST_F(InputDispatcherSingleWindowAnr, AnrFollowedByWindowRemoval) { |
| 10133 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10134 | AINPUT_SOURCE_TOUCHSCREEN, |
| 10135 | ui::LogicalDisplayId::DEFAULT, {WINDOW_LOCATION})); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10136 | |
| 10137 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
| 10138 | ASSERT_TRUE(sequenceNum); |
| 10139 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 10140 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 10141 | |
| 10142 | // Remove the window, but the input channel should remain alive. |
| 10143 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 10144 | |
| 10145 | mWindow->finishEvent(*sequenceNum); |
| 10146 | // The cancellation was generated during the ANR, and the window lost focus when it was removed. |
| 10147 | mWindow->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10148 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10149 | mWindow->consumeFocusEvent(false); |
| 10150 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10151 | // Since the window was removed, Dispatcher does not know the PID associated with the window |
| 10152 | // becoming responsive, so the policy is notified without the PID. |
| 10153 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt); |
| 10154 | } |
| 10155 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10156 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 10157 | virtual void SetUp() override { |
| 10158 | InputDispatcherTest::SetUp(); |
| 10159 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 10160 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 10161 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10162 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10163 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10164 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10165 | // 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] | 10166 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10167 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10168 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10169 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 10170 | mFocusedWindow->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10171 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10172 | |
| 10173 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10174 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 10175 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10176 | |
| 10177 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10178 | mDispatcher->onWindowInfosChanged( |
| 10179 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10180 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10181 | mFocusedWindow->consumeFocusEvent(true); |
| 10182 | } |
| 10183 | |
| 10184 | virtual void TearDown() override { |
| 10185 | InputDispatcherTest::TearDown(); |
| 10186 | |
| 10187 | mUnfocusedWindow.clear(); |
| 10188 | mFocusedWindow.clear(); |
| 10189 | } |
| 10190 | |
| 10191 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 10192 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10193 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 10194 | sp<FakeWindowHandle> mFocusedWindow; |
| 10195 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 10196 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 10197 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 10198 | |
| 10199 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 10200 | |
| 10201 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 10202 | |
| 10203 | private: |
| 10204 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10205 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10206 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10207 | ui::LogicalDisplayId::DEFAULT, location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10208 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10209 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10210 | ui::LogicalDisplayId::DEFAULT, location)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10211 | } |
| 10212 | }; |
| 10213 | |
| 10214 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 10215 | // should be ANR'd first. |
| 10216 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10217 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 10218 | injectMotionEvent(*mDispatcher, |
| 10219 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10220 | AINPUT_SOURCE_TOUCHSCREEN) |
| 10221 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 10222 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 10223 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 10224 | .build())); |
| 10225 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10226 | injectMotionEvent(*mDispatcher, |
| 10227 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 10228 | AINPUT_SOURCE_TOUCHSCREEN) |
| 10229 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 10230 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 10231 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 10232 | .build())); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10233 | mFocusedWindow->consumeMotionDown(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 10234 | mFocusedWindow->consumeMotionUp(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10235 | mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10236 | // We consumed all events, so no ANR |
| 10237 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10238 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 10239 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10240 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 10241 | injectMotionEvent(*mDispatcher, |
| 10242 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10243 | AINPUT_SOURCE_TOUCHSCREEN) |
| 10244 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 10245 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 10246 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 10247 | .build())); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10248 | const auto [unfocusedSequenceNum, _] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10249 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10250 | |
| 10251 | const std::chrono::duration timeout = |
| 10252 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10253 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 10254 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10255 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10256 | mFocusedWindow->consumeMotionDown(); |
| 10257 | // This cancel is generated because the connection was unresponsive |
| 10258 | mFocusedWindow->consumeMotionCancel(); |
| 10259 | mFocusedWindow->assertNoEvents(); |
| 10260 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10261 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10262 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 10263 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10264 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10265 | } |
| 10266 | |
| 10267 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 10268 | // it doesn't matter which order they should have ANR. |
| 10269 | // But we should receive ANR for both. |
| 10270 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 10271 | // Set the timeout for unfocused window to match the focused window |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 10272 | mUnfocusedWindow->setDispatchingTimeout( |
| 10273 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10274 | mDispatcher->onWindowInfosChanged( |
| 10275 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10276 | |
| 10277 | tapOnFocusedWindow(); |
| 10278 | // 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] | 10279 | // 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] | 10280 | std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken( |
| 10281 | mFocusedWindow->getDispatchingTimeout( |
| 10282 | DISPATCHING_TIMEOUT)), |
| 10283 | mFakePolicy->getUnresponsiveWindowToken(0ms)}; |
| 10284 | |
| 10285 | ASSERT_THAT(anrConnectionTokens, |
| 10286 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 10287 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10288 | |
| 10289 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10290 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10291 | |
| 10292 | mFocusedWindow->consumeMotionDown(); |
| 10293 | mFocusedWindow->consumeMotionUp(); |
| 10294 | mUnfocusedWindow->consumeMotionOutside(); |
| 10295 | |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 10296 | std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(), |
| 10297 | mFakePolicy->getResponsiveWindowToken()}; |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10298 | |
| 10299 | // Both applications should be marked as responsive, in any order |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 10300 | ASSERT_THAT(responsiveTokens, |
| 10301 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 10302 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10303 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10304 | } |
| 10305 | |
| 10306 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 10307 | // We should also log an error to account for the dropped event (not tested here). |
| 10308 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 10309 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 10310 | tapOnFocusedWindow(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10311 | mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10312 | // Receive the events, but don't respond |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10313 | const auto [downEventSequenceNum, downEvent] = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10314 | ASSERT_TRUE(downEventSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10315 | const auto [upEventSequenceNum, upEvent] = mFocusedWindow->receiveEvent(); // ACTION_UP |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10316 | ASSERT_TRUE(upEventSequenceNum); |
| 10317 | const std::chrono::duration timeout = |
| 10318 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10319 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10320 | |
| 10321 | // Tap once again |
| 10322 | // 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] | 10323 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10324 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10325 | ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10326 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10327 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10328 | FOCUSED_WINDOW_LOCATION)); |
| 10329 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 10330 | // valid touch target |
| 10331 | mUnfocusedWindow->assertNoEvents(); |
| 10332 | |
| 10333 | // Consume the first tap |
| 10334 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 10335 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 10336 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10337 | // The second tap did not go to the focused window |
| 10338 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10339 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10340 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 10341 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10342 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 10343 | } |
| 10344 | |
| 10345 | // If you tap outside of all windows, there will not be ANR |
| 10346 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10347 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10348 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10349 | ui::LogicalDisplayId::DEFAULT, LOCATION_OUTSIDE_ALL_WINDOWS)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10350 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10351 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 10352 | } |
| 10353 | |
| 10354 | // Since the focused window is paused, tapping on it should not produce any events |
| 10355 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 10356 | mFocusedWindow->setPaused(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10357 | mDispatcher->onWindowInfosChanged( |
| 10358 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10359 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10360 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10361 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10362 | ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10363 | |
| 10364 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 10365 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10366 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 10367 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 10368 | |
| 10369 | mFocusedWindow->assertNoEvents(); |
| 10370 | mUnfocusedWindow->assertNoEvents(); |
| 10371 | } |
| 10372 | |
| 10373 | /** |
| 10374 | * 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] | 10375 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10376 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 10377 | * |
| 10378 | * Warning!!! |
| 10379 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 10380 | * and the injection timeout that we specify when injecting the key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10381 | * We must have the injection timeout (100ms) be smaller than |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10382 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 10383 | * |
| 10384 | * If that value changes, this test should also change. |
| 10385 | */ |
| 10386 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 10387 | // Set a long ANR timeout to prevent it from triggering |
| 10388 | mFocusedWindow->setDispatchingTimeout(2s); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10389 | mDispatcher->onWindowInfosChanged( |
| 10390 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10391 | |
| 10392 | tapOnUnfocusedWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10393 | const auto [downSequenceNum, downEvent] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10394 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10395 | const auto [upSequenceNum, upEvent] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10396 | ASSERT_TRUE(upSequenceNum); |
| 10397 | // Don't finish the events yet, and send a key |
| 10398 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 10399 | // window even if motions are still being processed. |
| 10400 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10401 | InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10402 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 10403 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 10404 | /*injectionTimeout=*/100ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10405 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10406 | // 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] | 10407 | // and the key remains pending, waiting for the touch events to be processed. |
| 10408 | // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED |
| 10409 | // under the hood. |
| 10410 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 10411 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10412 | |
| 10413 | // 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] | 10414 | mFocusedWindow->setFocusable(false); |
| 10415 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10416 | mDispatcher->onWindowInfosChanged( |
| 10417 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10418 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10419 | |
| 10420 | // Focus events should precede the key events |
| 10421 | mUnfocusedWindow->consumeFocusEvent(true); |
| 10422 | mFocusedWindow->consumeFocusEvent(false); |
| 10423 | |
| 10424 | // Finish the tap events, which should unblock dispatcher |
| 10425 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 10426 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 10427 | |
| 10428 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 10429 | // can finally go to the newly focused "mUnfocusedWindow". |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10430 | mUnfocusedWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10431 | mFocusedWindow->assertNoEvents(); |
| 10432 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10433 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10434 | } |
| 10435 | |
| 10436 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 10437 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 10438 | // The other window should not be affected by that. |
| 10439 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 10440 | // Touch Window 1 |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10441 | mDispatcher->notifyMotion( |
| 10442 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 10443 | ui::LogicalDisplayId::DEFAULT, {FOCUSED_WINDOW_LOCATION})); |
| 10444 | mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10445 | |
| 10446 | // Touch Window 2 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10447 | mDispatcher->notifyMotion( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10448 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 10449 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10450 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10451 | |
| 10452 | const std::chrono::duration timeout = |
| 10453 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10454 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10455 | |
| 10456 | mUnfocusedWindow->consumeMotionDown(); |
| 10457 | mFocusedWindow->consumeMotionDown(); |
| 10458 | // Focused window may or may not receive ACTION_MOVE |
| 10459 | // But it should definitely receive ACTION_CANCEL due to the ANR |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10460 | const auto [moveOrCancelSequenceNum, event] = mFocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10461 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 10462 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 10463 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 10464 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10465 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 10466 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 10467 | mFocusedWindow->consumeMotionCancel(); |
| 10468 | } else { |
| 10469 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 10470 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10471 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10472 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 10473 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10474 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10475 | mUnfocusedWindow->assertNoEvents(); |
| 10476 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10477 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10478 | } |
| 10479 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10480 | /** |
| 10481 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 10482 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 10483 | * |
| 10484 | * If the user touches another application during this time, the key should be dropped. |
| 10485 | * Next, if a new focused window comes in, without toggling the focused application, |
| 10486 | * then no ANR should occur. |
| 10487 | * |
| 10488 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 10489 | * but in some cases the policy may not update the focused application. |
| 10490 | */ |
| 10491 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 10492 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 10493 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | c033dfb | 2023-10-03 10:45:16 -0700 | [diff] [blame] | 10494 | focusedApplication->setDispatchingTimeout(300ms); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10495 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, focusedApplication); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10496 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 10497 | mFocusedWindow->setFocusable(false); |
| 10498 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10499 | mDispatcher->onWindowInfosChanged( |
| 10500 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10501 | mFocusedWindow->consumeFocusEvent(false); |
| 10502 | |
| 10503 | // Send a key. The ANR timer should start because there is no focused window. |
| 10504 | // 'focusedApplication' will get blamed if this timer completes. |
| 10505 | // 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] | 10506 | InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10507 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 10508 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 10509 | /*injectionTimeout=*/100ms, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10510 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10511 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10512 | |
| 10513 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 10514 | // then the injected touches won't cause the focused event to get dropped. |
| 10515 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 10516 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 10517 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 10518 | // For this test, it means that the key would get delivered to the window once it becomes |
| 10519 | // focused. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10520 | std::this_thread::sleep_for(100ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10521 | |
| 10522 | // Touch unfocused window. This should force the pending key to get dropped. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10523 | mDispatcher->notifyMotion( |
| 10524 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 10525 | ui::LogicalDisplayId::DEFAULT, {UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10526 | |
| 10527 | // We do not consume the motion right away, because that would require dispatcher to first |
| 10528 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 10529 | // Set the focused window first. |
| 10530 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10531 | mDispatcher->onWindowInfosChanged( |
| 10532 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10533 | setFocusedWindow(mFocusedWindow); |
| 10534 | mFocusedWindow->consumeFocusEvent(true); |
| 10535 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 10536 | // to another application. This could be a bug / behaviour in the policy. |
| 10537 | |
| 10538 | mUnfocusedWindow->consumeMotionDown(); |
| 10539 | |
| 10540 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10541 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 10542 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 10543 | } |
| 10544 | |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 10545 | /** |
| 10546 | * If we are pruning input queue, we should never drop pointer events. Otherwise, we risk having |
| 10547 | * an inconsistent event stream inside the dispatcher. In this test, we make sure that the |
| 10548 | * dispatcher doesn't prune pointer events incorrectly. |
| 10549 | * |
| 10550 | * This test reproduces a crash in InputDispatcher. |
| 10551 | * To reproduce the crash, we need to simulate the conditions for "pruning input queue" to occur. |
| 10552 | * |
| 10553 | * Keep the currently focused application (mApplication), and have no focused window. |
| 10554 | * We set up two additional windows: |
| 10555 | * 1) The navigation bar window. This simulates the system "NavigationBar", which is used in the |
| 10556 | * 3-button navigation mode. This window injects a BACK button when it's touched. 2) The application |
| 10557 | * window. This window is not focusable, but is touchable. |
| 10558 | * |
| 10559 | * We first touch the navigation bar, which causes it to inject a key. Since there's no focused |
| 10560 | * window, the dispatcher doesn't process this key, and all other events inside dispatcher are now |
| 10561 | * blocked. The dispatcher is waiting for 'mApplication' to add a focused window. |
| 10562 | * |
| 10563 | * Now, we touch "Another window". This window is owned by a different application than |
| 10564 | * 'mApplication'. This causes the dispatcher to stop waiting for 'mApplication' to add a focused |
| 10565 | * window. Now, the "pruning input queue" behaviour should kick in, and the dispatcher should start |
| 10566 | * dropping the events from its queue. Ensure that no crash occurs. |
| 10567 | * |
| 10568 | * In this test, we are setting long timeouts to prevent ANRs and events dropped due to being stale. |
| 10569 | * This does not affect the test running time. |
| 10570 | */ |
| 10571 | TEST_F(InputDispatcherMultiWindowAnr, PruningInputQueueShouldNotDropPointerEvents) { |
| 10572 | std::shared_ptr<FakeApplicationHandle> systemUiApplication = |
| 10573 | std::make_shared<FakeApplicationHandle>(); |
| 10574 | systemUiApplication->setDispatchingTimeout(3000ms); |
| 10575 | mFakePolicy->setStaleEventTimeout(3000ms); |
| 10576 | sp<FakeWindowHandle> navigationBar = |
| 10577 | sp<FakeWindowHandle>::make(systemUiApplication, mDispatcher, "NavigationBar", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10578 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 10579 | navigationBar->setFocusable(false); |
| 10580 | navigationBar->setWatchOutsideTouch(true); |
| 10581 | navigationBar->setFrame(Rect(0, 0, 100, 100)); |
| 10582 | |
| 10583 | mApplication->setDispatchingTimeout(3000ms); |
| 10584 | // 'mApplication' is already focused, but we call it again here to make it explicit. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10585 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication); |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 10586 | |
| 10587 | std::shared_ptr<FakeApplicationHandle> anotherApplication = |
| 10588 | std::make_shared<FakeApplicationHandle>(); |
| 10589 | sp<FakeWindowHandle> appWindow = |
| 10590 | sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Another window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10591 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 10592 | appWindow->setFocusable(false); |
| 10593 | appWindow->setFrame(Rect(100, 100, 200, 200)); |
| 10594 | |
| 10595 | mDispatcher->onWindowInfosChanged( |
| 10596 | {{*navigationBar->getInfo(), *appWindow->getInfo()}, {}, 0, 0}); |
| 10597 | // 'mFocusedWindow' is no longer in the dispatcher window list, and therefore loses focus |
| 10598 | mFocusedWindow->consumeFocusEvent(false); |
| 10599 | |
| 10600 | // Touch down the navigation bar. It consumes the touch and injects a key into the dispatcher |
| 10601 | // in response. |
| 10602 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10603 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 10604 | .build()); |
| 10605 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 10606 | |
| 10607 | // Key will not be sent anywhere because we have no focused window. It will remain pending. |
| 10608 | // Pretend we are injecting KEYCODE_BACK, but it doesn't actually matter what key it is. |
| 10609 | InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10610 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 10611 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 10612 | /*injectionTimeout=*/100ms, |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 10613 | /*allowKeyRepeat=*/false); |
| 10614 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
| 10615 | |
| 10616 | // Finish the gesture - lift up finger and inject ACTION_UP key event |
| 10617 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 10618 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 10619 | .build()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10620 | result = injectKey(*mDispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, |
| 10621 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 10622 | /*injectionTimeout=*/100ms, |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 10623 | /*allowKeyRepeat=*/false); |
| 10624 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
| 10625 | // The key that was injected is blocking the dispatcher, so the navigation bar shouldn't be |
| 10626 | // getting any events yet. |
| 10627 | navigationBar->assertNoEvents(); |
| 10628 | |
| 10629 | // Now touch "Another window". This touch is going to a different application than the one we |
| 10630 | // are waiting for (which is 'mApplication'). |
| 10631 | // This should cause the dispatcher to drop the pending focus-dispatched events (like the key |
| 10632 | // trying to be injected) and to continue processing the rest of the events in the original |
| 10633 | // order. |
| 10634 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10635 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 10636 | .build()); |
| 10637 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 10638 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_OUTSIDE)); |
| 10639 | appWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 10640 | |
| 10641 | appWindow->assertNoEvents(); |
| 10642 | navigationBar->assertNoEvents(); |
| 10643 | } |
| 10644 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10645 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 10646 | // that has feature NO_INPUT_CHANNEL. |
| 10647 | // Layout: |
| 10648 | // Top (closest to user) |
| 10649 | // mNoInputWindow (above all windows) |
| 10650 | // mBottomWindow |
| 10651 | // Bottom (furthest from user) |
| 10652 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 10653 | virtual void SetUp() override { |
| 10654 | InputDispatcherTest::SetUp(); |
| 10655 | |
| 10656 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10657 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 10658 | "Window without input channel", |
| 10659 | ui::LogicalDisplayId::DEFAULT, |
| 10660 | /*createInputChannel=*/false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10661 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10662 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 10663 | // It's perfectly valid for this window to not have an associated input channel |
| 10664 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10665 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10666 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10667 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 10668 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10669 | mDispatcher->onWindowInfosChanged( |
| 10670 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10671 | } |
| 10672 | |
| 10673 | protected: |
| 10674 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 10675 | sp<FakeWindowHandle> mNoInputWindow; |
| 10676 | sp<FakeWindowHandle> mBottomWindow; |
| 10677 | }; |
| 10678 | |
| 10679 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 10680 | PointF touchedPoint = {10, 10}; |
| 10681 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10682 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10683 | AINPUT_SOURCE_TOUCHSCREEN, |
| 10684 | ui::LogicalDisplayId::DEFAULT, {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10685 | |
| 10686 | mNoInputWindow->assertNoEvents(); |
| 10687 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 10688 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 10689 | // and therefore should prevent mBottomWindow from receiving touches |
| 10690 | mBottomWindow->assertNoEvents(); |
| 10691 | } |
| 10692 | |
| 10693 | /** |
| 10694 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 10695 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 10696 | */ |
| 10697 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 10698 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10699 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 10700 | "Window with input channel and NO_INPUT_CHANNEL", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10701 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10702 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10703 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10704 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10705 | mDispatcher->onWindowInfosChanged( |
| 10706 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10707 | |
| 10708 | PointF touchedPoint = {10, 10}; |
| 10709 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10710 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10711 | AINPUT_SOURCE_TOUCHSCREEN, |
| 10712 | ui::LogicalDisplayId::DEFAULT, {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10713 | |
| 10714 | mNoInputWindow->assertNoEvents(); |
| 10715 | mBottomWindow->assertNoEvents(); |
| 10716 | } |
| 10717 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10718 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 10719 | protected: |
| 10720 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 10721 | sp<FakeWindowHandle> mWindow; |
| 10722 | sp<FakeWindowHandle> mMirror; |
| 10723 | |
| 10724 | virtual void SetUp() override { |
| 10725 | InputDispatcherTest::SetUp(); |
| 10726 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10727 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", |
| 10728 | ui::LogicalDisplayId::DEFAULT); |
| 10729 | mMirror = mWindow->clone(ui::LogicalDisplayId::DEFAULT); |
| 10730 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10731 | mWindow->setFocusable(true); |
| 10732 | mMirror->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10733 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10734 | } |
| 10735 | }; |
| 10736 | |
| 10737 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 10738 | // Request focus on a mirrored window |
| 10739 | setFocusedWindow(mMirror); |
| 10740 | |
| 10741 | // window gets focused |
| 10742 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10743 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10744 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10745 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10746 | } |
| 10747 | |
| 10748 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 10749 | // focusable. |
| 10750 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 10751 | setFocusedWindow(mMirror); |
| 10752 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10753 | // window gets focused because it is above the mirror |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10754 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10755 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10756 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10757 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10758 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10759 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10760 | mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10761 | |
| 10762 | mMirror->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10763 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10764 | |
| 10765 | // window loses focus since one of the windows associated with the token in not focusable |
| 10766 | mWindow->consumeFocusEvent(false); |
| 10767 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10768 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10769 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10770 | mWindow->assertNoEvents(); |
| 10771 | } |
| 10772 | |
| 10773 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 10774 | // invisible. |
| 10775 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 10776 | setFocusedWindow(mMirror); |
| 10777 | |
| 10778 | // window gets focused |
| 10779 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10780 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10781 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10782 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10783 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10784 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10785 | mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10786 | |
| 10787 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10788 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10789 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10790 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10791 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10792 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10793 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10794 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10795 | mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10796 | |
| 10797 | mWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10798 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10799 | |
| 10800 | // window loses focus only after all windows associated with the token become invisible. |
| 10801 | mWindow->consumeFocusEvent(false); |
| 10802 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10803 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10804 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10805 | mWindow->assertNoEvents(); |
| 10806 | } |
| 10807 | |
| 10808 | // A focused & mirrored window remains focused until both windows are removed. |
| 10809 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 10810 | setFocusedWindow(mMirror); |
| 10811 | |
| 10812 | // window gets focused |
| 10813 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10814 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10815 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10816 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10817 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10818 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10819 | mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10820 | |
| 10821 | // single window is removed but the window token remains focused |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10822 | mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10823 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10824 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10825 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10826 | mMirror->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10827 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10828 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10829 | mMirror->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10830 | |
| 10831 | // Both windows are removed |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10832 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10833 | mWindow->consumeFocusEvent(false); |
| 10834 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10835 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10836 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10837 | mWindow->assertNoEvents(); |
| 10838 | } |
| 10839 | |
| 10840 | // Focus request can be pending until one window becomes visible. |
| 10841 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 10842 | // Request focus on an invisible mirror. |
| 10843 | mWindow->setVisible(false); |
| 10844 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10845 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10846 | setFocusedWindow(mMirror); |
| 10847 | |
| 10848 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10849 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10850 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10851 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10852 | |
| 10853 | mMirror->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10854 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10855 | |
| 10856 | // window gets focused |
| 10857 | mWindow->consumeFocusEvent(true); |
| 10858 | // window gets the pending key event |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10859 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10860 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10861 | |
| 10862 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 10863 | protected: |
| 10864 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 10865 | sp<FakeWindowHandle> mWindow; |
| 10866 | sp<FakeWindowHandle> mSecondWindow; |
| 10867 | |
| 10868 | void SetUp() override { |
| 10869 | InputDispatcherTest::SetUp(); |
| 10870 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10871 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", |
| 10872 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10873 | mWindow->setFocusable(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10874 | mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", |
| 10875 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10876 | mSecondWindow->setFocusable(true); |
| 10877 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10878 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10879 | mDispatcher->onWindowInfosChanged( |
| 10880 | {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10881 | |
| 10882 | setFocusedWindow(mWindow); |
| 10883 | mWindow->consumeFocusEvent(true); |
| 10884 | } |
| 10885 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10886 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10887 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10888 | } |
| 10889 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10890 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 10891 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10892 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 10893 | auto request = mFakePolicy->assertSetPointerCaptureCalled(window, enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10894 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10895 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10896 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10897 | } |
| 10898 | }; |
| 10899 | |
| 10900 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 10901 | // Ensure that capture cannot be obtained for unfocused windows. |
| 10902 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 10903 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 10904 | mSecondWindow->assertNoEvents(); |
| 10905 | |
| 10906 | // Ensure that capture can be enabled from the focus window. |
| 10907 | requestAndVerifyPointerCapture(mWindow, true); |
| 10908 | |
| 10909 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 10910 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 10911 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 10912 | |
| 10913 | // Ensure that capture can be disabled from the window with capture. |
| 10914 | requestAndVerifyPointerCapture(mWindow, false); |
| 10915 | } |
| 10916 | |
| 10917 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10918 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10919 | |
| 10920 | setFocusedWindow(mSecondWindow); |
| 10921 | |
| 10922 | // Ensure that the capture disabled event was sent first. |
| 10923 | mWindow->consumeCaptureEvent(false); |
| 10924 | mWindow->consumeFocusEvent(false); |
| 10925 | mSecondWindow->consumeFocusEvent(true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 10926 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10927 | |
| 10928 | // 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] | 10929 | notifyPointerCaptureChanged({}); |
| 10930 | notifyPointerCaptureChanged(request); |
| 10931 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10932 | mWindow->assertNoEvents(); |
| 10933 | mSecondWindow->assertNoEvents(); |
| 10934 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 10935 | } |
| 10936 | |
| 10937 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10938 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10939 | |
| 10940 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10941 | notifyPointerCaptureChanged({}); |
| 10942 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10943 | |
| 10944 | // Ensure that Pointer Capture is disabled. |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 10945 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10946 | mWindow->consumeCaptureEvent(false); |
| 10947 | mWindow->assertNoEvents(); |
| 10948 | } |
| 10949 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 10950 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 10951 | requestAndVerifyPointerCapture(mWindow, true); |
| 10952 | |
| 10953 | // The first window loses focus. |
| 10954 | setFocusedWindow(mSecondWindow); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 10955 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 10956 | mWindow->consumeCaptureEvent(false); |
| 10957 | |
| 10958 | // Request Pointer Capture from the second window before the notification from InputReader |
| 10959 | // arrives. |
| 10960 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 10961 | auto request = mFakePolicy->assertSetPointerCaptureCalled(mSecondWindow, true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 10962 | |
| 10963 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10964 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 10965 | |
| 10966 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10967 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 10968 | |
| 10969 | mSecondWindow->consumeFocusEvent(true); |
| 10970 | mSecondWindow->consumeCaptureEvent(true); |
| 10971 | } |
| 10972 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10973 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 10974 | // App repeatedly enables and disables capture. |
| 10975 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 10976 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10977 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 10978 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10979 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 10980 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10981 | |
| 10982 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 10983 | // first request is now stale, this should do nothing. |
| 10984 | notifyPointerCaptureChanged(firstRequest); |
| 10985 | mWindow->assertNoEvents(); |
| 10986 | |
| 10987 | // InputReader notifies that the second request was enabled. |
| 10988 | notifyPointerCaptureChanged(secondRequest); |
| 10989 | mWindow->consumeCaptureEvent(true); |
| 10990 | } |
| 10991 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 10992 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 10993 | requestAndVerifyPointerCapture(mWindow, true); |
| 10994 | |
| 10995 | // App toggles pointer capture off and on. |
| 10996 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 10997 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 10998 | |
| 10999 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11000 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 11001 | |
| 11002 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 11003 | // preceding "disable" request. |
| 11004 | notifyPointerCaptureChanged(enableRequest); |
| 11005 | |
| 11006 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 11007 | // any notifications. |
| 11008 | mWindow->assertNoEvents(); |
| 11009 | } |
| 11010 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11011 | /** |
| 11012 | * One window. Hover mouse in the window, and then start capture. Make sure that the relative |
| 11013 | * mouse movements don't affect the previous mouse hovering state. |
| 11014 | * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no |
| 11015 | * HOVER_MOVE events). |
| 11016 | */ |
| 11017 | TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) { |
| 11018 | // Mouse hover on the window |
| 11019 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 11020 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 11021 | .build()); |
| 11022 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 11023 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 11024 | .build()); |
| 11025 | |
| 11026 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER))); |
| 11027 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE))); |
| 11028 | |
| 11029 | // Start pointer capture |
| 11030 | requestAndVerifyPointerCapture(mWindow, true); |
| 11031 | |
| 11032 | // Send some relative mouse movements and receive them in the window. |
| 11033 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE) |
| 11034 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11)) |
| 11035 | .build()); |
| 11036 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11), |
| 11037 | WithSource(AINPUT_SOURCE_MOUSE_RELATIVE))); |
| 11038 | |
| 11039 | // Stop pointer capture |
| 11040 | requestAndVerifyPointerCapture(mWindow, false); |
| 11041 | |
| 11042 | // Continue hovering on the window |
| 11043 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 11044 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115)) |
| 11045 | .build()); |
| 11046 | mWindow->consumeMotionEvent( |
| 11047 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 11048 | |
| 11049 | mWindow->assertNoEvents(); |
| 11050 | } |
| 11051 | |
Prabir Pradhan | 9d3d561 | 2024-06-06 20:34:26 +0000 | [diff] [blame] | 11052 | TEST_F(InputDispatcherPointerCaptureTests, MultiDisplayPointerCapture) { |
| 11053 | // The default display is the focused display to begin with. |
| 11054 | requestAndVerifyPointerCapture(mWindow, true); |
| 11055 | |
| 11056 | // Move the second window to a second display, make it the focused window on that display. |
| 11057 | mSecondWindow->editInfo()->displayId = SECOND_DISPLAY_ID; |
| 11058 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
| 11059 | setFocusedWindow(mSecondWindow); |
| 11060 | mSecondWindow->consumeFocusEvent(true); |
| 11061 | |
| 11062 | mWindow->assertNoEvents(); |
| 11063 | |
| 11064 | // The second window cannot gain capture because it is not on the focused display. |
| 11065 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 11066 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 11067 | mSecondWindow->assertNoEvents(); |
| 11068 | |
| 11069 | // Make the second display the focused display. |
| 11070 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
Arpit Singh | b65e2bd | 2024-06-03 09:48:16 +0000 | [diff] [blame^] | 11071 | mFakePolicy->assertFocusedDisplayNotified(SECOND_DISPLAY_ID); |
Prabir Pradhan | 9d3d561 | 2024-06-06 20:34:26 +0000 | [diff] [blame] | 11072 | |
| 11073 | // This causes the first window to lose pointer capture, and it's unable to request capture. |
| 11074 | mWindow->consumeCaptureEvent(false); |
| 11075 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
| 11076 | |
| 11077 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 11078 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 11079 | |
| 11080 | // The second window is now able to gain pointer capture successfully. |
| 11081 | requestAndVerifyPointerCapture(mSecondWindow, true); |
| 11082 | } |
| 11083 | |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11084 | using InputDispatcherPointerCaptureDeathTest = InputDispatcherPointerCaptureTests; |
| 11085 | |
| 11086 | TEST_F(InputDispatcherPointerCaptureDeathTest, |
| 11087 | NotifyPointerCaptureChangedWithWrongTokenAbortsDispatcher) { |
| 11088 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
| 11089 | ScopedSilentDeath _silentDeath; |
| 11090 | |
| 11091 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 11092 | auto request = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
| 11093 | |
| 11094 | // Dispatch a pointer changed event with a wrong token. |
| 11095 | request.window = mSecondWindow->getToken(); |
| 11096 | ASSERT_DEATH( |
| 11097 | { |
| 11098 | notifyPointerCaptureChanged(request); |
| 11099 | mSecondWindow->consumeCaptureEvent(true); |
| 11100 | }, |
| 11101 | "Unexpected requested window for Pointer Capture."); |
| 11102 | } |
| 11103 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11104 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 11105 | protected: |
| 11106 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11107 | |
| 11108 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 11109 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 11110 | |
| 11111 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 11112 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 11113 | |
| 11114 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 11115 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 11116 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 11117 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 11118 | MAXIMUM_OBSCURING_OPACITY); |
| 11119 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11120 | static constexpr gui::Uid TOUCHED_APP_UID{10001}; |
| 11121 | static constexpr gui::Uid APP_B_UID{10002}; |
| 11122 | static constexpr gui::Uid APP_C_UID{10003}; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11123 | |
| 11124 | sp<FakeWindowHandle> mTouchWindow; |
| 11125 | |
| 11126 | virtual void SetUp() override { |
| 11127 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11128 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11129 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 11130 | } |
| 11131 | |
| 11132 | virtual void TearDown() override { |
| 11133 | InputDispatcherTest::TearDown(); |
| 11134 | mTouchWindow.clear(); |
| 11135 | } |
| 11136 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11137 | sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 11138 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11139 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11140 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11141 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11142 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11143 | return window; |
| 11144 | } |
| 11145 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11146 | sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11147 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 11148 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11149 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ui::LogicalDisplayId::DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11150 | // Generate an arbitrary PID based on the UID |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11151 | 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] | 11152 | return window; |
| 11153 | } |
| 11154 | |
| 11155 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11156 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11157 | AINPUT_SOURCE_TOUCHSCREEN, |
| 11158 | ui::LogicalDisplayId::DEFAULT, points)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11159 | } |
| 11160 | }; |
| 11161 | |
| 11162 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11163 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11164 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11165 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11166 | |
| 11167 | touch(); |
| 11168 | |
| 11169 | mTouchWindow->assertNoEvents(); |
| 11170 | } |
| 11171 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11172 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11173 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 11174 | const sp<FakeWindowHandle>& w = |
| 11175 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11176 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11177 | |
| 11178 | touch(); |
| 11179 | |
| 11180 | mTouchWindow->assertNoEvents(); |
| 11181 | } |
| 11182 | |
| 11183 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11184 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 11185 | const sp<FakeWindowHandle>& w = |
| 11186 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11187 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11188 | |
| 11189 | touch(); |
| 11190 | |
| 11191 | w->assertNoEvents(); |
| 11192 | } |
| 11193 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11194 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11195 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11196 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11197 | |
| 11198 | touch(); |
| 11199 | |
| 11200 | mTouchWindow->consumeAnyMotionDown(); |
| 11201 | } |
| 11202 | |
| 11203 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11204 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11205 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11206 | w->setFrame(Rect(0, 0, 50, 50)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11207 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11208 | |
| 11209 | touch({PointF{100, 100}}); |
| 11210 | |
| 11211 | mTouchWindow->consumeAnyMotionDown(); |
| 11212 | } |
| 11213 | |
| 11214 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11215 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11216 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11217 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11218 | |
| 11219 | touch(); |
| 11220 | |
| 11221 | mTouchWindow->consumeAnyMotionDown(); |
| 11222 | } |
| 11223 | |
| 11224 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 11225 | const sp<FakeWindowHandle>& w = |
| 11226 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11227 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11228 | |
| 11229 | touch(); |
| 11230 | |
| 11231 | mTouchWindow->consumeAnyMotionDown(); |
| 11232 | } |
| 11233 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11234 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 11235 | const sp<FakeWindowHandle>& w = |
| 11236 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11237 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11238 | |
| 11239 | touch(); |
| 11240 | |
| 11241 | w->assertNoEvents(); |
| 11242 | } |
| 11243 | |
| 11244 | /** |
| 11245 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 11246 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 11247 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 11248 | */ |
| 11249 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11250 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 11251 | const sp<FakeWindowHandle>& w = |
| 11252 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11253 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11254 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11255 | |
| 11256 | touch(); |
| 11257 | |
| 11258 | w->consumeMotionOutside(); |
| 11259 | } |
| 11260 | |
| 11261 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 11262 | const sp<FakeWindowHandle>& w = |
| 11263 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11264 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11265 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11266 | |
| 11267 | touch(); |
| 11268 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 11269 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11270 | } |
| 11271 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11272 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11273 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11274 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11275 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11276 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11277 | |
| 11278 | touch(); |
| 11279 | |
| 11280 | mTouchWindow->consumeAnyMotionDown(); |
| 11281 | } |
| 11282 | |
| 11283 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 11284 | const sp<FakeWindowHandle>& w = |
| 11285 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11286 | MAXIMUM_OBSCURING_OPACITY); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11287 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11288 | |
| 11289 | touch(); |
| 11290 | |
| 11291 | mTouchWindow->consumeAnyMotionDown(); |
| 11292 | } |
| 11293 | |
| 11294 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11295 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11296 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11297 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11298 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11299 | |
| 11300 | touch(); |
| 11301 | |
| 11302 | mTouchWindow->assertNoEvents(); |
| 11303 | } |
| 11304 | |
| 11305 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 11306 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 11307 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11308 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 11309 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11310 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11311 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 11312 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11313 | mDispatcher->onWindowInfosChanged( |
| 11314 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11315 | |
| 11316 | touch(); |
| 11317 | |
| 11318 | mTouchWindow->assertNoEvents(); |
| 11319 | } |
| 11320 | |
| 11321 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 11322 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 11323 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11324 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 11325 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11326 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11327 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 11328 | OPACITY_FAR_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11329 | mDispatcher->onWindowInfosChanged( |
| 11330 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11331 | |
| 11332 | touch(); |
| 11333 | |
| 11334 | mTouchWindow->consumeAnyMotionDown(); |
| 11335 | } |
| 11336 | |
| 11337 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11338 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 11339 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11340 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11341 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11342 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11343 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 11344 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11345 | mDispatcher->onWindowInfosChanged( |
| 11346 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11347 | |
| 11348 | touch(); |
| 11349 | |
| 11350 | mTouchWindow->consumeAnyMotionDown(); |
| 11351 | } |
| 11352 | |
| 11353 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 11354 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11355 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11356 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11357 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11358 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 11359 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11360 | mDispatcher->onWindowInfosChanged( |
| 11361 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11362 | |
| 11363 | touch(); |
| 11364 | |
| 11365 | mTouchWindow->assertNoEvents(); |
| 11366 | } |
| 11367 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11368 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11369 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 11370 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11371 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 11372 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11373 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11374 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11375 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11376 | mDispatcher->onWindowInfosChanged( |
| 11377 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11378 | |
| 11379 | touch(); |
| 11380 | |
| 11381 | mTouchWindow->assertNoEvents(); |
| 11382 | } |
| 11383 | |
| 11384 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11385 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 11386 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11387 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 11388 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11389 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11390 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11391 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11392 | mDispatcher->onWindowInfosChanged( |
| 11393 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11394 | |
| 11395 | touch(); |
| 11396 | |
| 11397 | mTouchWindow->consumeAnyMotionDown(); |
| 11398 | } |
| 11399 | |
| 11400 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 11401 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11402 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 11403 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11404 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11405 | |
| 11406 | touch(); |
| 11407 | |
| 11408 | mTouchWindow->consumeAnyMotionDown(); |
| 11409 | } |
| 11410 | |
| 11411 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 11412 | const sp<FakeWindowHandle>& w = |
| 11413 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11414 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11415 | |
| 11416 | touch(); |
| 11417 | |
| 11418 | mTouchWindow->consumeAnyMotionDown(); |
| 11419 | } |
| 11420 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 11421 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11422 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 11423 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 11424 | const sp<FakeWindowHandle>& w = |
| 11425 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11426 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 11427 | |
| 11428 | touch(); |
| 11429 | |
| 11430 | mTouchWindow->assertNoEvents(); |
| 11431 | } |
| 11432 | |
| 11433 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 11434 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 11435 | const sp<FakeWindowHandle>& w = |
| 11436 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11437 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 11438 | |
| 11439 | touch(); |
| 11440 | |
| 11441 | mTouchWindow->consumeAnyMotionDown(); |
| 11442 | } |
| 11443 | |
| 11444 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11445 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 11446 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 11447 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11448 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11449 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11450 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11451 | |
| 11452 | touch(); |
| 11453 | |
| 11454 | mTouchWindow->consumeAnyMotionDown(); |
| 11455 | } |
| 11456 | |
| 11457 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11458 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 11459 | const sp<FakeWindowHandle>& w1 = |
| 11460 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 11461 | OPACITY_BELOW_THRESHOLD); |
| 11462 | const sp<FakeWindowHandle>& w2 = |
| 11463 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11464 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11465 | mDispatcher->onWindowInfosChanged( |
| 11466 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11467 | |
| 11468 | touch(); |
| 11469 | |
| 11470 | mTouchWindow->assertNoEvents(); |
| 11471 | } |
| 11472 | |
| 11473 | /** |
| 11474 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 11475 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 11476 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 11477 | */ |
| 11478 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11479 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 11480 | const sp<FakeWindowHandle>& wB = |
| 11481 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 11482 | OPACITY_BELOW_THRESHOLD); |
| 11483 | const sp<FakeWindowHandle>& wC = |
| 11484 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 11485 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11486 | mDispatcher->onWindowInfosChanged( |
| 11487 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11488 | |
| 11489 | touch(); |
| 11490 | |
| 11491 | mTouchWindow->assertNoEvents(); |
| 11492 | } |
| 11493 | |
| 11494 | /** |
| 11495 | * This test is testing that a window from a different UID but with same application token doesn't |
| 11496 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 11497 | */ |
| 11498 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11499 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 11500 | const sp<FakeWindowHandle>& w = |
| 11501 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 11502 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11503 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 11504 | |
| 11505 | touch(); |
| 11506 | |
| 11507 | mTouchWindow->consumeAnyMotionDown(); |
| 11508 | } |
| 11509 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11510 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 11511 | protected: |
| 11512 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 11513 | sp<FakeWindowHandle> mWindow; |
| 11514 | sp<FakeWindowHandle> mSecondWindow; |
| 11515 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11516 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11517 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 11518 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11519 | |
| 11520 | void SetUp() override { |
| 11521 | InputDispatcherTest::SetUp(); |
| 11522 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11523 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", |
| 11524 | ui::LogicalDisplayId::DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11525 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11526 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11527 | mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", |
| 11528 | ui::LogicalDisplayId::DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11529 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11530 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11531 | mSpyWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", |
| 11532 | ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11533 | mSpyWindow->setSpy(true); |
| 11534 | mSpyWindow->setTrustedOverlay(true); |
| 11535 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 11536 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11537 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11538 | mDispatcher->onWindowInfosChanged( |
| 11539 | {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 11540 | {}, |
| 11541 | 0, |
| 11542 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11543 | } |
| 11544 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11545 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 11546 | switch (fromSource) { |
| 11547 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 11548 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11549 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11550 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11551 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11552 | break; |
| 11553 | case AINPUT_SOURCE_STYLUS: |
| 11554 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11555 | injectMotionEvent(*mDispatcher, |
| 11556 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 11557 | AINPUT_SOURCE_STYLUS) |
| 11558 | .buttonState( |
| 11559 | AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 11560 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
| 11561 | .x(50) |
| 11562 | .y(50)) |
| 11563 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11564 | break; |
| 11565 | case AINPUT_SOURCE_MOUSE: |
| 11566 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11567 | injectMotionEvent(*mDispatcher, |
| 11568 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 11569 | AINPUT_SOURCE_MOUSE) |
| 11570 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 11571 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 11572 | ToolType::MOUSE) |
| 11573 | .x(50) |
| 11574 | .y(50)) |
| 11575 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11576 | break; |
| 11577 | default: |
| 11578 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 11579 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11580 | |
| 11581 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11582 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11583 | // Spy window should also receive motion event |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11584 | mSpyWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11585 | } |
| 11586 | |
| 11587 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 11588 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 11589 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11590 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11591 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11592 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11593 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11594 | |
| 11595 | // The drag window covers the entire display |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11596 | mDragWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", |
| 11597 | ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11598 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11599 | mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), |
| 11600 | *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 11601 | {}, |
| 11602 | 0, |
| 11603 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11604 | |
| 11605 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11606 | bool transferred = |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 11607 | mDispatcher->transferTouchGesture(mWindow->getToken(), mDragWindow->getToken(), |
| 11608 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11609 | if (transferred) { |
| 11610 | mWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11611 | mDragWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 11612 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11613 | } |
| 11614 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11615 | } |
| 11616 | }; |
| 11617 | |
| 11618 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11619 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11620 | |
| 11621 | // Move on window. |
| 11622 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11623 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11624 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11625 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11626 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11627 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11628 | mWindow->consumeDragEvent(false, 50, 50); |
| 11629 | mSecondWindow->assertNoEvents(); |
| 11630 | |
| 11631 | // Move to another window. |
| 11632 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11633 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11634 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11635 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11636 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11637 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11638 | mWindow->consumeDragEvent(true, 150, 50); |
| 11639 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 11640 | |
| 11641 | // Move back to original window. |
| 11642 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11643 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11644 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11645 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11646 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11647 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11648 | mWindow->consumeDragEvent(false, 50, 50); |
| 11649 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 11650 | |
| 11651 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11652 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11653 | {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11654 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11655 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11656 | mWindow->assertNoEvents(); |
| 11657 | mSecondWindow->assertNoEvents(); |
| 11658 | } |
| 11659 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11660 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11661 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11662 | |
| 11663 | // No cancel event after drag start |
| 11664 | mSpyWindow->assertNoEvents(); |
| 11665 | |
| 11666 | const MotionEvent secondFingerDownEvent = |
| 11667 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11668 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11669 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11670 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11671 | .build(); |
| 11672 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11673 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11674 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11675 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11676 | |
| 11677 | // Receives cancel for first pointer after next pointer down |
| 11678 | mSpyWindow->consumeMotionCancel(); |
| 11679 | mSpyWindow->consumeMotionDown(); |
| 11680 | |
| 11681 | mSpyWindow->assertNoEvents(); |
| 11682 | } |
| 11683 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11684 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11685 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11686 | |
| 11687 | // Move on window. |
| 11688 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11689 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11690 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11691 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11692 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11693 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11694 | mWindow->consumeDragEvent(false, 50, 50); |
| 11695 | mSecondWindow->assertNoEvents(); |
| 11696 | |
| 11697 | // Move to another window. |
| 11698 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11699 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11700 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11701 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11702 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11703 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11704 | mWindow->consumeDragEvent(true, 150, 50); |
| 11705 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 11706 | |
| 11707 | // drop to another window. |
| 11708 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11709 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11710 | {150, 50})) |
| 11711 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11712 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 11713 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11714 | mWindow->assertNoEvents(); |
| 11715 | mSecondWindow->assertNoEvents(); |
| 11716 | } |
| 11717 | |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 11718 | TEST_F(InputDispatcherDragTests, DragAndDropNotCancelledIfSomeOtherPointerIsPilfered) { |
| 11719 | startDrag(); |
| 11720 | |
| 11721 | // No cancel event after drag start |
| 11722 | mSpyWindow->assertNoEvents(); |
| 11723 | |
| 11724 | const MotionEvent secondFingerDownEvent = |
| 11725 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11726 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 11727 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11728 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 11729 | .build(); |
| 11730 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11731 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 11732 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11733 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11734 | |
| 11735 | // Receives cancel for first pointer after next pointer down |
| 11736 | mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 11737 | mSpyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithPointerIds({1}))); |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 11738 | mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 11739 | |
| 11740 | mSpyWindow->assertNoEvents(); |
| 11741 | |
| 11742 | // Spy window calls pilfer pointers |
| 11743 | EXPECT_EQ(OK, mDispatcher->pilferPointers(mSpyWindow->getToken())); |
| 11744 | mDragWindow->assertNoEvents(); |
| 11745 | |
| 11746 | const MotionEvent firstFingerMoveEvent = |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 11747 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 11748 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 11749 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(60).y(60)) |
| 11750 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 11751 | .build(); |
| 11752 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 11753 | injectMotionEvent(*mDispatcher, firstFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 11754 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11755 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11756 | |
| 11757 | // Drag window should still receive the new event |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 11758 | mDragWindow->consumeMotionEvent( |
| 11759 | AllOf(WithMotionAction(ACTION_MOVE), WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 11760 | mDragWindow->assertNoEvents(); |
| 11761 | } |
| 11762 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11763 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11764 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11765 | |
| 11766 | // Move on window and keep button pressed. |
| 11767 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11768 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11769 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 11770 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11771 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11772 | .build())) |
| 11773 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11774 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11775 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11776 | mWindow->consumeDragEvent(false, 50, 50); |
| 11777 | mSecondWindow->assertNoEvents(); |
| 11778 | |
| 11779 | // Move to another window and release button, expect to drop item. |
| 11780 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11781 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11782 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 11783 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11784 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11785 | .build())) |
| 11786 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11787 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11788 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11789 | mWindow->assertNoEvents(); |
| 11790 | mSecondWindow->assertNoEvents(); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 11791 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11792 | |
| 11793 | // nothing to the window. |
| 11794 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11795 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11796 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 11797 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11798 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11799 | .build())) |
| 11800 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11801 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11802 | mWindow->assertNoEvents(); |
| 11803 | mSecondWindow->assertNoEvents(); |
| 11804 | } |
| 11805 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11806 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11807 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11808 | |
| 11809 | // Set second window invisible. |
| 11810 | mSecondWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11811 | mDispatcher->onWindowInfosChanged( |
| 11812 | {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11813 | |
| 11814 | // Move on window. |
| 11815 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11816 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11817 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11818 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11819 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11820 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11821 | mWindow->consumeDragEvent(false, 50, 50); |
| 11822 | mSecondWindow->assertNoEvents(); |
| 11823 | |
| 11824 | // Move to another window. |
| 11825 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11826 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11827 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11828 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11829 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11830 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11831 | mWindow->consumeDragEvent(true, 150, 50); |
| 11832 | mSecondWindow->assertNoEvents(); |
| 11833 | |
| 11834 | // drop to another window. |
| 11835 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11836 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11837 | {150, 50})) |
| 11838 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11839 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 11840 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11841 | mWindow->assertNoEvents(); |
| 11842 | mSecondWindow->assertNoEvents(); |
| 11843 | } |
| 11844 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11845 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11846 | // Ensure window could track pointerIds if it didn't support split touch. |
| 11847 | mWindow->setPreventSplitting(true); |
| 11848 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11849 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11850 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 11851 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11852 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11853 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11854 | |
| 11855 | const MotionEvent secondFingerDownEvent = |
| 11856 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11857 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11858 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11859 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11860 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11861 | .build(); |
| 11862 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11863 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11864 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11865 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11866 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11867 | |
| 11868 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11869 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11870 | } |
| 11871 | |
| 11872 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 11873 | // First down on second window. |
| 11874 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11875 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 11876 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11877 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11878 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11879 | mSecondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11880 | |
| 11881 | // Second down on first window. |
| 11882 | const MotionEvent secondFingerDownEvent = |
| 11883 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11884 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11885 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11886 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 11887 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11888 | .build(); |
| 11889 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11890 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11891 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11892 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11893 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 11894 | mSecondWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11895 | |
| 11896 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11897 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11898 | |
| 11899 | // Move on window. |
| 11900 | const MotionEvent secondFingerMoveEvent = |
| 11901 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 11902 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11903 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 11904 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11905 | .build(); |
| 11906 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11907 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11908 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11909 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11910 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11911 | mWindow->consumeDragEvent(false, 50, 50); |
| 11912 | mSecondWindow->consumeMotionMove(); |
| 11913 | |
| 11914 | // Release the drag pointer should perform drop. |
| 11915 | const MotionEvent secondFingerUpEvent = |
| 11916 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 11917 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11918 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 11919 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11920 | .build(); |
| 11921 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11922 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11923 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11924 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 11925 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken()); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11926 | mWindow->assertNoEvents(); |
| 11927 | mSecondWindow->consumeMotionMove(); |
| 11928 | } |
| 11929 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11930 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11931 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11932 | |
| 11933 | // Update window of second display. |
| 11934 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11935 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11936 | mDispatcher->onWindowInfosChanged( |
| 11937 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 11938 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 11939 | {}, |
| 11940 | 0, |
| 11941 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11942 | |
| 11943 | // Let second display has a touch state. |
| 11944 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11945 | injectMotionEvent(*mDispatcher, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11946 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 11947 | AINPUT_SOURCE_TOUCHSCREEN) |
| 11948 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11949 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11950 | .build())); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 11951 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11952 | // Update window again. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11953 | mDispatcher->onWindowInfosChanged( |
| 11954 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 11955 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 11956 | {}, |
| 11957 | 0, |
| 11958 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11959 | |
| 11960 | // Move on window. |
| 11961 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11962 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11963 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11964 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11965 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11966 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11967 | mWindow->consumeDragEvent(false, 50, 50); |
| 11968 | mSecondWindow->assertNoEvents(); |
| 11969 | |
| 11970 | // Move to another window. |
| 11971 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11972 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11973 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11974 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11975 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11976 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11977 | mWindow->consumeDragEvent(true, 150, 50); |
| 11978 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 11979 | |
| 11980 | // drop to another window. |
| 11981 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11982 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11983 | {150, 50})) |
| 11984 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11985 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 11986 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11987 | mWindow->assertNoEvents(); |
| 11988 | mSecondWindow->assertNoEvents(); |
| 11989 | } |
| 11990 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11991 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 11992 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 11993 | // Move on window. |
| 11994 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11995 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11996 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 11997 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11998 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11999 | .x(50) |
| 12000 | .y(50)) |
| 12001 | .build())) |
| 12002 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12003 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12004 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12005 | mWindow->consumeDragEvent(false, 50, 50); |
| 12006 | mSecondWindow->assertNoEvents(); |
| 12007 | |
| 12008 | // Move to another window. |
| 12009 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12010 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12011 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 12012 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12013 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12014 | .x(150) |
| 12015 | .y(50)) |
| 12016 | .build())) |
| 12017 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12018 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12019 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12020 | mWindow->consumeDragEvent(true, 150, 50); |
| 12021 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 12022 | |
| 12023 | // drop to another window. |
| 12024 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12025 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12026 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 12027 | .buttonState(0) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12028 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12029 | .x(150) |
| 12030 | .y(50)) |
| 12031 | .build())) |
| 12032 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12033 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 12034 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12035 | mWindow->assertNoEvents(); |
| 12036 | mSecondWindow->assertNoEvents(); |
| 12037 | } |
| 12038 | |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12039 | /** |
| 12040 | * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag |
| 12041 | * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash. |
| 12042 | */ |
| 12043 | TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) { |
| 12044 | // Down on second window |
| 12045 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12046 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12047 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12048 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12049 | |
| 12050 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown()); |
| 12051 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown()); |
| 12052 | |
| 12053 | // Down on first window |
| 12054 | const MotionEvent secondFingerDownEvent = |
| 12055 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12056 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12057 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 12058 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 12059 | .build(); |
| 12060 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12061 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 12062 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12063 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12064 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 12065 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove()); |
| 12066 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1)); |
| 12067 | |
| 12068 | // Start drag on first window |
| 12069 | ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN)); |
| 12070 | |
| 12071 | // Trigger cancel |
| 12072 | mDispatcher->cancelCurrentTouch(); |
| 12073 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12074 | ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 12075 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)); |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12076 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel()); |
| 12077 | |
| 12078 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 12079 | // The D&D finished with nullptr |
| 12080 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
| 12081 | |
| 12082 | // Remove drag window |
| 12083 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
| 12084 | |
| 12085 | // Inject a simple gesture, ensure dispatcher not crashed |
| 12086 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12087 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12088 | ui::LogicalDisplayId::DEFAULT, PointF{50, 50})) |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12089 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12090 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 12091 | |
| 12092 | const MotionEvent moveEvent = |
| 12093 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12094 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12095 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12096 | .build(); |
| 12097 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12098 | injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT, |
| 12099 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12100 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12101 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove()); |
| 12102 | |
| 12103 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12104 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12105 | {50, 50})) |
| 12106 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12107 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp()); |
| 12108 | } |
| 12109 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 12110 | TEST_F(InputDispatcherDragTests, NoDragAndDropWithHoveringPointer) { |
| 12111 | // Start hovering over the window. |
| 12112 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12113 | injectMotionEvent(*mDispatcher, ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12114 | ui::LogicalDisplayId::DEFAULT, {50, 50})); |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 12115 | |
| 12116 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 12117 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 12118 | |
| 12119 | ASSERT_FALSE(startDrag(/*sendDown=*/false)) |
| 12120 | << "Drag and drop should not work with a hovering pointer"; |
| 12121 | } |
| 12122 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12123 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 12124 | |
| 12125 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 12126 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12127 | sp<FakeWindowHandle> window = |
| 12128 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 12129 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12130 | window->setDropInput(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12131 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12132 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12133 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12134 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12135 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12136 | |
| 12137 | // With the flag set, window should not get any input |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12138 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12139 | window->assertNoEvents(); |
| 12140 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12141 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12142 | AINPUT_SOURCE_TOUCHSCREEN, |
| 12143 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 12144 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12145 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 12146 | mDispatcher->waitForIdle(); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12147 | window->assertNoEvents(); |
| 12148 | |
| 12149 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12150 | window->setDropInput(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12151 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12152 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12153 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT)); |
| 12154 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12155 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12156 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12157 | AINPUT_SOURCE_TOUCHSCREEN, |
| 12158 | ui::LogicalDisplayId::DEFAULT)); |
| 12159 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12160 | window->assertNoEvents(); |
| 12161 | } |
| 12162 | |
| 12163 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 12164 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 12165 | std::make_shared<FakeApplicationHandle>(); |
| 12166 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 12167 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12168 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12169 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12170 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12171 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12172 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12173 | sp<FakeWindowHandle> window = |
| 12174 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 12175 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12176 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12177 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12178 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12179 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12180 | mDispatcher->onWindowInfosChanged( |
| 12181 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12182 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12183 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12184 | |
| 12185 | // With the flag set, window should not get any input |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12186 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12187 | window->assertNoEvents(); |
| 12188 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12189 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12190 | AINPUT_SOURCE_TOUCHSCREEN, |
| 12191 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 12192 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12193 | ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12194 | window->assertNoEvents(); |
| 12195 | |
| 12196 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12197 | window->setDropInputIfObscured(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12198 | mDispatcher->onWindowInfosChanged( |
| 12199 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12200 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12201 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT)); |
| 12202 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12203 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12204 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12205 | AINPUT_SOURCE_TOUCHSCREEN, |
| 12206 | ui::LogicalDisplayId::DEFAULT)); |
| 12207 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 12208 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12209 | window->assertNoEvents(); |
| 12210 | } |
| 12211 | |
| 12212 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 12213 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 12214 | std::make_shared<FakeApplicationHandle>(); |
| 12215 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 12216 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12217 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12218 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12219 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12220 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12221 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12222 | sp<FakeWindowHandle> window = |
| 12223 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 12224 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12225 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12226 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12227 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12228 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12229 | mDispatcher->onWindowInfosChanged( |
| 12230 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12231 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12232 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12233 | |
| 12234 | // With the flag set, window should not get any input |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12235 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12236 | window->assertNoEvents(); |
| 12237 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12238 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12239 | AINPUT_SOURCE_TOUCHSCREEN, |
| 12240 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 12241 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12242 | ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12243 | window->assertNoEvents(); |
| 12244 | |
| 12245 | // 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] | 12246 | mDispatcher->onWindowInfosChanged( |
| 12247 | {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12248 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12249 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT)); |
| 12250 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12251 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12252 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12253 | AINPUT_SOURCE_TOUCHSCREEN, |
| 12254 | ui::LogicalDisplayId::DEFAULT)); |
| 12255 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12256 | window->assertNoEvents(); |
| 12257 | } |
| 12258 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12259 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 12260 | protected: |
| 12261 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12262 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12263 | sp<FakeWindowHandle> mWindow; |
| 12264 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12265 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12266 | |
| 12267 | void SetUp() override { |
| 12268 | InputDispatcherTest::SetUp(); |
| 12269 | |
| 12270 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12271 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12272 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", |
| 12273 | ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12274 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12275 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12276 | mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", |
| 12277 | ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12278 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12279 | mThirdWindow = |
| 12280 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 12281 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 12282 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12283 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12284 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12285 | mDispatcher->onWindowInfosChanged( |
| 12286 | {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()}, |
| 12287 | {}, |
| 12288 | 0, |
| 12289 | 0}); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12290 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12291 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12292 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12293 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12294 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12295 | WINDOW_UID, /*hasPermission=*/true, |
| 12296 | ui::LogicalDisplayId::DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12297 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 12298 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12299 | mThirdWindow->assertNoEvents(); |
| 12300 | } |
| 12301 | |
| 12302 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 12303 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12304 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12305 | SECOND_DISPLAY_ID)) { |
| 12306 | mWindow->assertNoEvents(); |
| 12307 | mSecondWindow->assertNoEvents(); |
| 12308 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12309 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12310 | } |
| 12311 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12312 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12313 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 12314 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12315 | ui::LogicalDisplayId::DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12316 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 12317 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12318 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12319 | } |
| 12320 | }; |
| 12321 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12322 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 12323 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12324 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 12325 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12326 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12327 | } |
| 12328 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12329 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 12330 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12331 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 12332 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12333 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12334 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12335 | ownerUid, /*hasPermission=*/false, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12336 | ui::LogicalDisplayId::DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12337 | mWindow->assertNoEvents(); |
| 12338 | mSecondWindow->assertNoEvents(); |
| 12339 | } |
| 12340 | |
| 12341 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 12342 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12343 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 12344 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12345 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12346 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12347 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12348 | } |
| 12349 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12350 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 12351 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12352 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 12353 | windowInfo.ownerPid, windowInfo.ownerUid, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12354 | /*hasPermission=*/true, |
| 12355 | ui::LogicalDisplayId::DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12356 | mWindow->assertNoEvents(); |
| 12357 | mSecondWindow->assertNoEvents(); |
| 12358 | } |
| 12359 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12360 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 12361 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 12362 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 12363 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12364 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12365 | mWindow->assertNoEvents(); |
| 12366 | mSecondWindow->assertNoEvents(); |
| 12367 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 12368 | } |
| 12369 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12370 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 12371 | // Interact with the window first. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12372 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12373 | injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT)) |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12374 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12375 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12376 | |
| 12377 | // Then remove focus. |
| 12378 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12379 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12380 | |
| 12381 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 12382 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 12383 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 12384 | windowInfo.ownerPid, windowInfo.ownerUid, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12385 | /*hasPermission=*/false, |
| 12386 | ui::LogicalDisplayId::DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12387 | } |
| 12388 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12389 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 12390 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12391 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12392 | std::shared_ptr<FakeApplicationHandle> application = |
| 12393 | std::make_shared<FakeApplicationHandle>(); |
| 12394 | std::string name = "Fake Spy "; |
| 12395 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12396 | sp<FakeWindowHandle> spy = |
| 12397 | sp<FakeWindowHandle>::make(application, mDispatcher, name.c_str(), |
| 12398 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12399 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 12400 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12401 | return spy; |
| 12402 | } |
| 12403 | |
| 12404 | sp<FakeWindowHandle> createForeground() { |
| 12405 | std::shared_ptr<FakeApplicationHandle> application = |
| 12406 | std::make_shared<FakeApplicationHandle>(); |
| 12407 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 12408 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12409 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12410 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12411 | return window; |
| 12412 | } |
| 12413 | |
| 12414 | private: |
| 12415 | int mSpyCount{0}; |
| 12416 | }; |
| 12417 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 12418 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12419 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 12420 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 12421 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 12422 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
Siarhei Vishniakou | 21f77bd | 2023-07-18 17:51:35 -0700 | [diff] [blame] | 12423 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 12424 | ScopedSilentDeath _silentDeath; |
| 12425 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12426 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 12427 | spy->setTrustedOverlay(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12428 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 12429 | ".* not a trusted overlay"); |
| 12430 | } |
| 12431 | |
| 12432 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12433 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 12434 | */ |
| 12435 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12436 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12437 | mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12438 | |
| 12439 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12440 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12441 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12442 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12443 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12444 | } |
| 12445 | |
| 12446 | /** |
| 12447 | * Verify the order in which different input windows receive events. The touched foreground window |
| 12448 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 12449 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 12450 | * receive events before ones belows it. |
| 12451 | * |
| 12452 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 12453 | * spy1, spy2, window, spy3. |
| 12454 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 12455 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 12456 | * window. |
| 12457 | */ |
| 12458 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 12459 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12460 | auto spy1 = createSpy(); |
| 12461 | auto spy2 = createSpy(); |
| 12462 | auto spy3 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12463 | mDispatcher->onWindowInfosChanged( |
| 12464 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12465 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 12466 | const size_t numChannels = channels.size(); |
| 12467 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 12468 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12469 | if (!epollFd.ok()) { |
| 12470 | FAIL() << "Failed to create epoll fd"; |
| 12471 | } |
| 12472 | |
| 12473 | for (size_t i = 0; i < numChannels; i++) { |
| 12474 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 12475 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 12476 | FAIL() << "Failed to add fd to epoll"; |
| 12477 | } |
| 12478 | } |
| 12479 | |
| 12480 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12481 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12482 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12483 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12484 | |
| 12485 | std::vector<size_t> eventOrder; |
| 12486 | std::vector<struct epoll_event> events(numChannels); |
| 12487 | for (;;) { |
| 12488 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 12489 | (100ms).count()); |
| 12490 | if (nFds < 0) { |
| 12491 | FAIL() << "Failed to call epoll_wait"; |
| 12492 | } |
| 12493 | if (nFds == 0) { |
| 12494 | break; // epoll_wait timed out |
| 12495 | } |
| 12496 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 12497 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 12498 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12499 | channels[i]->consumeMotionDown(); |
| 12500 | } |
| 12501 | } |
| 12502 | |
| 12503 | // Verify the order in which the events were received. |
| 12504 | EXPECT_EQ(3u, eventOrder.size()); |
| 12505 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 12506 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 12507 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 12508 | } |
| 12509 | |
| 12510 | /** |
| 12511 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 12512 | */ |
| 12513 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 12514 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12515 | auto spy = createSpy(); |
| 12516 | spy->setTouchable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12517 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12518 | |
| 12519 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12520 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12521 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12522 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12523 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12524 | spy->assertNoEvents(); |
| 12525 | } |
| 12526 | |
| 12527 | /** |
| 12528 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 12529 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 12530 | * to the window. |
| 12531 | */ |
| 12532 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 12533 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12534 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12535 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12536 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12537 | |
| 12538 | // Inject an event outside the spy window's touchable region. |
| 12539 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12540 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12541 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12542 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12543 | window->consumeMotionDown(); |
| 12544 | spy->assertNoEvents(); |
| 12545 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12546 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12547 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12548 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12549 | window->consumeMotionUp(); |
| 12550 | spy->assertNoEvents(); |
| 12551 | |
| 12552 | // Inject an event inside the spy window's touchable region. |
| 12553 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12554 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12555 | ui::LogicalDisplayId::DEFAULT, {5, 10})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12556 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12557 | window->consumeMotionDown(); |
| 12558 | spy->consumeMotionDown(); |
| 12559 | } |
| 12560 | |
| 12561 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12562 | * 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] | 12563 | * 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] | 12564 | */ |
| 12565 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 12566 | auto window = createForeground(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12567 | window->setOwnerInfo(gui::Pid{12}, gui::Uid{34}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12568 | auto spy = createSpy(); |
| 12569 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12570 | spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12571 | spy->setFrame(Rect{0, 0, 20, 20}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12572 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12573 | |
| 12574 | // Inject an event outside the spy window's frame and touchable region. |
| 12575 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12576 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12577 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12578 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12579 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 12580 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12581 | } |
| 12582 | |
| 12583 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12584 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 12585 | * pointers that are down within its bounds. |
| 12586 | */ |
| 12587 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 12588 | auto windowLeft = createForeground(); |
| 12589 | windowLeft->setFrame({0, 0, 100, 200}); |
| 12590 | auto windowRight = createForeground(); |
| 12591 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12592 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12593 | spy->setFrame({0, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12594 | mDispatcher->onWindowInfosChanged( |
| 12595 | {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12596 | |
| 12597 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12598 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12599 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12600 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12601 | windowLeft->consumeMotionDown(); |
| 12602 | spy->consumeMotionDown(); |
| 12603 | |
| 12604 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 12605 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12606 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12607 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12608 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12609 | .build(); |
| 12610 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12611 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12612 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12613 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12614 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12615 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12616 | } |
| 12617 | |
| 12618 | /** |
| 12619 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 12620 | * the spy should receive the second pointer with ACTION_DOWN. |
| 12621 | */ |
| 12622 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 12623 | auto window = createForeground(); |
| 12624 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12625 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12626 | spyRight->setFrame({100, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12627 | mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12628 | |
| 12629 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12630 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12631 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12632 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12633 | window->consumeMotionDown(); |
| 12634 | spyRight->assertNoEvents(); |
| 12635 | |
| 12636 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 12637 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12638 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12639 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12640 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12641 | .build(); |
| 12642 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12643 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12644 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12645 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12646 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12647 | spyRight->consumeMotionDown(); |
| 12648 | } |
| 12649 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12650 | /** |
| 12651 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 12652 | * windows should be allowed to control split touch. |
| 12653 | */ |
| 12654 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 12655 | // 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] | 12656 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12657 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 12658 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12659 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12660 | auto window = createForeground(); |
| 12661 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12662 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12663 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12664 | |
| 12665 | // First finger down, no window touched. |
| 12666 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12667 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12668 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12669 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12670 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12671 | window->assertNoEvents(); |
| 12672 | |
| 12673 | // Second finger down on window, the window should receive touch down. |
| 12674 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 12675 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12676 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12677 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12678 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 12679 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12680 | .build(); |
| 12681 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12682 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12683 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12684 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12685 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12686 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12687 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12688 | } |
| 12689 | |
| 12690 | /** |
| 12691 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 12692 | * do not receive key events. |
| 12693 | */ |
| 12694 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12695 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12696 | spy->setFocusable(false); |
| 12697 | |
| 12698 | auto window = createForeground(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12699 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12700 | setFocusedWindow(window); |
| 12701 | window->consumeFocusEvent(true); |
| 12702 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12703 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12704 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12705 | window->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12706 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12707 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12708 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12709 | window->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12710 | |
| 12711 | spy->assertNoEvents(); |
| 12712 | } |
| 12713 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12714 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 12715 | |
| 12716 | /** |
| 12717 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 12718 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 12719 | */ |
| 12720 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 12721 | auto window = createForeground(); |
| 12722 | auto spy1 = createSpy(); |
| 12723 | auto spy2 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12724 | mDispatcher->onWindowInfosChanged( |
| 12725 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12726 | |
| 12727 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12728 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12729 | ui::LogicalDisplayId::DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12730 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12731 | window->consumeMotionDown(); |
| 12732 | spy1->consumeMotionDown(); |
| 12733 | spy2->consumeMotionDown(); |
| 12734 | |
| 12735 | // Pilfer pointers from the second spy window. |
| 12736 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 12737 | spy2->assertNoEvents(); |
| 12738 | spy1->consumeMotionCancel(); |
| 12739 | window->consumeMotionCancel(); |
| 12740 | |
| 12741 | // The rest of the gesture should only be sent to the second spy window. |
| 12742 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12743 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12744 | ui::LogicalDisplayId::DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12745 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12746 | spy2->consumeMotionMove(); |
| 12747 | spy1->assertNoEvents(); |
| 12748 | window->assertNoEvents(); |
| 12749 | } |
| 12750 | |
| 12751 | /** |
| 12752 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 12753 | * in the middle of the gesture. |
| 12754 | */ |
| 12755 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 12756 | auto window = createForeground(); |
| 12757 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12758 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12759 | |
| 12760 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12761 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12762 | ui::LogicalDisplayId::DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12763 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12764 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 12765 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12766 | |
| 12767 | window->releaseChannel(); |
| 12768 | |
| 12769 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 12770 | |
| 12771 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12772 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12773 | ui::LogicalDisplayId::DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12774 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12775 | spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12776 | } |
| 12777 | |
| 12778 | /** |
| 12779 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 12780 | * the spy, but not to any other windows. |
| 12781 | */ |
| 12782 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 12783 | auto spy = createSpy(); |
| 12784 | auto window = createForeground(); |
| 12785 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12786 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12787 | |
| 12788 | // First finger down on the window and the spy. |
| 12789 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12790 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12791 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12792 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12793 | spy->consumeMotionDown(); |
| 12794 | window->consumeMotionDown(); |
| 12795 | |
| 12796 | // Spy window pilfers the pointers. |
| 12797 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 12798 | window->consumeMotionCancel(); |
| 12799 | |
| 12800 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 12801 | const MotionEvent secondFingerDownEvent = |
| 12802 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12803 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12804 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12805 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 12806 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12807 | .build(); |
| 12808 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12809 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12810 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12811 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12812 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12813 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12814 | |
| 12815 | // Third finger goes down outside all windows, so injection should fail. |
| 12816 | const MotionEvent thirdFingerDownEvent = |
| 12817 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12818 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12819 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12820 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 12821 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 12822 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12823 | .build(); |
| 12824 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12825 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12826 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 12827 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12828 | |
| 12829 | spy->assertNoEvents(); |
| 12830 | window->assertNoEvents(); |
| 12831 | } |
| 12832 | |
| 12833 | /** |
| 12834 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 12835 | */ |
| 12836 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 12837 | auto spy = createSpy(); |
| 12838 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 12839 | auto window = createForeground(); |
| 12840 | window->setFrame(Rect(0, 0, 200, 200)); |
| 12841 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12842 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12843 | |
| 12844 | // First finger down on the window only |
| 12845 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12846 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12847 | ui::LogicalDisplayId::DEFAULT, {150, 150})) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12848 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12849 | window->consumeMotionDown(); |
| 12850 | |
| 12851 | // Second finger down on the spy and window |
| 12852 | const MotionEvent secondFingerDownEvent = |
| 12853 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12854 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12855 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12856 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 12857 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12858 | .build(); |
| 12859 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12860 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12861 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12862 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12863 | spy->consumeMotionDown(); |
| 12864 | window->consumeMotionPointerDown(1); |
| 12865 | |
| 12866 | // Third finger down on the spy and window |
| 12867 | const MotionEvent thirdFingerDownEvent = |
| 12868 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12869 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12870 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12871 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 12872 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 12873 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12874 | .build(); |
| 12875 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12876 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12877 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12878 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12879 | spy->consumeMotionPointerDown(1); |
| 12880 | window->consumeMotionPointerDown(2); |
| 12881 | |
| 12882 | // Spy window pilfers the pointers. |
| 12883 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12884 | window->consumeMotionPointerUp(/*idx=*/2, ui::LogicalDisplayId::DEFAULT, |
| 12885 | AMOTION_EVENT_FLAG_CANCELED); |
| 12886 | window->consumeMotionPointerUp(/*idx=*/1, ui::LogicalDisplayId::DEFAULT, |
| 12887 | AMOTION_EVENT_FLAG_CANCELED); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12888 | |
| 12889 | spy->assertNoEvents(); |
| 12890 | window->assertNoEvents(); |
| 12891 | } |
| 12892 | |
| 12893 | /** |
| 12894 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 12895 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 12896 | * window, then that window should receive ACTION_CANCEL. |
| 12897 | */ |
| 12898 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 12899 | auto spy = createSpy(); |
| 12900 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 12901 | auto window = createForeground(); |
| 12902 | window->setFrame(Rect(0, 0, 200, 200)); |
| 12903 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12904 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12905 | |
| 12906 | // First finger down on both spy and window |
| 12907 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12908 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12909 | ui::LogicalDisplayId::DEFAULT, {10, 10})) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12910 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12911 | window->consumeMotionDown(); |
| 12912 | spy->consumeMotionDown(); |
| 12913 | |
| 12914 | // Second finger down on the spy and window |
| 12915 | const MotionEvent secondFingerDownEvent = |
| 12916 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12917 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12918 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12919 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 12920 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12921 | .build(); |
| 12922 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12923 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12924 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12925 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12926 | spy->consumeMotionPointerDown(1); |
| 12927 | window->consumeMotionPointerDown(1); |
| 12928 | |
| 12929 | // Spy window pilfers the pointers. |
| 12930 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 12931 | window->consumeMotionCancel(); |
| 12932 | |
| 12933 | spy->assertNoEvents(); |
| 12934 | window->assertNoEvents(); |
| 12935 | } |
| 12936 | |
| 12937 | /** |
| 12938 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 12939 | * be sent to other windows |
| 12940 | */ |
| 12941 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 12942 | auto spy = createSpy(); |
| 12943 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 12944 | auto window = createForeground(); |
| 12945 | window->setFrame(Rect(0, 0, 200, 200)); |
| 12946 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12947 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12948 | |
| 12949 | // First finger down on both window and spy |
| 12950 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12951 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12952 | ui::LogicalDisplayId::DEFAULT, {10, 10})) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12953 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12954 | window->consumeMotionDown(); |
| 12955 | spy->consumeMotionDown(); |
| 12956 | |
| 12957 | // Spy window pilfers the pointers. |
| 12958 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 12959 | window->consumeMotionCancel(); |
| 12960 | |
| 12961 | // Second finger down on the window only |
| 12962 | const MotionEvent secondFingerDownEvent = |
| 12963 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12964 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12965 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12966 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 12967 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12968 | .build(); |
| 12969 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12970 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12971 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12972 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12973 | window->consumeMotionDown(); |
| 12974 | window->assertNoEvents(); |
| 12975 | |
| 12976 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 12977 | spy->consumeMotionMove(); |
| 12978 | spy->assertNoEvents(); |
| 12979 | } |
| 12980 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 12981 | /** |
| 12982 | * A window on the left and a window on the right. Also, a spy window that's above all of the |
| 12983 | * windows, and spanning both left and right windows. |
| 12984 | * Send simultaneous motion streams from two different devices, one to the left window, and another |
| 12985 | * to the right window. |
| 12986 | * Pilfer from spy window. |
| 12987 | * Check that the pilfering only affects the pointers that are actually being received by the spy. |
| 12988 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 12989 | TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer_legacy) { |
| 12990 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 12991 | sp<FakeWindowHandle> spy = createSpy(); |
| 12992 | spy->setFrame(Rect(0, 0, 200, 200)); |
| 12993 | sp<FakeWindowHandle> leftWindow = createForeground(); |
| 12994 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 12995 | |
| 12996 | sp<FakeWindowHandle> rightWindow = createForeground(); |
| 12997 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 12998 | |
| 12999 | constexpr int32_t stylusDeviceId = 1; |
| 13000 | constexpr int32_t touchDeviceId = 2; |
| 13001 | |
| 13002 | mDispatcher->onWindowInfosChanged( |
| 13003 | {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 13004 | |
| 13005 | // Stylus down on left window and spy |
| 13006 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 13007 | .deviceId(stylusDeviceId) |
| 13008 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 13009 | .build()); |
| 13010 | leftWindow->consumeMotionEvent( |
| 13011 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 13012 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 13013 | |
| 13014 | // Finger down on right window and spy - but spy already has stylus |
| 13015 | mDispatcher->notifyMotion( |
| 13016 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 13017 | .deviceId(touchDeviceId) |
| 13018 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 13019 | .build()); |
| 13020 | rightWindow->consumeMotionEvent( |
| 13021 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 13022 | spy->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 13023 | |
| 13024 | // Act: pilfer from spy. Spy is currently receiving touch events. |
| 13025 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 13026 | leftWindow->consumeMotionEvent( |
| 13027 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 13028 | rightWindow->consumeMotionEvent( |
| 13029 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 13030 | |
| 13031 | // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus |
| 13032 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 13033 | .deviceId(stylusDeviceId) |
| 13034 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52)) |
| 13035 | .build()); |
| 13036 | mDispatcher->notifyMotion( |
| 13037 | MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 13038 | .deviceId(touchDeviceId) |
| 13039 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52)) |
| 13040 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 13041 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 13042 | |
| 13043 | spy->assertNoEvents(); |
| 13044 | leftWindow->assertNoEvents(); |
| 13045 | rightWindow->assertNoEvents(); |
| 13046 | } |
| 13047 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13048 | /** |
| 13049 | * A window on the left and a window on the right. Also, a spy window that's above all of the |
| 13050 | * windows, and spanning both left and right windows. |
| 13051 | * Send simultaneous motion streams from two different devices, one to the left window, and another |
| 13052 | * to the right window. |
| 13053 | * Pilfer from spy window. |
| 13054 | * Check that the pilfering affects all of the pointers that are actually being received by the spy. |
| 13055 | * The spy should receive both the touch and the stylus events after pilfer. |
| 13056 | */ |
| 13057 | TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) { |
| 13058 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 13059 | sp<FakeWindowHandle> spy = createSpy(); |
| 13060 | spy->setFrame(Rect(0, 0, 200, 200)); |
| 13061 | sp<FakeWindowHandle> leftWindow = createForeground(); |
| 13062 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 13063 | |
| 13064 | sp<FakeWindowHandle> rightWindow = createForeground(); |
| 13065 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 13066 | |
| 13067 | constexpr int32_t stylusDeviceId = 1; |
| 13068 | constexpr int32_t touchDeviceId = 2; |
| 13069 | |
| 13070 | mDispatcher->onWindowInfosChanged( |
| 13071 | {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 13072 | |
| 13073 | // Stylus down on left window and spy |
| 13074 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 13075 | .deviceId(stylusDeviceId) |
| 13076 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 13077 | .build()); |
| 13078 | leftWindow->consumeMotionEvent( |
| 13079 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 13080 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 13081 | |
| 13082 | // Finger down on right window and spy |
| 13083 | mDispatcher->notifyMotion( |
| 13084 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 13085 | .deviceId(touchDeviceId) |
| 13086 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 13087 | .build()); |
| 13088 | rightWindow->consumeMotionEvent( |
| 13089 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 13090 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 13091 | |
| 13092 | // Act: pilfer from spy. Spy is currently receiving touch events. |
| 13093 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 13094 | leftWindow->consumeMotionEvent( |
| 13095 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId))); |
| 13096 | rightWindow->consumeMotionEvent( |
| 13097 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 13098 | |
| 13099 | // Continue movements from both stylus and touch. Touch and stylus will be delivered to spy |
Siarhei Vishniakou | 92bca1c | 2024-04-01 14:06:59 -0700 | [diff] [blame] | 13100 | // Instead of sending the two MOVE events for each input device together, and then receiving |
| 13101 | // them both, process them one at at time. InputConsumer is always in the batching mode, which |
| 13102 | // means that the two MOVE events will be initially put into a batch. Once the events are |
| 13103 | // batched, the 'consume' call may result in any of the MOVE events to be sent first (depending |
| 13104 | // on the implementation of InputConsumer), which would mean that the order of the received |
| 13105 | // events could be different depending on whether there are 1 or 2 events pending in the |
| 13106 | // InputChannel at the time the test calls 'consume'. To make assertions simpler here, and to |
| 13107 | // avoid this confusing behaviour, send and receive each MOVE event separately. |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13108 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 13109 | .deviceId(stylusDeviceId) |
| 13110 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52)) |
| 13111 | .build()); |
Siarhei Vishniakou | 92bca1c | 2024-04-01 14:06:59 -0700 | [diff] [blame] | 13112 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13113 | mDispatcher->notifyMotion( |
| 13114 | MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 13115 | .deviceId(touchDeviceId) |
| 13116 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52)) |
| 13117 | .build()); |
Siarhei Vishniakou | 92bca1c | 2024-04-01 14:06:59 -0700 | [diff] [blame] | 13118 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13119 | |
| 13120 | spy->assertNoEvents(); |
| 13121 | leftWindow->assertNoEvents(); |
| 13122 | rightWindow->assertNoEvents(); |
| 13123 | } |
| 13124 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 13125 | TEST_F(InputDispatcherPilferPointersTest, NoPilferingWithHoveringPointers) { |
| 13126 | auto window = createForeground(); |
| 13127 | auto spy = createSpy(); |
| 13128 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 13129 | |
| 13130 | mDispatcher->notifyMotion( |
| 13131 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 13132 | .deviceId(1) |
| 13133 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(100).y(200)) |
| 13134 | .build()); |
| 13135 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13136 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13137 | |
| 13138 | // Pilfer pointers from the spy window should fail. |
| 13139 | EXPECT_NE(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 13140 | spy->assertNoEvents(); |
| 13141 | window->assertNoEvents(); |
| 13142 | } |
| 13143 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13144 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 13145 | public: |
| 13146 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 13147 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 13148 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13149 | sp<FakeWindowHandle> overlay = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 13150 | "Stylus interceptor window", |
| 13151 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13152 | overlay->setFocusable(false); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13153 | overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 13154 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 13155 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13156 | overlay->setTrustedOverlay(true); |
| 13157 | |
| 13158 | std::shared_ptr<FakeApplicationHandle> application = |
| 13159 | std::make_shared<FakeApplicationHandle>(); |
| 13160 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 13161 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13162 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13163 | window->setFocusable(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13164 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13165 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13166 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13167 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13168 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 13169 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13170 | return {std::move(overlay), std::move(window)}; |
| 13171 | } |
| 13172 | |
| 13173 | void sendFingerEvent(int32_t action) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 13174 | mDispatcher->notifyMotion( |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13175 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13176 | ui::LogicalDisplayId::DEFAULT, {PointF{20, 20}})); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13177 | } |
| 13178 | |
| 13179 | void sendStylusEvent(int32_t action) { |
| 13180 | NotifyMotionArgs motionArgs = |
| 13181 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13182 | ui::LogicalDisplayId::DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 13183 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 13184 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13185 | } |
| 13186 | }; |
| 13187 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 13188 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 13189 | |
| 13190 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
Siarhei Vishniakou | ad3b682 | 2023-06-22 14:17:35 -0700 | [diff] [blame] | 13191 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 13192 | ScopedSilentDeath _silentDeath; |
| 13193 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13194 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 13195 | overlay->setTrustedOverlay(false); |
| 13196 | // 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] | 13197 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 13198 | {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13199 | ".* not a trusted overlay"); |
| 13200 | } |
| 13201 | |
| 13202 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 13203 | auto [overlay, window] = setupStylusOverlayScenario(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13204 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13205 | |
| 13206 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 13207 | overlay->consumeMotionDown(); |
| 13208 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 13209 | overlay->consumeMotionUp(); |
| 13210 | |
| 13211 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 13212 | window->consumeMotionDown(); |
| 13213 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 13214 | window->consumeMotionUp(); |
| 13215 | |
| 13216 | overlay->assertNoEvents(); |
| 13217 | window->assertNoEvents(); |
| 13218 | } |
| 13219 | |
| 13220 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 13221 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 13222 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13223 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13224 | |
| 13225 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 13226 | overlay->consumeMotionDown(); |
| 13227 | window->consumeMotionDown(); |
| 13228 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 13229 | overlay->consumeMotionUp(); |
| 13230 | window->consumeMotionUp(); |
| 13231 | |
| 13232 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 13233 | window->consumeMotionDown(); |
| 13234 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 13235 | window->consumeMotionUp(); |
| 13236 | |
| 13237 | overlay->assertNoEvents(); |
| 13238 | window->assertNoEvents(); |
| 13239 | } |
| 13240 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 13241 | /** |
| 13242 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 13243 | * The scenario is as follows: |
| 13244 | * - The stylus interceptor overlay is configured as a spy window. |
| 13245 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 13246 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 13247 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 13248 | */ |
| 13249 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 13250 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 13251 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13252 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 13253 | |
| 13254 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 13255 | overlay->consumeMotionDown(); |
| 13256 | window->consumeMotionDown(); |
| 13257 | |
| 13258 | // The interceptor pilfers the pointers. |
| 13259 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 13260 | window->consumeMotionCancel(); |
| 13261 | |
| 13262 | // The interceptor configures itself so that it is no longer a spy. |
| 13263 | overlay->setSpy(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13264 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 13265 | |
| 13266 | // It continues to receive the rest of the stylus gesture. |
| 13267 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 13268 | overlay->consumeMotionMove(); |
| 13269 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 13270 | overlay->consumeMotionUp(); |
| 13271 | |
| 13272 | window->assertNoEvents(); |
| 13273 | } |
| 13274 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13275 | struct User { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13276 | gui::Pid mPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 13277 | gui::Uid mUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13278 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 13279 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 13280 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13281 | User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid) |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13282 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 13283 | |
| 13284 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13285 | return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13286 | ui::LogicalDisplayId::DEFAULT, {100, 200}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13287 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 13288 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 13289 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 13290 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 13291 | } |
| 13292 | |
| 13293 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 13294 | return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13295 | ui::LogicalDisplayId::INVALID, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13296 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 13297 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13298 | mPolicyFlags); |
| 13299 | } |
| 13300 | |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13301 | sp<FakeWindowHandle> createWindow(const char* name) const { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13302 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 13303 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13304 | sp<FakeWindowHandle> window = |
| 13305 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, name, |
| 13306 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13307 | window->setOwnerInfo(mPid, mUid); |
| 13308 | return window; |
| 13309 | } |
| 13310 | }; |
| 13311 | |
| 13312 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 13313 | |
| 13314 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13315 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13316 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13317 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13318 | |
| 13319 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 13320 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 13321 | window->consumeMotionDown(); |
| 13322 | |
| 13323 | setFocusedWindow(window); |
| 13324 | window->consumeFocusEvent(true); |
| 13325 | |
| 13326 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 13327 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13328 | window->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13329 | } |
| 13330 | |
| 13331 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13332 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13333 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13334 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13335 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13336 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13337 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 13338 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 13339 | |
| 13340 | setFocusedWindow(window); |
| 13341 | window->consumeFocusEvent(true); |
| 13342 | |
| 13343 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 13344 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 13345 | window->assertNoEvents(); |
| 13346 | } |
| 13347 | |
| 13348 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13349 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13350 | auto window = owner.createWindow("Owned window"); |
| 13351 | auto spy = owner.createWindow("Owned spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13352 | spy->setSpy(true); |
| 13353 | spy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13354 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13355 | |
| 13356 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 13357 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 13358 | spy->consumeMotionDown(); |
| 13359 | window->consumeMotionDown(); |
| 13360 | } |
| 13361 | |
| 13362 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13363 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13364 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13365 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13366 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13367 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13368 | randosSpy->setSpy(true); |
| 13369 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13370 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13371 | |
| 13372 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 13373 | // not receive the event. |
| 13374 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 13375 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 13376 | randosSpy->assertNoEvents(); |
| 13377 | window->consumeMotionDown(); |
| 13378 | } |
| 13379 | |
| 13380 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13381 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13382 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13383 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13384 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13385 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13386 | randosSpy->setSpy(true); |
| 13387 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13388 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13389 | |
| 13390 | // A user that has injection permission can inject into any window. |
| 13391 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13392 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13393 | ui::LogicalDisplayId::DEFAULT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13394 | randosSpy->consumeMotionDown(); |
| 13395 | window->consumeMotionDown(); |
| 13396 | |
| 13397 | setFocusedWindow(randosSpy); |
| 13398 | randosSpy->consumeFocusEvent(true); |
| 13399 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13400 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13401 | randosSpy->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13402 | window->assertNoEvents(); |
| 13403 | } |
| 13404 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 13405 | TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13406 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13407 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13408 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13409 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13410 | auto randosWindow = rando.createWindow("Rando's window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13411 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 13412 | randosWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13413 | mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13414 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 13415 | // 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] | 13416 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 13417 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 13418 | window->consumeMotionDown(); |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 13419 | randosWindow->assertNoEvents(); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13420 | } |
| 13421 | |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13422 | using InputDispatcherPointerInWindowTest = InputDispatcherTest; |
| 13423 | |
| 13424 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWhenHovering) { |
| 13425 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 13426 | |
| 13427 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13428 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13429 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13430 | sp<FakeWindowHandle> right = |
| 13431 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 13432 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13433 | right->setFrame(Rect(100, 0, 200, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13434 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", |
| 13435 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13436 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 13437 | spy->setTrustedOverlay(true); |
| 13438 | spy->setSpy(true); |
| 13439 | |
| 13440 | mDispatcher->onWindowInfosChanged( |
| 13441 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 13442 | |
| 13443 | // Hover into the left window. |
| 13444 | mDispatcher->notifyMotion( |
| 13445 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 13446 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(50).y(50)) |
| 13447 | .build()); |
| 13448 | |
| 13449 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13450 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13451 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13452 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13453 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13454 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13455 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13456 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13457 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13458 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13459 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13460 | /*pointerId=*/0)); |
| 13461 | |
| 13462 | // Hover move to the right window. |
| 13463 | mDispatcher->notifyMotion( |
| 13464 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 13465 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 13466 | .build()); |
| 13467 | |
| 13468 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 13469 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13470 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 13471 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13472 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13473 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13474 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13475 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13476 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13477 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13478 | ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13479 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13480 | /*pointerId=*/0)); |
| 13481 | |
| 13482 | // Stop hovering. |
| 13483 | mDispatcher->notifyMotion( |
| 13484 | MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 13485 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 13486 | .build()); |
| 13487 | |
| 13488 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 13489 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 13490 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13491 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13492 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13493 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13494 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13495 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13496 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13497 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13498 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13499 | /*pointerId=*/0)); |
| 13500 | } |
| 13501 | |
| 13502 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWithSplitTouch) { |
| 13503 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 13504 | |
| 13505 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13506 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13507 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13508 | sp<FakeWindowHandle> right = |
| 13509 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 13510 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13511 | right->setFrame(Rect(100, 0, 200, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13512 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", |
| 13513 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13514 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 13515 | spy->setTrustedOverlay(true); |
| 13516 | spy->setSpy(true); |
| 13517 | |
| 13518 | mDispatcher->onWindowInfosChanged( |
| 13519 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 13520 | |
| 13521 | // First pointer down on left window. |
| 13522 | mDispatcher->notifyMotion( |
| 13523 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 13524 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 13525 | .build()); |
| 13526 | |
| 13527 | left->consumeMotionDown(); |
| 13528 | spy->consumeMotionDown(); |
| 13529 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13530 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13531 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13532 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13533 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13534 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13535 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13536 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13537 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13538 | /*pointerId=*/0)); |
| 13539 | |
| 13540 | // Second pointer down on right window. |
| 13541 | mDispatcher->notifyMotion( |
| 13542 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 13543 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 13544 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 13545 | .build()); |
| 13546 | |
| 13547 | left->consumeMotionMove(); |
| 13548 | right->consumeMotionDown(); |
| 13549 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 13550 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13551 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13552 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13553 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13554 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13555 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13556 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13557 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13558 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13559 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13560 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13561 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13562 | /*pointerId=*/1)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13563 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13564 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13565 | /*pointerId=*/1)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13566 | ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13567 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13568 | /*pointerId=*/1)); |
| 13569 | |
| 13570 | // Second pointer up. |
| 13571 | mDispatcher->notifyMotion( |
| 13572 | MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 13573 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 13574 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 13575 | .build()); |
| 13576 | |
| 13577 | left->consumeMotionMove(); |
| 13578 | right->consumeMotionUp(); |
| 13579 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 13580 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13581 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13582 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13583 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13584 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13585 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13586 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13587 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13588 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13589 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13590 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13591 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13592 | /*pointerId=*/1)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13593 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13594 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13595 | /*pointerId=*/1)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13596 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13597 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13598 | /*pointerId=*/1)); |
| 13599 | |
| 13600 | // First pointer up. |
| 13601 | mDispatcher->notifyMotion( |
| 13602 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 13603 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 13604 | .build()); |
| 13605 | |
| 13606 | left->consumeMotionUp(); |
| 13607 | spy->consumeMotionUp(); |
| 13608 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13609 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13610 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13611 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13612 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13613 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13614 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13615 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13616 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13617 | /*pointerId=*/0)); |
| 13618 | } |
| 13619 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13620 | TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse_legacy) { |
| 13621 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13622 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 13623 | |
| 13624 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13625 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13626 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13627 | sp<FakeWindowHandle> right = |
| 13628 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 13629 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13630 | right->setFrame(Rect(100, 0, 200, 100)); |
| 13631 | |
| 13632 | mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 13633 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13634 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13635 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13636 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13637 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13638 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13639 | /*pointerId=*/0)); |
| 13640 | |
| 13641 | // Hover move into the window. |
| 13642 | mDispatcher->notifyMotion( |
| 13643 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 13644 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50)) |
| 13645 | .rawXCursorPosition(50) |
| 13646 | .rawYCursorPosition(50) |
| 13647 | .deviceId(DEVICE_ID) |
| 13648 | .build()); |
| 13649 | |
| 13650 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13651 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13652 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13653 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13654 | /*pointerId=*/0)); |
| 13655 | |
| 13656 | // Move the mouse with another device. This cancels the hovering pointer from the first device. |
| 13657 | mDispatcher->notifyMotion( |
| 13658 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 13659 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50)) |
| 13660 | .rawXCursorPosition(51) |
| 13661 | .rawYCursorPosition(50) |
| 13662 | .deviceId(SECOND_DEVICE_ID) |
| 13663 | .build()); |
| 13664 | |
| 13665 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 13666 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13667 | |
| 13668 | // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets |
| 13669 | // a HOVER_EXIT from the first device. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13670 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13671 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13672 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13673 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13674 | SECOND_DEVICE_ID, |
| 13675 | /*pointerId=*/0)); |
| 13676 | |
| 13677 | // Move the mouse outside the window. Document the current behavior, where the window does not |
| 13678 | // receive HOVER_EXIT even though the mouse left the window. |
| 13679 | mDispatcher->notifyMotion( |
| 13680 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 13681 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50)) |
| 13682 | .rawXCursorPosition(150) |
| 13683 | .rawYCursorPosition(50) |
| 13684 | .deviceId(SECOND_DEVICE_ID) |
| 13685 | .build()); |
| 13686 | |
| 13687 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 13688 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13689 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13690 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13691 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13692 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13693 | SECOND_DEVICE_ID, |
| 13694 | /*pointerId=*/0)); |
| 13695 | } |
| 13696 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13697 | /** |
| 13698 | * TODO(b/313689709) - correctly support multiple mouse devices, because they should be controlling |
| 13699 | * the same cursor, and therefore have a shared motion event stream. |
| 13700 | */ |
| 13701 | TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse) { |
| 13702 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 13703 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 13704 | |
| 13705 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13706 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13707 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13708 | sp<FakeWindowHandle> right = |
| 13709 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 13710 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13711 | right->setFrame(Rect(100, 0, 200, 100)); |
| 13712 | |
| 13713 | mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 13714 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13715 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13716 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13717 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13718 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13719 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13720 | /*pointerId=*/0)); |
| 13721 | |
| 13722 | // Hover move into the window. |
| 13723 | mDispatcher->notifyMotion( |
| 13724 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 13725 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50)) |
| 13726 | .rawXCursorPosition(50) |
| 13727 | .rawYCursorPosition(50) |
| 13728 | .deviceId(DEVICE_ID) |
| 13729 | .build()); |
| 13730 | |
| 13731 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13732 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13733 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13734 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13735 | /*pointerId=*/0)); |
| 13736 | |
| 13737 | // Move the mouse with another device |
| 13738 | mDispatcher->notifyMotion( |
| 13739 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 13740 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50)) |
| 13741 | .rawXCursorPosition(51) |
| 13742 | .rawYCursorPosition(50) |
| 13743 | .deviceId(SECOND_DEVICE_ID) |
| 13744 | .build()); |
| 13745 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13746 | |
| 13747 | // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets |
| 13748 | // a HOVER_EXIT from the first device. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13749 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13750 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13751 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13752 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13753 | SECOND_DEVICE_ID, |
| 13754 | /*pointerId=*/0)); |
| 13755 | |
| 13756 | // Move the mouse outside the window. Document the current behavior, where the window does not |
| 13757 | // receive HOVER_EXIT even though the mouse left the window. |
| 13758 | mDispatcher->notifyMotion( |
| 13759 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 13760 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50)) |
| 13761 | .rawXCursorPosition(150) |
| 13762 | .rawYCursorPosition(50) |
| 13763 | .deviceId(SECOND_DEVICE_ID) |
| 13764 | .build()); |
| 13765 | |
| 13766 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13767 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13768 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13769 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13770 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13771 | SECOND_DEVICE_ID, |
| 13772 | /*pointerId=*/0)); |
| 13773 | } |
| 13774 | |
Arpit Singh | b65e2bd | 2024-06-03 09:48:16 +0000 | [diff] [blame^] | 13775 | TEST_F(InputDispatcherTest, FocusedDisplayChangeIsNotified) { |
| 13776 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 13777 | mFakePolicy->assertFocusedDisplayNotified(SECOND_DISPLAY_ID); |
| 13778 | } |
| 13779 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 13780 | } // namespace android::inputdispatcher |