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_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 415 | NotifySwitchArgs args(InputEvent::nextId(), /*eventTime=*/20, /*policyFlags=*/0, |
| 416 | /*switchValues=*/1, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 417 | /*switchMask=*/2); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 418 | mDispatcher->notifySwitch(args); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 419 | |
| 420 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 421 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 422 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 423 | } |
| 424 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 425 | namespace { |
| 426 | |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 427 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 428 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 429 | class FakeMonitorReceiver { |
| 430 | public: |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 431 | FakeMonitorReceiver(InputDispatcher& dispatcher, const std::string name, |
| 432 | ui::LogicalDisplayId displayId) |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 433 | : mInputReceiver(*dispatcher.createInputMonitor(displayId, name, MONITOR_PID), name) {} |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 434 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 435 | sp<IBinder> getToken() { return mInputReceiver.getToken(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 436 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 437 | void consumeKeyDown(ui::LogicalDisplayId expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 438 | mInputReceiver.consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 439 | expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | std::optional<int32_t> receiveEvent() { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 443 | const auto [sequenceNum, _] = mInputReceiver.receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED); |
| 444 | return sequenceNum; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 447 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver.finishEvent(consumeSeq); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 448 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 449 | void consumeMotionDown(ui::LogicalDisplayId expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 450 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 451 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 454 | void consumeMotionMove(ui::LogicalDisplayId expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 455 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 456 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 459 | void consumeMotionUp(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_UP, |
| 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 consumeMotionCancel(ui::LogicalDisplayId expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 465 | mInputReceiver.consumeMotionEvent( |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 466 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 467 | WithDisplayId(expectedDisplayId), |
| 468 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
| 469 | } |
| 470 | |
| 471 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 472 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 473 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 474 | mInputReceiver.consumeEvent(InputEventType::MOTION, action, ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 475 | /*expectedFlags=*/0); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 479 | mInputReceiver.consumeMotionEvent(matcher); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 482 | std::unique_ptr<MotionEvent> consumeMotion() { return mInputReceiver.consumeMotion(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 483 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 484 | void assertNoEvents() { mInputReceiver.assertNoEvents(CONSUME_TIMEOUT_NO_EVENT_EXPECTED); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 485 | |
| 486 | private: |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 487 | FakeInputReceiver mInputReceiver; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 488 | }; |
| 489 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 490 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 491 | InputDispatcher& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 492 | ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 493 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 494 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 495 | bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 496 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 497 | KeyEvent event; |
| 498 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 499 | |
| 500 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 501 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 502 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 503 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 504 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 505 | if (!allowKeyRepeat) { |
| 506 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 507 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 508 | // Inject event until dispatch out. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 509 | return dispatcher.injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 510 | } |
| 511 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 512 | static void assertInjectedKeyTimesOut(InputDispatcher& dispatcher) { |
| 513 | InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 514 | injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 515 | ui::LogicalDisplayId::INVALID, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 516 | CONSUME_TIMEOUT_NO_EVENT_EXPECTED); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 517 | if (result != InputEventInjectionResult::TIMED_OUT) { |
| 518 | FAIL() << "Injection should have timed out, but got " << ftl::enum_string(result); |
| 519 | } |
| 520 | } |
| 521 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 522 | static InputEventInjectionResult injectKeyDown( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 523 | InputDispatcher& dispatcher, |
| 524 | ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 525 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 528 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 529 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 530 | // has to be woken up to process the repeating key. |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 531 | static InputEventInjectionResult injectKeyDownNoRepeat( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 532 | InputDispatcher& dispatcher, |
| 533 | ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 534 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 535 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 536 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 537 | } |
| 538 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 539 | static InputEventInjectionResult injectKeyUp( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 540 | InputDispatcher& dispatcher, |
| 541 | ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 542 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 545 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 546 | InputDispatcher& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 547 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 548 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 549 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 550 | return dispatcher.injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 551 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 554 | static InputEventInjectionResult injectMotionEvent( |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 555 | InputDispatcher& dispatcher, int32_t action, int32_t source, ui::LogicalDisplayId displayId, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 556 | const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 557 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 558 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 559 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 560 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 561 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 562 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 563 | MotionEventBuilder motionBuilder = |
| 564 | MotionEventBuilder(action, source) |
| 565 | .displayId(displayId) |
| 566 | .eventTime(eventTime) |
| 567 | .rawXCursorPosition(cursorPosition.x) |
| 568 | .rawYCursorPosition(cursorPosition.y) |
| 569 | .pointer( |
| 570 | PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y)); |
| 571 | if (MotionEvent::getActionMasked(action) == ACTION_DOWN) { |
| 572 | motionBuilder.downTime(eventTime); |
| 573 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 574 | |
| 575 | // Inject event until dispatch out. |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 576 | return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode, |
| 577 | targetUid, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 578 | } |
| 579 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 580 | static InputEventInjectionResult injectMotionDown(InputDispatcher& dispatcher, int32_t source, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 581 | ui::LogicalDisplayId displayId, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 582 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 583 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 586 | static InputEventInjectionResult injectMotionUp(InputDispatcher& dispatcher, int32_t source, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 587 | ui::LogicalDisplayId displayId, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 588 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 589 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 592 | static NotifyKeyArgs generateKeyArgs( |
| 593 | int32_t action, ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 594 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 595 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 596 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 597 | AINPUT_SOURCE_KEYBOARD, displayId, POLICY_FLAG_PASS_TO_USER, action, |
| 598 | /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 599 | |
| 600 | return args; |
| 601 | } |
| 602 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 603 | [[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 604 | ui::LogicalDisplayId displayId, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 605 | const std::vector<PointF>& points) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 606 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 607 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 608 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 609 | } |
| 610 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 611 | PointerProperties pointerProperties[pointerCount]; |
| 612 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 613 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 614 | for (size_t i = 0; i < pointerCount; i++) { |
| 615 | pointerProperties[i].clear(); |
| 616 | pointerProperties[i].id = i; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 617 | pointerProperties[i].toolType = ToolType::FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 618 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 619 | pointerCoords[i].clear(); |
| 620 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 621 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 622 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 623 | |
| 624 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 625 | // Define a valid motion event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 626 | NotifyMotionArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, source, |
| 627 | displayId, POLICY_FLAG_PASS_TO_USER, action, /*actionButton=*/0, |
| 628 | /*flags=*/0, AMETA_NONE, /*buttonState=*/0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 629 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 630 | pointerCoords, /*xPrecision=*/0, /*yPrecision=*/0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 631 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 632 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /*videoFrames=*/{}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 633 | |
| 634 | return args; |
| 635 | } |
| 636 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 637 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 638 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 639 | } |
| 640 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 641 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
| 642 | ui::LogicalDisplayId displayId) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 643 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 644 | } |
| 645 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 646 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 647 | const PointerCaptureRequest& request) { |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 648 | return NotifyPointerCaptureChangedArgs(InputEvent::nextId(), systemTime(SYSTEM_TIME_MONOTONIC), |
| 649 | request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 650 | } |
| 651 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 652 | } // namespace |
| 653 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 654 | /** |
| 655 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 656 | * broken channel. |
| 657 | */ |
| 658 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 659 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 660 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 661 | "Window that breaks its input channel", |
| 662 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 663 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 664 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 665 | |
| 666 | // Window closes its channel, but the window remains. |
| 667 | window->destroyReceiver(); |
| 668 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 669 | } |
| 670 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 671 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 672 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 673 | sp<FakeWindowHandle> window = |
| 674 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 675 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 676 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 677 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 678 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 679 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 680 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 681 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 682 | |
| 683 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 684 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 685 | } |
| 686 | |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 687 | using InputDispatcherDeathTest = InputDispatcherTest; |
| 688 | |
| 689 | /** |
| 690 | * When 'onWindowInfosChanged' arguments contain a duplicate entry for the same window, dispatcher |
| 691 | * should crash. |
| 692 | */ |
| 693 | TEST_F(InputDispatcherDeathTest, DuplicateWindowInfosAbortDispatcher) { |
| 694 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
| 695 | ScopedSilentDeath _silentDeath; |
| 696 | |
| 697 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 698 | sp<FakeWindowHandle> window = |
| 699 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 700 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 701 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 702 | {{*window->getInfo(), *window->getInfo()}, {}, 0, 0}), |
| 703 | "Incorrect WindowInfosUpdate provided"); |
| 704 | } |
| 705 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 706 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 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); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 711 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 712 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 713 | // Inject a MotionEvent to an unknown display. |
| 714 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 715 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 716 | ui::LogicalDisplayId::INVALID)) |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 717 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 718 | |
| 719 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 720 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 723 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 724 | * Calling onWindowInfosChanged once should not cause any issues. |
| 725 | * 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] | 726 | * called twice. |
| 727 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 728 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 729 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 730 | sp<FakeWindowHandle> window = |
| 731 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 732 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 733 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 734 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 735 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 736 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 737 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 738 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 739 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 740 | |
| 741 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 742 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 746 | * Calling onWindowInfosChanged twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 747 | */ |
| 748 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 749 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 750 | sp<FakeWindowHandle> window = |
| 751 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 752 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 753 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 754 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 755 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 756 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 757 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 758 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 759 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 760 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 761 | |
| 762 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 763 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 766 | // The foreground window should receive the first touch down event. |
| 767 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 768 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 769 | sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 770 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 771 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 772 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 773 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 774 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 775 | mDispatcher->onWindowInfosChanged( |
| 776 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 777 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 778 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 779 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 780 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 781 | |
| 782 | // 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] | 783 | windowTop->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 784 | windowSecond->assertNoEvents(); |
| 785 | } |
| 786 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 787 | /** |
| 788 | * Two windows: A top window, and a wallpaper behind the window. |
| 789 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 790 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 791 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 792 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 793 | */ |
| 794 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 795 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 796 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 797 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 798 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 799 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 800 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 801 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 802 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 803 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 804 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 805 | mDispatcher->onWindowInfosChanged( |
| 806 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 807 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 808 | injectMotionEvent(*mDispatcher, |
| 809 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 810 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(200)) |
| 811 | .build())) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 812 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 813 | |
| 814 | // Both foreground window and its wallpaper should receive the touch down |
| 815 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 816 | wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 817 | |
| 818 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 819 | injectMotionEvent(*mDispatcher, |
| 820 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 821 | .pointer(PointerBuilder(0, ToolType::FINGER).x(110).y(200)) |
| 822 | .build())) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 823 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 824 | |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 825 | foregroundWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 826 | wallpaperWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 827 | |
| 828 | // Now the foreground window goes away, but the wallpaper stays |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 829 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 830 | foregroundWindow->consumeMotionCancel(); |
| 831 | // 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] | 832 | wallpaperWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | /** |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 836 | * Two fingers down on the window, and lift off the first finger. |
| 837 | * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event |
| 838 | * contains a single pointer. |
| 839 | */ |
| 840 | TEST_F(InputDispatcherTest, CancelAfterPointer0Up) { |
| 841 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 842 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 843 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 844 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 845 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 846 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 847 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 848 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 849 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 850 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 851 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 852 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 853 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 854 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 855 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 856 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 857 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 858 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 859 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 860 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 861 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 862 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 863 | |
| 864 | // Remove the window. The gesture should be canceled |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 865 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 866 | const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}}; |
| 867 | window->consumeMotionEvent( |
| 868 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers))); |
| 869 | } |
| 870 | |
| 871 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 872 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 873 | * with the following differences: |
| 874 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 875 | * clean up the connection. |
| 876 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 877 | * Ensure that there's no crash in the dispatcher. |
| 878 | */ |
| 879 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 880 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 881 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 882 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 883 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 884 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 885 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 886 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 887 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 888 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 889 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 890 | mDispatcher->onWindowInfosChanged( |
| 891 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 892 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 893 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 894 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 895 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 896 | |
| 897 | // Both foreground window and its wallpaper should receive the touch down |
| 898 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 899 | wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 900 | |
| 901 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 902 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 903 | ui::LogicalDisplayId::DEFAULT, {110, 200})) |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 904 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 905 | |
| 906 | foregroundWindow->consumeMotionMove(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 907 | wallpaperWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 908 | |
| 909 | // Wallpaper closes its channel, but the window remains. |
| 910 | wallpaperWindow->destroyReceiver(); |
| 911 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 912 | |
| 913 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 914 | // is no longer valid. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 915 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 916 | foregroundWindow->consumeMotionCancel(); |
| 917 | } |
| 918 | |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 919 | /** |
| 920 | * Two windows: left and right, and a separate wallpaper window underneath each. Device A sends a |
| 921 | * down event to the left window. Device B sends a down event to the right window. Next, the right |
| 922 | * window disappears. Both the right window and its wallpaper window should receive cancel event. |
| 923 | * The left window and its wallpaper window should not receive any events. |
| 924 | */ |
| 925 | TEST_F(InputDispatcherTest, MultiDeviceDisappearingWindowWithWallpaperWindows) { |
| 926 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 927 | sp<FakeWindowHandle> leftForegroundWindow = |
| 928 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 929 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 930 | leftForegroundWindow->setFrame(Rect(0, 0, 100, 100)); |
| 931 | leftForegroundWindow->setDupTouchToWallpaper(true); |
| 932 | sp<FakeWindowHandle> leftWallpaperWindow = |
| 933 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 934 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 935 | leftWallpaperWindow->setFrame(Rect(0, 0, 100, 100)); |
| 936 | leftWallpaperWindow->setIsWallpaper(true); |
| 937 | |
| 938 | sp<FakeWindowHandle> rightForegroundWindow = |
| 939 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 940 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 941 | rightForegroundWindow->setFrame(Rect(100, 0, 200, 100)); |
| 942 | rightForegroundWindow->setDupTouchToWallpaper(true); |
| 943 | sp<FakeWindowHandle> rightWallpaperWindow = |
| 944 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 945 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 946 | rightWallpaperWindow->setFrame(Rect(100, 0, 200, 100)); |
| 947 | rightWallpaperWindow->setIsWallpaper(true); |
| 948 | |
| 949 | mDispatcher->onWindowInfosChanged( |
| 950 | {{*leftForegroundWindow->getInfo(), *leftWallpaperWindow->getInfo(), |
| 951 | *rightForegroundWindow->getInfo(), *rightWallpaperWindow->getInfo()}, |
| 952 | {}, |
| 953 | 0, |
| 954 | 0}); |
| 955 | |
| 956 | const DeviceId deviceA = 9; |
| 957 | const DeviceId deviceB = 3; |
| 958 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 959 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 960 | .deviceId(deviceA) |
| 961 | .build()); |
| 962 | leftForegroundWindow->consumeMotionEvent( |
| 963 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 964 | leftWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 965 | WithDeviceId(deviceA), |
| 966 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 967 | |
| 968 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 969 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 970 | .deviceId(deviceB) |
| 971 | .build()); |
| 972 | rightForegroundWindow->consumeMotionEvent( |
| 973 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 974 | rightWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 975 | WithDeviceId(deviceB), |
| 976 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 977 | |
| 978 | // Now right foreground window disappears, but right wallpaper window remains. |
| 979 | mDispatcher->onWindowInfosChanged( |
| 980 | {{*leftForegroundWindow->getInfo(), *leftWallpaperWindow->getInfo(), |
| 981 | *rightWallpaperWindow->getInfo()}, |
| 982 | {}, |
| 983 | 0, |
| 984 | 0}); |
| 985 | |
| 986 | // Left foreground window and left wallpaper window still exist, and should not receive any |
| 987 | // events. |
| 988 | leftForegroundWindow->assertNoEvents(); |
| 989 | leftWallpaperWindow->assertNoEvents(); |
| 990 | // Since right foreground window disappeared, right wallpaper window and right foreground window |
| 991 | // should receive cancel events. |
| 992 | rightForegroundWindow->consumeMotionEvent( |
| 993 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB))); |
| 994 | rightWallpaperWindow->consumeMotionEvent( |
| 995 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB), |
| 996 | WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_CANCELED))); |
| 997 | } |
| 998 | |
| 999 | /** |
| 1000 | * Three windows arranged horizontally and without any overlap. Every window has a |
| 1001 | * wallpaper window underneath. The middle window also has SLIPPERY flag. |
| 1002 | * Device A sends a down event to the left window. Device B sends a down event to the middle window. |
| 1003 | * Next, device B sends move event to the right window. Touch for device B should slip from the |
| 1004 | * middle window to the right window. Also, the right wallpaper window should receive a down event. |
| 1005 | * The middle window and its wallpaper window should receive a cancel event. The left window should |
| 1006 | * not receive any events. If device B continues to report events, the right window and its |
| 1007 | * wallpaper window should receive remaining events. |
| 1008 | */ |
| 1009 | TEST_F(InputDispatcherTest, MultiDeviceSlipperyTouchWithWallpaperWindow) { |
| 1010 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1011 | sp<FakeWindowHandle> leftForegroundWindow = |
| 1012 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1013 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1014 | leftForegroundWindow->setFrame(Rect(0, 0, 100, 100)); |
| 1015 | leftForegroundWindow->setDupTouchToWallpaper(true); |
| 1016 | sp<FakeWindowHandle> leftWallpaperWindow = |
| 1017 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1018 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1019 | leftWallpaperWindow->setFrame(Rect(0, 0, 100, 100)); |
| 1020 | leftWallpaperWindow->setIsWallpaper(true); |
| 1021 | |
| 1022 | sp<FakeWindowHandle> middleForegroundWindow = |
| 1023 | sp<FakeWindowHandle>::make(application, mDispatcher, "Middle foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1024 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1025 | middleForegroundWindow->setFrame(Rect(100, 0, 200, 100)); |
| 1026 | middleForegroundWindow->setDupTouchToWallpaper(true); |
| 1027 | middleForegroundWindow->setSlippery(true); |
| 1028 | sp<FakeWindowHandle> middleWallpaperWindow = |
| 1029 | sp<FakeWindowHandle>::make(application, mDispatcher, "Middle wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1030 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1031 | middleWallpaperWindow->setFrame(Rect(100, 0, 200, 100)); |
| 1032 | middleWallpaperWindow->setIsWallpaper(true); |
| 1033 | |
| 1034 | sp<FakeWindowHandle> rightForegroundWindow = |
| 1035 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1036 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1037 | rightForegroundWindow->setFrame(Rect(200, 0, 300, 100)); |
| 1038 | rightForegroundWindow->setDupTouchToWallpaper(true); |
| 1039 | sp<FakeWindowHandle> rightWallpaperWindow = |
| 1040 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1041 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1042 | rightWallpaperWindow->setFrame(Rect(200, 0, 300, 100)); |
| 1043 | rightWallpaperWindow->setIsWallpaper(true); |
| 1044 | |
| 1045 | mDispatcher->onWindowInfosChanged( |
| 1046 | {{*leftForegroundWindow->getInfo(), *leftWallpaperWindow->getInfo(), |
| 1047 | *middleForegroundWindow->getInfo(), *middleWallpaperWindow->getInfo(), |
| 1048 | *rightForegroundWindow->getInfo(), *rightWallpaperWindow->getInfo()}, |
| 1049 | {}, |
| 1050 | 0, |
| 1051 | 0}); |
| 1052 | |
| 1053 | const DeviceId deviceA = 9; |
| 1054 | const DeviceId deviceB = 3; |
| 1055 | // Device A sends a DOWN event to the left window |
| 1056 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1057 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 1058 | .deviceId(deviceA) |
| 1059 | .build()); |
| 1060 | leftForegroundWindow->consumeMotionEvent( |
| 1061 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 1062 | leftWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 1063 | WithDeviceId(deviceA), |
| 1064 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 1065 | // Device B sends a DOWN event to the middle window |
| 1066 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1067 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 1068 | .deviceId(deviceB) |
| 1069 | .build()); |
| 1070 | middleForegroundWindow->consumeMotionEvent( |
| 1071 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 1072 | middleWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 1073 | WithDeviceId(deviceB), |
| 1074 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 1075 | // Move the events of device B to the top of the right window. |
| 1076 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1077 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(50)) |
| 1078 | .deviceId(deviceB) |
| 1079 | .build()); |
| 1080 | middleForegroundWindow->consumeMotionEvent( |
| 1081 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB))); |
| 1082 | middleWallpaperWindow->consumeMotionEvent( |
| 1083 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB), |
| 1084 | WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_CANCELED))); |
| 1085 | rightForegroundWindow->consumeMotionEvent( |
| 1086 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 1087 | rightWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 1088 | WithDeviceId(deviceB), |
| 1089 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 1090 | // Make sure the window on the right can receive the remaining events. |
| 1091 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1092 | .pointer(PointerBuilder(0, ToolType::FINGER).x(251).y(51)) |
| 1093 | .deviceId(deviceB) |
| 1094 | .build()); |
| 1095 | leftForegroundWindow->assertNoEvents(); |
| 1096 | leftWallpaperWindow->assertNoEvents(); |
| 1097 | middleForegroundWindow->assertNoEvents(); |
| 1098 | middleWallpaperWindow->assertNoEvents(); |
| 1099 | rightForegroundWindow->consumeMotionEvent( |
| 1100 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB))); |
| 1101 | rightWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), |
| 1102 | WithDeviceId(deviceB), |
| 1103 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 1104 | } |
| 1105 | |
| 1106 | /** |
| 1107 | * Similar to the test above, we have three windows, they are arranged horizontally and without any |
| 1108 | * overlap, and every window has a wallpaper window. The middle window is a simple window, without |
| 1109 | * any special flags. Device A reports a down event that lands in left window. Device B sends a down |
| 1110 | * event to the middle window and then touch is transferred from the middle window to the right |
| 1111 | * window. The right window and its wallpaper window should receive a down event. The middle window |
| 1112 | * and its wallpaper window should receive a cancel event. The left window should not receive any |
| 1113 | * events. Subsequent events reported by device B should go to the right window and its wallpaper. |
| 1114 | */ |
| 1115 | TEST_F(InputDispatcherTest, MultiDeviceTouchTransferWithWallpaperWindows) { |
| 1116 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1117 | sp<FakeWindowHandle> leftForegroundWindow = |
| 1118 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1119 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1120 | leftForegroundWindow->setFrame(Rect(0, 0, 100, 100)); |
| 1121 | leftForegroundWindow->setDupTouchToWallpaper(true); |
| 1122 | sp<FakeWindowHandle> leftWallpaperWindow = |
| 1123 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1124 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1125 | leftWallpaperWindow->setFrame(Rect(0, 0, 100, 100)); |
| 1126 | leftWallpaperWindow->setIsWallpaper(true); |
| 1127 | |
| 1128 | sp<FakeWindowHandle> middleForegroundWindow = |
| 1129 | sp<FakeWindowHandle>::make(application, mDispatcher, "Middle foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1130 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1131 | middleForegroundWindow->setFrame(Rect(100, 0, 200, 100)); |
| 1132 | middleForegroundWindow->setDupTouchToWallpaper(true); |
| 1133 | sp<FakeWindowHandle> middleWallpaperWindow = |
| 1134 | sp<FakeWindowHandle>::make(application, mDispatcher, "Middle wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1135 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1136 | middleWallpaperWindow->setFrame(Rect(100, 0, 200, 100)); |
| 1137 | middleWallpaperWindow->setIsWallpaper(true); |
| 1138 | |
| 1139 | sp<FakeWindowHandle> rightForegroundWindow = |
| 1140 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1141 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1142 | rightForegroundWindow->setFrame(Rect(200, 0, 300, 100)); |
| 1143 | rightForegroundWindow->setDupTouchToWallpaper(true); |
| 1144 | sp<FakeWindowHandle> rightWallpaperWindow = |
| 1145 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1146 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1147 | rightWallpaperWindow->setFrame(Rect(200, 0, 300, 100)); |
| 1148 | rightWallpaperWindow->setIsWallpaper(true); |
| 1149 | |
| 1150 | mDispatcher->onWindowInfosChanged( |
| 1151 | {{*leftForegroundWindow->getInfo(), *leftWallpaperWindow->getInfo(), |
| 1152 | *middleForegroundWindow->getInfo(), *middleWallpaperWindow->getInfo(), |
| 1153 | *rightForegroundWindow->getInfo(), *rightWallpaperWindow->getInfo()}, |
| 1154 | {}, |
| 1155 | 0, |
| 1156 | 0}); |
| 1157 | |
| 1158 | const DeviceId deviceA = 9; |
| 1159 | const DeviceId deviceB = 3; |
| 1160 | // Device A touch down on the left window |
| 1161 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1162 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 1163 | .deviceId(deviceA) |
| 1164 | .build()); |
| 1165 | leftForegroundWindow->consumeMotionEvent( |
| 1166 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 1167 | leftWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 1168 | WithDeviceId(deviceA), |
| 1169 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 1170 | // Device B touch down on the middle window |
| 1171 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1172 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 1173 | .deviceId(deviceB) |
| 1174 | .build()); |
| 1175 | middleForegroundWindow->consumeMotionEvent( |
| 1176 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 1177 | middleWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 1178 | WithDeviceId(deviceB), |
| 1179 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 1180 | |
| 1181 | // Transfer touch from the middle window to the right window. |
| 1182 | ASSERT_TRUE(mDispatcher->transferTouchGesture(middleForegroundWindow->getToken(), |
| 1183 | rightForegroundWindow->getToken())); |
| 1184 | |
| 1185 | middleForegroundWindow->consumeMotionEvent( |
| 1186 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB))); |
| 1187 | middleWallpaperWindow->consumeMotionEvent( |
| 1188 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB), |
| 1189 | WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_CANCELED))); |
| 1190 | rightForegroundWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 1191 | WithDeviceId(deviceB), |
| 1192 | WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
| 1193 | rightWallpaperWindow->consumeMotionEvent( |
| 1194 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB), |
| 1195 | WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
| 1196 | |
| 1197 | // Make sure the right window can receive the remaining events. |
| 1198 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1199 | .pointer(PointerBuilder(0, ToolType::FINGER).x(251).y(51)) |
| 1200 | .deviceId(deviceB) |
| 1201 | .build()); |
| 1202 | leftForegroundWindow->assertNoEvents(); |
| 1203 | leftWallpaperWindow->assertNoEvents(); |
| 1204 | middleForegroundWindow->assertNoEvents(); |
| 1205 | middleWallpaperWindow->assertNoEvents(); |
| 1206 | rightForegroundWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), |
| 1207 | WithDeviceId(deviceB), |
| 1208 | WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
| 1209 | rightWallpaperWindow->consumeMotionEvent( |
| 1210 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB), |
| 1211 | WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
| 1212 | } |
| 1213 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1214 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 1215 | public ::testing::WithParamInterface<bool> {}; |
| 1216 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 1217 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1218 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1219 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1220 | * The top window gets a multitouch gesture. |
| 1221 | * Ensure that wallpaper gets the same gesture. |
| 1222 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1223 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1224 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1225 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1226 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 1227 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1228 | foregroundWindow->setDupTouchToWallpaper(true); |
| 1229 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1230 | |
| 1231 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1232 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 1233 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1234 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1235 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1236 | mDispatcher->onWindowInfosChanged( |
| 1237 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1238 | |
| 1239 | // Touch down on top window |
| 1240 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1241 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 1242 | ui::LogicalDisplayId::DEFAULT, {100, 100})) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1243 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1244 | |
| 1245 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1246 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1247 | wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1248 | |
| 1249 | // Second finger down on the top window |
| 1250 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1251 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1252 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1253 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1254 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1255 | .build(); |
| 1256 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1257 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1258 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1259 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1260 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1261 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1262 | EXPECTED_WALLPAPER_FLAGS); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1263 | |
| 1264 | const MotionEvent secondFingerUpEvent = |
| 1265 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1266 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1267 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1268 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1269 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1270 | .build(); |
| 1271 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1272 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1273 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1274 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 33cfc6d | 2024-06-11 20:17:44 +0000 | [diff] [blame] | 1275 | foregroundWindow->consumeMotionPointerUp(/*pointerIdx=*/0, |
| 1276 | WithDisplayId(ui::LogicalDisplayId::DEFAULT)); |
| 1277 | wallpaperWindow->consumeMotionPointerUp(/*pointerIdx=*/0, |
| 1278 | AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT), |
| 1279 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1280 | |
| 1281 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1282 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1283 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1284 | AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1285 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1286 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1287 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1288 | .x(100) |
| 1289 | .y(100)) |
| 1290 | .build(), |
| 1291 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1292 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1293 | foregroundWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
| 1294 | wallpaperWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | /** |
| 1298 | * Two windows: a window on the left and window on the right. |
| 1299 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1300 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1301 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1302 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1303 | * ACTION_POINTER_DOWN(1). |
| 1304 | */ |
| 1305 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1306 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1307 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 1308 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1309 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1310 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1311 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1312 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 1313 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1314 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1315 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1316 | |
| 1317 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1318 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 1319 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1320 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1321 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1322 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1323 | mDispatcher->onWindowInfosChanged( |
| 1324 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 1325 | {}, |
| 1326 | 0, |
| 1327 | 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1328 | |
| 1329 | // Touch down on left window |
| 1330 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1331 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 1332 | ui::LogicalDisplayId::DEFAULT, {100, 100})) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1333 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1334 | |
| 1335 | // Both foreground window and its wallpaper should receive the touch down |
| 1336 | leftWindow->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1337 | wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1338 | |
| 1339 | // Second finger down on the right window |
| 1340 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1341 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1342 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1343 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1344 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1345 | .build(); |
| 1346 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1347 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1348 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1349 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1350 | |
| 1351 | leftWindow->consumeMotionMove(); |
| 1352 | // Since the touch is split, right window gets ACTION_DOWN |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1353 | rightWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 1354 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1355 | EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1356 | |
| 1357 | // Now, leftWindow, which received the first finger, disappears. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1358 | mDispatcher->onWindowInfosChanged( |
| 1359 | {{*rightWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1360 | leftWindow->consumeMotionCancel(); |
| 1361 | // 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] | 1362 | wallpaperWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1363 | |
| 1364 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 1365 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 1366 | const MotionEvent secondFingerMoveEvent = |
| 1367 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1368 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1369 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1370 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1371 | .build(); |
| 1372 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1373 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1374 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 1375 | rightWindow->consumeMotionMove(); |
| 1376 | |
| 1377 | leftWindow->assertNoEvents(); |
| 1378 | rightWindow->assertNoEvents(); |
| 1379 | wallpaperWindow->assertNoEvents(); |
| 1380 | } |
| 1381 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1382 | /** |
| 1383 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 1384 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 1385 | * The right window should receive ACTION_DOWN. |
| 1386 | */ |
| 1387 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1388 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1389 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 1390 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1391 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1392 | leftWindow->setDupTouchToWallpaper(true); |
| 1393 | leftWindow->setSlippery(true); |
| 1394 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1395 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 1396 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1397 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1398 | |
| 1399 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1400 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 1401 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1402 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1403 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1404 | mDispatcher->onWindowInfosChanged( |
| 1405 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 1406 | {}, |
| 1407 | 0, |
| 1408 | 0}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1409 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1410 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1411 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1412 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 1413 | ui::LogicalDisplayId::DEFAULT, {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1414 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1415 | |
| 1416 | // Both foreground window and its wallpaper should receive the touch down |
| 1417 | leftWindow->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1418 | wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1419 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1420 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1421 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1422 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1423 | ui::LogicalDisplayId::DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1424 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1425 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1426 | leftWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1427 | rightWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 1428 | wallpaperWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1431 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1432 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 1433 | * interactive, it might stop sending this flag. |
| 1434 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 1435 | * to have a consistent input stream. |
| 1436 | * |
| 1437 | * Test procedure: |
| 1438 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 1439 | * DOWN (new gesture). |
| 1440 | * |
| 1441 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 1442 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 1443 | * |
| 1444 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 1445 | * regular window below) to emulate the actual situation where it happens on the device. |
| 1446 | */ |
| 1447 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 1448 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1449 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 1450 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1451 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1452 | spyWindow->setTrustedOverlay(true); |
| 1453 | spyWindow->setSpy(true); |
| 1454 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1455 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1456 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1457 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1458 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1459 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1460 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1461 | |
| 1462 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1463 | mDispatcher->notifyMotion( |
| 1464 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1465 | .deviceId(touchDeviceId) |
| 1466 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1467 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1468 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1469 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1470 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1471 | .deviceId(touchDeviceId) |
| 1472 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1473 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1474 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 1475 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1476 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1477 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1478 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1479 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1480 | |
| 1481 | // Cancel the current gesture. Send the cancel without the default policy flags. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1482 | mDispatcher->notifyMotion( |
| 1483 | MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 1484 | .deviceId(touchDeviceId) |
| 1485 | .policyFlags(0) |
| 1486 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1487 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 1488 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1489 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 1490 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 1491 | |
| 1492 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 1493 | // 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] | 1494 | mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1495 | |
| 1496 | // Start new gesture |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1497 | mDispatcher->notifyMotion( |
| 1498 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1499 | .deviceId(touchDeviceId) |
| 1500 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1501 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1502 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1503 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1504 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1505 | |
| 1506 | // No more events |
| 1507 | spyWindow->assertNoEvents(); |
| 1508 | window->assertNoEvents(); |
| 1509 | } |
| 1510 | |
| 1511 | /** |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 1512 | * Same as the above 'TwoPointerCancelInconsistentPolicy' test, but for hovers. |
| 1513 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 1514 | * interactive, it might stop sending this flag. |
| 1515 | * We've already ensured the consistency of the touch event in this case, and we should also ensure |
| 1516 | * the consistency of the hover event in this case. |
| 1517 | * |
| 1518 | * Test procedure: |
| 1519 | * HOVER_ENTER -> HOVER_MOVE -> (stop sending POLICY_FLAG_PASS_TO_USER) -> HOVER_EXIT |
| 1520 | * HOVER_ENTER -> HOVER_MOVE -> HOVER_EXIT |
| 1521 | * |
| 1522 | * We expect to receive two full streams of hover events. |
| 1523 | */ |
| 1524 | TEST_F(InputDispatcherTest, HoverEventInconsistentPolicy) { |
| 1525 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1526 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1527 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1528 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 1529 | window->setFrame(Rect(0, 0, 300, 300)); |
| 1530 | |
| 1531 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1532 | |
| 1533 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1534 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1535 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 1536 | .build()); |
| 1537 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1538 | |
| 1539 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1540 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1541 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 1542 | .build()); |
| 1543 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 1544 | |
| 1545 | // Send hover exit without the default policy flags. |
| 1546 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 1547 | .policyFlags(0) |
| 1548 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 1549 | .build()); |
| 1550 | |
| 1551 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1552 | |
| 1553 | // Send a simple hover event stream, ensure dispatcher not crashed and window can receive |
| 1554 | // right event. |
| 1555 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1556 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1557 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(200).y(201)) |
| 1558 | .build()); |
| 1559 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1560 | |
| 1561 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1562 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1563 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 1564 | .build()); |
| 1565 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 1566 | |
| 1567 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 1568 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1569 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 1570 | .build()); |
| 1571 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1572 | } |
| 1573 | |
| 1574 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1575 | * Two windows: a window on the left and a window on the right. |
| 1576 | * Mouse is hovered from the right window into the left window. |
| 1577 | * Next, we tap on the left window, where the cursor was last seen. |
| 1578 | * The second tap is done onto the right window. |
| 1579 | * The mouse and tap are from two different devices. |
| 1580 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 1581 | * explicitly helps during debugging. |
| 1582 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 1583 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 1584 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1585 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap_legacy) { |
| 1586 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
| 1587 | |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1588 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1589 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 1590 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1591 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1592 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1593 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 1594 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1595 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 1596 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1597 | mDispatcher->onWindowInfosChanged( |
| 1598 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1599 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 1600 | // stale. |
| 1601 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1602 | const int32_t mouseDeviceId = 6; |
| 1603 | const int32_t touchDeviceId = 4; |
| 1604 | // Move the cursor from right |
| 1605 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1606 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1607 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1608 | AINPUT_SOURCE_MOUSE) |
| 1609 | .deviceId(mouseDeviceId) |
| 1610 | .downTime(baseTime + 10) |
| 1611 | .eventTime(baseTime + 20) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1612 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1613 | .build())); |
| 1614 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 1615 | |
| 1616 | // .. to the left window |
| 1617 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1618 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1619 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1620 | AINPUT_SOURCE_MOUSE) |
| 1621 | .deviceId(mouseDeviceId) |
| 1622 | .downTime(baseTime + 10) |
| 1623 | .eventTime(baseTime + 30) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1624 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1625 | .build())); |
| 1626 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 1627 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 1628 | // Now tap the left window |
| 1629 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1630 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1631 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 1632 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1633 | .deviceId(touchDeviceId) |
| 1634 | .downTime(baseTime + 40) |
| 1635 | .eventTime(baseTime + 40) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1636 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1637 | .build())); |
| 1638 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 1639 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1640 | |
| 1641 | // release tap |
| 1642 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1643 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1644 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1645 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1646 | .deviceId(touchDeviceId) |
| 1647 | .downTime(baseTime + 40) |
| 1648 | .eventTime(baseTime + 50) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1649 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1650 | .build())); |
| 1651 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 1652 | |
| 1653 | // Tap the window on the right |
| 1654 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1655 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1656 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 1657 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1658 | .deviceId(touchDeviceId) |
| 1659 | .downTime(baseTime + 60) |
| 1660 | .eventTime(baseTime + 60) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1661 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1662 | .build())); |
| 1663 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1664 | |
| 1665 | // release tap |
| 1666 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1667 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1668 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1669 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1670 | .deviceId(touchDeviceId) |
| 1671 | .downTime(baseTime + 60) |
| 1672 | .eventTime(baseTime + 70) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1673 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1674 | .build())); |
| 1675 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 1676 | |
| 1677 | // No more events |
| 1678 | leftWindow->assertNoEvents(); |
| 1679 | rightWindow->assertNoEvents(); |
| 1680 | } |
| 1681 | |
| 1682 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1683 | * Two windows: a window on the left and a window on the right. |
| 1684 | * Mouse is hovered from the right window into the left window. |
| 1685 | * Next, we tap on the left window, where the cursor was last seen. |
| 1686 | * The second tap is done onto the right window. |
| 1687 | * The mouse and tap are from two different devices. |
| 1688 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 1689 | * explicitly helps during debugging. |
| 1690 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 1691 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 1692 | */ |
| 1693 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 1694 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 1695 | |
| 1696 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1697 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 1698 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1699 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1700 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1701 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 1702 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1703 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 1704 | |
| 1705 | mDispatcher->onWindowInfosChanged( |
| 1706 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 1707 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 1708 | // stale. |
| 1709 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1710 | const int32_t mouseDeviceId = 6; |
| 1711 | const int32_t touchDeviceId = 4; |
| 1712 | // Move the cursor from right |
| 1713 | mDispatcher->notifyMotion( |
| 1714 | MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 1715 | .deviceId(mouseDeviceId) |
| 1716 | .downTime(baseTime + 10) |
| 1717 | .eventTime(baseTime + 20) |
| 1718 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100)) |
| 1719 | .build()); |
| 1720 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 1721 | |
| 1722 | // .. to the left window |
| 1723 | mDispatcher->notifyMotion( |
| 1724 | MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 1725 | .deviceId(mouseDeviceId) |
| 1726 | .downTime(baseTime + 10) |
| 1727 | .eventTime(baseTime + 30) |
| 1728 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100)) |
| 1729 | .build()); |
| 1730 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 1731 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 1732 | // Now tap the left window |
| 1733 | mDispatcher->notifyMotion( |
| 1734 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1735 | .deviceId(touchDeviceId) |
| 1736 | .downTime(baseTime + 40) |
| 1737 | .eventTime(baseTime + 40) |
| 1738 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1739 | .build()); |
| 1740 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1741 | |
| 1742 | // release tap |
| 1743 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1744 | .deviceId(touchDeviceId) |
| 1745 | .downTime(baseTime + 40) |
| 1746 | .eventTime(baseTime + 50) |
| 1747 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1748 | .build()); |
| 1749 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 1750 | |
| 1751 | // Tap the window on the right |
| 1752 | mDispatcher->notifyMotion( |
| 1753 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1754 | .deviceId(touchDeviceId) |
| 1755 | .downTime(baseTime + 60) |
| 1756 | .eventTime(baseTime + 60) |
| 1757 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 1758 | .build()); |
| 1759 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1760 | |
| 1761 | // release tap |
| 1762 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1763 | .deviceId(touchDeviceId) |
| 1764 | .downTime(baseTime + 60) |
| 1765 | .eventTime(baseTime + 70) |
| 1766 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 1767 | .build()); |
| 1768 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 1769 | |
| 1770 | // No more events |
| 1771 | leftWindow->assertNoEvents(); |
| 1772 | rightWindow->assertNoEvents(); |
| 1773 | } |
| 1774 | |
| 1775 | /** |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1776 | * Start hovering in a window. While this hover is still active, make another window appear on top. |
| 1777 | * The top, obstructing window has no input channel, so it's not supposed to receive input. |
| 1778 | * While the top window is present, the hovering is stopped. |
| 1779 | * Later, hovering gets resumed again. |
| 1780 | * Ensure that new hover gesture is handled correctly. |
| 1781 | * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly |
| 1782 | * to the window that's currently being hovered over. |
| 1783 | */ |
| 1784 | TEST_F(InputDispatcherTest, HoverWhileWindowAppears) { |
| 1785 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1786 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1787 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1788 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1789 | |
| 1790 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1791 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1792 | |
| 1793 | // Start hovering in the window |
| 1794 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1795 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1796 | .build()); |
| 1797 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1798 | |
| 1799 | // Now, an obscuring window appears! |
| 1800 | sp<FakeWindowHandle> obscuringWindow = |
| 1801 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1802 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1803 | /*createInputChannel=*/false); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1804 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1805 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 1806 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 1807 | obscuringWindow->setNoInputChannel(true); |
| 1808 | obscuringWindow->setFocusable(false); |
| 1809 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1810 | mDispatcher->onWindowInfosChanged( |
| 1811 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1812 | |
| 1813 | // While this new obscuring window is present, the hovering is stopped |
| 1814 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 1815 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1816 | .build()); |
| 1817 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1818 | |
| 1819 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1820 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1821 | |
| 1822 | // And a new hover gesture starts. |
| 1823 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1824 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1825 | .build()); |
| 1826 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1827 | } |
| 1828 | |
| 1829 | /** |
| 1830 | * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to |
| 1831 | * the obscuring window. |
| 1832 | */ |
| 1833 | TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) { |
| 1834 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1835 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1836 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1837 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1838 | |
| 1839 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1840 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1841 | |
| 1842 | // Start hovering in the window |
| 1843 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1844 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1845 | .build()); |
| 1846 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1847 | |
| 1848 | // Now, an obscuring window appears! |
| 1849 | sp<FakeWindowHandle> obscuringWindow = |
| 1850 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1851 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1852 | /*createInputChannel=*/false); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1853 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1854 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 1855 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 1856 | obscuringWindow->setNoInputChannel(true); |
| 1857 | obscuringWindow->setFocusable(false); |
| 1858 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1859 | mDispatcher->onWindowInfosChanged( |
| 1860 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1861 | |
| 1862 | // While this new obscuring window is present, the hovering continues. The event can't go to the |
| 1863 | // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window. |
| 1864 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1865 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1866 | .build()); |
| 1867 | obscuringWindow->assertNoEvents(); |
| 1868 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1869 | |
| 1870 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1871 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1872 | |
| 1873 | // Hovering continues in the same position. The hovering pointer re-enters the bottom window, |
| 1874 | // so it should generate a HOVER_ENTER |
| 1875 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1876 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1877 | .build()); |
| 1878 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1879 | |
| 1880 | // Now the MOVE should be getting dispatched normally |
| 1881 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1882 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 1883 | .build()); |
| 1884 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 1885 | } |
| 1886 | |
| 1887 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1888 | * Hover mouse over a window, and then send ACTION_SCROLL. Ensure both the hover and the scroll |
| 1889 | * events are delivered to the window. |
| 1890 | */ |
| 1891 | TEST_F(InputDispatcherTest, HoverMoveAndScroll) { |
| 1892 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1893 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1894 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1895 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1896 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1897 | |
| 1898 | // Start hovering in the window |
| 1899 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 1900 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 1901 | .build()); |
| 1902 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1903 | |
| 1904 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 1905 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 1906 | .build()); |
| 1907 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 1908 | |
| 1909 | // Scroll with the mouse |
| 1910 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_SCROLL, AINPUT_SOURCE_MOUSE) |
| 1911 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 1912 | .build()); |
| 1913 | window->consumeMotionEvent(WithMotionAction(ACTION_SCROLL)); |
| 1914 | } |
| 1915 | |
Siarhei Vishniakou | a8aaeb8 | 2024-08-12 12:00:28 -0700 | [diff] [blame] | 1916 | /** |
| 1917 | * Two windows: a trusted overlay and a regular window underneath. Both windows are visible. |
| 1918 | * Mouse is hovered, and the hover event should only go to the overlay. |
| 1919 | * However, next, the touchable region of the trusted overlay shrinks. The mouse position hasn't |
| 1920 | * changed, but the cursor would now end up hovering above the regular window underneatch. |
| 1921 | * If the mouse is now clicked, this would generate an ACTION_DOWN event, which would go to the |
| 1922 | * regular window. However, the trusted overlay is also watching for outside touch. |
| 1923 | * The trusted overlay should get two events: |
| 1924 | * 1) The ACTION_OUTSIDE event, since the click is now not inside its touchable region |
| 1925 | * 2) The HOVER_EXIT event, since the mouse pointer is no longer hovering inside this window |
| 1926 | * |
| 1927 | * This test reproduces a crash where there is an overlap between dispatch modes for the trusted |
| 1928 | * overlay touch target, since the event is causing both an ACTION_OUTSIDE, and as a HOVER_EXIT. |
| 1929 | */ |
| 1930 | TEST_F(InputDispatcherTest, MouseClickUnderShrinkingTrustedOverlay) { |
| 1931 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 1932 | sp<FakeWindowHandle> overlay = sp<FakeWindowHandle>::make(app, mDispatcher, "Trusted overlay", |
| 1933 | ui::LogicalDisplayId::DEFAULT); |
| 1934 | overlay->setTrustedOverlay(true); |
| 1935 | overlay->setWatchOutsideTouch(true); |
| 1936 | overlay->setFrame(Rect(0, 0, 200, 200)); |
| 1937 | |
| 1938 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(app, mDispatcher, "Regular window", |
| 1939 | ui::LogicalDisplayId::DEFAULT); |
| 1940 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1941 | |
| 1942 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 1943 | // Hover the mouse into the overlay |
| 1944 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 1945 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 1946 | .build()); |
| 1947 | overlay->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1948 | |
| 1949 | // Now, shrink the touchable region of the overlay! This will cause the cursor to suddenly have |
| 1950 | // the regular window as the touch target |
| 1951 | overlay->setTouchableRegion(Region({0, 0, 0, 0})); |
| 1952 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 1953 | |
| 1954 | // Now we can click with the mouse. The click should go into the regular window |
| 1955 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 1956 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 1957 | .build()); |
| 1958 | overlay->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1959 | overlay->consumeMotionEvent(WithMotionAction(ACTION_OUTSIDE)); |
| 1960 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 1961 | } |
| 1962 | |
| 1963 | /** |
| 1964 | * Similar to above, but also has a spy on top that also catches the HOVER |
| 1965 | * events. Also, instead of ACTION_DOWN, we are continuing to send the hovering |
| 1966 | * stream to ensure that the spy receives hover events correctly. |
| 1967 | */ |
| 1968 | TEST_F(InputDispatcherTest, MouseClickUnderShrinkingTrustedOverlayWithSpy) { |
| 1969 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 1970 | sp<FakeWindowHandle> spyWindow = |
| 1971 | sp<FakeWindowHandle>::make(app, mDispatcher, "Spy", ui::LogicalDisplayId::DEFAULT); |
| 1972 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1973 | spyWindow->setTrustedOverlay(true); |
| 1974 | spyWindow->setSpy(true); |
| 1975 | sp<FakeWindowHandle> overlay = sp<FakeWindowHandle>::make(app, mDispatcher, "Trusted overlay", |
| 1976 | ui::LogicalDisplayId::DEFAULT); |
| 1977 | overlay->setTrustedOverlay(true); |
| 1978 | overlay->setWatchOutsideTouch(true); |
| 1979 | overlay->setFrame(Rect(0, 0, 200, 200)); |
| 1980 | |
| 1981 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(app, mDispatcher, "Regular window", |
| 1982 | ui::LogicalDisplayId::DEFAULT); |
| 1983 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1984 | |
| 1985 | mDispatcher->onWindowInfosChanged( |
| 1986 | {{*spyWindow->getInfo(), *overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 1987 | // Hover the mouse into the overlay |
| 1988 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 1989 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 1990 | .build()); |
| 1991 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1992 | overlay->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1993 | |
| 1994 | // Now, shrink the touchable region of the overlay! This will cause the cursor to suddenly have |
| 1995 | // the regular window as the touch target |
| 1996 | overlay->setTouchableRegion(Region({0, 0, 0, 0})); |
| 1997 | mDispatcher->onWindowInfosChanged( |
| 1998 | {{*spyWindow->getInfo(), *overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 1999 | |
| 2000 | // Now we can click with the mouse. The click should go into the regular window |
| 2001 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 2002 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 2003 | .build()); |
| 2004 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2005 | overlay->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2006 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2007 | } |
| 2008 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2009 | using InputDispatcherMultiDeviceTest = InputDispatcherTest; |
| 2010 | |
| 2011 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2012 | * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that |
| 2013 | * touch is dropped, because stylus should be preferred over touch. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2014 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2015 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownBlocksTouchDown) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2016 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2017 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2018 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2019 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2020 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2021 | |
| 2022 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2023 | |
| 2024 | constexpr int32_t touchDeviceId = 4; |
| 2025 | constexpr int32_t stylusDeviceId = 2; |
| 2026 | |
| 2027 | // Stylus down |
| 2028 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2029 | .deviceId(stylusDeviceId) |
| 2030 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2031 | .build()); |
| 2032 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2033 | |
| 2034 | // Touch down |
| 2035 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2036 | .deviceId(touchDeviceId) |
| 2037 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2038 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2039 | |
| 2040 | // Touch move |
| 2041 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2042 | .deviceId(touchDeviceId) |
| 2043 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2044 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2045 | // Touch is ignored because stylus is already down |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2046 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2047 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2048 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2049 | .deviceId(stylusDeviceId) |
| 2050 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2051 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2052 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2053 | WithCoords(101, 111))); |
| 2054 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2055 | window->assertNoEvents(); |
| 2056 | } |
| 2057 | |
| 2058 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2059 | * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that |
| 2060 | * touch is not dropped, because multiple devices are allowed to be active in the same window. |
| 2061 | */ |
| 2062 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownDoesNotBlockTouchDown) { |
| 2063 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2064 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2065 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2066 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2067 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2068 | |
| 2069 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2070 | |
| 2071 | constexpr int32_t touchDeviceId = 4; |
| 2072 | constexpr int32_t stylusDeviceId = 2; |
| 2073 | |
| 2074 | // Stylus down |
| 2075 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2076 | .deviceId(stylusDeviceId) |
| 2077 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2078 | .build()); |
| 2079 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2080 | |
| 2081 | // Touch down |
| 2082 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2083 | .deviceId(touchDeviceId) |
| 2084 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2085 | .build()); |
| 2086 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2087 | |
| 2088 | // Touch move |
| 2089 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2090 | .deviceId(touchDeviceId) |
| 2091 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2092 | .build()); |
| 2093 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2094 | |
| 2095 | // Stylus move |
| 2096 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2097 | .deviceId(stylusDeviceId) |
| 2098 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2099 | .build()); |
| 2100 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2101 | WithCoords(101, 111))); |
| 2102 | |
| 2103 | window->assertNoEvents(); |
| 2104 | } |
| 2105 | |
| 2106 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2107 | * 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] | 2108 | * 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] | 2109 | * Similar test as above, but with added SPY window. |
| 2110 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2111 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyBlocksTouchDown) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2112 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2113 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2114 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2115 | ui::LogicalDisplayId::DEFAULT); |
| 2116 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 2117 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2118 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2119 | spyWindow->setTrustedOverlay(true); |
| 2120 | spyWindow->setSpy(true); |
| 2121 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2122 | |
| 2123 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 2124 | |
| 2125 | constexpr int32_t touchDeviceId = 4; |
| 2126 | constexpr int32_t stylusDeviceId = 2; |
| 2127 | |
| 2128 | // Stylus down |
| 2129 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2130 | .deviceId(stylusDeviceId) |
| 2131 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2132 | .build()); |
| 2133 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2134 | spyWindow->consumeMotionEvent( |
| 2135 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2136 | |
| 2137 | // Touch down |
| 2138 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2139 | .deviceId(touchDeviceId) |
| 2140 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2141 | .build()); |
| 2142 | |
| 2143 | // Touch move |
| 2144 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2145 | .deviceId(touchDeviceId) |
| 2146 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2147 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2148 | |
| 2149 | // Touch is ignored because stylus is already down |
| 2150 | |
| 2151 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2152 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2153 | .deviceId(stylusDeviceId) |
| 2154 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2155 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2156 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2157 | WithCoords(101, 111))); |
| 2158 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2159 | WithCoords(101, 111))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2160 | |
| 2161 | window->assertNoEvents(); |
| 2162 | spyWindow->assertNoEvents(); |
| 2163 | } |
| 2164 | |
| 2165 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2166 | * One window and one spy window. Stylus down on the window. Next, touch from another device goes |
| 2167 | * down. Ensure that touch is not dropped, because multiple devices can be active at the same time. |
| 2168 | * Similar test as above, but with added SPY window. |
| 2169 | */ |
| 2170 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyDoesNotBlockTouchDown) { |
| 2171 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2172 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2173 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2174 | ui::LogicalDisplayId::DEFAULT); |
| 2175 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 2176 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2177 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2178 | spyWindow->setTrustedOverlay(true); |
| 2179 | spyWindow->setSpy(true); |
| 2180 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2181 | |
| 2182 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 2183 | |
| 2184 | constexpr int32_t touchDeviceId = 4; |
| 2185 | constexpr int32_t stylusDeviceId = 2; |
| 2186 | |
| 2187 | // Stylus down |
| 2188 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2189 | .deviceId(stylusDeviceId) |
| 2190 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2191 | .build()); |
| 2192 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2193 | spyWindow->consumeMotionEvent( |
| 2194 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2195 | |
| 2196 | // Touch down |
| 2197 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2198 | .deviceId(touchDeviceId) |
| 2199 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2200 | .build()); |
| 2201 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2202 | spyWindow->consumeMotionEvent( |
| 2203 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2204 | |
| 2205 | // Touch move |
| 2206 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2207 | .deviceId(touchDeviceId) |
| 2208 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2209 | .build()); |
| 2210 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2211 | spyWindow->consumeMotionEvent( |
| 2212 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2213 | |
| 2214 | // Subsequent stylus movements are delivered correctly |
| 2215 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2216 | .deviceId(stylusDeviceId) |
| 2217 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2218 | .build()); |
| 2219 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2220 | WithCoords(101, 111))); |
| 2221 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2222 | WithCoords(101, 111))); |
| 2223 | |
| 2224 | window->assertNoEvents(); |
| 2225 | spyWindow->assertNoEvents(); |
| 2226 | } |
| 2227 | |
| 2228 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2229 | * 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] | 2230 | * touch is dropped, because stylus hover takes precedence. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2231 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2232 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverBlocksTouchDown) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2233 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2234 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2235 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2236 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2237 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2238 | |
| 2239 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2240 | |
| 2241 | constexpr int32_t touchDeviceId = 4; |
| 2242 | constexpr int32_t stylusDeviceId = 2; |
| 2243 | |
| 2244 | // Stylus down on the window |
| 2245 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2246 | .deviceId(stylusDeviceId) |
| 2247 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2248 | .build()); |
| 2249 | window->consumeMotionEvent( |
| 2250 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2251 | |
| 2252 | // Touch down on window |
| 2253 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2254 | .deviceId(touchDeviceId) |
| 2255 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2256 | .build()); |
| 2257 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2258 | .deviceId(touchDeviceId) |
| 2259 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2260 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2261 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2262 | // Touch is ignored because stylus is hovering |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2263 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2264 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2265 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2266 | .deviceId(stylusDeviceId) |
| 2267 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2268 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2269 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2270 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2271 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2272 | // and subsequent touches continue to be ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2273 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2274 | .deviceId(touchDeviceId) |
| 2275 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2276 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2277 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2278 | } |
| 2279 | |
| 2280 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2281 | * One window. Stylus hover on the window. Next, touch from another device goes down. Ensure that |
| 2282 | * touch is not dropped, because stylus hover and touch can be both active at the same time. |
| 2283 | */ |
| 2284 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverDoesNotBlockTouchDown) { |
| 2285 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2286 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2287 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2288 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2289 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2290 | |
| 2291 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2292 | |
| 2293 | constexpr int32_t touchDeviceId = 4; |
| 2294 | constexpr int32_t stylusDeviceId = 2; |
| 2295 | |
| 2296 | // Stylus down on the window |
| 2297 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2298 | .deviceId(stylusDeviceId) |
| 2299 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2300 | .build()); |
| 2301 | window->consumeMotionEvent( |
| 2302 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2303 | |
| 2304 | // Touch down on window |
| 2305 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2306 | .deviceId(touchDeviceId) |
| 2307 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2308 | .build()); |
| 2309 | // Touch move on window |
| 2310 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2311 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2312 | .deviceId(touchDeviceId) |
| 2313 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2314 | .build()); |
| 2315 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2316 | |
| 2317 | // Subsequent stylus movements are delivered correctly |
| 2318 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2319 | .deviceId(stylusDeviceId) |
| 2320 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2321 | .build()); |
| 2322 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2323 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
| 2324 | |
| 2325 | // and subsequent touches continue to work |
| 2326 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2327 | .deviceId(touchDeviceId) |
| 2328 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2329 | .build()); |
| 2330 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2331 | window->assertNoEvents(); |
| 2332 | } |
| 2333 | |
| 2334 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2335 | * 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] | 2336 | * Ensure that touch is canceled, because stylus hover should take precedence. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2337 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2338 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusHover) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2339 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2340 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2341 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2342 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2343 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2344 | |
| 2345 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2346 | |
| 2347 | constexpr int32_t touchDeviceId = 4; |
| 2348 | constexpr int32_t stylusDeviceId = 2; |
| 2349 | |
| 2350 | // Touch down on window |
| 2351 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2352 | .deviceId(touchDeviceId) |
| 2353 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2354 | .build()); |
| 2355 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2356 | .deviceId(touchDeviceId) |
| 2357 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2358 | .build()); |
| 2359 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2360 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2361 | |
| 2362 | // Stylus hover on the window |
| 2363 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2364 | .deviceId(stylusDeviceId) |
| 2365 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2366 | .build()); |
| 2367 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2368 | .deviceId(stylusDeviceId) |
| 2369 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2370 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2371 | // Stylus hover movement causes touch to be canceled |
| 2372 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 2373 | WithCoords(141, 146))); |
| 2374 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 2375 | WithDeviceId(stylusDeviceId), WithCoords(100, 110))); |
| 2376 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2377 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2378 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2379 | // Subsequent touch movements are ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2380 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2381 | .deviceId(touchDeviceId) |
| 2382 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2383 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2384 | |
| 2385 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2389 | * One window. Touch down on the window. Then, stylus hover on the window from another device. |
| 2390 | * Ensure that touch is not canceled, because stylus hover can be active at the same time as touch. |
| 2391 | */ |
| 2392 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsNotCanceledByStylusHover) { |
| 2393 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2394 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2395 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2396 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2397 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2398 | |
| 2399 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2400 | |
| 2401 | constexpr int32_t touchDeviceId = 4; |
| 2402 | constexpr int32_t stylusDeviceId = 2; |
| 2403 | |
| 2404 | // Touch down on window |
| 2405 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2406 | .deviceId(touchDeviceId) |
| 2407 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2408 | .build()); |
| 2409 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2410 | .deviceId(touchDeviceId) |
| 2411 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2412 | .build()); |
| 2413 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2414 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2415 | |
| 2416 | // Stylus hover on the window |
| 2417 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2418 | .deviceId(stylusDeviceId) |
| 2419 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2420 | .build()); |
| 2421 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2422 | .deviceId(stylusDeviceId) |
| 2423 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2424 | .build()); |
| 2425 | // Stylus hover movement is received normally |
| 2426 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 2427 | WithDeviceId(stylusDeviceId), WithCoords(100, 110))); |
| 2428 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2429 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
| 2430 | |
| 2431 | // Subsequent touch movements also work |
| 2432 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2433 | .deviceId(touchDeviceId) |
| 2434 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2435 | .build()); |
| 2436 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId), |
| 2437 | WithCoords(142, 147))); |
| 2438 | |
| 2439 | window->assertNoEvents(); |
| 2440 | } |
| 2441 | |
| 2442 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2443 | * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that |
| 2444 | * the latest stylus takes over. That is, old stylus should be canceled and the new stylus should |
| 2445 | * become active. |
| 2446 | */ |
| 2447 | TEST_F(InputDispatcherMultiDeviceTest, LatestStylusWins) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2448 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2449 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2450 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2451 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2452 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2453 | |
| 2454 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2455 | |
| 2456 | constexpr int32_t stylusDeviceId1 = 3; |
| 2457 | constexpr int32_t stylusDeviceId2 = 5; |
| 2458 | |
| 2459 | // Touch down on window |
| 2460 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2461 | .deviceId(stylusDeviceId1) |
| 2462 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100)) |
| 2463 | .build()); |
| 2464 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2465 | .deviceId(stylusDeviceId1) |
| 2466 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2467 | .build()); |
| 2468 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1))); |
| 2469 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2470 | |
| 2471 | // Second stylus down |
| 2472 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2473 | .deviceId(stylusDeviceId2) |
| 2474 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10)) |
| 2475 | .build()); |
| 2476 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2477 | .deviceId(stylusDeviceId2) |
| 2478 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11)) |
| 2479 | .build()); |
| 2480 | |
| 2481 | // First stylus is canceled, second one takes over. |
| 2482 | window->consumeMotionEvent( |
| 2483 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId1))); |
| 2484 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2))); |
| 2485 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2))); |
| 2486 | |
| 2487 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2488 | .deviceId(stylusDeviceId1) |
| 2489 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2490 | .build()); |
| 2491 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2492 | window->assertNoEvents(); |
| 2493 | } |
| 2494 | |
| 2495 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2496 | * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that |
| 2497 | * both stylus devices can function simultaneously. |
| 2498 | */ |
| 2499 | TEST_F(InputDispatcherMultiDeviceTest, TwoStylusDevicesActiveAtTheSameTime) { |
| 2500 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2501 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2502 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2503 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2504 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2505 | |
| 2506 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2507 | |
| 2508 | constexpr int32_t stylusDeviceId1 = 3; |
| 2509 | constexpr int32_t stylusDeviceId2 = 5; |
| 2510 | |
| 2511 | // Touch down on window |
| 2512 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2513 | .deviceId(stylusDeviceId1) |
| 2514 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100)) |
| 2515 | .build()); |
| 2516 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2517 | .deviceId(stylusDeviceId1) |
| 2518 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2519 | .build()); |
| 2520 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1))); |
| 2521 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2522 | |
| 2523 | // Second stylus down |
| 2524 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2525 | .deviceId(stylusDeviceId2) |
| 2526 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10)) |
| 2527 | .build()); |
| 2528 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2529 | .deviceId(stylusDeviceId2) |
| 2530 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11)) |
| 2531 | .build()); |
| 2532 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2))); |
| 2533 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2))); |
| 2534 | |
| 2535 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2536 | .deviceId(stylusDeviceId1) |
| 2537 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2538 | .build()); |
| 2539 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2540 | window->assertNoEvents(); |
| 2541 | } |
| 2542 | |
| 2543 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2544 | * One window. Touch down on the window. Then, stylus down on the window from another device. |
| 2545 | * Ensure that is canceled, because stylus down should be preferred over touch. |
| 2546 | */ |
| 2547 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusDown) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2548 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2549 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2550 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2551 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2552 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2553 | |
| 2554 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2555 | |
| 2556 | constexpr int32_t touchDeviceId = 4; |
| 2557 | constexpr int32_t stylusDeviceId = 2; |
| 2558 | |
| 2559 | // Touch down on window |
| 2560 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2561 | .deviceId(touchDeviceId) |
| 2562 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2563 | .build()); |
| 2564 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2565 | .deviceId(touchDeviceId) |
| 2566 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2567 | .build()); |
| 2568 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2569 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2570 | |
| 2571 | // Stylus down on the window |
| 2572 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2573 | .deviceId(stylusDeviceId) |
| 2574 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2575 | .build()); |
| 2576 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 2577 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2578 | |
| 2579 | // Subsequent stylus movements are delivered correctly |
| 2580 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2581 | .deviceId(stylusDeviceId) |
| 2582 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2583 | .build()); |
| 2584 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2585 | WithCoords(101, 111))); |
| 2586 | } |
| 2587 | |
| 2588 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2589 | * One window. Touch down on the window. Then, stylus down on the window from another device. |
| 2590 | * Ensure that both touch and stylus are functioning independently. |
| 2591 | */ |
| 2592 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsNotCanceledByStylusDown) { |
| 2593 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2594 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2595 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2596 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2597 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2598 | |
| 2599 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2600 | |
| 2601 | constexpr int32_t touchDeviceId = 4; |
| 2602 | constexpr int32_t stylusDeviceId = 2; |
| 2603 | |
| 2604 | // Touch down on window |
| 2605 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2606 | .deviceId(touchDeviceId) |
| 2607 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2608 | .build()); |
| 2609 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2610 | .deviceId(touchDeviceId) |
| 2611 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2612 | .build()); |
| 2613 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2614 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2615 | |
| 2616 | // Stylus down on the window |
| 2617 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2618 | .deviceId(stylusDeviceId) |
| 2619 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2620 | .build()); |
| 2621 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2622 | |
| 2623 | // Subsequent stylus movements are delivered correctly |
| 2624 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2625 | .deviceId(stylusDeviceId) |
| 2626 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2627 | .build()); |
| 2628 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2629 | WithCoords(101, 111))); |
| 2630 | |
| 2631 | // Touch continues to work too |
| 2632 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2633 | .deviceId(touchDeviceId) |
| 2634 | .pointer(PointerBuilder(0, ToolType::FINGER).x(148).y(149)) |
| 2635 | .build()); |
| 2636 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2637 | } |
| 2638 | |
| 2639 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2640 | * Two windows: a window on the left and a window on the right. |
| 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 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2646 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch_legacy) { |
| 2647 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 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 | f372b81 | 2023-02-14 18:06:51 -0800 | [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 | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2655 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2656 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2657 | mDispatcher->onWindowInfosChanged( |
| 2658 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2659 | |
| 2660 | const int32_t touchDeviceId = 4; |
| 2661 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2662 | |
| 2663 | // Start hovering over the left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 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()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2668 | leftWindow->consumeMotionEvent( |
| 2669 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2670 | |
| 2671 | // Mouse down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 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()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2677 | |
| 2678 | leftWindow->consumeMotionEvent( |
| 2679 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2680 | leftWindow->consumeMotionEvent( |
| 2681 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2682 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 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()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2690 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2691 | |
| 2692 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2693 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2694 | .deviceId(touchDeviceId) |
| 2695 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2696 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2697 | leftWindow->assertNoEvents(); |
| 2698 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2699 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2700 | |
| 2701 | // Second touch pointer down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 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()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2707 | // Since this is now a new splittable pointer going down on the left window, and it's coming |
| 2708 | // from a different device, the current gesture in the left window (pointer down) should first |
| 2709 | // be canceled. |
| 2710 | leftWindow->consumeMotionEvent( |
| 2711 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2712 | leftWindow->consumeMotionEvent( |
| 2713 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2714 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2715 | // current implementation. |
| 2716 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2717 | rightWindow->consumeMotionEvent( |
| 2718 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2719 | |
| 2720 | leftWindow->assertNoEvents(); |
| 2721 | rightWindow->assertNoEvents(); |
| 2722 | } |
| 2723 | |
| 2724 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2725 | * 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] | 2726 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2727 | * down. Then, on the left window, also place second touch pointer down. |
| 2728 | * This test tries to reproduce a crash. |
| 2729 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2730 | */ |
| 2731 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch) { |
| 2732 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2733 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2734 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2735 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2736 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2737 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2738 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2739 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2740 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2741 | |
| 2742 | mDispatcher->onWindowInfosChanged( |
| 2743 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2744 | |
| 2745 | const int32_t touchDeviceId = 4; |
| 2746 | const int32_t mouseDeviceId = 6; |
| 2747 | |
| 2748 | // Start hovering over the left window |
| 2749 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2750 | .deviceId(mouseDeviceId) |
| 2751 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2752 | .build()); |
| 2753 | leftWindow->consumeMotionEvent( |
| 2754 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2755 | |
| 2756 | // Mouse down on left window |
| 2757 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2758 | .deviceId(mouseDeviceId) |
| 2759 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2760 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2761 | .build()); |
| 2762 | |
| 2763 | leftWindow->consumeMotionEvent( |
| 2764 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2765 | leftWindow->consumeMotionEvent( |
| 2766 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2767 | |
| 2768 | mDispatcher->notifyMotion( |
| 2769 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2770 | .deviceId(mouseDeviceId) |
| 2771 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2772 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2773 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2774 | .build()); |
| 2775 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2776 | |
| 2777 | // First touch pointer down on right window |
| 2778 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2779 | .deviceId(touchDeviceId) |
| 2780 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2781 | .build()); |
| 2782 | leftWindow->assertNoEvents(); |
| 2783 | |
| 2784 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2785 | |
| 2786 | // Second touch pointer down on left window |
| 2787 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2788 | .deviceId(touchDeviceId) |
| 2789 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2790 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2791 | .build()); |
| 2792 | // Since this is now a new splittable pointer going down on the left window, and it's coming |
| 2793 | // from a different device, it will be split and delivered to left window separately. |
| 2794 | leftWindow->consumeMotionEvent( |
| 2795 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2796 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2797 | // current implementation. |
| 2798 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2799 | rightWindow->consumeMotionEvent( |
| 2800 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2801 | |
| 2802 | leftWindow->assertNoEvents(); |
| 2803 | rightWindow->assertNoEvents(); |
| 2804 | } |
| 2805 | |
| 2806 | /** |
| 2807 | * 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] | 2808 | * Mouse is hovered on the left window and stylus is hovered on the right window. |
| 2809 | */ |
| 2810 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHover) { |
| 2811 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2812 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2813 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2814 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2815 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2816 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2817 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2818 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2819 | |
| 2820 | mDispatcher->onWindowInfosChanged( |
| 2821 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2822 | |
| 2823 | const int32_t stylusDeviceId = 3; |
| 2824 | const int32_t mouseDeviceId = 6; |
| 2825 | |
| 2826 | // Start hovering over the left window |
| 2827 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2828 | .deviceId(mouseDeviceId) |
| 2829 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 2830 | .build()); |
| 2831 | leftWindow->consumeMotionEvent( |
| 2832 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2833 | |
| 2834 | // Stylus hovered on right window |
| 2835 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2836 | .deviceId(stylusDeviceId) |
| 2837 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(300).y(100)) |
| 2838 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2839 | rightWindow->consumeMotionEvent( |
| 2840 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2841 | |
| 2842 | // Subsequent HOVER_MOVE events are dispatched correctly. |
| 2843 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 2844 | .deviceId(mouseDeviceId) |
| 2845 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2846 | .build()); |
| 2847 | leftWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2848 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2849 | |
| 2850 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2851 | .deviceId(stylusDeviceId) |
| 2852 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(310).y(110)) |
| 2853 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2854 | rightWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2855 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2856 | |
| 2857 | leftWindow->assertNoEvents(); |
| 2858 | rightWindow->assertNoEvents(); |
| 2859 | } |
| 2860 | |
| 2861 | /** |
| 2862 | * Three windows: a window on the left and a window on the right. |
| 2863 | * And a spy window that's positioned above all of them. |
| 2864 | * Stylus down on the left window and remains down. Touch goes down on the right and remains down. |
| 2865 | * Check the stream that's received by the spy. |
| 2866 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2867 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy_legacy) { |
| 2868 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2869 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2870 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2871 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 2872 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2873 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 2874 | spyWindow->setTrustedOverlay(true); |
| 2875 | spyWindow->setSpy(true); |
| 2876 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2877 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2878 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2879 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2880 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2881 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2882 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2883 | |
| 2884 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2885 | |
| 2886 | mDispatcher->onWindowInfosChanged( |
| 2887 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2888 | |
| 2889 | const int32_t stylusDeviceId = 1; |
| 2890 | const int32_t touchDeviceId = 2; |
| 2891 | |
| 2892 | // Stylus down on the left window |
| 2893 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2894 | .deviceId(stylusDeviceId) |
| 2895 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2896 | .build()); |
| 2897 | leftWindow->consumeMotionEvent( |
| 2898 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2899 | spyWindow->consumeMotionEvent( |
| 2900 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2901 | |
| 2902 | // Touch down on the right window |
| 2903 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2904 | .deviceId(touchDeviceId) |
| 2905 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2906 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2907 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2908 | rightWindow->consumeMotionEvent( |
| 2909 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2910 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2911 | // Spy window does not receive touch events, because stylus events take precedence, and it |
| 2912 | // already has an active stylus gesture. |
| 2913 | |
| 2914 | // 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] | 2915 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2916 | .deviceId(stylusDeviceId) |
| 2917 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2918 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2919 | leftWindow->consumeMotionEvent( |
| 2920 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 2921 | spyWindow->consumeMotionEvent( |
| 2922 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2923 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2924 | // Further MOVE events keep going to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2925 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2926 | .deviceId(touchDeviceId) |
| 2927 | .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110)) |
| 2928 | .build()); |
| 2929 | rightWindow->consumeMotionEvent( |
| 2930 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2931 | |
| 2932 | spyWindow->assertNoEvents(); |
| 2933 | leftWindow->assertNoEvents(); |
| 2934 | rightWindow->assertNoEvents(); |
| 2935 | } |
| 2936 | |
| 2937 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2938 | * Three windows: a window on the left and a window on the right. |
| 2939 | * And a spy window that's positioned above all of them. |
| 2940 | * Stylus down on the left window and remains down. Touch goes down on the right and remains down. |
| 2941 | * Check the stream that's received by the spy. |
| 2942 | */ |
| 2943 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy) { |
| 2944 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2945 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2946 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2947 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 2948 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2949 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 2950 | spyWindow->setTrustedOverlay(true); |
| 2951 | spyWindow->setSpy(true); |
| 2952 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2953 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2954 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2955 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2956 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2957 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2958 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2959 | |
| 2960 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2961 | |
| 2962 | mDispatcher->onWindowInfosChanged( |
| 2963 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2964 | |
| 2965 | const int32_t stylusDeviceId = 1; |
| 2966 | const int32_t touchDeviceId = 2; |
| 2967 | |
| 2968 | // Stylus down on the left window |
| 2969 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2970 | .deviceId(stylusDeviceId) |
| 2971 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2972 | .build()); |
| 2973 | leftWindow->consumeMotionEvent( |
| 2974 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2975 | spyWindow->consumeMotionEvent( |
| 2976 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2977 | |
| 2978 | // Touch down on the right window |
| 2979 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2980 | .deviceId(touchDeviceId) |
| 2981 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2982 | .build()); |
| 2983 | leftWindow->assertNoEvents(); |
| 2984 | rightWindow->consumeMotionEvent( |
| 2985 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2986 | spyWindow->consumeMotionEvent( |
| 2987 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2988 | |
| 2989 | // Stylus movements continue. They should be delivered to the left window and to the spy window |
| 2990 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2991 | .deviceId(stylusDeviceId) |
| 2992 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2993 | .build()); |
| 2994 | leftWindow->consumeMotionEvent( |
| 2995 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 2996 | spyWindow->consumeMotionEvent( |
| 2997 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 2998 | |
| 2999 | // Further touch MOVE events keep going to the right window and to the spy |
| 3000 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3001 | .deviceId(touchDeviceId) |
| 3002 | .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110)) |
| 3003 | .build()); |
| 3004 | rightWindow->consumeMotionEvent( |
| 3005 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3006 | spyWindow->consumeMotionEvent( |
| 3007 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3008 | |
| 3009 | spyWindow->assertNoEvents(); |
| 3010 | leftWindow->assertNoEvents(); |
| 3011 | rightWindow->assertNoEvents(); |
| 3012 | } |
| 3013 | |
| 3014 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3015 | * Three windows: a window on the left, a window on the right, and a spy window positioned above |
| 3016 | * both. |
| 3017 | * Check hover in left window and touch down in the right window. |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3018 | * 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] | 3019 | * At the same time, left and right should be getting independent streams of hovering and touch, |
| 3020 | * respectively. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3021 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3022 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverBlocksTouchWithSpy) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3023 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3024 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3025 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3026 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 3027 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3028 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 3029 | spyWindow->setTrustedOverlay(true); |
| 3030 | spyWindow->setSpy(true); |
| 3031 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3032 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 3033 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3034 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3035 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3036 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 3037 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3038 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3039 | |
| 3040 | mDispatcher->onWindowInfosChanged( |
| 3041 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3042 | |
| 3043 | const int32_t stylusDeviceId = 1; |
| 3044 | const int32_t touchDeviceId = 2; |
| 3045 | |
| 3046 | // Stylus hover on the left window |
| 3047 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3048 | .deviceId(stylusDeviceId) |
| 3049 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 3050 | .build()); |
| 3051 | leftWindow->consumeMotionEvent( |
| 3052 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3053 | spyWindow->consumeMotionEvent( |
| 3054 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3055 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3056 | // Touch down on the right window. Spy doesn't receive this touch because it already has |
| 3057 | // stylus hovering there. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3058 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3059 | .deviceId(touchDeviceId) |
| 3060 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3061 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3062 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3063 | spyWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3064 | rightWindow->consumeMotionEvent( |
| 3065 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3066 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3067 | // 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] | 3068 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3069 | .deviceId(stylusDeviceId) |
| 3070 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 3071 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3072 | leftWindow->consumeMotionEvent( |
| 3073 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3074 | spyWindow->consumeMotionEvent( |
| 3075 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3076 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3077 | // Touch movements continue. They should be delivered to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3078 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3079 | .deviceId(touchDeviceId) |
| 3080 | .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101)) |
| 3081 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3082 | rightWindow->consumeMotionEvent( |
| 3083 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3084 | |
| 3085 | spyWindow->assertNoEvents(); |
| 3086 | leftWindow->assertNoEvents(); |
| 3087 | rightWindow->assertNoEvents(); |
| 3088 | } |
| 3089 | |
| 3090 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3091 | * Three windows: a window on the left, a window on the right, and a spy window positioned above |
| 3092 | * both. |
| 3093 | * Check hover in left window and touch down in the right window. |
| 3094 | * At first, spy should receive hover. Next, spy should receive touch. |
| 3095 | * At the same time, left and right should be getting independent streams of hovering and touch, |
| 3096 | * respectively. |
| 3097 | */ |
| 3098 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverDoesNotBlockTouchWithSpy) { |
| 3099 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3100 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3101 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3102 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 3103 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3104 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 3105 | spyWindow->setTrustedOverlay(true); |
| 3106 | spyWindow->setSpy(true); |
| 3107 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3108 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 3109 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3110 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3111 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3112 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 3113 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3114 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3115 | |
| 3116 | mDispatcher->onWindowInfosChanged( |
| 3117 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3118 | |
| 3119 | const int32_t stylusDeviceId = 1; |
| 3120 | const int32_t touchDeviceId = 2; |
| 3121 | |
| 3122 | // Stylus hover on the left window |
| 3123 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3124 | .deviceId(stylusDeviceId) |
| 3125 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 3126 | .build()); |
| 3127 | leftWindow->consumeMotionEvent( |
| 3128 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3129 | spyWindow->consumeMotionEvent( |
| 3130 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3131 | |
| 3132 | // Touch down on the right window. |
| 3133 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3134 | .deviceId(touchDeviceId) |
| 3135 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3136 | .build()); |
| 3137 | leftWindow->assertNoEvents(); |
| 3138 | spyWindow->consumeMotionEvent( |
| 3139 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3140 | rightWindow->consumeMotionEvent( |
| 3141 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3142 | |
| 3143 | // Stylus movements continue. They should be delivered to the left window and the spy. |
| 3144 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3145 | .deviceId(stylusDeviceId) |
| 3146 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 3147 | .build()); |
| 3148 | leftWindow->consumeMotionEvent( |
| 3149 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3150 | spyWindow->consumeMotionEvent( |
| 3151 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3152 | |
| 3153 | // Touch movements continue. They should be delivered to the right window and the spy |
| 3154 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3155 | .deviceId(touchDeviceId) |
| 3156 | .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101)) |
| 3157 | .build()); |
| 3158 | rightWindow->consumeMotionEvent( |
| 3159 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3160 | spyWindow->consumeMotionEvent( |
| 3161 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3162 | |
| 3163 | spyWindow->assertNoEvents(); |
| 3164 | leftWindow->assertNoEvents(); |
| 3165 | rightWindow->assertNoEvents(); |
| 3166 | } |
| 3167 | |
| 3168 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3169 | * On a single window, use two different devices: mouse and touch. |
| 3170 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 3171 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 3172 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 3173 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 3174 | * represent a new gesture. |
| 3175 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3176 | TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown_legacy) { |
| 3177 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3178 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3179 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3180 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3181 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3182 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3183 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3184 | |
| 3185 | const int32_t touchDeviceId = 4; |
| 3186 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3187 | |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3188 | // First touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3189 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3190 | .deviceId(touchDeviceId) |
| 3191 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3192 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3193 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3194 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3195 | .deviceId(touchDeviceId) |
| 3196 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3197 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3198 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3199 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3200 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3201 | .deviceId(touchDeviceId) |
| 3202 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3203 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3204 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3205 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3206 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3207 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 3208 | |
| 3209 | // Mouse down. The touch should be canceled |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3210 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3211 | .deviceId(mouseDeviceId) |
| 3212 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3213 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3214 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3215 | |
| 3216 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 3217 | WithPointerCount(1u))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3218 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3219 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3220 | mDispatcher->notifyMotion( |
| 3221 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3222 | .deviceId(mouseDeviceId) |
| 3223 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3224 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3225 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3226 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3227 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3228 | |
| 3229 | // Second touch pointer down. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3230 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3231 | .deviceId(touchDeviceId) |
| 3232 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3233 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3234 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3235 | // Since we already canceled this touch gesture, it will be ignored until a completely new |
| 3236 | // gesture is started. This is easier to implement than trying to keep track of the new pointer |
| 3237 | // and generating an ACTION_DOWN instead of ACTION_POINTER_DOWN. |
| 3238 | // However, mouse movements should continue to work. |
| 3239 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3240 | .deviceId(mouseDeviceId) |
| 3241 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3242 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110)) |
| 3243 | .build()); |
| 3244 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 3245 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3246 | window->assertNoEvents(); |
| 3247 | } |
| 3248 | |
| 3249 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3250 | * On a single window, use two different devices: mouse and touch. |
| 3251 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 3252 | * Mouse is clicked next, which should not interfere with the touch stream. |
| 3253 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is also |
| 3254 | * delivered correctly. |
| 3255 | */ |
| 3256 | TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown) { |
| 3257 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3258 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3259 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3260 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3261 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3262 | |
| 3263 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3264 | |
| 3265 | const int32_t touchDeviceId = 4; |
| 3266 | const int32_t mouseDeviceId = 6; |
| 3267 | |
| 3268 | // First touch pointer down |
| 3269 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3270 | .deviceId(touchDeviceId) |
| 3271 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3272 | .build()); |
| 3273 | // Second touch pointer down |
| 3274 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3275 | .deviceId(touchDeviceId) |
| 3276 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3277 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3278 | .build()); |
| 3279 | // First touch pointer lifts. The second one remains down |
| 3280 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3281 | .deviceId(touchDeviceId) |
| 3282 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3283 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3284 | .build()); |
| 3285 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3286 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3287 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 3288 | |
| 3289 | // Mouse down |
| 3290 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3291 | .deviceId(mouseDeviceId) |
| 3292 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3293 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3294 | .build()); |
| 3295 | |
| 3296 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3297 | |
| 3298 | mDispatcher->notifyMotion( |
| 3299 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3300 | .deviceId(mouseDeviceId) |
| 3301 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3302 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3303 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3304 | .build()); |
| 3305 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3306 | |
| 3307 | // Second touch pointer down. |
| 3308 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3309 | .deviceId(touchDeviceId) |
| 3310 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3311 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3312 | .build()); |
| 3313 | window->consumeMotionEvent(AllOf(WithMotionAction(POINTER_0_DOWN), WithDeviceId(touchDeviceId), |
| 3314 | WithPointerCount(2u))); |
| 3315 | |
| 3316 | // Mouse movements should continue to work |
| 3317 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3318 | .deviceId(mouseDeviceId) |
| 3319 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3320 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110)) |
| 3321 | .build()); |
| 3322 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 3323 | |
| 3324 | window->assertNoEvents(); |
| 3325 | } |
| 3326 | |
| 3327 | /** |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3328 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels |
| 3329 | * the injected event. |
| 3330 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3331 | TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent_legacy) { |
| 3332 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3333 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3334 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3335 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3336 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3337 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3338 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3339 | |
| 3340 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3341 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 3342 | // completion. |
| 3343 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3344 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3345 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3346 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3347 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3348 | .build())); |
| 3349 | window->consumeMotionEvent( |
| 3350 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3351 | |
| 3352 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer |
| 3353 | // should be canceled and the new gesture should take over. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3354 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3355 | .deviceId(touchDeviceId) |
| 3356 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3357 | .build()); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3358 | |
| 3359 | window->consumeMotionEvent( |
| 3360 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3361 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3362 | } |
| 3363 | |
| 3364 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3365 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event runs |
| 3366 | * parallel to the injected event. |
| 3367 | */ |
| 3368 | TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent) { |
| 3369 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3370 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3371 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3372 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3373 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3374 | |
| 3375 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3376 | |
| 3377 | const int32_t touchDeviceId = 4; |
| 3378 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 3379 | // completion. |
| 3380 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3381 | injectMotionEvent(*mDispatcher, |
| 3382 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3383 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
| 3384 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 3385 | .build())); |
| 3386 | window->consumeMotionEvent( |
| 3387 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3388 | |
| 3389 | // Now a real touch comes. The injected pointer will remain, and the new gesture will also be |
| 3390 | // allowed through. |
| 3391 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3392 | .deviceId(touchDeviceId) |
| 3393 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3394 | .build()); |
| 3395 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3396 | } |
| 3397 | |
| 3398 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3399 | * This test is similar to the test above, but the sequence of injected events is different. |
| 3400 | * |
| 3401 | * Two windows: a window on the left and a window on the right. |
| 3402 | * Mouse is hovered over the left window. |
| 3403 | * Next, we tap on the left window, where the cursor was last seen. |
| 3404 | * |
| 3405 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 3406 | * the left window. |
| 3407 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 3408 | * window (first), and then another on the left window (second). |
| 3409 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 3410 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 3411 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3412 | TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch_legacy) { |
| 3413 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3414 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3415 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 3416 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3417 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3418 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3419 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 3420 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3421 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3422 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3423 | mDispatcher->onWindowInfosChanged( |
| 3424 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3425 | |
| 3426 | const int32_t mouseDeviceId = 6; |
| 3427 | const int32_t touchDeviceId = 4; |
| 3428 | // Hover over the left window. Keep the cursor there. |
| 3429 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3430 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3431 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3432 | AINPUT_SOURCE_MOUSE) |
| 3433 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3434 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3435 | .build())); |
| 3436 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3437 | |
| 3438 | // Tap on left window |
| 3439 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3440 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3441 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3442 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3443 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3444 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3445 | .build())); |
| 3446 | |
| 3447 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3448 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3449 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3450 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3451 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3452 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3453 | .build())); |
| 3454 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3455 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3456 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 3457 | |
| 3458 | // First finger down on right window |
| 3459 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3460 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3461 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3462 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3463 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3464 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3465 | .build())); |
| 3466 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3467 | |
| 3468 | // Second finger down on the left window |
| 3469 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3470 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3471 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3472 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3473 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3474 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3475 | .build())); |
| 3476 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3477 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 3478 | |
| 3479 | // No more events |
| 3480 | leftWindow->assertNoEvents(); |
| 3481 | rightWindow->assertNoEvents(); |
| 3482 | } |
| 3483 | |
| 3484 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3485 | * This test is similar to the test above, but the sequence of injected events is different. |
| 3486 | * |
| 3487 | * Two windows: a window on the left and a window on the right. |
| 3488 | * Mouse is hovered over the left window. |
| 3489 | * Next, we tap on the left window, where the cursor was last seen. |
| 3490 | * |
| 3491 | * After that, we send one finger down onto the right window, and then a second finger down onto |
| 3492 | * the left window. |
| 3493 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 3494 | * window (first), and then another on the left window (second). |
| 3495 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 3496 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 3497 | */ |
| 3498 | TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch) { |
| 3499 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3500 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3501 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 3502 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3503 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3504 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3505 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 3506 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3507 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3508 | |
| 3509 | mDispatcher->onWindowInfosChanged( |
| 3510 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3511 | |
| 3512 | const int32_t mouseDeviceId = 6; |
| 3513 | const int32_t touchDeviceId = 4; |
| 3514 | // Hover over the left window. Keep the cursor there. |
| 3515 | mDispatcher->notifyMotion( |
| 3516 | MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3517 | .deviceId(mouseDeviceId) |
| 3518 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 3519 | .build()); |
| 3520 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3521 | |
| 3522 | // Tap on left window |
| 3523 | mDispatcher->notifyMotion( |
| 3524 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3525 | .deviceId(touchDeviceId) |
| 3526 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3527 | .build()); |
| 3528 | |
| 3529 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3530 | .deviceId(touchDeviceId) |
| 3531 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3532 | .build()); |
| 3533 | leftWindow->consumeMotionEvent( |
| 3534 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3535 | leftWindow->consumeMotionEvent( |
| 3536 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithDeviceId(touchDeviceId))); |
| 3537 | |
| 3538 | // First finger down on right window |
| 3539 | mDispatcher->notifyMotion( |
| 3540 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3541 | .deviceId(touchDeviceId) |
| 3542 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3543 | .build()); |
| 3544 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3545 | |
| 3546 | // Second finger down on the left window |
| 3547 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3548 | .deviceId(touchDeviceId) |
| 3549 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3550 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 3551 | .build()); |
| 3552 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3553 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 3554 | |
| 3555 | // No more events |
| 3556 | leftWindow->assertNoEvents(); |
| 3557 | rightWindow->assertNoEvents(); |
| 3558 | } |
| 3559 | |
| 3560 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3561 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 3562 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 3563 | * touch is gone, stylus hovering should start working again. |
| 3564 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3565 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverIgnoresTouchTap) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3566 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3567 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3568 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3569 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3570 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3571 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3572 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3573 | |
| 3574 | const int32_t stylusDeviceId = 5; |
| 3575 | const int32_t touchDeviceId = 4; |
| 3576 | // Start hovering with stylus |
| 3577 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3578 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3579 | MotionEventBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3580 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3581 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3582 | .build())); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3583 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3584 | |
| 3585 | // Finger down on the window |
| 3586 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3587 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3588 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3589 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3590 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3591 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3592 | // The touch device should be ignored! |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3593 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3594 | // Continue hovering with stylus. |
| 3595 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3596 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3597 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3598 | AINPUT_SOURCE_STYLUS) |
| 3599 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3600 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3601 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3602 | // Hovers continue to work |
| 3603 | window->consumeMotionEvent( |
| 3604 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3605 | |
| 3606 | // Lift up the finger |
| 3607 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3608 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3609 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3610 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3611 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3612 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3613 | .build())); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3614 | |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3615 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3616 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3617 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3618 | AINPUT_SOURCE_STYLUS) |
| 3619 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3620 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3621 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3622 | window->consumeMotionEvent( |
| 3623 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3624 | window->assertNoEvents(); |
| 3625 | } |
| 3626 | |
| 3627 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3628 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 3629 | * While the touch is down, hovering from the stylus is not affected. After the touch is gone, |
| 3630 | * check that the stylus hovering continues to work. |
| 3631 | */ |
| 3632 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverWithTouchTap) { |
| 3633 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3634 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3635 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3636 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3637 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3638 | |
| 3639 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3640 | |
| 3641 | const int32_t stylusDeviceId = 5; |
| 3642 | const int32_t touchDeviceId = 4; |
| 3643 | // Start hovering with stylus |
| 3644 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3645 | .deviceId(stylusDeviceId) |
| 3646 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3647 | .build()); |
| 3648 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3649 | |
| 3650 | // Finger down on the window |
| 3651 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3652 | .deviceId(touchDeviceId) |
| 3653 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3654 | .build()); |
| 3655 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3656 | |
| 3657 | // Continue hovering with stylus. |
| 3658 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3659 | .deviceId(stylusDeviceId) |
| 3660 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60)) |
| 3661 | .build()); |
| 3662 | // Hovers continue to work |
| 3663 | window->consumeMotionEvent( |
| 3664 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3665 | |
| 3666 | // Lift up the finger |
| 3667 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3668 | .deviceId(touchDeviceId) |
| 3669 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3670 | .build()); |
| 3671 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDeviceId(touchDeviceId))); |
| 3672 | |
| 3673 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3674 | .deviceId(stylusDeviceId) |
| 3675 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70)) |
| 3676 | .build()); |
| 3677 | window->consumeMotionEvent( |
| 3678 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3679 | window->assertNoEvents(); |
| 3680 | } |
| 3681 | |
| 3682 | /** |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3683 | * If stylus is down anywhere on the screen, then touches should not be delivered to windows that |
| 3684 | * have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 3685 | * |
| 3686 | * Two windows: one on the left and one on the right. |
| 3687 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 3688 | * Stylus down on the left window, and then touch down on the right window. |
| 3689 | * Check that the right window doesn't get touches while the stylus is down on the left window. |
| 3690 | */ |
| 3691 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusDownBlocksTouch) { |
| 3692 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3693 | sp<FakeWindowHandle> leftWindow = |
| 3694 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3695 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3696 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3697 | |
| 3698 | sp<FakeWindowHandle> sbtRightWindow = |
| 3699 | sp<FakeWindowHandle>::make(application, mDispatcher, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3700 | "Stylus blocks touch (right) window", |
| 3701 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3702 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3703 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 3704 | |
| 3705 | mDispatcher->onWindowInfosChanged( |
| 3706 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 3707 | |
| 3708 | const int32_t stylusDeviceId = 5; |
| 3709 | const int32_t touchDeviceId = 4; |
| 3710 | |
| 3711 | // Stylus down in the left window |
| 3712 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3713 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 3714 | .deviceId(stylusDeviceId) |
| 3715 | .build()); |
| 3716 | leftWindow->consumeMotionEvent( |
| 3717 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 3718 | |
| 3719 | // Finger tap on the right window |
| 3720 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3721 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3722 | .deviceId(touchDeviceId) |
| 3723 | .build()); |
| 3724 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3725 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3726 | .deviceId(touchDeviceId) |
| 3727 | .build()); |
| 3728 | |
| 3729 | // The touch should be blocked, because stylus is down somewhere else on screen! |
| 3730 | sbtRightWindow->assertNoEvents(); |
| 3731 | |
| 3732 | // Continue stylus motion, and ensure it's not impacted. |
| 3733 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 3734 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3735 | .deviceId(stylusDeviceId) |
| 3736 | .build()); |
| 3737 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3738 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3739 | .deviceId(stylusDeviceId) |
| 3740 | .build()); |
| 3741 | leftWindow->consumeMotionEvent( |
| 3742 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 3743 | leftWindow->consumeMotionEvent( |
| 3744 | AllOf(WithMotionAction(ACTION_UP), WithDeviceId(stylusDeviceId))); |
| 3745 | |
| 3746 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3747 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3748 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3749 | .deviceId(touchDeviceId) |
| 3750 | .build()); |
| 3751 | sbtRightWindow->consumeMotionEvent( |
| 3752 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3753 | } |
| 3754 | |
| 3755 | /** |
| 3756 | * If stylus is hovering anywhere on the screen, then touches should not be delivered to windows |
| 3757 | * that have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 3758 | * |
| 3759 | * Two windows: one on the left and one on the right. |
| 3760 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 3761 | * Stylus hover on the left window, and then touch down on the right window. |
| 3762 | * Check that the right window doesn't get touches while the stylus is hovering on the left window. |
| 3763 | */ |
| 3764 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusHoverBlocksTouch) { |
| 3765 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3766 | sp<FakeWindowHandle> leftWindow = |
| 3767 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3768 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3769 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3770 | |
| 3771 | sp<FakeWindowHandle> sbtRightWindow = |
| 3772 | sp<FakeWindowHandle>::make(application, mDispatcher, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3773 | "Stylus blocks touch (right) window", |
| 3774 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3775 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3776 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 3777 | |
| 3778 | mDispatcher->onWindowInfosChanged( |
| 3779 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 3780 | |
| 3781 | const int32_t stylusDeviceId = 5; |
| 3782 | const int32_t touchDeviceId = 4; |
| 3783 | |
| 3784 | // Stylus hover in the left window |
| 3785 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3786 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 3787 | .deviceId(stylusDeviceId) |
| 3788 | .build()); |
| 3789 | leftWindow->consumeMotionEvent( |
| 3790 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3791 | |
| 3792 | // Finger tap on the right window |
| 3793 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3794 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3795 | .deviceId(touchDeviceId) |
| 3796 | .build()); |
| 3797 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3798 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3799 | .deviceId(touchDeviceId) |
| 3800 | .build()); |
| 3801 | |
| 3802 | // The touch should be blocked, because stylus is hovering somewhere else on screen! |
| 3803 | sbtRightWindow->assertNoEvents(); |
| 3804 | |
| 3805 | // Continue stylus motion, and ensure it's not impacted. |
| 3806 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3807 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3808 | .deviceId(stylusDeviceId) |
| 3809 | .build()); |
| 3810 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3811 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3812 | .deviceId(stylusDeviceId) |
| 3813 | .build()); |
| 3814 | leftWindow->consumeMotionEvent( |
| 3815 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3816 | leftWindow->consumeMotionEvent( |
| 3817 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId))); |
| 3818 | |
| 3819 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3820 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3821 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3822 | .deviceId(touchDeviceId) |
| 3823 | .build()); |
| 3824 | sbtRightWindow->consumeMotionEvent( |
| 3825 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3826 | } |
| 3827 | |
| 3828 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3829 | * A spy window above a window with no input channel. |
| 3830 | * Start hovering with a stylus device, and then tap with it. |
| 3831 | * Ensure spy window receives the entire sequence. |
| 3832 | */ |
| 3833 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 3834 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3835 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 3836 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3837 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3838 | spyWindow->setTrustedOverlay(true); |
| 3839 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3840 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3841 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3842 | window->setNoInputChannel(true); |
| 3843 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3844 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3845 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3846 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3847 | // Start hovering with stylus |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3848 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3849 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3850 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3851 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3852 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3853 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3854 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3855 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3856 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3857 | |
| 3858 | // Stylus touches down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3859 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3860 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3861 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3862 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3863 | |
| 3864 | // Stylus goes up |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3865 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3866 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3867 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3868 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 3869 | |
| 3870 | // Again hover |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3871 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3872 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3873 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3874 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3875 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3876 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3877 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3878 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3879 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3880 | |
| 3881 | // No more events |
| 3882 | spyWindow->assertNoEvents(); |
| 3883 | window->assertNoEvents(); |
| 3884 | } |
| 3885 | |
| 3886 | /** |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 3887 | * A stale stylus HOVER_EXIT event is injected. Since it's a stale event, it should generally be |
| 3888 | * rejected. But since we already have an ongoing gesture, this event should be processed. |
| 3889 | * This prevents inconsistent events being handled inside the dispatcher. |
| 3890 | */ |
| 3891 | TEST_F(InputDispatcherTest, StaleStylusHoverGestureIsComplete) { |
| 3892 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3893 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3894 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3895 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 3896 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3897 | |
| 3898 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3899 | |
| 3900 | // Start hovering with stylus |
| 3901 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3902 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3903 | .build()); |
| 3904 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3905 | |
| 3906 | NotifyMotionArgs hoverExit = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3907 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3908 | .build(); |
| 3909 | // Make this 'hoverExit' event stale |
| 3910 | mFakePolicy->setStaleEventTimeout(100ms); |
| 3911 | std::this_thread::sleep_for(100ms); |
| 3912 | |
| 3913 | // It shouldn't be dropped by the dispatcher, even though it's stale. |
| 3914 | mDispatcher->notifyMotion(hoverExit); |
| 3915 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3916 | |
| 3917 | // Stylus starts hovering again! There should be no crash. |
| 3918 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3919 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(51)) |
| 3920 | .build()); |
| 3921 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3922 | } |
| 3923 | |
| 3924 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3925 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 3926 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 3927 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 3928 | * While the mouse is down, new move events from the touch device should be ignored. |
| 3929 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3930 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove_legacy) { |
| 3931 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3932 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3933 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 3934 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3935 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3936 | spyWindow->setTrustedOverlay(true); |
| 3937 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3938 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3939 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3940 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3941 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3942 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3943 | |
| 3944 | const int32_t mouseDeviceId = 7; |
| 3945 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3946 | |
| 3947 | // Hover a bit with mouse first |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3948 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3949 | .deviceId(mouseDeviceId) |
| 3950 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3951 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3952 | spyWindow->consumeMotionEvent( |
| 3953 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3954 | window->consumeMotionEvent( |
| 3955 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3956 | |
| 3957 | // Start touching |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3958 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3959 | .deviceId(touchDeviceId) |
| 3960 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3961 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3962 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3963 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3964 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3965 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3966 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3967 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3968 | .deviceId(touchDeviceId) |
| 3969 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 3970 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3971 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3972 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3973 | |
| 3974 | // Pilfer the stream |
| 3975 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3976 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 3977 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3978 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3979 | .deviceId(touchDeviceId) |
| 3980 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 3981 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3982 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3983 | |
| 3984 | // Mouse down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3985 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3986 | .deviceId(mouseDeviceId) |
| 3987 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3988 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3989 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3990 | |
| 3991 | spyWindow->consumeMotionEvent( |
| 3992 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 3993 | spyWindow->consumeMotionEvent( |
| 3994 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3995 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3996 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3997 | mDispatcher->notifyMotion( |
| 3998 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3999 | .deviceId(mouseDeviceId) |
| 4000 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4001 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4002 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 4003 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4004 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4005 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4006 | |
| 4007 | // Mouse move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4008 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4009 | .deviceId(mouseDeviceId) |
| 4010 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4011 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 4012 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4013 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 4014 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 4015 | |
| 4016 | // Touch move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4017 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 4018 | .deviceId(touchDeviceId) |
| 4019 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 4020 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4021 | |
| 4022 | // No more events |
| 4023 | spyWindow->assertNoEvents(); |
| 4024 | window->assertNoEvents(); |
| 4025 | } |
| 4026 | |
| 4027 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4028 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 4029 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 4030 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 4031 | * While the mouse is down, new move events from the touch device should continue to work. |
| 4032 | */ |
| 4033 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 4034 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 4035 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4036 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 4037 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4038 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 4039 | spyWindow->setTrustedOverlay(true); |
| 4040 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4041 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4042 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4043 | window->setFrame(Rect(0, 0, 200, 200)); |
| 4044 | |
| 4045 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 4046 | |
| 4047 | const int32_t mouseDeviceId = 7; |
| 4048 | const int32_t touchDeviceId = 4; |
| 4049 | |
| 4050 | // Hover a bit with mouse first |
| 4051 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4052 | .deviceId(mouseDeviceId) |
| 4053 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 4054 | .build()); |
| 4055 | spyWindow->consumeMotionEvent( |
| 4056 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 4057 | window->consumeMotionEvent( |
| 4058 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 4059 | |
| 4060 | // Start touching |
| 4061 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4062 | .deviceId(touchDeviceId) |
| 4063 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4064 | .build()); |
| 4065 | |
| 4066 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4067 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4068 | |
| 4069 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 4070 | .deviceId(touchDeviceId) |
| 4071 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 4072 | .build()); |
| 4073 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 4074 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 4075 | |
| 4076 | // Pilfer the stream |
| 4077 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 4078 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 4079 | // Hover is not pilfered! Only touch. |
| 4080 | |
| 4081 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 4082 | .deviceId(touchDeviceId) |
| 4083 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 4084 | .build()); |
| 4085 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 4086 | |
| 4087 | // Mouse down |
| 4088 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4089 | .deviceId(mouseDeviceId) |
| 4090 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4091 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 4092 | .build()); |
| 4093 | |
| 4094 | spyWindow->consumeMotionEvent( |
| 4095 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 4096 | spyWindow->consumeMotionEvent( |
| 4097 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 4098 | window->consumeMotionEvent( |
| 4099 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 4100 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 4101 | |
| 4102 | mDispatcher->notifyMotion( |
| 4103 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 4104 | .deviceId(mouseDeviceId) |
| 4105 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4106 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4107 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 4108 | .build()); |
| 4109 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4110 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4111 | |
| 4112 | // Mouse move! |
| 4113 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4114 | .deviceId(mouseDeviceId) |
| 4115 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4116 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 4117 | .build()); |
| 4118 | spyWindow->consumeMotionEvent( |
| 4119 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 4120 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 4121 | |
| 4122 | // Touch move! |
| 4123 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 4124 | .deviceId(touchDeviceId) |
| 4125 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 4126 | .build()); |
| 4127 | spyWindow->consumeMotionEvent( |
| 4128 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 4129 | |
| 4130 | // No more events |
| 4131 | spyWindow->assertNoEvents(); |
| 4132 | window->assertNoEvents(); |
| 4133 | } |
| 4134 | |
| 4135 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4136 | * On the display, have a single window, and also an area where there's no window. |
| 4137 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 4138 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 4139 | */ |
| 4140 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 4141 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4142 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4143 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4144 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4145 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4146 | |
| 4147 | // Touch down on the empty space |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4148 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4149 | |
| 4150 | mDispatcher->waitForIdle(); |
| 4151 | window->assertNoEvents(); |
| 4152 | |
| 4153 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4154 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4155 | mDispatcher->waitForIdle(); |
| 4156 | window->consumeMotionDown(); |
| 4157 | } |
| 4158 | |
| 4159 | /** |
| 4160 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 4161 | * non-touchable window. |
| 4162 | */ |
| 4163 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 4164 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4165 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4166 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4167 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 4168 | window1->setTouchable(false); |
| 4169 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4170 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4171 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 4172 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4173 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4174 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4175 | // Touch down on the non-touchable window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4176 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4177 | |
| 4178 | mDispatcher->waitForIdle(); |
| 4179 | window1->assertNoEvents(); |
| 4180 | window2->assertNoEvents(); |
| 4181 | |
| 4182 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4183 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4184 | mDispatcher->waitForIdle(); |
| 4185 | window2->consumeMotionDown(); |
| 4186 | } |
| 4187 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4188 | /** |
| 4189 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 4190 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 4191 | */ |
| 4192 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 4193 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4194 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4195 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4196 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 4197 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4198 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4199 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 4200 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4201 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4202 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4203 | // Touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4204 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4205 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4206 | |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4207 | const std::unique_ptr<MotionEvent> firstDown = |
| 4208 | window1->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 4209 | ASSERT_EQ(firstDown->getDownTime(), firstDown->getEventTime()); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4210 | window2->assertNoEvents(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4211 | |
| 4212 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4213 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4214 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4215 | |
| 4216 | const std::unique_ptr<MotionEvent> secondDown = |
| 4217 | window2->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 4218 | ASSERT_EQ(secondDown->getDownTime(), secondDown->getEventTime()); |
| 4219 | ASSERT_NE(firstDown->getDownTime(), secondDown->getDownTime()); |
| 4220 | // We currently send MOVE events to all windows receiving a split touch when there is any change |
| 4221 | // in the touch state, even when none of the pointers in the split window actually moved. |
| 4222 | // Document this behavior in the test. |
| 4223 | window1->consumeMotionMove(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4224 | |
| 4225 | // Now move the pointer on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4226 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4227 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4228 | |
| 4229 | window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime())); |
| 4230 | window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime())); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4231 | |
| 4232 | // Now add new touch down on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4233 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4234 | mDispatcher->waitForIdle(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4235 | |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4236 | window2->consumeMotionEvent( |
| 4237 | AllOf(WithMotionAction(POINTER_1_DOWN), WithDownTime(secondDown->getDownTime()))); |
| 4238 | window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime())); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4239 | |
| 4240 | // Now move the pointer on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4241 | mDispatcher->notifyMotion( |
| 4242 | generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4243 | mDispatcher->waitForIdle(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4244 | |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4245 | window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime())); |
| 4246 | window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime())); |
| 4247 | |
| 4248 | // Now add new touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4249 | mDispatcher->notifyMotion( |
| 4250 | generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4251 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4252 | |
| 4253 | window1->consumeMotionEvent( |
| 4254 | AllOf(WithMotionAction(POINTER_1_DOWN), WithDownTime(firstDown->getDownTime()))); |
| 4255 | window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime())); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4256 | } |
| 4257 | |
Siarhei Vishniakou | ac42d24 | 2024-06-28 10:43:53 -0700 | [diff] [blame] | 4258 | /** |
| 4259 | * When events are not split, the downTime should be adjusted such that the downTime corresponds |
| 4260 | * to the event time of the first ACTION_DOWN. If a new window appears, it should not affect |
| 4261 | * the event routing because the first window prevents splitting. |
| 4262 | */ |
| 4263 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTimeForNewWindow) { |
Siarhei Vishniakou | a3fe664 | 2024-07-18 11:58:25 -0700 | [diff] [blame] | 4264 | SCOPED_FLAG_OVERRIDE(split_all_touches, false); |
Siarhei Vishniakou | ac42d24 | 2024-06-28 10:43:53 -0700 | [diff] [blame] | 4265 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4266 | sp<FakeWindowHandle> window1 = |
| 4267 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
| 4268 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 4269 | window1->setPreventSplitting(true); |
| 4270 | |
| 4271 | sp<FakeWindowHandle> window2 = |
| 4272 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
| 4273 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 4274 | |
| 4275 | mDispatcher->onWindowInfosChanged({{*window1->getInfo()}, {}, 0, 0}); |
| 4276 | |
| 4277 | // Touch down on the first window |
| 4278 | NotifyMotionArgs downArgs = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4279 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4280 | .build(); |
| 4281 | mDispatcher->notifyMotion(downArgs); |
| 4282 | |
| 4283 | window1->consumeMotionEvent( |
| 4284 | AllOf(WithMotionAction(ACTION_DOWN), WithDownTime(downArgs.downTime))); |
| 4285 | |
| 4286 | // Second window is added |
| 4287 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
| 4288 | |
| 4289 | // Now touch down on the window with another pointer |
| 4290 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4291 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4292 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 4293 | .downTime(downArgs.downTime) |
| 4294 | .build()); |
| 4295 | window1->consumeMotionPointerDown(1, AllOf(WithDownTime(downArgs.downTime))); |
| 4296 | |
| 4297 | // Finish the gesture |
| 4298 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4299 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4300 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 4301 | .downTime(downArgs.downTime) |
| 4302 | .build()); |
| 4303 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4304 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4305 | .downTime(downArgs.downTime) |
| 4306 | .build()); |
| 4307 | window1->consumeMotionPointerUp(1, AllOf(WithDownTime(downArgs.downTime))); |
| 4308 | window1->consumeMotionEvent( |
| 4309 | AllOf(WithMotionAction(ACTION_UP), WithDownTime(downArgs.downTime))); |
| 4310 | window2->assertNoEvents(); |
| 4311 | } |
| 4312 | |
| 4313 | /** |
| 4314 | * When splitting touch events, the downTime should be adjusted such that the downTime corresponds |
| 4315 | * to the event time of the first ACTION_DOWN sent to the new window. |
| 4316 | * If a new window that does not support split appears on the screen and gets touched with the |
| 4317 | * second finger, it should not get any events because it doesn't want split touches. At the same |
| 4318 | * time, the first window should not get the pointer_down event because it supports split touches |
| 4319 | * (and the touch occurred outside of the bounds of window1). |
| 4320 | */ |
| 4321 | TEST_F(InputDispatcherTest, SplitTouchesDropsEventForNonSplittableSecondWindow) { |
Siarhei Vishniakou | a3fe664 | 2024-07-18 11:58:25 -0700 | [diff] [blame] | 4322 | SCOPED_FLAG_OVERRIDE(split_all_touches, false); |
Siarhei Vishniakou | ac42d24 | 2024-06-28 10:43:53 -0700 | [diff] [blame] | 4323 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4324 | sp<FakeWindowHandle> window1 = |
| 4325 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
| 4326 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 4327 | |
| 4328 | sp<FakeWindowHandle> window2 = |
| 4329 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
| 4330 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 4331 | |
| 4332 | mDispatcher->onWindowInfosChanged({{*window1->getInfo()}, {}, 0, 0}); |
| 4333 | |
| 4334 | // Touch down on the first window |
| 4335 | NotifyMotionArgs downArgs = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4336 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4337 | .build(); |
| 4338 | mDispatcher->notifyMotion(downArgs); |
| 4339 | |
| 4340 | window1->consumeMotionEvent( |
| 4341 | AllOf(WithMotionAction(ACTION_DOWN), WithDownTime(downArgs.downTime))); |
| 4342 | |
| 4343 | // Second window is added |
| 4344 | window2->setPreventSplitting(true); |
| 4345 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
| 4346 | |
| 4347 | // Now touch down on the window with another pointer |
| 4348 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4349 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4350 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 4351 | .downTime(downArgs.downTime) |
| 4352 | .build()); |
| 4353 | // Event is dropped because window2 doesn't support split touch, and window1 does. |
| 4354 | |
| 4355 | // Complete the gesture |
| 4356 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4357 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4358 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 4359 | .downTime(downArgs.downTime) |
| 4360 | .build()); |
| 4361 | // A redundant MOVE event is generated that doesn't carry any new information |
| 4362 | window1->consumeMotionEvent( |
| 4363 | AllOf(WithMotionAction(ACTION_MOVE), WithDownTime(downArgs.downTime))); |
| 4364 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4365 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4366 | .downTime(downArgs.downTime) |
| 4367 | .build()); |
| 4368 | |
| 4369 | window1->consumeMotionEvent( |
| 4370 | AllOf(WithMotionAction(ACTION_UP), WithDownTime(downArgs.downTime))); |
| 4371 | window1->assertNoEvents(); |
| 4372 | window2->assertNoEvents(); |
| 4373 | } |
| 4374 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4375 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4376 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4377 | sp<FakeWindowHandle> windowLeft = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 4378 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4379 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4380 | sp<FakeWindowHandle> windowRight = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 4381 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4382 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4383 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4384 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4385 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4386 | mDispatcher->onWindowInfosChanged( |
| 4387 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4388 | |
| 4389 | // 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] | 4390 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4391 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4392 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4393 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4394 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4395 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4396 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4397 | |
| 4398 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4399 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4400 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4401 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4402 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4403 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4404 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4405 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 4406 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4407 | |
| 4408 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4409 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4410 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4411 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4412 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4413 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4414 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4415 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4416 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4417 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4418 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4419 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4420 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 4421 | AINPUT_SOURCE_MOUSE) |
| 4422 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4423 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4424 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4425 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4426 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4427 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4428 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4429 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4430 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 4431 | AINPUT_SOURCE_MOUSE) |
| 4432 | .buttonState(0) |
| 4433 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4434 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4435 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4436 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4437 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4438 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4439 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4440 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4441 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4442 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4443 | .build())); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4444 | windowLeft->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4445 | |
| 4446 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4447 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4448 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4449 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4450 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4451 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4452 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4453 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4454 | |
| 4455 | // No more events |
| 4456 | windowLeft->assertNoEvents(); |
| 4457 | windowRight->assertNoEvents(); |
| 4458 | } |
| 4459 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4460 | /** |
| 4461 | * Put two fingers down (and don't release them) and click the mouse button. |
| 4462 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 4463 | * currently active gesture should be canceled, and the new one should proceed. |
| 4464 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4465 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick_legacy) { |
| 4466 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4467 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4468 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4469 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4470 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4471 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4472 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4473 | |
| 4474 | const int32_t touchDeviceId = 4; |
| 4475 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4476 | |
| 4477 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4478 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4479 | .deviceId(touchDeviceId) |
| 4480 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4481 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4482 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4483 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4484 | .deviceId(touchDeviceId) |
| 4485 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4486 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 4487 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4488 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4489 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4490 | |
| 4491 | // Inject a series of mouse events for a mouse click |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4492 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4493 | .deviceId(mouseDeviceId) |
| 4494 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4495 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4496 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4497 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 4498 | WithPointerCount(2u))); |
| 4499 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 4500 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4501 | mDispatcher->notifyMotion( |
| 4502 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 4503 | .deviceId(mouseDeviceId) |
| 4504 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4505 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4506 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4507 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4508 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4509 | |
| 4510 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 4511 | // already canceled gesture, it should be ignored. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4512 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 4513 | .deviceId(touchDeviceId) |
| 4514 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 4515 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 4516 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4517 | window->assertNoEvents(); |
| 4518 | } |
| 4519 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4520 | /** |
| 4521 | * Put two fingers down (and don't release them) and click the mouse button. |
| 4522 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 4523 | * currently active gesture should not be canceled, and the new one should proceed in parallel. |
| 4524 | */ |
| 4525 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 4526 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 4527 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4528 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4529 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4530 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4531 | |
| 4532 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 4533 | |
| 4534 | const int32_t touchDeviceId = 4; |
| 4535 | const int32_t mouseDeviceId = 6; |
| 4536 | |
| 4537 | // Two pointers down |
| 4538 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4539 | .deviceId(touchDeviceId) |
| 4540 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4541 | .build()); |
| 4542 | |
| 4543 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4544 | .deviceId(touchDeviceId) |
| 4545 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4546 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 4547 | .build()); |
| 4548 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4549 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4550 | |
| 4551 | // Send a series of mouse events for a mouse click |
| 4552 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4553 | .deviceId(mouseDeviceId) |
| 4554 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4555 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4556 | .build()); |
| 4557 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 4558 | |
| 4559 | mDispatcher->notifyMotion( |
| 4560 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 4561 | .deviceId(mouseDeviceId) |
| 4562 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4563 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4564 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4565 | .build()); |
| 4566 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4567 | |
| 4568 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 4569 | // already active gesture, it should be sent normally. |
| 4570 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 4571 | .deviceId(touchDeviceId) |
| 4572 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 4573 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 4574 | .build()); |
| 4575 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 4576 | window->assertNoEvents(); |
| 4577 | } |
| 4578 | |
Siarhei Vishniakou | 07cdda9 | 2024-07-01 16:45:08 -0700 | [diff] [blame] | 4579 | /** |
| 4580 | * A spy window sits above a window with NO_INPUT_CHANNEL. Ensure that the spy receives events even |
| 4581 | * though the window underneath should not get any events. |
| 4582 | */ |
| 4583 | TEST_F(InputDispatcherTest, NonSplittableSpyAboveNoInputChannelWindowSinglePointer) { |
| 4584 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4585 | |
| 4586 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 4587 | ui::LogicalDisplayId::DEFAULT); |
| 4588 | spyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4589 | spyWindow->setTrustedOverlay(true); |
| 4590 | spyWindow->setPreventSplitting(true); |
| 4591 | spyWindow->setSpy(true); |
| 4592 | // Another window below spy that has both NO_INPUT_CHANNEL and PREVENT_SPLITTING |
| 4593 | sp<FakeWindowHandle> inputSinkWindow = |
| 4594 | sp<FakeWindowHandle>::make(application, mDispatcher, "Input sink below spy", |
| 4595 | ui::LogicalDisplayId::DEFAULT); |
| 4596 | inputSinkWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4597 | inputSinkWindow->setTrustedOverlay(true); |
| 4598 | inputSinkWindow->setPreventSplitting(true); |
| 4599 | inputSinkWindow->setNoInputChannel(true); |
| 4600 | |
| 4601 | mDispatcher->onWindowInfosChanged( |
| 4602 | {{*spyWindow->getInfo(), *inputSinkWindow->getInfo()}, {}, 0, 0}); |
| 4603 | |
| 4604 | // Tap the spy window |
| 4605 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4606 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(51)) |
| 4607 | .build()); |
| 4608 | mDispatcher->notifyMotion( |
| 4609 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4610 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(51)) |
| 4611 | .build()); |
| 4612 | |
| 4613 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 4614 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP))); |
| 4615 | inputSinkWindow->assertNoEvents(); |
| 4616 | } |
| 4617 | |
| 4618 | /** |
| 4619 | * A spy window sits above a window with NO_INPUT_CHANNEL. Ensure that the spy receives events even |
| 4620 | * though the window underneath should not get any events. |
| 4621 | * Same test as above, but with two pointers touching instead of one. |
| 4622 | */ |
| 4623 | TEST_F(InputDispatcherTest, NonSplittableSpyAboveNoInputChannelWindowTwoPointers) { |
| 4624 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4625 | |
| 4626 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 4627 | ui::LogicalDisplayId::DEFAULT); |
| 4628 | spyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4629 | spyWindow->setTrustedOverlay(true); |
| 4630 | spyWindow->setPreventSplitting(true); |
| 4631 | spyWindow->setSpy(true); |
| 4632 | // Another window below spy that would have both NO_INPUT_CHANNEL and PREVENT_SPLITTING |
| 4633 | sp<FakeWindowHandle> inputSinkWindow = |
| 4634 | sp<FakeWindowHandle>::make(application, mDispatcher, "Input sink below spy", |
| 4635 | ui::LogicalDisplayId::DEFAULT); |
| 4636 | inputSinkWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4637 | inputSinkWindow->setTrustedOverlay(true); |
| 4638 | inputSinkWindow->setPreventSplitting(true); |
| 4639 | inputSinkWindow->setNoInputChannel(true); |
| 4640 | |
| 4641 | mDispatcher->onWindowInfosChanged( |
| 4642 | {{*spyWindow->getInfo(), *inputSinkWindow->getInfo()}, {}, 0, 0}); |
| 4643 | |
| 4644 | // Both fingers land into the spy window |
| 4645 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4646 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(51)) |
| 4647 | .build()); |
| 4648 | mDispatcher->notifyMotion( |
| 4649 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4650 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(51)) |
| 4651 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(11)) |
| 4652 | .build()); |
| 4653 | mDispatcher->notifyMotion( |
| 4654 | MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4655 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(51)) |
| 4656 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(11)) |
| 4657 | .build()); |
| 4658 | mDispatcher->notifyMotion( |
| 4659 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4660 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(51)) |
| 4661 | .build()); |
| 4662 | |
| 4663 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 4664 | spyWindow->consumeMotionPointerDown(1, WithPointerCount(2)); |
| 4665 | spyWindow->consumeMotionPointerUp(1, WithPointerCount(2)); |
| 4666 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP))); |
| 4667 | inputSinkWindow->assertNoEvents(); |
| 4668 | } |
| 4669 | |
| 4670 | /** Check the behaviour for cases where input sink prevents or doesn't prevent splitting. */ |
| 4671 | class SpyThatPreventsSplittingWithApplicationFixture : public InputDispatcherTest, |
| 4672 | public ::testing::WithParamInterface<bool> { |
| 4673 | }; |
| 4674 | |
| 4675 | /** |
| 4676 | * Three windows: |
| 4677 | * - An application window (app window) |
| 4678 | * - A spy window that does not overlap the app window. Has PREVENT_SPLITTING flag |
| 4679 | * - A window below the spy that has NO_INPUT_CHANNEL (call it 'inputSink') |
| 4680 | * |
| 4681 | * The spy window is side-by-side with the app window. The inputSink is below the spy. |
| 4682 | * We first touch the area outside of the appWindow, but inside spyWindow. |
| 4683 | * Only the SPY window should get the DOWN event. |
| 4684 | * The spy pilfers after receiving the first DOWN event. |
| 4685 | * Next, we touch the app window. |
| 4686 | * The spy should receive POINTER_DOWN(1) (since spy is preventing splits). |
| 4687 | * Also, since the spy is already pilfering the first pointer, it will be sent the remaining new |
| 4688 | * pointers automatically, as well. |
| 4689 | * Next, the first pointer (from the spy) is lifted. |
| 4690 | * Spy should get POINTER_UP(0). |
| 4691 | * This event should not go to the app because the app never received this pointer to begin with. |
| 4692 | * Now, lift the remaining pointer and check that the spy receives UP event. |
| 4693 | * |
| 4694 | * Finally, send a new ACTION_DOWN event to the spy and check that it's received. |
| 4695 | * This test attempts to reproduce a crash in the dispatcher. |
| 4696 | */ |
| 4697 | TEST_P(SpyThatPreventsSplittingWithApplicationFixture, SpyThatPreventsSplittingWithApplication) { |
Siarhei Vishniakou | a3fe664 | 2024-07-18 11:58:25 -0700 | [diff] [blame] | 4698 | SCOPED_FLAG_OVERRIDE(split_all_touches, false); |
Siarhei Vishniakou | 07cdda9 | 2024-07-01 16:45:08 -0700 | [diff] [blame] | 4699 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4700 | |
| 4701 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 4702 | ui::LogicalDisplayId::DEFAULT); |
| 4703 | spyWindow->setFrame(Rect(100, 100, 200, 200)); |
| 4704 | spyWindow->setTrustedOverlay(true); |
| 4705 | spyWindow->setPreventSplitting(true); |
| 4706 | spyWindow->setSpy(true); |
| 4707 | // Another window below spy that has both NO_INPUT_CHANNEL and PREVENT_SPLITTING |
| 4708 | sp<FakeWindowHandle> inputSinkWindow = |
| 4709 | sp<FakeWindowHandle>::make(application, mDispatcher, "Input sink below spy", |
| 4710 | ui::LogicalDisplayId::DEFAULT); |
| 4711 | inputSinkWindow->setFrame(Rect(100, 100, 200, 200)); // directly below the spy |
| 4712 | inputSinkWindow->setTrustedOverlay(true); |
| 4713 | inputSinkWindow->setPreventSplitting(GetParam()); |
| 4714 | inputSinkWindow->setNoInputChannel(true); |
| 4715 | |
| 4716 | sp<FakeWindowHandle> appWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "App", |
| 4717 | ui::LogicalDisplayId::DEFAULT); |
| 4718 | appWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4719 | |
| 4720 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
| 4721 | mDispatcher->onWindowInfosChanged( |
| 4722 | {{*spyWindow->getInfo(), *inputSinkWindow->getInfo(), *appWindow->getInfo()}, |
| 4723 | {}, |
| 4724 | 0, |
| 4725 | 0}); |
| 4726 | |
| 4727 | // First finger lands outside of the appWindow, but inside of the spy window |
| 4728 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4729 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 4730 | .build()); |
| 4731 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 4732 | |
| 4733 | mDispatcher->pilferPointers(spyWindow->getToken()); |
| 4734 | |
| 4735 | // Second finger lands in the app, and goes to the spy window. It doesn't go to the app because |
| 4736 | // the spy is already pilfering the first pointer, and this automatically grants the remaining |
| 4737 | // new pointers to the spy, as well. |
| 4738 | mDispatcher->notifyMotion( |
| 4739 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4740 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 4741 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 4742 | .build()); |
| 4743 | |
| 4744 | spyWindow->consumeMotionPointerDown(1, WithPointerCount(2)); |
| 4745 | |
| 4746 | // Now lift up the first pointer |
| 4747 | mDispatcher->notifyMotion( |
| 4748 | MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4749 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 4750 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 4751 | .build()); |
| 4752 | spyWindow->consumeMotionPointerUp(0, WithPointerCount(2)); |
| 4753 | |
| 4754 | // And lift the remaining pointer! |
| 4755 | mDispatcher->notifyMotion( |
| 4756 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4757 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 4758 | .build()); |
| 4759 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithPointerCount(1))); |
| 4760 | |
| 4761 | // Now send a new DOWN, which should again go to spy. |
| 4762 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4763 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 4764 | .build()); |
| 4765 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 4766 | // The app window doesn't get any events this entire time because the spy received the events |
| 4767 | // first and pilfered, which makes all new pointers go to it as well. |
| 4768 | appWindow->assertNoEvents(); |
| 4769 | } |
| 4770 | |
| 4771 | // Behaviour should be the same regardless of whether inputSink supports splitting. |
| 4772 | INSTANTIATE_TEST_SUITE_P(SpyThatPreventsSplittingWithApplication, |
| 4773 | SpyThatPreventsSplittingWithApplicationFixture, testing::Bool()); |
| 4774 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4775 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 4776 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4777 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4778 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 4779 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4780 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4781 | spyWindow->setTrustedOverlay(true); |
| 4782 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4783 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4784 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4785 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4786 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4787 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4788 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4789 | |
| 4790 | // Send mouse cursor to the window |
| 4791 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4792 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4793 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4794 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4795 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4796 | .build())); |
| 4797 | |
| 4798 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4799 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4800 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4801 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4802 | |
| 4803 | window->assertNoEvents(); |
| 4804 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4805 | } |
| 4806 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4807 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows_legacy) { |
| 4808 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4809 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4810 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4811 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 4812 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4813 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4814 | spyWindow->setTrustedOverlay(true); |
| 4815 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4816 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4817 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4818 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4819 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4820 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4821 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4822 | |
| 4823 | // Send mouse cursor to the window |
| 4824 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4825 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4826 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4827 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4828 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4829 | .build())); |
| 4830 | |
| 4831 | // Move mouse cursor |
| 4832 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4833 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4834 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4835 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4836 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4837 | .build())); |
| 4838 | |
| 4839 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4840 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4841 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4842 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4843 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4844 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4845 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4846 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4847 | // Touch down on the window |
| 4848 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4849 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4850 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 4851 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4852 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4853 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4854 | .build())); |
| 4855 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4856 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4857 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4858 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4859 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4860 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4861 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4862 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4863 | |
| 4864 | // pilfer the motion, retaining the gesture on the spy window. |
| 4865 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 4866 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 4867 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4868 | |
| 4869 | // Touch UP on the window |
| 4870 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4871 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4872 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 4873 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4874 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4875 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4876 | .build())); |
| 4877 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4878 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4879 | |
| 4880 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 4881 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 4882 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 4883 | |
| 4884 | // One more tap - DOWN |
| 4885 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4886 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4887 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 4888 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4889 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4890 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4891 | .build())); |
| 4892 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4893 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4894 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4895 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4896 | |
| 4897 | // Touch UP on the window |
| 4898 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4899 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4900 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 4901 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4902 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4903 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4904 | .build())); |
| 4905 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4906 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4907 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4908 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4909 | |
| 4910 | window->assertNoEvents(); |
| 4911 | spyWindow->assertNoEvents(); |
| 4912 | } |
| 4913 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4914 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 4915 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 4916 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4917 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4918 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 4919 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4920 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4921 | spyWindow->setTrustedOverlay(true); |
| 4922 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4923 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4924 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4925 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4926 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4927 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4928 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 4929 | |
| 4930 | // Send mouse cursor to the window |
| 4931 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4932 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 4933 | .build()); |
| 4934 | |
| 4935 | // Move mouse cursor |
| 4936 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4937 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 4938 | .build()); |
| 4939 | |
| 4940 | window->consumeMotionEvent( |
| 4941 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4942 | spyWindow->consumeMotionEvent( |
| 4943 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4944 | window->consumeMotionEvent( |
| 4945 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4946 | spyWindow->consumeMotionEvent( |
| 4947 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4948 | // Touch down on the window |
| 4949 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4950 | .deviceId(SECOND_DEVICE_ID) |
| 4951 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
| 4952 | .build()); |
| 4953 | window->consumeMotionEvent( |
| 4954 | AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4955 | spyWindow->consumeMotionEvent( |
| 4956 | AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4957 | |
| 4958 | // pilfer the motion, retaining the gesture on the spy window. |
| 4959 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 4960 | window->consumeMotionEvent( |
| 4961 | AllOf(WithMotionAction(ACTION_CANCEL), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4962 | // Mouse hover is not pilfered |
| 4963 | |
| 4964 | // Touch UP on the window |
| 4965 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4966 | .deviceId(SECOND_DEVICE_ID) |
| 4967 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
| 4968 | .build()); |
| 4969 | spyWindow->consumeMotionEvent( |
| 4970 | AllOf(WithMotionAction(ACTION_UP), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4971 | |
| 4972 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 4973 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 4974 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 4975 | |
| 4976 | // One more tap - DOWN |
| 4977 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4978 | .deviceId(SECOND_DEVICE_ID) |
| 4979 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
| 4980 | .build()); |
| 4981 | window->consumeMotionEvent( |
| 4982 | AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4983 | spyWindow->consumeMotionEvent( |
| 4984 | AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4985 | |
| 4986 | // Touch UP on the window |
| 4987 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4988 | .deviceId(SECOND_DEVICE_ID) |
| 4989 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
| 4990 | .build()); |
| 4991 | window->consumeMotionEvent( |
| 4992 | AllOf(WithMotionAction(ACTION_UP), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4993 | spyWindow->consumeMotionEvent( |
| 4994 | AllOf(WithMotionAction(ACTION_UP), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4995 | |
| 4996 | // Mouse movement continues normally as well |
| 4997 | // Move mouse cursor |
| 4998 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4999 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(120).y(130)) |
| 5000 | .build()); |
| 5001 | window->consumeMotionEvent( |
| 5002 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 5003 | spyWindow->consumeMotionEvent( |
| 5004 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 5005 | |
| 5006 | window->assertNoEvents(); |
| 5007 | spyWindow->assertNoEvents(); |
| 5008 | } |
| 5009 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5010 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 5011 | // directly in this test. |
| 5012 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5013 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5014 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5015 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5016 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5017 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5018 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5019 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5020 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5021 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5022 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5023 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5024 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 5025 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 5026 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5027 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 5028 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5029 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5030 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5031 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5032 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 5033 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 5034 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5035 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 5036 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 5037 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5038 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5039 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5040 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5041 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 5042 | AINPUT_SOURCE_MOUSE) |
| 5043 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 5044 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 5045 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5046 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 5047 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5048 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5049 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5050 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5051 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 5052 | AINPUT_SOURCE_MOUSE) |
| 5053 | .buttonState(0) |
| 5054 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 5055 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5056 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 5057 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5058 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5059 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5060 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5061 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 5062 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 5063 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5064 | .build())); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5065 | window->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5066 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 5067 | // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the |
| 5068 | // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail. |
| 5069 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5070 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5071 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 5072 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 5073 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5074 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 5075 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 5076 | } |
| 5077 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 5078 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 5079 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 5080 | * is generated. |
| 5081 | */ |
| 5082 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 5083 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5084 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5085 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 5086 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 5087 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5088 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 5089 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5090 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 5091 | |
| 5092 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5093 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 5094 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 5095 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5096 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 5097 | .build())); |
| 5098 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 5099 | |
| 5100 | // Remove the window, but keep the channel. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5101 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 5102 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 5103 | } |
| 5104 | |
| 5105 | /** |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 5106 | * Test that invalid HOVER events sent by accessibility do not cause a fatal crash. |
| 5107 | */ |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 5108 | TEST_F_WITH_FLAGS(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash, |
| 5109 | REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(com::android::input::flags, |
| 5110 | a11y_crash_on_inconsistent_event_stream))) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 5111 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5112 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5113 | ui::LogicalDisplayId::DEFAULT); |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 5114 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 5115 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5116 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 5117 | |
| 5118 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 5119 | |
| 5120 | MotionEventBuilder hoverEnterBuilder = |
| 5121 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 5122 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 5123 | .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 5124 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5125 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 5126 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5127 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 5128 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 5129 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 5130 | } |
| 5131 | |
| 5132 | /** |
Siarhei Vishniakou | 9d0d65e | 2024-08-02 12:10:05 -0700 | [diff] [blame] | 5133 | * Invalid events injected by input filter are rejected. |
| 5134 | */ |
| 5135 | TEST_F(InputDispatcherTest, InvalidA11yEventsGetRejected) { |
| 5136 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5137 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5138 | ui::LogicalDisplayId::DEFAULT); |
| 5139 | |
| 5140 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
| 5141 | |
| 5142 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 5143 | |
| 5144 | // a11y sets 'POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY' policy flag during injection, so define |
| 5145 | // a custom injection function here for convenience. |
| 5146 | auto injectFromAccessibility = [&](int32_t action, float x, float y) { |
| 5147 | MotionEvent event = MotionEventBuilder(action, AINPUT_SOURCE_TOUCHSCREEN) |
| 5148 | .pointer(PointerBuilder(0, ToolType::FINGER).x(x).y(y)) |
| 5149 | .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT) |
| 5150 | .build(); |
| 5151 | return injectMotionEvent(*mDispatcher, event, 100ms, |
| 5152 | InputEventInjectionSync::WAIT_FOR_RESULT, /*targetUid=*/{}, |
| 5153 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_FILTERED | |
| 5154 | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY); |
| 5155 | }; |
| 5156 | |
| 5157 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5158 | injectFromAccessibility(ACTION_DOWN, /*x=*/300, /*y=*/400)); |
| 5159 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5160 | injectFromAccessibility(ACTION_MOVE, /*x=*/310, /*y=*/420)); |
| 5161 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 5162 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 5163 | // finger is still down, so a new DOWN event should be rejected! |
| 5164 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 5165 | injectFromAccessibility(ACTION_DOWN, /*x=*/340, /*y=*/410)); |
| 5166 | |
| 5167 | // if the gesture is correctly finished, new down event will succeed |
| 5168 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5169 | injectFromAccessibility(ACTION_MOVE, /*x=*/320, /*y=*/430)); |
| 5170 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5171 | injectFromAccessibility(ACTION_UP, /*x=*/320, /*y=*/430)); |
| 5172 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 5173 | window->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 5174 | |
| 5175 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5176 | injectFromAccessibility(ACTION_DOWN, /*x=*/350, /*y=*/460)); |
| 5177 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 5178 | } |
| 5179 | |
| 5180 | /** |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 5181 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. |
| 5182 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 5183 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover_legacy) { |
| 5184 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 5185 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5186 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5187 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 5188 | window->setFrame(Rect(0, 0, 100, 100)); |
| 5189 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5190 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 5191 | |
| 5192 | const int32_t mouseDeviceId = 7; |
| 5193 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 5194 | |
| 5195 | // Start hovering with the mouse |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5196 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 5197 | .deviceId(mouseDeviceId) |
| 5198 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 5199 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 5200 | window->consumeMotionEvent( |
| 5201 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 5202 | |
| 5203 | // Touch goes down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5204 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5205 | .deviceId(touchDeviceId) |
| 5206 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5207 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 5208 | |
| 5209 | window->consumeMotionEvent( |
| 5210 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 5211 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 5212 | } |
| 5213 | |
| 5214 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 5215 | * If mouse is hovering when the touch goes down, the hovering should not be stopped. |
| 5216 | */ |
| 5217 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { |
| 5218 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 5219 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5220 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5221 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 5222 | window->setFrame(Rect(0, 0, 100, 100)); |
| 5223 | |
| 5224 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 5225 | |
| 5226 | const int32_t mouseDeviceId = 7; |
| 5227 | const int32_t touchDeviceId = 4; |
| 5228 | |
| 5229 | // Start hovering with the mouse |
| 5230 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 5231 | .deviceId(mouseDeviceId) |
| 5232 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 5233 | .build()); |
| 5234 | window->consumeMotionEvent( |
| 5235 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 5236 | |
| 5237 | // Touch goes down |
| 5238 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5239 | .deviceId(touchDeviceId) |
| 5240 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5241 | .build()); |
| 5242 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 5243 | } |
| 5244 | |
| 5245 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 5246 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 5247 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 5248 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 5249 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 5250 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap_legacy) { |
| 5251 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 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 = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5254 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 5255 | window->setFrame(Rect(0, 0, 100, 100)); |
| 5256 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5257 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 5258 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 5259 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 5260 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 5261 | ASSERT_NO_FATAL_FAILURE( |
| 5262 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 5263 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 5264 | |
| 5265 | // Tap on the window |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 5266 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5267 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 5268 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 5269 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 5270 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 5271 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 5272 | |
| 5273 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 5274 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 5275 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 5276 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 5277 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 5278 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 5279 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 5280 | ASSERT_NO_FATAL_FAILURE( |
| 5281 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 5282 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 5283 | } |
| 5284 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 5285 | /** |
| 5286 | * Send a mouse hover event followed by a tap from touchscreen. |
| 5287 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 5288 | * stream's source has been switched. |
| 5289 | */ |
| 5290 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 5291 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 5292 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5293 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5294 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 5295 | window->setFrame(Rect(0, 0, 100, 100)); |
| 5296 | |
| 5297 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 5298 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 5299 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 5300 | .build()); |
| 5301 | |
| 5302 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 5303 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 5304 | |
| 5305 | // Tap on the window |
| 5306 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5307 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 5308 | .build()); |
| 5309 | |
| 5310 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 5311 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 5312 | |
| 5313 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 5314 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 5315 | |
| 5316 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 5317 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 5318 | .build()); |
| 5319 | |
| 5320 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 5321 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 5322 | } |
| 5323 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5324 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 5325 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5326 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 5327 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5328 | ui::LogicalDisplayId::DEFAULT); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5329 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 5330 | sp<FakeWindowHandle> windowSecondDisplay = |
| 5331 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 5332 | SECOND_DISPLAY_ID); |
| 5333 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 5334 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5335 | mDispatcher->onWindowInfosChanged( |
| 5336 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5337 | |
| 5338 | // Set cursor position in window in default display and check that hover enter and move |
| 5339 | // events are generated. |
| 5340 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5341 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5342 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 5343 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5344 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5345 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5346 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 5347 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5348 | |
| 5349 | // Remove all windows in secondary display and check that no event happens on window in |
| 5350 | // primary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5351 | mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0}); |
| 5352 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5353 | windowDefaultDisplay->assertNoEvents(); |
| 5354 | |
| 5355 | // Move cursor position in window in default display and check that only hover move |
| 5356 | // event is generated and not hover enter event. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5357 | mDispatcher->onWindowInfosChanged( |
| 5358 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5359 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5360 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5361 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 5362 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5363 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5364 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5365 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 5366 | windowDefaultDisplay->consumeMotionEvent( |
| 5367 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 5368 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5369 | windowDefaultDisplay->assertNoEvents(); |
| 5370 | } |
| 5371 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 5372 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5373 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 5374 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5375 | sp<FakeWindowHandle> windowLeft = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 5376 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 5377 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5378 | sp<FakeWindowHandle> windowRight = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 5379 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 5380 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 5381 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5382 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 5383 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5384 | mDispatcher->onWindowInfosChanged( |
| 5385 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 5386 | |
| 5387 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 5388 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5389 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5390 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5391 | ui::LogicalDisplayId::DEFAULT, {610, 400}, {599, 400})); |
| 5392 | windowLeft->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 5393 | windowRight->assertNoEvents(); |
| 5394 | } |
| 5395 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5396 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5397 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5398 | sp<FakeWindowHandle> window = |
| 5399 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5400 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5401 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5402 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5403 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5404 | setFocusedWindow(window); |
| 5405 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5406 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5407 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5408 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5409 | |
| 5410 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5411 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5412 | |
| 5413 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 5414 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5415 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5416 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT, AKEY_EVENT_FLAG_CANCELED); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5417 | } |
| 5418 | |
| 5419 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5420 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5421 | sp<FakeWindowHandle> window = |
| 5422 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5423 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5424 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5425 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5426 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5427 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5428 | AINPUT_SOURCE_TOUCHSCREEN, |
| 5429 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5430 | |
| 5431 | // Window should receive motion down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5432 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5433 | |
| 5434 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 5435 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5436 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 5437 | window->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5438 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5439 | } |
| 5440 | |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 5441 | TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) { |
| 5442 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5443 | sp<FakeWindowHandle> window = |
| 5444 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5445 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 5446 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5447 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 5448 | |
| 5449 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 5450 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10)) |
| 5451 | .build()); |
| 5452 | |
| 5453 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 5454 | |
| 5455 | // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT |
| 5456 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
| 5457 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 5458 | |
| 5459 | // After the device has been reset, a new hovering stream can be sent to the window |
| 5460 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 5461 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15)) |
| 5462 | .build()); |
| 5463 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 5464 | } |
| 5465 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5466 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 5467 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5468 | sp<FakeWindowHandle> window = |
| 5469 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5470 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5471 | window->setFocusable(true); |
| 5472 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5473 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5474 | setFocusedWindow(window); |
| 5475 | |
| 5476 | window->consumeFocusEvent(true); |
| 5477 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5478 | const NotifyKeyArgs keyArgs = |
| 5479 | generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5480 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 5481 | const nsecs_t injectTime = keyArgs.eventTime; |
| 5482 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5483 | mDispatcher->notifyKey(keyArgs); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5484 | // 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] | 5485 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5486 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 5487 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 5488 | } |
| 5489 | |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 5490 | /** |
| 5491 | * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set. |
| 5492 | */ |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5493 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 5494 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5495 | sp<FakeWindowHandle> window = |
| 5496 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5497 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5498 | window->setFocusable(true); |
| 5499 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5500 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5501 | setFocusedWindow(window); |
| 5502 | |
| 5503 | window->consumeFocusEvent(true); |
| 5504 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5505 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
| 5506 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 5507 | |
| 5508 | // Set a value that's significantly larger than the default consumption timeout. If the |
| 5509 | // implementation is correct, the actual value doesn't matter; it won't slow down the test. |
| 5510 | mFakePolicy->setInterceptKeyTimeout(600ms); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5511 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 5512 | // Window should receive key event immediately when same key up. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5513 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5514 | } |
| 5515 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5516 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5517 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 5518 | * WATCH_OUTSIDE_TOUCH. |
| 5519 | * Both windows are owned by the same UID. |
| 5520 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 5521 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 5522 | */ |
| 5523 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 5524 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5525 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5526 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5527 | window->setFrame(Rect{0, 0, 100, 100}); |
| 5528 | |
| 5529 | sp<FakeWindowHandle> outsideWindow = |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 5530 | sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5531 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5532 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 5533 | outsideWindow->setWatchOutsideTouch(true); |
| 5534 | // 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] | 5535 | mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5536 | |
| 5537 | // Tap on first window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5538 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5539 | AINPUT_SOURCE_TOUCHSCREEN, |
| 5540 | ui::LogicalDisplayId::DEFAULT, {PointF{50, 50}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5541 | window->consumeMotionDown(); |
| 5542 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 5543 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 5544 | outsideWindow->consumeMotionEvent( |
| 5545 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 5546 | |
| 5547 | // Ensure outsideWindow doesn't get any more events for the gesture. |
| 5548 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5549 | ui::LogicalDisplayId::DEFAULT, {PointF{51, 51}})); |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 5550 | window->consumeMotionMove(); |
| 5551 | outsideWindow->assertNoEvents(); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5552 | } |
| 5553 | |
| 5554 | /** |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 5555 | * Three windows: |
| 5556 | * - Left window |
| 5557 | * - Right window |
| 5558 | * - Outside window(watch for ACTION_OUTSIDE events) |
| 5559 | * The windows "left" and "outside" share the same owner, the window "right" has a different owner, |
| 5560 | * In order to allow the outside window can receive the ACTION_OUTSIDE events, the outside window is |
| 5561 | * positioned above the "left" and "right" windows, and it doesn't overlap with them. |
| 5562 | * |
| 5563 | * First, device A report a down event landed in the right window, the outside window can receive |
| 5564 | * an ACTION_OUTSIDE event that with zeroed coordinates, the device B report a down event landed |
| 5565 | * in the left window, the outside window can receive an ACTION_OUTSIDE event the with valid |
| 5566 | * coordinates, after these, device A and device B continue report MOVE event, the right and left |
| 5567 | * window can receive it, but outside window event can't receive it. |
| 5568 | */ |
| 5569 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinatesWhenMultiDevice) { |
| 5570 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5571 | sp<FakeWindowHandle> leftWindow = |
| 5572 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5573 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 5574 | leftWindow->setFrame(Rect{0, 0, 100, 100}); |
| 5575 | leftWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
| 5576 | |
| 5577 | sp<FakeWindowHandle> outsideWindow = |
| 5578 | sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5579 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 5580 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 5581 | outsideWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
| 5582 | outsideWindow->setWatchOutsideTouch(true); |
| 5583 | |
| 5584 | std::shared_ptr<FakeApplicationHandle> anotherApplication = |
| 5585 | std::make_shared<FakeApplicationHandle>(); |
| 5586 | sp<FakeWindowHandle> rightWindow = |
| 5587 | sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Right Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5588 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 5589 | rightWindow->setFrame(Rect{100, 0, 200, 100}); |
| 5590 | rightWindow->setOwnerInfo(gui::Pid{2}, gui::Uid{202}); |
| 5591 | |
| 5592 | // OutsideWindow must be above left window and right window to receive ACTION_OUTSIDE events |
| 5593 | // when left window or right window is tapped |
| 5594 | mDispatcher->onWindowInfosChanged( |
| 5595 | {{*outsideWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, |
| 5596 | {}, |
| 5597 | 0, |
| 5598 | 0}); |
| 5599 | |
| 5600 | const DeviceId deviceA = 9; |
| 5601 | const DeviceId deviceB = 3; |
| 5602 | |
| 5603 | // Tap on right window use device A |
| 5604 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5605 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 5606 | .deviceId(deviceA) |
| 5607 | .build()); |
| 5608 | leftWindow->assertNoEvents(); |
| 5609 | rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 5610 | // Right window is belonged to another owner, so outsideWindow should receive ACTION_OUTSIDE |
| 5611 | // with zeroed coords. |
| 5612 | outsideWindow->consumeMotionEvent( |
| 5613 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithDeviceId(deviceA), WithCoords(0, 0))); |
| 5614 | |
| 5615 | // Tap on left window use device B |
| 5616 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5617 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5618 | .deviceId(deviceB) |
| 5619 | .build()); |
| 5620 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 5621 | rightWindow->assertNoEvents(); |
| 5622 | // Because new gesture down on the left window that has the same owner with outside Window, the |
| 5623 | // outside Window should receive the ACTION_OUTSIDE with coords. |
| 5624 | outsideWindow->consumeMotionEvent( |
| 5625 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithDeviceId(deviceB), WithCoords(-50, -50))); |
| 5626 | |
| 5627 | // Ensure that windows that can only accept outside do not receive remaining gestures |
| 5628 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5629 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(51)) |
| 5630 | .deviceId(deviceA) |
| 5631 | .build()); |
| 5632 | leftWindow->assertNoEvents(); |
| 5633 | rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceA))); |
| 5634 | |
| 5635 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5636 | .pointer(PointerBuilder(0, ToolType::FINGER).x(51).y(51)) |
| 5637 | .deviceId(deviceB) |
| 5638 | .build()); |
| 5639 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB))); |
| 5640 | rightWindow->assertNoEvents(); |
| 5641 | outsideWindow->assertNoEvents(); |
| 5642 | } |
| 5643 | |
| 5644 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5645 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 5646 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 5647 | * ACTION_OUTSIDE event is sent per gesture. |
| 5648 | */ |
| 5649 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 5650 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 5651 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5652 | sp<FakeWindowHandle> window = |
| 5653 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5654 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5655 | window->setWatchOutsideTouch(true); |
| 5656 | window->setFrame(Rect{0, 0, 100, 100}); |
| 5657 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5658 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5659 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5660 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 5661 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5662 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5663 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5664 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5665 | mDispatcher->onWindowInfosChanged( |
| 5666 | {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5667 | |
| 5668 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5669 | mDispatcher->notifyMotion( |
| 5670 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5671 | ui::LogicalDisplayId::DEFAULT, {PointF{-10, -10}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5672 | window->assertNoEvents(); |
| 5673 | secondWindow->assertNoEvents(); |
| 5674 | |
| 5675 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 5676 | // Now, `window` should get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5677 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5678 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5679 | {PointF{-10, -10}, PointF{105, 105}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5680 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 5681 | window->consumeMotionEvent( |
| 5682 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5683 | secondWindow->consumeMotionDown(); |
| 5684 | thirdWindow->assertNoEvents(); |
| 5685 | |
| 5686 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 5687 | // 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] | 5688 | mDispatcher->notifyMotion( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5689 | generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5690 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5691 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5692 | window->assertNoEvents(); |
| 5693 | secondWindow->consumeMotionMove(); |
| 5694 | thirdWindow->consumeMotionDown(); |
| 5695 | } |
| 5696 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5697 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 5698 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5699 | sp<FakeWindowHandle> window = |
| 5700 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5701 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5702 | window->setFocusable(true); |
| 5703 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5704 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5705 | setFocusedWindow(window); |
| 5706 | |
| 5707 | window->consumeFocusEvent(true); |
| 5708 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5709 | const NotifyKeyArgs keyDown = |
| 5710 | generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT); |
| 5711 | const NotifyKeyArgs keyUp = |
| 5712 | generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5713 | mDispatcher->notifyKey(keyDown); |
| 5714 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5715 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5716 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
| 5717 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5718 | |
| 5719 | // 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] | 5720 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5721 | |
| 5722 | window->consumeFocusEvent(false); |
| 5723 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5724 | mDispatcher->notifyKey(keyDown); |
| 5725 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5726 | window->assertNoEvents(); |
| 5727 | } |
| 5728 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5729 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
Siarhei Vishniakou | a3fe664 | 2024-07-18 11:58:25 -0700 | [diff] [blame] | 5730 | SCOPED_FLAG_OVERRIDE(split_all_touches, false); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5731 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5732 | sp<FakeWindowHandle> window = |
| 5733 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5734 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5735 | // Ensure window is non-split and have some transform. |
| 5736 | window->setPreventSplitting(true); |
| 5737 | window->setWindowOffset(20, 40); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5738 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5739 | |
| 5740 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5741 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 5742 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5743 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5744 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5745 | |
| 5746 | const MotionEvent secondFingerDownEvent = |
| 5747 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5748 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5749 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 5750 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 5751 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5752 | .build(); |
| 5753 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5754 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5755 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 5756 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5757 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5758 | std::unique_ptr<MotionEvent> event = window->consumeMotionEvent(); |
| 5759 | ASSERT_NE(nullptr, event); |
| 5760 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 5761 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 5762 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 5763 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 5764 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5765 | } |
| 5766 | |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 5767 | /** |
| 5768 | * Two windows: a splittable and a non-splittable. |
| 5769 | * The non-splittable window shouldn't receive any "incomplete" gestures. |
| 5770 | * Send the first pointer to the splittable window, and then touch the non-splittable window. |
| 5771 | * The second pointer should be dropped because the initial window is splittable, so it won't get |
| 5772 | * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any |
| 5773 | * "incomplete" gestures. |
| 5774 | */ |
| 5775 | TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) { |
Siarhei Vishniakou | a3fe664 | 2024-07-18 11:58:25 -0700 | [diff] [blame] | 5776 | SCOPED_FLAG_OVERRIDE(split_all_touches, false); |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 5777 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5778 | sp<FakeWindowHandle> leftWindow = |
| 5779 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5780 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 5781 | leftWindow->setPreventSplitting(false); |
| 5782 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5783 | sp<FakeWindowHandle> rightWindow = |
| 5784 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5785 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 5786 | rightWindow->setPreventSplitting(true); |
| 5787 | rightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 5788 | mDispatcher->onWindowInfosChanged( |
| 5789 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 5790 | |
| 5791 | // Touch down on left, splittable window |
| 5792 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5793 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5794 | .build()); |
| 5795 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 5796 | |
| 5797 | mDispatcher->notifyMotion( |
| 5798 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5799 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 5800 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
| 5801 | .build()); |
| 5802 | leftWindow->assertNoEvents(); |
| 5803 | rightWindow->assertNoEvents(); |
| 5804 | } |
| 5805 | |
Siarhei Vishniakou | 27fc9f1 | 2024-07-13 23:59:47 -0700 | [diff] [blame] | 5806 | /** |
Siarhei Vishniakou | fe34698 | 2024-08-07 00:17:19 +0000 | [diff] [blame] | 5807 | * Three windows: |
| 5808 | * 1) A window on the left, with flag dup_to_wallpaper |
| 5809 | * 2) A window on the right, with flag slippery |
| 5810 | * 3) A wallpaper window under the left window |
| 5811 | * When touch slips from right window to left, the wallpaper should receive a similar slippery |
| 5812 | * enter event. Later on, when another device becomes active, the wallpaper should receive |
| 5813 | * consistent streams from the new device, and also from the old device. |
| 5814 | * This test attempts to reproduce a crash in the dispatcher where the wallpaper target's downTime |
| 5815 | * was not getting set during slippery entrance. |
| 5816 | */ |
| 5817 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlipperyAndMultiWindowMultiTouch) { |
| 5818 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 5819 | std::shared_ptr<FakeApplicationHandle> application1 = std::make_shared<FakeApplicationHandle>(); |
| 5820 | std::shared_ptr<FakeApplicationHandle> application2 = std::make_shared<FakeApplicationHandle>(); |
| 5821 | std::shared_ptr<FakeApplicationHandle> application3 = std::make_shared<FakeApplicationHandle>(); |
| 5822 | sp<FakeWindowHandle> wallpaper = |
| 5823 | sp<FakeWindowHandle>::make(application1, mDispatcher, "wallpaper", |
| 5824 | ui::LogicalDisplayId::DEFAULT); |
| 5825 | wallpaper->setIsWallpaper(true); |
| 5826 | wallpaper->setPreventSplitting(true); |
| 5827 | wallpaper->setTouchable(false); |
| 5828 | |
| 5829 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application2, mDispatcher, "Left", |
| 5830 | ui::LogicalDisplayId::DEFAULT); |
| 5831 | leftWindow->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 5832 | leftWindow->setDupTouchToWallpaper(true); |
| 5833 | |
| 5834 | sp<FakeWindowHandle> rightWindow = |
| 5835 | sp<FakeWindowHandle>::make(application3, mDispatcher, "Right", |
| 5836 | ui::LogicalDisplayId::DEFAULT); |
| 5837 | rightWindow->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 5838 | rightWindow->setSlippery(true); |
| 5839 | rightWindow->setWatchOutsideTouch(true); |
| 5840 | rightWindow->setTrustedOverlay(true); |
| 5841 | |
| 5842 | mDispatcher->onWindowInfosChanged( |
| 5843 | {{*rightWindow->getInfo(), *leftWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0}); |
| 5844 | |
| 5845 | const DeviceId deviceA = 3; |
| 5846 | const DeviceId deviceB = 9; |
| 5847 | |
| 5848 | // First finger from device A into right window |
| 5849 | NotifyMotionArgs deviceADownArgs = |
| 5850 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5851 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 5852 | .deviceId(deviceA) |
| 5853 | .build(); |
| 5854 | |
| 5855 | mDispatcher->notifyMotion(deviceADownArgs); |
| 5856 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 5857 | |
| 5858 | // Move the finger of device A from right window into left window. It should slip. |
| 5859 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5860 | .pointer(PointerBuilder(0, ToolType::FINGER).x(80).y(50)) |
| 5861 | .deviceId(deviceA) |
| 5862 | .downTime(deviceADownArgs.downTime) |
| 5863 | .build()); |
| 5864 | |
| 5865 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 5866 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 5867 | wallpaper->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 5868 | |
| 5869 | // Finger from device B down into left window |
| 5870 | NotifyMotionArgs deviceBDownArgs = |
| 5871 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5872 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40)) |
| 5873 | .deviceId(deviceB) |
| 5874 | .build(); |
| 5875 | mDispatcher->notifyMotion(deviceBDownArgs); |
| 5876 | leftWindow->consumeMotionEvent(AllOf(WithDeviceId(deviceB), WithMotionAction(ACTION_DOWN))); |
| 5877 | wallpaper->consumeMotionEvent(AllOf(WithDeviceId(deviceB), WithMotionAction(ACTION_DOWN))); |
| 5878 | |
| 5879 | rightWindow->consumeMotionEvent(AllOf(WithDeviceId(deviceB), WithMotionAction(ACTION_OUTSIDE))); |
| 5880 | |
| 5881 | // Move finger from device B, still keeping it in the left window |
| 5882 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5883 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(50)) |
| 5884 | .deviceId(deviceB) |
| 5885 | .downTime(deviceBDownArgs.downTime) |
| 5886 | .build()); |
| 5887 | leftWindow->consumeMotionEvent(AllOf(WithDeviceId(deviceB), WithMotionAction(ACTION_MOVE))); |
| 5888 | wallpaper->consumeMotionEvent(AllOf(WithDeviceId(deviceB), WithMotionAction(ACTION_MOVE))); |
| 5889 | |
| 5890 | // Lift the finger from device B |
| 5891 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 5892 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(50)) |
| 5893 | .deviceId(deviceB) |
| 5894 | .downTime(deviceBDownArgs.downTime) |
| 5895 | .build()); |
| 5896 | leftWindow->consumeMotionEvent(AllOf(WithDeviceId(deviceB), WithMotionAction(ACTION_UP))); |
| 5897 | wallpaper->consumeMotionEvent(AllOf(WithDeviceId(deviceB), WithMotionAction(ACTION_UP))); |
| 5898 | |
| 5899 | // Move the finger of device A, keeping it in the left window |
| 5900 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5901 | .pointer(PointerBuilder(0, ToolType::FINGER).x(70).y(50)) |
| 5902 | .deviceId(deviceA) |
| 5903 | .downTime(deviceADownArgs.downTime) |
| 5904 | .build()); |
| 5905 | |
| 5906 | leftWindow->consumeMotionEvent(AllOf(WithDeviceId(deviceA), WithMotionAction(ACTION_MOVE))); |
| 5907 | wallpaper->consumeMotionEvent(AllOf(WithDeviceId(deviceA), WithMotionAction(ACTION_MOVE))); |
| 5908 | |
| 5909 | // Second finger down from device A, into the right window. It should be split into: |
| 5910 | // MOVE for the left window (due to existing implementation) + a DOWN into the right window |
| 5911 | // Wallpaper will not receive this new pointer, and it will only get the MOVE event. |
| 5912 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5913 | .pointer(PointerBuilder(0, ToolType::FINGER).x(70).y(50)) |
| 5914 | .pointer(PointerBuilder(1, ToolType::FINGER).x(140).y(50)) |
| 5915 | .deviceId(deviceA) |
| 5916 | .downTime(deviceADownArgs.downTime) |
| 5917 | .build()); |
| 5918 | auto firstFingerMoveFromDeviceA = AllOf(WithDeviceId(deviceA), WithMotionAction(ACTION_MOVE), |
| 5919 | WithPointerCount(1), WithPointerId(0, 0)); |
| 5920 | leftWindow->consumeMotionEvent(firstFingerMoveFromDeviceA); |
| 5921 | wallpaper->consumeMotionEvent(firstFingerMoveFromDeviceA); |
| 5922 | rightWindow->consumeMotionEvent( |
| 5923 | AllOf(WithDeviceId(deviceA), WithMotionAction(ACTION_DOWN), WithPointerId(0, 1))); |
| 5924 | |
| 5925 | // Lift up the second finger. |
| 5926 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 5927 | .pointer(PointerBuilder(0, ToolType::FINGER).x(70).y(50)) |
| 5928 | .pointer(PointerBuilder(1, ToolType::FINGER).x(140).y(50)) |
| 5929 | .deviceId(deviceA) |
| 5930 | .downTime(deviceADownArgs.downTime) |
| 5931 | .build()); |
| 5932 | |
| 5933 | rightWindow->consumeMotionEvent(AllOf(WithDeviceId(deviceA), WithMotionAction(ACTION_UP))); |
| 5934 | leftWindow->consumeMotionEvent(firstFingerMoveFromDeviceA); |
| 5935 | wallpaper->consumeMotionEvent(firstFingerMoveFromDeviceA); |
| 5936 | |
| 5937 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 5938 | .pointer(PointerBuilder(0, ToolType::FINGER).x(70).y(50)) |
| 5939 | .deviceId(deviceA) |
| 5940 | .downTime(deviceADownArgs.downTime) |
| 5941 | .build()); |
| 5942 | |
| 5943 | leftWindow->consumeMotionEvent(AllOf(WithDeviceId(deviceA), WithMotionAction(ACTION_UP))); |
| 5944 | wallpaper->consumeMotionEvent(AllOf(WithDeviceId(deviceA), WithMotionAction(ACTION_UP))); |
| 5945 | rightWindow->assertNoEvents(); |
| 5946 | } |
| 5947 | |
| 5948 | /** |
| 5949 | * Same test as above, but with enable_multi_device_same_window_stream flag set to false. |
| 5950 | */ |
| 5951 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlipperyAndMultiWindowMultiTouch_legacy) { |
| 5952 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
| 5953 | std::shared_ptr<FakeApplicationHandle> application1 = std::make_shared<FakeApplicationHandle>(); |
| 5954 | std::shared_ptr<FakeApplicationHandle> application2 = std::make_shared<FakeApplicationHandle>(); |
| 5955 | std::shared_ptr<FakeApplicationHandle> application3 = std::make_shared<FakeApplicationHandle>(); |
| 5956 | sp<FakeWindowHandle> wallpaper = |
| 5957 | sp<FakeWindowHandle>::make(application1, mDispatcher, "wallpaper", |
| 5958 | ui::LogicalDisplayId::DEFAULT); |
| 5959 | wallpaper->setIsWallpaper(true); |
| 5960 | wallpaper->setPreventSplitting(true); |
| 5961 | wallpaper->setTouchable(false); |
| 5962 | |
| 5963 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application2, mDispatcher, "Left", |
| 5964 | ui::LogicalDisplayId::DEFAULT); |
| 5965 | leftWindow->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 5966 | leftWindow->setDupTouchToWallpaper(true); |
| 5967 | |
| 5968 | sp<FakeWindowHandle> rightWindow = |
| 5969 | sp<FakeWindowHandle>::make(application3, mDispatcher, "Right", |
| 5970 | ui::LogicalDisplayId::DEFAULT); |
| 5971 | rightWindow->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 5972 | rightWindow->setSlippery(true); |
| 5973 | rightWindow->setWatchOutsideTouch(true); |
| 5974 | rightWindow->setTrustedOverlay(true); |
| 5975 | |
| 5976 | mDispatcher->onWindowInfosChanged( |
| 5977 | {{*rightWindow->getInfo(), *leftWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0}); |
| 5978 | |
| 5979 | const DeviceId deviceA = 3; |
| 5980 | const DeviceId deviceB = 9; |
| 5981 | |
| 5982 | // First finger from device A into right window |
| 5983 | NotifyMotionArgs deviceADownArgs = |
| 5984 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5985 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 5986 | .deviceId(deviceA) |
| 5987 | .build(); |
| 5988 | |
| 5989 | mDispatcher->notifyMotion(deviceADownArgs); |
| 5990 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 5991 | |
| 5992 | // Move the finger of device A from right window into left window. It should slip. |
| 5993 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5994 | .pointer(PointerBuilder(0, ToolType::FINGER).x(80).y(50)) |
| 5995 | .deviceId(deviceA) |
| 5996 | .downTime(deviceADownArgs.downTime) |
| 5997 | .build()); |
| 5998 | |
| 5999 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 6000 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 6001 | wallpaper->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 6002 | |
| 6003 | // Finger from device B down into left window |
| 6004 | NotifyMotionArgs deviceBDownArgs = |
| 6005 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6006 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40)) |
| 6007 | .deviceId(deviceB) |
| 6008 | .build(); |
| 6009 | mDispatcher->notifyMotion(deviceBDownArgs); |
| 6010 | leftWindow->consumeMotionEvent(AllOf(WithDeviceId(deviceA), WithMotionAction(ACTION_CANCEL))); |
| 6011 | leftWindow->consumeMotionEvent(AllOf(WithDeviceId(deviceB), WithMotionAction(ACTION_DOWN))); |
| 6012 | wallpaper->consumeMotionEvent(AllOf(WithDeviceId(deviceA), WithMotionAction(ACTION_CANCEL))); |
| 6013 | wallpaper->consumeMotionEvent(AllOf(WithDeviceId(deviceB), WithMotionAction(ACTION_DOWN))); |
| 6014 | |
| 6015 | rightWindow->consumeMotionEvent(AllOf(WithDeviceId(deviceB), WithMotionAction(ACTION_OUTSIDE))); |
| 6016 | |
| 6017 | // Move finger from device B, still keeping it in the left window |
| 6018 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6019 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(50)) |
| 6020 | .deviceId(deviceB) |
| 6021 | .downTime(deviceBDownArgs.downTime) |
| 6022 | .build()); |
| 6023 | leftWindow->consumeMotionEvent(AllOf(WithDeviceId(deviceB), WithMotionAction(ACTION_MOVE))); |
| 6024 | wallpaper->consumeMotionEvent(AllOf(WithDeviceId(deviceB), WithMotionAction(ACTION_MOVE))); |
| 6025 | |
| 6026 | // Lift the finger from device B |
| 6027 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6028 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(50)) |
| 6029 | .deviceId(deviceB) |
| 6030 | .downTime(deviceBDownArgs.downTime) |
| 6031 | .build()); |
| 6032 | leftWindow->consumeMotionEvent(AllOf(WithDeviceId(deviceB), WithMotionAction(ACTION_UP))); |
| 6033 | wallpaper->consumeMotionEvent(AllOf(WithDeviceId(deviceB), WithMotionAction(ACTION_UP))); |
| 6034 | |
| 6035 | // Move the finger of device A, keeping it in the left window |
| 6036 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6037 | .pointer(PointerBuilder(0, ToolType::FINGER).x(70).y(50)) |
| 6038 | .deviceId(deviceA) |
| 6039 | .downTime(deviceADownArgs.downTime) |
| 6040 | .build()); |
| 6041 | // This device was already canceled, so MOVE events will not be arriving to the windows from it. |
| 6042 | |
| 6043 | // Second finger down from device A, into the right window. It should be split into: |
| 6044 | // MOVE for the left window (due to existing implementation) + a DOWN into the right window |
| 6045 | // Wallpaper will not receive this new pointer, and it will only get the MOVE event. |
| 6046 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6047 | .pointer(PointerBuilder(0, ToolType::FINGER).x(70).y(50)) |
| 6048 | .pointer(PointerBuilder(1, ToolType::FINGER).x(140).y(50)) |
| 6049 | .deviceId(deviceA) |
| 6050 | .downTime(deviceADownArgs.downTime) |
| 6051 | .build()); |
| 6052 | rightWindow->consumeMotionEvent( |
| 6053 | AllOf(WithDeviceId(deviceA), WithMotionAction(ACTION_DOWN), WithPointerId(0, 1))); |
| 6054 | |
| 6055 | // Lift up the second finger. |
| 6056 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6057 | .pointer(PointerBuilder(0, ToolType::FINGER).x(70).y(50)) |
| 6058 | .pointer(PointerBuilder(1, ToolType::FINGER).x(140).y(50)) |
| 6059 | .deviceId(deviceA) |
| 6060 | .downTime(deviceADownArgs.downTime) |
| 6061 | .build()); |
| 6062 | |
| 6063 | rightWindow->consumeMotionEvent(AllOf(WithDeviceId(deviceA), WithMotionAction(ACTION_UP))); |
| 6064 | |
| 6065 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6066 | .pointer(PointerBuilder(0, ToolType::FINGER).x(70).y(50)) |
| 6067 | .deviceId(deviceA) |
| 6068 | .downTime(deviceADownArgs.downTime) |
| 6069 | .build()); |
| 6070 | rightWindow->assertNoEvents(); |
| 6071 | } |
| 6072 | |
| 6073 | /** |
Siarhei Vishniakou | 27fc9f1 | 2024-07-13 23:59:47 -0700 | [diff] [blame] | 6074 | * Two windows: left and right. The left window has PREVENT_SPLITTING input config. Device A sends a |
| 6075 | * down event to the right window. Device B sends a down event to the left window, and then a |
| 6076 | * POINTER_DOWN event to the right window. However, since the left window prevents splitting, the |
| 6077 | * POINTER_DOWN event should only go to the left window, and not to the right window. |
| 6078 | * This test attempts to reproduce a crash. |
| 6079 | */ |
| 6080 | TEST_F(InputDispatcherTest, MultiDeviceTwoWindowsPreventSplitting) { |
Siarhei Vishniakou | a3fe664 | 2024-07-18 11:58:25 -0700 | [diff] [blame] | 6081 | SCOPED_FLAG_OVERRIDE(split_all_touches, false); |
Siarhei Vishniakou | 27fc9f1 | 2024-07-13 23:59:47 -0700 | [diff] [blame] | 6082 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6083 | sp<FakeWindowHandle> leftWindow = |
| 6084 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window (prevent splitting)", |
| 6085 | ui::LogicalDisplayId::DEFAULT); |
| 6086 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6087 | leftWindow->setPreventSplitting(true); |
| 6088 | |
| 6089 | sp<FakeWindowHandle> rightWindow = |
| 6090 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right window", |
| 6091 | ui::LogicalDisplayId::DEFAULT); |
| 6092 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 6093 | |
| 6094 | mDispatcher->onWindowInfosChanged( |
| 6095 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 6096 | |
| 6097 | const DeviceId deviceA = 9; |
| 6098 | const DeviceId deviceB = 3; |
| 6099 | // Touch the right window with device A |
| 6100 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6101 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 6102 | .deviceId(deviceA) |
| 6103 | .build()); |
| 6104 | rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 6105 | // Touch the left window with device B |
| 6106 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6107 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6108 | .deviceId(deviceB) |
| 6109 | .build()); |
| 6110 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 6111 | // Send a second pointer from device B to the right window. It shouldn't go to the right window |
| 6112 | // because the left window prevents splitting. |
| 6113 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6114 | .deviceId(deviceB) |
| 6115 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6116 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 6117 | .build()); |
| 6118 | leftWindow->consumeMotionPointerDown(1, WithDeviceId(deviceB)); |
| 6119 | |
| 6120 | // Finish the gesture for both devices |
| 6121 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6122 | .deviceId(deviceB) |
| 6123 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6124 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 6125 | .build()); |
| 6126 | leftWindow->consumeMotionPointerUp(1, WithDeviceId(deviceB)); |
| 6127 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6128 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6129 | .deviceId(deviceB) |
| 6130 | .build()); |
| 6131 | leftWindow->consumeMotionEvent( |
| 6132 | AllOf(WithMotionAction(ACTION_UP), WithDeviceId(deviceB), WithPointerId(0, 0))); |
| 6133 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6134 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 6135 | .deviceId(deviceA) |
| 6136 | .build()); |
| 6137 | rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDeviceId(deviceA))); |
| 6138 | } |
| 6139 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 6140 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) { |
| 6141 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6142 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 6143 | ui::LogicalDisplayId::DEFAULT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 6144 | window->setFrame(Rect(0, 0, 400, 400)); |
| 6145 | sp<FakeWindowHandle> trustedOverlay = |
| 6146 | sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6147 | ui::LogicalDisplayId::DEFAULT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 6148 | trustedOverlay->setSpy(true); |
| 6149 | trustedOverlay->setTrustedOverlay(true); |
| 6150 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6151 | mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 6152 | |
| 6153 | // Start a three-finger touchpad swipe |
| 6154 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 6155 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 6156 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 6157 | .build()); |
| 6158 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 6159 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 6160 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 6161 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 6162 | .build()); |
| 6163 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 6164 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 6165 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 6166 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 6167 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 6168 | .build()); |
| 6169 | |
| 6170 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 6171 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 6172 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN)); |
| 6173 | |
| 6174 | // Move the swipe a bit |
| 6175 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 6176 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 6177 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 6178 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 6179 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 6180 | .build()); |
| 6181 | |
| 6182 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 6183 | |
| 6184 | // End the swipe |
| 6185 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 6186 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 6187 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 6188 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 6189 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 6190 | .build()); |
| 6191 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 6192 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 6193 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 6194 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 6195 | .build()); |
| 6196 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 6197 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 6198 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 6199 | .build()); |
| 6200 | |
| 6201 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP)); |
| 6202 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 6203 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 6204 | |
| 6205 | window->assertNoEvents(); |
| 6206 | } |
| 6207 | |
| 6208 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) { |
| 6209 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6210 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 6211 | ui::LogicalDisplayId::DEFAULT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 6212 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6213 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 6214 | |
| 6215 | // Start a three-finger touchpad swipe |
| 6216 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 6217 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 6218 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 6219 | .build()); |
| 6220 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 6221 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 6222 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 6223 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 6224 | .build()); |
| 6225 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 6226 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 6227 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 6228 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 6229 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 6230 | .build()); |
| 6231 | |
| 6232 | // Move the swipe a bit |
| 6233 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 6234 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 6235 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 6236 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 6237 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 6238 | .build()); |
| 6239 | |
| 6240 | // End the swipe |
| 6241 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 6242 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 6243 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 6244 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 6245 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 6246 | .build()); |
| 6247 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 6248 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 6249 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 6250 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 6251 | .build()); |
| 6252 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 6253 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 6254 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 6255 | .build()); |
| 6256 | |
| 6257 | window->assertNoEvents(); |
| 6258 | } |
| 6259 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 6260 | /** |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6261 | * Send a two-pointer gesture to a single window. The window's orientation changes in response to |
| 6262 | * the first pointer. |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 6263 | * 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] | 6264 | */ |
| 6265 | TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) { |
| 6266 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6267 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 6268 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6269 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 6270 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6271 | |
| 6272 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6273 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6274 | .downTime(baseTime + 10) |
| 6275 | .eventTime(baseTime + 10) |
| 6276 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 6277 | .build()); |
| 6278 | |
| 6279 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 6280 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6281 | // Change the transform so that the orientation is now different from original. |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 6282 | window->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6283 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 6284 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6285 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6286 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6287 | .downTime(baseTime + 10) |
| 6288 | .eventTime(baseTime + 30) |
| 6289 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 6290 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 6291 | .build()); |
| 6292 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 6293 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 6294 | |
| 6295 | // 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] | 6296 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6297 | .downTime(baseTime + 10) |
| 6298 | .eventTime(baseTime + 40) |
| 6299 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 6300 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 6301 | .build()); |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 6302 | |
| 6303 | window->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 6304 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6305 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6306 | .downTime(baseTime + 10) |
| 6307 | .eventTime(baseTime + 50) |
| 6308 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 6309 | .build()); |
| 6310 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 6311 | window->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 6312 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6313 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6314 | .downTime(baseTime + 60) |
| 6315 | .eventTime(baseTime + 60) |
| 6316 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40)) |
| 6317 | .build()); |
| 6318 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 6319 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6320 | } |
| 6321 | |
| 6322 | /** |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 6323 | * When there are multiple screens, such as screen projection to TV or screen recording, if the |
| 6324 | * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and |
| 6325 | * its coordinates should be converted by the transform of the windows of target screen. |
| 6326 | */ |
| 6327 | TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) { |
| 6328 | // This case will create a window and a spy window on the default display and mirror |
| 6329 | // window on the second display. cancel event is sent through spy window pilferPointers |
| 6330 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6331 | |
| 6332 | sp<FakeWindowHandle> spyWindowDefaultDisplay = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6333 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 6334 | ui::LogicalDisplayId::DEFAULT); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 6335 | spyWindowDefaultDisplay->setTrustedOverlay(true); |
| 6336 | spyWindowDefaultDisplay->setSpy(true); |
| 6337 | |
| 6338 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 6339 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6340 | ui::LogicalDisplayId::DEFAULT); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 6341 | windowDefaultDisplay->setWindowTransform(1, 0, 0, 1); |
| 6342 | |
| 6343 | sp<FakeWindowHandle> windowSecondDisplay = windowDefaultDisplay->clone(SECOND_DISPLAY_ID); |
| 6344 | windowSecondDisplay->setWindowTransform(2, 0, 0, 2); |
| 6345 | |
| 6346 | // Add the windows to the dispatcher |
| 6347 | mDispatcher->onWindowInfosChanged( |
| 6348 | {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(), |
| 6349 | *windowSecondDisplay->getInfo()}, |
| 6350 | {}, |
| 6351 | 0, |
| 6352 | 0}); |
| 6353 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6354 | // Send down to ui::LogicalDisplayId::DEFAULT |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 6355 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6356 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6357 | ui::LogicalDisplayId::DEFAULT, {100, 100})) |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 6358 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6359 | |
| 6360 | spyWindowDefaultDisplay->consumeMotionDown(); |
| 6361 | windowDefaultDisplay->consumeMotionDown(); |
| 6362 | |
| 6363 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken())); |
| 6364 | |
| 6365 | // windowDefaultDisplay gets cancel |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6366 | std::unique_ptr<MotionEvent> event = windowDefaultDisplay->consumeMotionEvent(); |
| 6367 | ASSERT_NE(nullptr, event); |
| 6368 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event->getAction()); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 6369 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6370 | // The cancel event is sent to windowDefaultDisplay of the ui::LogicalDisplayId::DEFAULT |
| 6371 | // display, so the coordinates of the cancel are converted by windowDefaultDisplay's transform, |
| 6372 | // the x and y coordinates are both 100, otherwise if the cancel event is sent to |
| 6373 | // windowSecondDisplay of SECOND_DISPLAY_ID, the x and y coordinates are 200 |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6374 | EXPECT_EQ(100, event->getX(0)); |
| 6375 | EXPECT_EQ(100, event->getY(0)); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 6376 | } |
| 6377 | |
| 6378 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 6379 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 6380 | * |
| 6381 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 6382 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 6383 | * space. |
| 6384 | */ |
| 6385 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 6386 | public: |
| 6387 | void SetUp() override { |
| 6388 | InputDispatcherTest::SetUp(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6389 | removeAllWindowsAndDisplays(); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 6390 | } |
| 6391 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 6392 | void addDisplayInfo(ui::LogicalDisplayId displayId, const ui::Transform& transform) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 6393 | gui::DisplayInfo info; |
| 6394 | info.displayId = displayId; |
| 6395 | info.transform = transform; |
| 6396 | mDisplayInfos.push_back(std::move(info)); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6397 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 6398 | } |
| 6399 | |
| 6400 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 6401 | mWindowInfos.push_back(*windowHandle->getInfo()); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6402 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 6403 | } |
| 6404 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6405 | void removeAllWindowsAndDisplays() { |
| 6406 | mDisplayInfos.clear(); |
| 6407 | mWindowInfos.clear(); |
| 6408 | } |
| 6409 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 6410 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 6411 | // on the display. |
| 6412 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 6413 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 6414 | // respectively. |
| 6415 | ui::Transform displayTransform; |
| 6416 | displayTransform.set(2, 0, 0, 4); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6417 | addDisplayInfo(ui::LogicalDisplayId::DEFAULT, displayTransform); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 6418 | |
| 6419 | std::shared_ptr<FakeApplicationHandle> application = |
| 6420 | std::make_shared<FakeApplicationHandle>(); |
| 6421 | |
| 6422 | // Add two windows to the display. Their frames are represented in the display space. |
| 6423 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6424 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6425 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 6426 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 6427 | addWindow(firstWindow); |
| 6428 | |
| 6429 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6430 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6431 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 6432 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 6433 | addWindow(secondWindow); |
| 6434 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 6435 | } |
| 6436 | |
| 6437 | private: |
| 6438 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 6439 | std::vector<gui::WindowInfo> mWindowInfos; |
| 6440 | }; |
| 6441 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6442 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 6443 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 6444 | // 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] | 6445 | // selected so that if the hit test was performed with the point and the bounds being in |
| 6446 | // different coordinate spaces, the event would end up in the incorrect window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6447 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6448 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6449 | ui::LogicalDisplayId::DEFAULT, {PointF{75, 55}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 6450 | |
| 6451 | firstWindow->consumeMotionDown(); |
| 6452 | secondWindow->assertNoEvents(); |
| 6453 | } |
| 6454 | |
| 6455 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 6456 | // the event should be treated as being in the logical display space. |
| 6457 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 6458 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 6459 | // Send down to the first window. The point is represented in the logical display space. The |
| 6460 | // point is selected so that if the hit test was done in logical display space, then it would |
| 6461 | // end up in the incorrect window. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6462 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 6463 | PointF{75 * 2, 55 * 4}); |
| 6464 | |
| 6465 | firstWindow->consumeMotionDown(); |
| 6466 | secondWindow->assertNoEvents(); |
| 6467 | } |
| 6468 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 6469 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 6470 | // event should be treated as being in the logical display space. |
| 6471 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 6472 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 6473 | |
| 6474 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 6475 | ui::Transform injectedEventTransform; |
| 6476 | injectedEventTransform.set(matrix); |
| 6477 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 6478 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 6479 | |
| 6480 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6481 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 6482 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 6483 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 6484 | .x(untransformedPoint.x) |
| 6485 | .y(untransformedPoint.y)) |
| 6486 | .build(); |
| 6487 | event.transform(matrix); |
| 6488 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6489 | injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 6490 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 6491 | |
| 6492 | firstWindow->consumeMotionDown(); |
| 6493 | secondWindow->assertNoEvents(); |
| 6494 | } |
| 6495 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 6496 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 6497 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 6498 | |
| 6499 | // Send down to the second window. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6500 | mDispatcher->notifyMotion( |
| 6501 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6502 | ui::LogicalDisplayId::DEFAULT, {PointF{150, 220}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 6503 | |
| 6504 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6505 | std::unique_ptr<MotionEvent> event = secondWindow->consumeMotionEvent(); |
| 6506 | ASSERT_NE(nullptr, event); |
| 6507 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 6508 | |
| 6509 | // 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] | 6510 | EXPECT_EQ(300, event->getRawX(0)); |
| 6511 | EXPECT_EQ(880, event->getRawY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 6512 | |
| 6513 | // Ensure that the x and y values are in the window's coordinate space. |
| 6514 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 6515 | // 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] | 6516 | EXPECT_EQ(100, event->getX(0)); |
| 6517 | EXPECT_EQ(80, event->getY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 6518 | } |
| 6519 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 6520 | TEST_F(InputDispatcherDisplayProjectionTest, CancelMotionWithCorrectCoordinates) { |
| 6521 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 6522 | // The monitor will always receive events in the logical display's coordinate space, because |
| 6523 | // it does not have a window. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6524 | FakeMonitorReceiver monitor{*mDispatcher, "Monitor", ui::LogicalDisplayId::DEFAULT}; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 6525 | |
| 6526 | // Send down to the first window. |
| 6527 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6528 | ui::LogicalDisplayId::DEFAULT, {PointF{50, 100}})); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 6529 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 6530 | monitor.consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 6531 | |
| 6532 | // Second pointer goes down on second window. |
| 6533 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6534 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 6535 | {PointF{50, 100}, PointF{150, 220}})); |
| 6536 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80))); |
| 6537 | const std::map<int32_t, PointF> expectedMonitorPointers{{0, PointF{100, 400}}, |
| 6538 | {1, PointF{300, 880}}}; |
| 6539 | monitor.consumeMotionEvent( |
| 6540 | AllOf(WithMotionAction(POINTER_1_DOWN), WithPointers(expectedMonitorPointers))); |
| 6541 | |
| 6542 | mDispatcher->cancelCurrentTouch(); |
| 6543 | |
| 6544 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 6545 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 80))); |
| 6546 | monitor.consumeMotionEvent( |
| 6547 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedMonitorPointers))); |
| 6548 | } |
| 6549 | |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 6550 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeDownWithCorrectCoordinates) { |
| 6551 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 6552 | |
| 6553 | // Send down to the first window. |
| 6554 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6555 | ui::LogicalDisplayId::DEFAULT, {PointF{50, 100}})); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 6556 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 6557 | |
| 6558 | // The pointer is transferred to the second window, and the second window receives it in the |
| 6559 | // correct coordinate space. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6560 | mDispatcher->transferTouchGesture(firstWindow->getToken(), secondWindow->getToken()); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 6561 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 6562 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(-100, -400))); |
| 6563 | } |
| 6564 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 6565 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverEnterExitWithCorrectCoordinates) { |
| 6566 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 6567 | |
| 6568 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 6569 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6570 | ui::LogicalDisplayId::DEFAULT, |
| 6571 | {PointF{150, 220}})); |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 6572 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 6573 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 6574 | |
| 6575 | // Touch down at the same location and ensure a hover exit is synthesized. |
| 6576 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6577 | ui::LogicalDisplayId::DEFAULT, |
| 6578 | {PointF{150, 220}})); |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 6579 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 6580 | WithRawCoords(300, 880))); |
| 6581 | secondWindow->consumeMotionEvent( |
| 6582 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 6583 | secondWindow->assertNoEvents(); |
| 6584 | firstWindow->assertNoEvents(); |
| 6585 | } |
| 6586 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 6587 | // Same as above, but while the window is being mirrored. |
| 6588 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 6589 | SynthesizeHoverEnterExitWithCorrectCoordinatesWhenMirrored) { |
| 6590 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 6591 | |
| 6592 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 6593 | ui::Transform secondDisplayTransform; |
| 6594 | secondDisplayTransform.set(matrix); |
| 6595 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 6596 | |
| 6597 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 6598 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 6599 | addWindow(secondWindowClone); |
| 6600 | |
| 6601 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 6602 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6603 | ui::LogicalDisplayId::DEFAULT, |
| 6604 | {PointF{150, 220}})); |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 6605 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 6606 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 6607 | |
| 6608 | // Touch down at the same location and ensure a hover exit is synthesized for the correct |
| 6609 | // display. |
| 6610 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6611 | ui::LogicalDisplayId::DEFAULT, |
| 6612 | {PointF{150, 220}})); |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 6613 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 6614 | WithRawCoords(300, 880))); |
| 6615 | secondWindow->consumeMotionEvent( |
| 6616 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 6617 | secondWindow->assertNoEvents(); |
| 6618 | firstWindow->assertNoEvents(); |
| 6619 | } |
| 6620 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 6621 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverCancelationWithCorrectCoordinates) { |
| 6622 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 6623 | |
| 6624 | // Send hover enter to second window |
| 6625 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6626 | ui::LogicalDisplayId::DEFAULT, |
| 6627 | {PointF{150, 220}})); |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 6628 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 6629 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 6630 | |
| 6631 | mDispatcher->cancelCurrentTouch(); |
| 6632 | |
| 6633 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 6634 | WithRawCoords(300, 880))); |
| 6635 | secondWindow->assertNoEvents(); |
| 6636 | firstWindow->assertNoEvents(); |
| 6637 | } |
| 6638 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 6639 | // Same as above, but while the window is being mirrored. |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 6640 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 6641 | SynthesizeHoverCancelationWithCorrectCoordinatesWhenMirrored) { |
| 6642 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 6643 | |
| 6644 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 6645 | ui::Transform secondDisplayTransform; |
| 6646 | secondDisplayTransform.set(matrix); |
| 6647 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 6648 | |
| 6649 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 6650 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 6651 | addWindow(secondWindowClone); |
| 6652 | |
| 6653 | // Send hover enter to second window |
| 6654 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6655 | ui::LogicalDisplayId::DEFAULT, |
| 6656 | {PointF{150, 220}})); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 6657 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 6658 | WithCoords(100, 80), WithRawCoords(300, 880), |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6659 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 6660 | |
| 6661 | mDispatcher->cancelCurrentTouch(); |
| 6662 | |
| 6663 | // Ensure the cancelation happens with the correct displayId and the correct coordinates. |
| 6664 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 6665 | WithRawCoords(300, 880), |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6666 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 6667 | secondWindow->assertNoEvents(); |
| 6668 | firstWindow->assertNoEvents(); |
| 6669 | } |
| 6670 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6671 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ |
| 6672 | class InputDispatcherDisplayOrientationFixture |
| 6673 | : public InputDispatcherDisplayProjectionTest, |
| 6674 | public ::testing::WithParamInterface<ui::Rotation> {}; |
| 6675 | |
| 6676 | // This test verifies the touchable region of a window for all rotations of the display by tapping |
| 6677 | // in different locations on the display, specifically points close to the four corners of a |
| 6678 | // window. |
| 6679 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { |
| 6680 | constexpr static int32_t displayWidth = 400; |
| 6681 | constexpr static int32_t displayHeight = 800; |
| 6682 | |
| 6683 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6684 | |
| 6685 | const auto rotation = GetParam(); |
| 6686 | |
| 6687 | // Set up the display with the specified rotation. |
| 6688 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 6689 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 6690 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 6691 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 6692 | logicalDisplayWidth, logicalDisplayHeight); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6693 | addDisplayInfo(ui::LogicalDisplayId::DEFAULT, displayTransform); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6694 | |
| 6695 | // Create a window with its bounds determined in the logical display. |
| 6696 | const Rect frameInLogicalDisplay(100, 100, 200, 300); |
| 6697 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6698 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 6699 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6700 | window->setFrame(frameInDisplay, displayTransform); |
| 6701 | addWindow(window); |
| 6702 | |
| 6703 | // The following points in logical display space should be inside the window. |
| 6704 | static const std::array<vec2, 4> insidePoints{ |
| 6705 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 6706 | for (const auto pointInsideWindow : insidePoints) { |
| 6707 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); |
| 6708 | const PointF pointInDisplaySpace{p.x, p.y}; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6709 | mDispatcher->notifyMotion( |
| 6710 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6711 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6712 | window->consumeMotionDown(); |
| 6713 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6714 | mDispatcher->notifyMotion( |
| 6715 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6716 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6717 | window->consumeMotionUp(); |
| 6718 | } |
| 6719 | |
| 6720 | // The following points in logical display space should be outside the window. |
| 6721 | static const std::array<vec2, 5> outsidePoints{ |
| 6722 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 6723 | for (const auto pointOutsideWindow : outsidePoints) { |
| 6724 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); |
| 6725 | const PointF pointInDisplaySpace{p.x, p.y}; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6726 | mDispatcher->notifyMotion( |
| 6727 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6728 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6729 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6730 | mDispatcher->notifyMotion( |
| 6731 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6732 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6733 | } |
| 6734 | window->assertNoEvents(); |
| 6735 | } |
| 6736 | |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6737 | // This test verifies the occlusion detection for all rotations of the display by tapping |
| 6738 | // in different locations on the display, specifically points close to the four corners of a |
| 6739 | // window. |
| 6740 | TEST_P(InputDispatcherDisplayOrientationFixture, BlockUntrustClickInDifferentOrientations) { |
| 6741 | constexpr static int32_t displayWidth = 400; |
| 6742 | constexpr static int32_t displayHeight = 800; |
| 6743 | |
| 6744 | std::shared_ptr<FakeApplicationHandle> untrustedWindowApplication = |
| 6745 | std::make_shared<FakeApplicationHandle>(); |
| 6746 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6747 | |
| 6748 | const auto rotation = GetParam(); |
| 6749 | |
| 6750 | // Set up the display with the specified rotation. |
| 6751 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 6752 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 6753 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 6754 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 6755 | logicalDisplayWidth, logicalDisplayHeight); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6756 | addDisplayInfo(ui::LogicalDisplayId::DEFAULT, displayTransform); |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6757 | |
| 6758 | // Create a window that not trusted. |
| 6759 | const Rect untrustedWindowFrameInLogicalDisplay(100, 100, 200, 300); |
| 6760 | |
| 6761 | const Rect untrustedWindowFrameInDisplay = |
| 6762 | displayTransform.inverse().transform(untrustedWindowFrameInLogicalDisplay); |
| 6763 | |
| 6764 | sp<FakeWindowHandle> untrustedWindow = |
| 6765 | sp<FakeWindowHandle>::make(untrustedWindowApplication, mDispatcher, "UntrustedWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6766 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6767 | untrustedWindow->setFrame(untrustedWindowFrameInDisplay, displayTransform); |
| 6768 | untrustedWindow->setTrustedOverlay(false); |
| 6769 | untrustedWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6770 | untrustedWindow->setTouchable(false); |
| 6771 | untrustedWindow->setAlpha(1.0f); |
| 6772 | untrustedWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
| 6773 | addWindow(untrustedWindow); |
| 6774 | |
| 6775 | // Create a simple app window below the untrusted window. |
| 6776 | const Rect simpleAppWindowFrameInLogicalDisplay(0, 0, 300, 600); |
| 6777 | const Rect simpleAppWindowFrameInDisplay = |
| 6778 | displayTransform.inverse().transform(simpleAppWindowFrameInLogicalDisplay); |
| 6779 | |
| 6780 | sp<FakeWindowHandle> simpleAppWindow = |
| 6781 | sp<FakeWindowHandle>::make(application, mDispatcher, "SimpleAppWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6782 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6783 | simpleAppWindow->setFrame(simpleAppWindowFrameInDisplay, displayTransform); |
| 6784 | simpleAppWindow->setOwnerInfo(gui::Pid{2}, gui::Uid{202}); |
| 6785 | addWindow(simpleAppWindow); |
| 6786 | |
| 6787 | // The following points in logical display space should be inside the untrusted window, so |
| 6788 | // the simple window could not receive events that coordinate is these point. |
| 6789 | static const std::array<vec2, 4> untrustedPoints{ |
| 6790 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 6791 | |
| 6792 | for (const auto untrustedPoint : untrustedPoints) { |
| 6793 | const vec2 p = displayTransform.inverse().transform(untrustedPoint); |
| 6794 | const PointF pointInDisplaySpace{p.x, p.y}; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6795 | mDispatcher->notifyMotion( |
| 6796 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6797 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
| 6798 | mDispatcher->notifyMotion( |
| 6799 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6800 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6801 | } |
| 6802 | untrustedWindow->assertNoEvents(); |
| 6803 | simpleAppWindow->assertNoEvents(); |
| 6804 | // The following points in logical display space should be outside the untrusted window, so |
| 6805 | // the simple window should receive events that coordinate is these point. |
| 6806 | static const std::array<vec2, 5> trustedPoints{ |
| 6807 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 6808 | for (const auto trustedPoint : trustedPoints) { |
| 6809 | const vec2 p = displayTransform.inverse().transform(trustedPoint); |
| 6810 | const PointF pointInDisplaySpace{p.x, p.y}; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6811 | mDispatcher->notifyMotion( |
| 6812 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6813 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
| 6814 | simpleAppWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6815 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6816 | mDispatcher->notifyMotion( |
| 6817 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6818 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
| 6819 | simpleAppWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6820 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 6821 | } |
| 6822 | untrustedWindow->assertNoEvents(); |
| 6823 | } |
| 6824 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6825 | // Run the precision tests for all rotations. |
| 6826 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, |
| 6827 | InputDispatcherDisplayOrientationFixture, |
| 6828 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, |
| 6829 | ui::ROTATION_270), |
| 6830 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { |
| 6831 | return ftl::enum_string(testParamInfo.param); |
| 6832 | }); |
| 6833 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6834 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 6835 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6836 | |
| 6837 | class TransferTouchFixture : public InputDispatcherTest, |
| 6838 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 6839 | |
| 6840 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6841 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6842 | |
| 6843 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6844 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6845 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6846 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6847 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6848 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6849 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6850 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6851 | sp<FakeWindowHandle> wallpaper = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6852 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 6853 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6854 | wallpaper->setIsWallpaper(true); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6855 | // 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] | 6856 | mDispatcher->onWindowInfosChanged( |
| 6857 | {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6858 | setFocusedWindow(firstWindow); |
| 6859 | firstWindow->consumeFocusEvent(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6860 | |
| 6861 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6862 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6863 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6864 | ui::LogicalDisplayId::DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6865 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6866 | // Only the first window should get the down event |
| 6867 | firstWindow->consumeMotionDown(); |
| 6868 | secondWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6869 | wallpaper->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6870 | // Dispatcher reports pointer down outside focus for the wallpaper |
| 6871 | mFakePolicy->assertOnPointerDownEquals(wallpaper->getToken()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6872 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6873 | // Transfer touch to the second window |
| 6874 | TransferFunction f = GetParam(); |
| 6875 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 6876 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6877 | // The first window gets cancel and the second gets down |
| 6878 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6879 | secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 6880 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 6881 | wallpaper->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6882 | // There should not be any changes to the focused window when transferring touch |
| 6883 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertOnPointerDownWasNotCalled()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6884 | |
| 6885 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6886 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6887 | ui::LogicalDisplayId::DEFAULT)); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6888 | // The first window gets no events and the second gets up |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6889 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6890 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6891 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6892 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6893 | } |
| 6894 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6895 | /** |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6896 | * When 'transferTouchGesture' API is invoked, dispatcher needs to find the "best" window to take |
| 6897 | * touch from. When we have spy windows, there are several windows to choose from: either spy, or |
| 6898 | * 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] | 6899 | * natural to the user. |
| 6900 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 6901 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 6902 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 6903 | * the first window to the second. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6904 | * The main goal here is to test the behaviour of 'transferTouchGesture' API, but it's still valid |
| 6905 | * to test the other API, as well. |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6906 | */ |
| 6907 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 6908 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6909 | |
| 6910 | // Create a couple of windows + a spy window |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6911 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 6912 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6913 | spyWindow->setTrustedOverlay(true); |
| 6914 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6915 | sp<FakeWindowHandle> firstWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "First", |
| 6916 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6917 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6918 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 6919 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6920 | |
| 6921 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6922 | mDispatcher->onWindowInfosChanged( |
| 6923 | {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6924 | |
| 6925 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6926 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6927 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6928 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6929 | // Only the first window and spy should get the down event |
| 6930 | spyWindow->consumeMotionDown(); |
| 6931 | firstWindow->consumeMotionDown(); |
| 6932 | |
| 6933 | // 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] | 6934 | // if f === 'transferTouchGesture'. |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6935 | TransferFunction f = GetParam(); |
| 6936 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 6937 | ASSERT_TRUE(success); |
| 6938 | // The first window gets cancel and the second gets down |
| 6939 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6940 | secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 6941 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6942 | |
| 6943 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6944 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6945 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6946 | // The first window gets no events and the second+spy get up |
| 6947 | firstWindow->assertNoEvents(); |
| 6948 | spyWindow->consumeMotionUp(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6949 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6950 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6951 | } |
| 6952 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6953 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6954 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6955 | |
| 6956 | PointF touchPoint = {10, 10}; |
| 6957 | |
| 6958 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6959 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6960 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6961 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6962 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6963 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6964 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6965 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6966 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6967 | |
| 6968 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6969 | mDispatcher->onWindowInfosChanged( |
| 6970 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6971 | |
| 6972 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6973 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6974 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6975 | ui::LogicalDisplayId::DEFAULT, {touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6976 | // Only the first window should get the down event |
| 6977 | firstWindow->consumeMotionDown(); |
| 6978 | secondWindow->assertNoEvents(); |
| 6979 | |
| 6980 | // Send pointer down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6981 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6982 | ui::LogicalDisplayId::DEFAULT, |
| 6983 | {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6984 | // Only the first window should get the pointer down event |
| 6985 | firstWindow->consumeMotionPointerDown(1); |
| 6986 | secondWindow->assertNoEvents(); |
| 6987 | |
| 6988 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6989 | TransferFunction f = GetParam(); |
| 6990 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 6991 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6992 | // The first window gets cancel and the second gets down and pointer down |
| 6993 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6994 | secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 6995 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 6996 | secondWindow->consumeMotionPointerDown(1, ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6997 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6998 | |
| 6999 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7000 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7001 | ui::LogicalDisplayId::DEFAULT, |
| 7002 | {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 7003 | // The first window gets nothing and the second gets pointer up |
| 7004 | firstWindow->assertNoEvents(); |
Prabir Pradhan | 33cfc6d | 2024-06-11 20:17:44 +0000 | [diff] [blame] | 7005 | secondWindow->consumeMotionPointerUp(/*pointerIdx=*/1, |
| 7006 | AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT), |
| 7007 | WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE), |
| 7008 | WithPointerCount(2))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 7009 | |
| 7010 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7011 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7012 | ui::LogicalDisplayId::DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 7013 | // The first window gets nothing and the second gets up |
| 7014 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7015 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 7016 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 7017 | } |
| 7018 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7019 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 7020 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7021 | |
| 7022 | // Create a couple of windows |
| 7023 | sp<FakeWindowHandle> firstWindow = |
| 7024 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7025 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7026 | firstWindow->setDupTouchToWallpaper(true); |
| 7027 | sp<FakeWindowHandle> secondWindow = |
| 7028 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7029 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7030 | secondWindow->setDupTouchToWallpaper(true); |
| 7031 | |
| 7032 | sp<FakeWindowHandle> wallpaper1 = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7033 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", |
| 7034 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7035 | wallpaper1->setIsWallpaper(true); |
| 7036 | |
| 7037 | sp<FakeWindowHandle> wallpaper2 = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7038 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", |
| 7039 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7040 | wallpaper2->setIsWallpaper(true); |
| 7041 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7042 | mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(), |
| 7043 | *secondWindow->getInfo(), *wallpaper2->getInfo()}, |
| 7044 | {}, |
| 7045 | 0, |
| 7046 | 0}); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7047 | |
| 7048 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7049 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7050 | AINPUT_SOURCE_TOUCHSCREEN, |
| 7051 | ui::LogicalDisplayId::DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7052 | |
| 7053 | // Only the first window should get the down event |
| 7054 | firstWindow->consumeMotionDown(); |
| 7055 | secondWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7056 | wallpaper1->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7057 | wallpaper2->assertNoEvents(); |
| 7058 | |
| 7059 | // Transfer touch focus to the second window |
| 7060 | TransferFunction f = GetParam(); |
| 7061 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 7062 | ASSERT_TRUE(success); |
| 7063 | |
| 7064 | // The first window gets cancel and the second gets down |
| 7065 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7066 | secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 7067 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 7068 | wallpaper1->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
| 7069 | wallpaper2->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 7070 | EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7071 | |
| 7072 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7073 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7074 | ui::LogicalDisplayId::DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7075 | // The first window gets no events and the second gets up |
| 7076 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7077 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 7078 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7079 | wallpaper1->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7080 | wallpaper2->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 7081 | EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 7082 | } |
| 7083 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 7084 | // 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] | 7085 | // 'transferTouchGesture' and 'transferTouchOnDisplay' are equivalent in behaviour. They only differ |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 7086 | // for the case where there are multiple pointers split across several windows. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 7087 | INSTANTIATE_TEST_SUITE_P( |
| 7088 | InputDispatcherTransferFunctionTests, TransferTouchFixture, |
| 7089 | ::testing::Values( |
| 7090 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, sp<IBinder> /*ignored*/, |
| 7091 | sp<IBinder> destChannelToken) { |
| 7092 | return dispatcher->transferTouchOnDisplay(destChannelToken, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7093 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 7094 | }, |
| 7095 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, sp<IBinder> from, |
| 7096 | sp<IBinder> to) { |
| 7097 | return dispatcher->transferTouchGesture(from, to, |
| 7098 | /*isDragAndDrop=*/false); |
| 7099 | })); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 7100 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 7101 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7102 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 7103 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7104 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7105 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7106 | ui::LogicalDisplayId::DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 7107 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 7108 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7109 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7110 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7111 | ui::LogicalDisplayId::DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 7112 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 7113 | |
| 7114 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7115 | mDispatcher->onWindowInfosChanged( |
| 7116 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 7117 | |
| 7118 | PointF pointInFirst = {300, 200}; |
| 7119 | PointF pointInSecond = {300, 600}; |
| 7120 | |
| 7121 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7122 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7123 | AINPUT_SOURCE_TOUCHSCREEN, |
| 7124 | ui::LogicalDisplayId::DEFAULT, {pointInFirst})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 7125 | // Only the first window should get the down event |
| 7126 | firstWindow->consumeMotionDown(); |
| 7127 | secondWindow->assertNoEvents(); |
| 7128 | |
| 7129 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7130 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7131 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7132 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 7133 | // The first window gets a move and the second a down |
| 7134 | firstWindow->consumeMotionMove(); |
| 7135 | secondWindow->consumeMotionDown(); |
| 7136 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 7137 | // Transfer touch to the second window |
| 7138 | mDispatcher->transferTouchGesture(firstWindow->getToken(), secondWindow->getToken()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 7139 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 7140 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7141 | secondWindow->consumeMotionPointerDown(1, ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 7142 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 7143 | |
| 7144 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7145 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7146 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7147 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 7148 | // The first window gets nothing and the second gets pointer up |
| 7149 | firstWindow->assertNoEvents(); |
Prabir Pradhan | 33cfc6d | 2024-06-11 20:17:44 +0000 | [diff] [blame] | 7150 | secondWindow->consumeMotionPointerUp(/*pointerIdx=*/1, |
| 7151 | AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT), |
| 7152 | WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE), |
| 7153 | WithPointerCount(2))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 7154 | |
| 7155 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7156 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7157 | ui::LogicalDisplayId::DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 7158 | // The first window gets nothing and the second gets up |
| 7159 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7160 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 7161 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 7162 | } |
| 7163 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 7164 | // Same as TransferTouch_TwoPointersSplitTouch, but using 'transferTouchOnDisplay' api. |
| 7165 | // Unlike 'transferTouchGesture', calling 'transferTouchOnDisplay' when there are two windows |
| 7166 | // receiving touch is not supported, so the touch should continue on those windows and the |
| 7167 | // transferred-to window should get nothing. |
| 7168 | TEST_F(InputDispatcherTest, TransferTouchOnDisplay_TwoPointersSplitTouch) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 7169 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7170 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 7171 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7172 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7173 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 7174 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 7175 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 7176 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7177 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7178 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 7179 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 7180 | |
| 7181 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7182 | mDispatcher->onWindowInfosChanged( |
| 7183 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 7184 | |
| 7185 | PointF pointInFirst = {300, 200}; |
| 7186 | PointF pointInSecond = {300, 600}; |
| 7187 | |
| 7188 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7189 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7190 | AINPUT_SOURCE_TOUCHSCREEN, |
| 7191 | ui::LogicalDisplayId::DEFAULT, {pointInFirst})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 7192 | // Only the first window should get the down event |
| 7193 | firstWindow->consumeMotionDown(); |
| 7194 | secondWindow->assertNoEvents(); |
| 7195 | |
| 7196 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7197 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7198 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7199 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 7200 | // The first window gets a move and the second a down |
| 7201 | firstWindow->consumeMotionMove(); |
| 7202 | secondWindow->consumeMotionDown(); |
| 7203 | |
| 7204 | // Transfer touch focus to the second window |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7205 | const bool transferred = mDispatcher->transferTouchOnDisplay(secondWindow->getToken(), |
| 7206 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 7207 | // The 'transferTouchOnDisplay' call should not succeed, because there are 2 touched windows |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 7208 | ASSERT_FALSE(transferred); |
| 7209 | firstWindow->assertNoEvents(); |
| 7210 | secondWindow->assertNoEvents(); |
| 7211 | |
| 7212 | // The rest of the dispatch should proceed as normal |
| 7213 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7214 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7215 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7216 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 7217 | // The first window gets MOVE and the second gets pointer up |
| 7218 | firstWindow->consumeMotionMove(); |
| 7219 | secondWindow->consumeMotionUp(); |
| 7220 | |
| 7221 | // Send up event to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7222 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7223 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 7224 | // The first window gets nothing and the second gets up |
| 7225 | firstWindow->consumeMotionUp(); |
| 7226 | secondWindow->assertNoEvents(); |
| 7227 | } |
| 7228 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7229 | // 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] | 7230 | // 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] | 7231 | // the windows info of second display before default display. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 7232 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7233 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7234 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7235 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", |
| 7236 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7237 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7238 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7239 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", |
| 7240 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7241 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7242 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7243 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 7244 | firstWindowInPrimary->clone(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7245 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7246 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 7247 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7248 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7249 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 7250 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7251 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7252 | |
| 7253 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7254 | mDispatcher->onWindowInfosChanged( |
| 7255 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 7256 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 7257 | *secondWindowInPrimary->getInfo()}, |
| 7258 | {}, |
| 7259 | 0, |
| 7260 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7261 | |
| 7262 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7263 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7264 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7265 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7266 | |
| 7267 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7268 | firstWindowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7269 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 7270 | // Transfer touch |
| 7271 | ASSERT_TRUE(mDispatcher->transferTouchGesture(firstWindowInPrimary->getToken(), |
| 7272 | secondWindowInPrimary->getToken())); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7273 | // The first window gets cancel. |
| 7274 | firstWindowInPrimary->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7275 | secondWindowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 7276 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7277 | |
| 7278 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7279 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7280 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7281 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7282 | firstWindowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7283 | secondWindowInPrimary->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 7284 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7285 | |
| 7286 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7287 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7288 | {150, 50})) |
| 7289 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7290 | firstWindowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7291 | secondWindowInPrimary->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 7292 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7293 | } |
| 7294 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 7295 | // Same as TransferTouch_CloneSurface, but this touch on the secondary display and use |
| 7296 | // 'transferTouchOnDisplay' api. |
| 7297 | TEST_F(InputDispatcherTest, TransferTouchOnDisplay_CloneSurface) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7298 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7299 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7300 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", |
| 7301 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7302 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7303 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7304 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", |
| 7305 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7306 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7307 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7308 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 7309 | firstWindowInPrimary->clone(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7310 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7311 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 7312 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7313 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7314 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 7315 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7316 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7317 | |
| 7318 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7319 | mDispatcher->onWindowInfosChanged( |
| 7320 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 7321 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 7322 | *secondWindowInPrimary->getInfo()}, |
| 7323 | {}, |
| 7324 | 0, |
| 7325 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7326 | |
| 7327 | // Touch on second display. |
| 7328 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7329 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 7330 | {50, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7331 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7332 | |
| 7333 | // Window should receive motion event. |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 7334 | firstWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7335 | |
| 7336 | // Transfer touch focus |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 7337 | ASSERT_TRUE(mDispatcher->transferTouchOnDisplay(secondWindowInSecondary->getToken(), |
| 7338 | SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7339 | |
| 7340 | // The first window gets cancel. |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 7341 | firstWindowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 7342 | secondWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, |
| 7343 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7344 | |
| 7345 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7346 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7347 | SECOND_DISPLAY_ID, {150, 50})) |
| 7348 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 7349 | firstWindowInSecondary->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 7350 | secondWindowInSecondary->consumeMotionMove(SECOND_DISPLAY_ID, |
| 7351 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7352 | |
| 7353 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7354 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7355 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 7356 | firstWindowInSecondary->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 7357 | secondWindowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 7358 | } |
| 7359 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7360 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7361 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7362 | sp<FakeWindowHandle> window = |
| 7363 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7364 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7365 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7366 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7367 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7368 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7369 | |
| 7370 | window->consumeFocusEvent(true); |
| 7371 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7372 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7373 | |
| 7374 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7375 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 7376 | |
| 7377 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 7378 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 7379 | mFakePolicy->assertUserActivityPoked(); |
| 7380 | } |
| 7381 | |
| 7382 | TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) { |
| 7383 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7384 | sp<FakeWindowHandle> window = |
| 7385 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7386 | ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 7387 | |
| 7388 | window->setDisableUserActivity(true); |
| 7389 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7390 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 7391 | setFocusedWindow(window); |
| 7392 | |
| 7393 | window->consumeFocusEvent(true); |
| 7394 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 7395 | mDispatcher->notifyKey( |
| 7396 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 7397 | |
| 7398 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7399 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 7400 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 7401 | // Should have not poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 7402 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 7403 | mFakePolicy->assertUserActivityNotPoked(); |
| 7404 | } |
| 7405 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 7406 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceivePolicyConsumedKey) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 7407 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7408 | sp<FakeWindowHandle> window = |
| 7409 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7410 | ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 7411 | |
| 7412 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7413 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 7414 | setFocusedWindow(window); |
| 7415 | |
| 7416 | window->consumeFocusEvent(true); |
| 7417 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 7418 | mFakePolicy->setConsumeKeyBeforeDispatching(true); |
| 7419 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7420 | mDispatcher->notifyKey( |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 7421 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 7422 | mDispatcher->waitForIdle(); |
| 7423 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 7424 | // Key is not passed down |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 7425 | window->assertNoEvents(); |
| 7426 | |
| 7427 | // Should have poked user activity |
| 7428 | mFakePolicy->assertUserActivityPoked(); |
| 7429 | } |
| 7430 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 7431 | TEST_F(InputDispatcherTest, FocusedWindow_PolicyConsumedKeyIgnoresDisableUserActivity) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 7432 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7433 | sp<FakeWindowHandle> window = |
| 7434 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7435 | ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 7436 | |
| 7437 | window->setDisableUserActivity(true); |
| 7438 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7439 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 7440 | setFocusedWindow(window); |
| 7441 | |
| 7442 | window->consumeFocusEvent(true); |
| 7443 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 7444 | mFakePolicy->setConsumeKeyBeforeDispatching(true); |
| 7445 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7446 | mDispatcher->notifyKey( |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 7447 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 7448 | mDispatcher->waitForIdle(); |
| 7449 | |
| 7450 | // System key is not passed down |
| 7451 | window->assertNoEvents(); |
| 7452 | |
| 7453 | // Should have poked user activity |
| 7454 | mFakePolicy->assertUserActivityPoked(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7455 | } |
| 7456 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 7457 | class DisableUserActivityInputDispatcherTest : public InputDispatcherTest, |
| 7458 | public ::testing::WithParamInterface<bool> {}; |
| 7459 | |
| 7460 | TEST_P(DisableUserActivityInputDispatcherTest, NotPassedToUserUserActivity) { |
| 7461 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7462 | sp<FakeWindowHandle> window = |
| 7463 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7464 | ui::LogicalDisplayId::DEFAULT); |
| 7465 | |
| 7466 | window->setDisableUserActivity(GetParam()); |
| 7467 | |
| 7468 | window->setFocusable(true); |
| 7469 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 7470 | setFocusedWindow(window); |
| 7471 | |
| 7472 | window->consumeFocusEvent(true); |
| 7473 | |
| 7474 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 7475 | .keyCode(AKEYCODE_A) |
| 7476 | .policyFlags(0) |
| 7477 | .build()); |
| 7478 | mDispatcher->waitForIdle(); |
| 7479 | |
| 7480 | // Key is not passed down |
| 7481 | window->assertNoEvents(); |
| 7482 | |
| 7483 | // Should not have poked user activity |
| 7484 | mFakePolicy->assertUserActivityNotPoked(); |
| 7485 | } |
| 7486 | |
| 7487 | INSTANTIATE_TEST_CASE_P(DisableUserActivity, DisableUserActivityInputDispatcherTest, |
| 7488 | ::testing::Bool()); |
| 7489 | |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 7490 | TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) { |
| 7491 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7492 | sp<FakeWindowHandle> window = |
| 7493 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7494 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 7495 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7496 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 7497 | |
| 7498 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7499 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7500 | ui::LogicalDisplayId::DEFAULT, {100, 100})) |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 7501 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7502 | |
| 7503 | window->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7504 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 7505 | |
| 7506 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 7507 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 7508 | mFakePolicy->assertUserActivityPoked(); |
| 7509 | } |
| 7510 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7511 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7512 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7513 | sp<FakeWindowHandle> window = |
| 7514 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7515 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7516 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7517 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7518 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7519 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7520 | mDispatcher->waitForIdle(); |
| 7521 | |
| 7522 | window->assertNoEvents(); |
| 7523 | } |
| 7524 | |
| 7525 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 7526 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7527 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7528 | sp<FakeWindowHandle> window = |
| 7529 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7530 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7531 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7532 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7533 | |
| 7534 | // Send key |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7535 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7536 | // Send motion |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7537 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7538 | AINPUT_SOURCE_TOUCHSCREEN, |
| 7539 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7540 | |
| 7541 | // Window should receive only the motion event |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7542 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7543 | window->assertNoEvents(); // Key event or focus event will not be received |
| 7544 | } |
| 7545 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 7546 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 7547 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7548 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 7549 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7550 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7551 | ui::LogicalDisplayId::DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 7552 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 7553 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 7554 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7555 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7556 | ui::LogicalDisplayId::DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 7557 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 7558 | |
| 7559 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7560 | mDispatcher->onWindowInfosChanged( |
| 7561 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 7562 | |
| 7563 | PointF pointInFirst = {300, 200}; |
| 7564 | PointF pointInSecond = {300, 600}; |
| 7565 | |
| 7566 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7567 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7568 | AINPUT_SOURCE_TOUCHSCREEN, |
| 7569 | ui::LogicalDisplayId::DEFAULT, {pointInFirst})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 7570 | // Only the first window should get the down event |
| 7571 | firstWindow->consumeMotionDown(); |
| 7572 | secondWindow->assertNoEvents(); |
| 7573 | |
| 7574 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7575 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7576 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7577 | {pointInFirst, pointInSecond})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 7578 | // The first window gets a move and the second a down |
| 7579 | firstWindow->consumeMotionMove(); |
| 7580 | secondWindow->consumeMotionDown(); |
| 7581 | |
| 7582 | // Send pointer cancel to the second window |
| 7583 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7584 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 7585 | ui::LogicalDisplayId::DEFAULT, {pointInFirst, pointInSecond}); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 7586 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7587 | mDispatcher->notifyMotion(pointerUpMotionArgs); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 7588 | // The first window gets move and the second gets cancel. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7589 | firstWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 7590 | secondWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 7591 | |
| 7592 | // Send up event. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7593 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7594 | ui::LogicalDisplayId::DEFAULT)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 7595 | // The first window gets up and the second gets nothing. |
| 7596 | firstWindow->consumeMotionUp(); |
| 7597 | secondWindow->assertNoEvents(); |
| 7598 | } |
| 7599 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 7600 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 7601 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7602 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7603 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 7604 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7605 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 7606 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 7607 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 7608 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 7609 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7610 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 7611 | window->assertNoEvents(); |
| 7612 | mDispatcher->waitForIdle(); |
| 7613 | } |
| 7614 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7615 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 7616 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 7617 | /** |
| 7618 | * Two entities that receive touch: A window, and a global monitor. |
| 7619 | * The touch goes to the window, and then the window disappears. |
| 7620 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 7621 | * for the monitor, as well. |
| 7622 | * 1. foregroundWindow |
| 7623 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 7624 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7625 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 7626 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7627 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 7628 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 7629 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7630 | FakeMonitorReceiver monitor = |
| 7631 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 7632 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7633 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 7634 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7635 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7636 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 7637 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7638 | |
| 7639 | // Both the foreground window and the global monitor should receive the touch down |
| 7640 | window->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7641 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 7642 | |
| 7643 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7644 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7645 | ui::LogicalDisplayId::DEFAULT, {110, 200})) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 7646 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7647 | |
| 7648 | window->consumeMotionMove(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7649 | monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 7650 | |
| 7651 | // Now the foreground window goes away |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7652 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 7653 | window->consumeMotionCancel(); |
| 7654 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 7655 | |
| 7656 | // If more events come in, there will be no more foreground window to send them to. This will |
| 7657 | // cause a cancel for the monitor, as well. |
| 7658 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7659 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7660 | ui::LogicalDisplayId::DEFAULT, {120, 200})) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 7661 | << "Injection should fail because the window was removed"; |
| 7662 | window->assertNoEvents(); |
| 7663 | // Global monitor now gets the cancel |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7664 | monitor.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 7665 | } |
| 7666 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7667 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7668 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7669 | sp<FakeWindowHandle> window = |
| 7670 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7671 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7672 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 7673 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7674 | FakeMonitorReceiver monitor = |
| 7675 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 7676 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7677 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7678 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7679 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7680 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7681 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 7682 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 7683 | } |
| 7684 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7685 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7686 | FakeMonitorReceiver monitor = |
| 7687 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 7688 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7689 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7690 | sp<FakeWindowHandle> window = |
| 7691 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7692 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7693 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 7694 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7695 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7696 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7697 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7698 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7699 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 7700 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 7701 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7702 | // Pilfer pointers from the monitor. |
| 7703 | // This should not do anything and the window should continue to receive events. |
| 7704 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 7705 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7706 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7707 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7708 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7709 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7710 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7711 | monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
| 7712 | window->consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 7713 | } |
| 7714 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7715 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 7716 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7717 | sp<FakeWindowHandle> window = |
| 7718 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7719 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7720 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 7721 | window->setWindowOffset(20, 40); |
| 7722 | window->setWindowTransform(0, 1, -1, 0); |
| 7723 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7724 | FakeMonitorReceiver monitor = |
| 7725 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 7726 | |
| 7727 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7728 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7729 | ui::LogicalDisplayId::DEFAULT)) |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 7730 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7731 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7732 | std::unique_ptr<MotionEvent> event = monitor.consumeMotion(); |
| 7733 | ASSERT_NE(nullptr, event); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 7734 | // Even though window has transform, gesture monitor must not. |
| 7735 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 7736 | } |
| 7737 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7738 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 7739 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7740 | FakeMonitorReceiver monitor = |
| 7741 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 7742 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7743 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7744 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7745 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7746 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 7747 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 7748 | } |
| 7749 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7750 | /** |
| 7751 | * Two displays |
| 7752 | * The first monitor has a foreground window, a monitor |
| 7753 | * The second window has only one monitor. |
| 7754 | * We first inject a Down event into the first display, this injection should succeed and both |
| 7755 | * the foreground window and monitor should receive a down event, then inject a Down event into |
| 7756 | * the second display as well, this injection should fail, at this point, the first display |
| 7757 | * window and monitor should not receive a cancel or any other event. |
| 7758 | * Continue to inject Move and UP events to the first display, the events should be received |
| 7759 | * normally by the foreground window and monitor. |
| 7760 | */ |
| 7761 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCanceledWhenAnotherEmptyDisplayReceiveEvents) { |
| 7762 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7763 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 7764 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7765 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7766 | FakeMonitorReceiver monitor = |
| 7767 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7768 | FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 7769 | |
| 7770 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 7771 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7772 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7773 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7774 | << "The down event injected into the first display should succeed"; |
| 7775 | |
| 7776 | window->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7777 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7778 | |
| 7779 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 7780 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 7781 | {100, 200})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7782 | << "The down event injected into the second display should fail since there's no " |
| 7783 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7784 | |
| 7785 | // Continue to inject event to first display. |
| 7786 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7787 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7788 | ui::LogicalDisplayId::DEFAULT, {110, 220})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7789 | << "The move event injected into the first display should succeed"; |
| 7790 | |
| 7791 | window->consumeMotionMove(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7792 | monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7793 | |
| 7794 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7795 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7796 | {110, 220})) |
| 7797 | << "The up event injected into the first display should succeed"; |
| 7798 | |
| 7799 | window->consumeMotionUp(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7800 | monitor.consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7801 | |
| 7802 | window->assertNoEvents(); |
| 7803 | monitor.assertNoEvents(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7804 | secondMonitor.assertNoEvents(); |
| 7805 | } |
| 7806 | |
| 7807 | /** |
| 7808 | * Two displays |
| 7809 | * There is a monitor and foreground window on each display. |
| 7810 | * First, we inject down events into each of the two displays, at this point, the foreground windows |
| 7811 | * and monitors on both displays should receive down events. |
| 7812 | * At this point, the foreground window of the second display goes away, the gone window should |
| 7813 | * receive the cancel event, and the other windows and monitors should not receive any events. |
| 7814 | * Inject a move event into the second display. At this point, the injection should fail because |
| 7815 | * the second display no longer has a foreground window. At this point, the monitor on the second |
| 7816 | * display should receive a cancel event, and any windows or monitors on the first display should |
| 7817 | * not receive any events, and any subsequent injection of events into the second display should |
| 7818 | * also fail. |
| 7819 | * Continue to inject events into the first display, and the events should all be injected |
| 7820 | * successfully and received normally. |
| 7821 | */ |
| 7822 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCancelWhenAnotherDisplayMonitorTouchCanceled) { |
| 7823 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7824 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 7825 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7826 | sp<FakeWindowHandle> secondWindow = |
| 7827 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondForeground", |
| 7828 | SECOND_DISPLAY_ID); |
| 7829 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7830 | FakeMonitorReceiver monitor = |
| 7831 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7832 | FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 7833 | |
| 7834 | // There is a foreground window on both displays. |
| 7835 | mDispatcher->onWindowInfosChanged({{*window->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
| 7836 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7837 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7838 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7839 | << "The down event injected into the first display should succeed"; |
| 7840 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7841 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 7842 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7843 | |
| 7844 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7845 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 7846 | {100, 200})) |
| 7847 | << "The down event injected into the second display should succeed"; |
| 7848 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7849 | secondWindow->consumeMotionDown(SECOND_DISPLAY_ID); |
| 7850 | secondMonitor.consumeMotionDown(SECOND_DISPLAY_ID); |
| 7851 | |
| 7852 | // Now second window is gone away. |
| 7853 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 7854 | |
| 7855 | // The gone window should receive a cancel, and the monitor on the second display should not |
| 7856 | // receive any events. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7857 | secondWindow->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 7858 | secondMonitor.assertNoEvents(); |
| 7859 | |
| 7860 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 7861 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7862 | SECOND_DISPLAY_ID, {110, 220})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7863 | << "The move event injected into the second display should fail because there's no " |
| 7864 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7865 | // Now the monitor on the second display should receive a cancel event. |
| 7866 | secondMonitor.consumeMotionCancel(SECOND_DISPLAY_ID); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7867 | |
| 7868 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7869 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7870 | ui::LogicalDisplayId::DEFAULT, {110, 200})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7871 | << "The move event injected into the first display should succeed"; |
| 7872 | |
| 7873 | window->consumeMotionMove(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7874 | monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7875 | |
| 7876 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7877 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 7878 | {110, 220})) |
| 7879 | << "The up event injected into the second display should fail because there's no " |
| 7880 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7881 | |
| 7882 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7883 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7884 | {110, 220})) |
| 7885 | << "The up event injected into the first display should succeed"; |
| 7886 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7887 | window->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
| 7888 | monitor.consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7889 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7890 | window->assertNoEvents(); |
| 7891 | monitor.assertNoEvents(); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7892 | secondWindow->assertNoEvents(); |
| 7893 | secondMonitor.assertNoEvents(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7894 | } |
| 7895 | |
| 7896 | /** |
| 7897 | * One display with transform |
| 7898 | * There is a foreground window and a monitor on the display |
| 7899 | * Inject down event and move event sequentially, the foreground window and monitor can receive down |
| 7900 | * event and move event, then let the foreground window go away, the foreground window receives |
| 7901 | * cancel event, inject move event again, the monitor receives cancel event, all the events received |
| 7902 | * by the monitor should be with the same transform as the display |
| 7903 | */ |
| 7904 | TEST_F(InputDispatcherMonitorTest, MonitorTouchCancelEventWithDisplayTransform) { |
| 7905 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7906 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 7907 | ui::LogicalDisplayId::DEFAULT); |
| 7908 | FakeMonitorReceiver monitor = |
| 7909 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7910 | |
| 7911 | ui::Transform transform; |
| 7912 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 7913 | |
| 7914 | gui::DisplayInfo displayInfo; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7915 | displayInfo.displayId = ui::LogicalDisplayId::DEFAULT; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7916 | displayInfo.transform = transform; |
| 7917 | |
| 7918 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
| 7919 | |
| 7920 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7921 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7922 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7923 | << "The down event injected should succeed"; |
| 7924 | |
| 7925 | window->consumeMotionDown(); |
| 7926 | std::unique_ptr<MotionEvent> downMotionEvent = monitor.consumeMotion(); |
| 7927 | EXPECT_EQ(transform, downMotionEvent->getTransform()); |
| 7928 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, downMotionEvent->getAction()); |
| 7929 | |
| 7930 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7931 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7932 | ui::LogicalDisplayId::DEFAULT, {110, 220})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7933 | << "The move event injected should succeed"; |
| 7934 | |
| 7935 | window->consumeMotionMove(); |
| 7936 | std::unique_ptr<MotionEvent> moveMotionEvent = monitor.consumeMotion(); |
| 7937 | EXPECT_EQ(transform, moveMotionEvent->getTransform()); |
| 7938 | EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, moveMotionEvent->getAction()); |
| 7939 | |
| 7940 | // Let foreground window gone |
| 7941 | mDispatcher->onWindowInfosChanged({{}, {displayInfo}, 0, 0}); |
| 7942 | |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7943 | // Foreground window should receive a cancel event, but not the monitor. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7944 | window->consumeMotionCancel(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7945 | |
| 7946 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 7947 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7948 | ui::LogicalDisplayId::DEFAULT, {110, 220})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7949 | << "The move event injected should failed"; |
| 7950 | // Now foreground should not receive any events, but monitor should receive a cancel event |
| 7951 | // with transform that same as display's display. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7952 | std::unique_ptr<MotionEvent> cancelMotionEvent = monitor.consumeMotion(); |
| 7953 | EXPECT_EQ(transform, cancelMotionEvent->getTransform()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7954 | EXPECT_EQ(ui::LogicalDisplayId::DEFAULT, cancelMotionEvent->getDisplayId()); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7955 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, cancelMotionEvent->getAction()); |
| 7956 | |
| 7957 | // Other event inject to this display should fail. |
| 7958 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 7959 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7960 | ui::LogicalDisplayId::DEFAULT, {110, 220})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7961 | << "The up event injected should fail because the touched window was removed"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7962 | window->assertNoEvents(); |
| 7963 | monitor.assertNoEvents(); |
| 7964 | } |
| 7965 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7966 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7967 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7968 | sp<FakeWindowHandle> window = |
| 7969 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7970 | ui::LogicalDisplayId::DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7971 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7972 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7973 | |
| 7974 | NotifyMotionArgs motionArgs = |
| 7975 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7976 | ui::LogicalDisplayId::DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7977 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7978 | mDispatcher->notifyMotion(motionArgs); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7979 | // Window should receive motion down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7980 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7981 | |
| 7982 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7983 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7984 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 7985 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 7986 | motionArgs.pointerCoords[0].getX() - 10); |
| 7987 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7988 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7989 | window->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7990 | } |
| 7991 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7992 | /** |
| 7993 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 7994 | * the device default right away. In the test scenario, we check both the default value, |
| 7995 | * and the action of enabling / disabling. |
| 7996 | */ |
| 7997 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7998 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7999 | sp<FakeWindowHandle> window = |
| 8000 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 8001 | ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8002 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8003 | |
| 8004 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8005 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8006 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8007 | |
| 8008 | SCOPED_TRACE("Check default value of touch mode"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8009 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8010 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8011 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8012 | |
| 8013 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8014 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8015 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8016 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8017 | |
| 8018 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8019 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8020 | /*hasPermission=*/true, ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8021 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8022 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8023 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8024 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8025 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8026 | |
| 8027 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8028 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8029 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8030 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8031 | |
| 8032 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 8033 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8034 | /*hasPermission=*/true, ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 8035 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8036 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8037 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8038 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8039 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8040 | |
| 8041 | window->assertNoEvents(); |
| 8042 | } |
| 8043 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 8044 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8045 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8046 | sp<FakeWindowHandle> window = |
| 8047 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 8048 | ui::LogicalDisplayId::DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 8049 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8050 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8051 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 8052 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8053 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8054 | setFocusedWindow(window); |
| 8055 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8056 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 8057 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8058 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 8059 | mDispatcher->notifyKey(keyArgs); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 8060 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8061 | std::unique_ptr<KeyEvent> event = window->consumeKey(); |
| 8062 | ASSERT_NE(event, nullptr); |
| 8063 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 8064 | ASSERT_NE(verified, nullptr); |
| 8065 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 8066 | |
| 8067 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 8068 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 8069 | ASSERT_EQ(keyArgs.source, verified->source); |
| 8070 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 8071 | |
| 8072 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 8073 | |
| 8074 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 8075 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 8076 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 8077 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 8078 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 8079 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 8080 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 8081 | } |
| 8082 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 8083 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8084 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8085 | sp<FakeWindowHandle> window = |
| 8086 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 8087 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 8088 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8089 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 8090 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 8091 | ui::Transform transform; |
| 8092 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 8093 | |
| 8094 | gui::DisplayInfo displayInfo; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8095 | displayInfo.displayId = ui::LogicalDisplayId::DEFAULT; |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 8096 | displayInfo.transform = transform; |
| 8097 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 8098 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 8099 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8100 | const NotifyMotionArgs motionArgs = |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 8101 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8102 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8103 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 8104 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8105 | std::unique_ptr<MotionEvent> event = window->consumeMotionEvent(); |
| 8106 | ASSERT_NE(nullptr, event); |
| 8107 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 8108 | ASSERT_NE(verified, nullptr); |
| 8109 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 8110 | |
| 8111 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 8112 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 8113 | EXPECT_EQ(motionArgs.source, verified->source); |
| 8114 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 8115 | |
| 8116 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 8117 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 8118 | const vec2 rawXY = |
| 8119 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 8120 | motionArgs.pointerCoords[0].getXYValue()); |
| 8121 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 8122 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 8123 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 8124 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 8125 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 8126 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 8127 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 8128 | } |
| 8129 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 8130 | /** |
| 8131 | * Ensure that separate calls to sign the same data are generating the same key. |
| 8132 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 8133 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 8134 | * tests. |
| 8135 | */ |
| 8136 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 8137 | KeyEvent event = getTestKeyEvent(); |
| 8138 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 8139 | |
| 8140 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 8141 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 8142 | ASSERT_EQ(hmac1, hmac2); |
| 8143 | } |
| 8144 | |
| 8145 | /** |
| 8146 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 8147 | */ |
| 8148 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 8149 | KeyEvent event = getTestKeyEvent(); |
| 8150 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 8151 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 8152 | |
| 8153 | verifiedEvent.deviceId += 1; |
| 8154 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 8155 | |
| 8156 | verifiedEvent.source += 1; |
| 8157 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 8158 | |
| 8159 | verifiedEvent.eventTimeNanos += 1; |
| 8160 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 8161 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 8162 | verifiedEvent.displayId = ui::LogicalDisplayId{verifiedEvent.displayId.val() + 1}; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 8163 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 8164 | |
| 8165 | verifiedEvent.action += 1; |
| 8166 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 8167 | |
| 8168 | verifiedEvent.downTimeNanos += 1; |
| 8169 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 8170 | |
| 8171 | verifiedEvent.flags += 1; |
| 8172 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 8173 | |
| 8174 | verifiedEvent.keyCode += 1; |
| 8175 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 8176 | |
| 8177 | verifiedEvent.scanCode += 1; |
| 8178 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 8179 | |
| 8180 | verifiedEvent.metaState += 1; |
| 8181 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 8182 | |
| 8183 | verifiedEvent.repeatCount += 1; |
| 8184 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 8185 | } |
| 8186 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8187 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 8188 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8189 | sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 8190 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8191 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8192 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 8193 | ui::LogicalDisplayId::DEFAULT); |
| 8194 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8195 | |
| 8196 | // Top window is also focusable but is not granted focus. |
| 8197 | windowTop->setFocusable(true); |
| 8198 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8199 | mDispatcher->onWindowInfosChanged( |
| 8200 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8201 | setFocusedWindow(windowSecond); |
| 8202 | |
| 8203 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8204 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8205 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8206 | |
| 8207 | // Focused window should receive event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8208 | windowSecond->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8209 | windowTop->assertNoEvents(); |
| 8210 | } |
| 8211 | |
| 8212 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 8213 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8214 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
| 8215 | ui::LogicalDisplayId::DEFAULT); |
| 8216 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8217 | |
| 8218 | window->setFocusable(true); |
| 8219 | // Release channel for window is no longer valid. |
| 8220 | window->releaseChannel(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8221 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8222 | setFocusedWindow(window); |
| 8223 | |
| 8224 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8225 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8226 | |
| 8227 | // window channel is invalid, so it should not receive any input event. |
| 8228 | window->assertNoEvents(); |
| 8229 | } |
| 8230 | |
| 8231 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 8232 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8233 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
| 8234 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 8235 | window->setFocusable(false); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8236 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8237 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8238 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8239 | setFocusedWindow(window); |
| 8240 | |
| 8241 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8242 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8243 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 8244 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8245 | window->assertNoEvents(); |
| 8246 | } |
| 8247 | |
| 8248 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 8249 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8250 | sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 8251 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8252 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8253 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 8254 | ui::LogicalDisplayId::DEFAULT); |
| 8255 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8256 | |
| 8257 | windowTop->setFocusable(true); |
| 8258 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8259 | mDispatcher->onWindowInfosChanged( |
| 8260 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8261 | setFocusedWindow(windowTop); |
| 8262 | windowTop->consumeFocusEvent(true); |
| 8263 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 8264 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8265 | mDispatcher->onWindowInfosChanged( |
| 8266 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8267 | windowSecond->consumeFocusEvent(true); |
| 8268 | windowTop->consumeFocusEvent(false); |
| 8269 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8270 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8271 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8272 | |
| 8273 | // Focused window should receive event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8274 | windowSecond->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8275 | } |
| 8276 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 8277 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8278 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8279 | sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 8280 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8281 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8282 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 8283 | ui::LogicalDisplayId::DEFAULT); |
| 8284 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8285 | |
| 8286 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 8287 | windowSecond->setFocusable(false); |
| 8288 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8289 | mDispatcher->onWindowInfosChanged( |
| 8290 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 8291 | setFocusedWindow(windowTop); |
| 8292 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8293 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8294 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 8295 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8296 | |
| 8297 | // Event should be dropped. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8298 | windowTop->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8299 | windowSecond->assertNoEvents(); |
| 8300 | } |
| 8301 | |
| 8302 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 8303 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8304 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
| 8305 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8306 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8307 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8308 | ui::LogicalDisplayId::DEFAULT); |
| 8309 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8310 | |
| 8311 | window->setFocusable(true); |
| 8312 | previousFocusedWindow->setFocusable(true); |
| 8313 | window->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8314 | mDispatcher->onWindowInfosChanged( |
| 8315 | {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8316 | setFocusedWindow(previousFocusedWindow); |
| 8317 | previousFocusedWindow->consumeFocusEvent(true); |
| 8318 | |
| 8319 | // Requesting focus on invisible window takes focus from currently focused window. |
| 8320 | setFocusedWindow(window); |
| 8321 | previousFocusedWindow->consumeFocusEvent(false); |
| 8322 | |
| 8323 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8324 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8325 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8326 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8327 | |
| 8328 | // Window does not get focus event or key down. |
| 8329 | window->assertNoEvents(); |
| 8330 | |
| 8331 | // Window becomes visible. |
| 8332 | window->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8333 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8334 | |
| 8335 | // Window receives focus event. |
| 8336 | window->consumeFocusEvent(true); |
| 8337 | // Focused window receives key down. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8338 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8339 | } |
| 8340 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 8341 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 8342 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8343 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "window", |
| 8344 | ui::LogicalDisplayId::DEFAULT); |
| 8345 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 8346 | |
| 8347 | // window is granted focus. |
| 8348 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8349 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 8350 | setFocusedWindow(window); |
| 8351 | window->consumeFocusEvent(true); |
| 8352 | |
| 8353 | // When a display is removed window loses focus. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8354 | mDispatcher->displayRemoved(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 8355 | window->consumeFocusEvent(false); |
| 8356 | } |
| 8357 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8358 | /** |
| 8359 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 8360 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 8361 | * of the 'slipperyEnterWindow'. |
| 8362 | * |
| 8363 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 8364 | * a way so that the touched location is no longer covered by the top window. |
| 8365 | * |
| 8366 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 8367 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 8368 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 8369 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 8370 | * with ACTION_DOWN). |
| 8371 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 8372 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 8373 | * |
| 8374 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 8375 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 8376 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 8377 | * |
| 8378 | * In this test, we ensure that the event received by the bottom window has |
| 8379 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 8380 | */ |
| 8381 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8382 | constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 8383 | constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1}; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8384 | |
| 8385 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8386 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8387 | |
| 8388 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8389 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 8390 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 8391 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8392 | // Make sure this one overlaps the bottom window |
| 8393 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 8394 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 8395 | // one. Windows with the same owner are not considered to be occluding each other. |
| 8396 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 8397 | |
| 8398 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8399 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 8400 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8401 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 8402 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8403 | mDispatcher->onWindowInfosChanged( |
| 8404 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8405 | |
| 8406 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8407 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8408 | AINPUT_SOURCE_TOUCHSCREEN, |
| 8409 | ui::LogicalDisplayId::DEFAULT, {{50, 50}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8410 | slipperyExitWindow->consumeMotionDown(); |
| 8411 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8412 | mDispatcher->onWindowInfosChanged( |
| 8413 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8414 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8415 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8416 | AINPUT_SOURCE_TOUCHSCREEN, |
| 8417 | ui::LogicalDisplayId::DEFAULT, {{51, 51}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8418 | |
| 8419 | slipperyExitWindow->consumeMotionCancel(); |
| 8420 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8421 | slipperyEnterWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8422 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 8423 | } |
| 8424 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 8425 | /** |
| 8426 | * Two windows, one on the left and another on the right. The left window is slippery. The right |
| 8427 | * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the |
| 8428 | * touch moves from the left window into the right window, the gesture should continue to go to the |
| 8429 | * left window. Touch shouldn't slip because the right window can't receive touches. This test |
| 8430 | * reproduces a crash. |
| 8431 | */ |
| 8432 | TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { |
| 8433 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 8434 | |
| 8435 | sp<FakeWindowHandle> leftSlipperyWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8436 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 8437 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 8438 | leftSlipperyWindow->setSlippery(true); |
| 8439 | leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 8440 | |
| 8441 | sp<FakeWindowHandle> rightDropTouchesWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8442 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 8443 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 8444 | rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100)); |
| 8445 | rightDropTouchesWindow->setDropInput(true); |
| 8446 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8447 | mDispatcher->onWindowInfosChanged( |
| 8448 | {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 8449 | |
| 8450 | // Start touch in the left window |
| 8451 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8452 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8453 | .build()); |
| 8454 | leftSlipperyWindow->consumeMotionDown(); |
| 8455 | |
| 8456 | // And move it into the right window |
| 8457 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8458 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 8459 | .build()); |
| 8460 | |
| 8461 | // Since the right window isn't eligible to receive input, touch does not slip. |
| 8462 | // The left window continues to receive the gesture. |
| 8463 | leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 8464 | rightDropTouchesWindow->assertNoEvents(); |
| 8465 | } |
| 8466 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 8467 | /** |
| 8468 | * A single window is on screen first. Touch is injected into that window. Next, a second window |
| 8469 | * appears. Since the first window is slippery, touch will move from the first window to the second. |
| 8470 | */ |
| 8471 | TEST_F(InputDispatcherTest, InjectedTouchSlips) { |
| 8472 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 8473 | sp<FakeWindowHandle> originalWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8474 | sp<FakeWindowHandle>::make(application, mDispatcher, "Original", |
| 8475 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 8476 | originalWindow->setFrame(Rect(0, 0, 200, 200)); |
| 8477 | originalWindow->setSlippery(true); |
| 8478 | |
| 8479 | sp<FakeWindowHandle> appearingWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8480 | sp<FakeWindowHandle>::make(application, mDispatcher, "Appearing", |
| 8481 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 8482 | appearingWindow->setFrame(Rect(0, 0, 200, 200)); |
| 8483 | |
| 8484 | mDispatcher->onWindowInfosChanged({{*originalWindow->getInfo()}, {}, 0, 0}); |
| 8485 | |
| 8486 | // Touch down on the original window |
| 8487 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8488 | injectMotionEvent(*mDispatcher, |
| 8489 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8490 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 8491 | .build())); |
| 8492 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 8493 | |
| 8494 | // Now, a new window appears. This could be, for example, a notification shade that appears |
| 8495 | // after user starts to drag down on the launcher window. |
| 8496 | mDispatcher->onWindowInfosChanged( |
| 8497 | {{*appearingWindow->getInfo(), *originalWindow->getInfo()}, {}, 0, 0}); |
| 8498 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8499 | injectMotionEvent(*mDispatcher, |
| 8500 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8501 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(110)) |
| 8502 | .build())); |
| 8503 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 8504 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 8505 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8506 | injectMotionEvent(*mDispatcher, |
| 8507 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8508 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 8509 | .build())); |
| 8510 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 8511 | |
| 8512 | originalWindow->assertNoEvents(); |
| 8513 | appearingWindow->assertNoEvents(); |
| 8514 | } |
| 8515 | |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 8516 | /** |
| 8517 | * Three windows: |
| 8518 | * - left window, which has FLAG_SLIPPERY, so it supports slippery exit |
| 8519 | * - right window |
| 8520 | * - spy window |
| 8521 | * The three windows do not overlap. |
| 8522 | * |
| 8523 | * We have two devices reporting events: |
| 8524 | * - Device A reports ACTION_DOWN, which lands in the left window |
| 8525 | * - Device B reports ACTION_DOWN, which lands in the spy window. |
| 8526 | * - Now, device B reports ACTION_MOVE events which move to the right window. |
| 8527 | * |
| 8528 | * The right window should not receive any events because the spy window is not a foreground window, |
| 8529 | * and also it does not support slippery touches. |
| 8530 | */ |
| 8531 | TEST_F(InputDispatcherTest, MultiDeviceSpyWindowSlipTest) { |
| 8532 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 8533 | sp<FakeWindowHandle> leftWindow = |
| 8534 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8535 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 8536 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 8537 | leftWindow->setSlippery(true); |
| 8538 | |
| 8539 | sp<FakeWindowHandle> rightWindow = |
| 8540 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8541 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 8542 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 8543 | |
| 8544 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8545 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", |
| 8546 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 8547 | spyWindow->setFrame(Rect(200, 0, 300, 100)); |
| 8548 | spyWindow->setSpy(true); |
| 8549 | spyWindow->setTrustedOverlay(true); |
| 8550 | |
| 8551 | mDispatcher->onWindowInfosChanged( |
| 8552 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *spyWindow->getInfo()}, {}, 0, 0}); |
| 8553 | |
| 8554 | const DeviceId deviceA = 9; |
| 8555 | const DeviceId deviceB = 3; |
| 8556 | |
| 8557 | // Tap on left window with device A |
| 8558 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8559 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8560 | .deviceId(deviceA) |
| 8561 | .build()); |
| 8562 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 8563 | |
| 8564 | // Tap on spy window with device B |
| 8565 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8566 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(50)) |
| 8567 | .deviceId(deviceB) |
| 8568 | .build()); |
| 8569 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 8570 | |
| 8571 | // Move to right window with device B. Touches should not slip to the right window, because spy |
| 8572 | // window is not a foreground window, and it does not have FLAG_SLIPPERY |
| 8573 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8574 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 8575 | .deviceId(deviceB) |
| 8576 | .build()); |
| 8577 | leftWindow->assertNoEvents(); |
| 8578 | rightWindow->assertNoEvents(); |
| 8579 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB))); |
| 8580 | } |
| 8581 | |
| 8582 | /** |
| 8583 | * Three windows arranged horizontally and without any overlap. |
| 8584 | * The left and right windows have FLAG_SLIPPERY. The middle window does not have any special flags. |
| 8585 | * |
| 8586 | * We have two devices reporting events: |
| 8587 | * - Device A reports ACTION_DOWN which lands in the left window |
| 8588 | * - Device B reports ACTION_DOWN which lands in the right window |
| 8589 | * - Device B reports ACTION_MOVE that shifts to the middle window. |
| 8590 | * This should cause touches for Device B to slip from the right window to the middle window. |
| 8591 | * The right window should receive ACTION_CANCEL for device B and the |
| 8592 | * middle window should receive down event for Device B. |
| 8593 | * If device B reports more ACTION_MOVE events, the middle window should receive remaining events. |
| 8594 | */ |
| 8595 | TEST_F(InputDispatcherTest, MultiDeviceSlipperyWindowTest) { |
Siarhei Vishniakou | dd56df1 | 2024-05-20 14:56:38 -0700 | [diff] [blame] | 8596 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 8597 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 8598 | sp<FakeWindowHandle> leftWindow = |
| 8599 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8600 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 8601 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 8602 | leftWindow->setSlippery(true); |
| 8603 | |
| 8604 | sp<FakeWindowHandle> middleWindow = |
| 8605 | sp<FakeWindowHandle>::make(application, mDispatcher, "middle window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8606 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 8607 | middleWindow->setFrame(Rect(100, 0, 200, 100)); |
| 8608 | |
| 8609 | sp<FakeWindowHandle> rightWindow = |
| 8610 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8611 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 8612 | rightWindow->setFrame(Rect(200, 0, 300, 100)); |
| 8613 | rightWindow->setSlippery(true); |
| 8614 | |
| 8615 | mDispatcher->onWindowInfosChanged( |
| 8616 | {{*leftWindow->getInfo(), *middleWindow->getInfo(), *rightWindow->getInfo()}, |
| 8617 | {}, |
| 8618 | 0, |
| 8619 | 0}); |
| 8620 | |
| 8621 | const DeviceId deviceA = 9; |
| 8622 | const DeviceId deviceB = 3; |
| 8623 | |
| 8624 | // Tap on left window with device A |
| 8625 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8626 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8627 | .deviceId(deviceA) |
| 8628 | .build()); |
| 8629 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 8630 | |
| 8631 | // Tap on right window with device B |
| 8632 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8633 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(50)) |
| 8634 | .deviceId(deviceB) |
| 8635 | .build()); |
| 8636 | rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 8637 | |
| 8638 | // Move to middle window with device B. Touches should slip to middle window, because right |
| 8639 | // window is a foreground window that's associated with device B and has FLAG_SLIPPERY. |
| 8640 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8641 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 8642 | .deviceId(deviceB) |
| 8643 | .build()); |
| 8644 | rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB))); |
| 8645 | middleWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 8646 | |
| 8647 | // Move to middle window with device A. Touches should slip to middle window, because left |
| 8648 | // window is a foreground window that's associated with device A and has FLAG_SLIPPERY. |
| 8649 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8650 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 8651 | .deviceId(deviceA) |
| 8652 | .build()); |
| 8653 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceA))); |
| 8654 | middleWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 8655 | |
| 8656 | // Ensure that middle window can receive the remaining move events. |
| 8657 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8658 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(51)) |
| 8659 | .deviceId(deviceB) |
| 8660 | .build()); |
| 8661 | leftWindow->assertNoEvents(); |
| 8662 | middleWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB))); |
| 8663 | rightWindow->assertNoEvents(); |
| 8664 | } |
| 8665 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8666 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 8667 | using Uid = gui::Uid; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8668 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 8669 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8670 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 8671 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8672 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8673 | leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8674 | |
| 8675 | sp<FakeWindowHandle> rightSpy = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8676 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", |
| 8677 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8678 | rightSpy->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8679 | rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8680 | rightSpy->setSpy(true); |
| 8681 | rightSpy->setTrustedOverlay(true); |
| 8682 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8683 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 8684 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8685 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8686 | rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8687 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8688 | mDispatcher->onWindowInfosChanged( |
| 8689 | {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8690 | |
| 8691 | // Touch in the left window |
| 8692 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8693 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8694 | .build()); |
| 8695 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown()); |
| 8696 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 8697 | ASSERT_NO_FATAL_FAILURE( |
| 8698 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8699 | |
| 8700 | // Touch another finger over the right windows |
| 8701 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8702 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8703 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 8704 | .build()); |
| 8705 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown()); |
| 8706 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown()); |
| 8707 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove()); |
| 8708 | mDispatcher->waitForIdle(); |
| 8709 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 8710 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, |
| 8711 | {Uid{101}, Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8712 | |
| 8713 | // Release finger over left window. The UP actions are not treated as device interaction. |
| 8714 | // The windows that did not receive the UP pointer will receive MOVE events, but since this |
| 8715 | // is part of the UP action, we do not treat this as device interaction. |
| 8716 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8717 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8718 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 8719 | .build()); |
| 8720 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp()); |
| 8721 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 8722 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 8723 | mDispatcher->waitForIdle(); |
| 8724 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 8725 | |
| 8726 | // Move remaining finger |
| 8727 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8728 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 8729 | .build()); |
| 8730 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 8731 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 8732 | mDispatcher->waitForIdle(); |
| 8733 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 8734 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8735 | |
| 8736 | // Release all fingers |
| 8737 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8738 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 8739 | .build()); |
| 8740 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp()); |
| 8741 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp()); |
| 8742 | mDispatcher->waitForIdle(); |
| 8743 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 8744 | } |
| 8745 | |
| 8746 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) { |
| 8747 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 8748 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8749 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 8750 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8751 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8752 | window->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8753 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8754 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8755 | setFocusedWindow(window); |
| 8756 | ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true)); |
| 8757 | |
| 8758 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8759 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT)); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8760 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 8761 | ASSERT_NO_FATAL_FAILURE( |
| 8762 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8763 | |
| 8764 | // The UP actions are not treated as device interaction. |
| 8765 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8766 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT)); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8767 | mDispatcher->waitForIdle(); |
| 8768 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 8769 | } |
| 8770 | |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8771 | TEST_F(InputDispatcherTest, HoverEnterExitSynthesisUsesNewEventId) { |
| 8772 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 8773 | |
| 8774 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8775 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8776 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8777 | sp<FakeWindowHandle> right = |
| 8778 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 8779 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8780 | right->setFrame(Rect(100, 0, 200, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8781 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", |
| 8782 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8783 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 8784 | spy->setTrustedOverlay(true); |
| 8785 | spy->setSpy(true); |
| 8786 | |
| 8787 | mDispatcher->onWindowInfosChanged( |
| 8788 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 8789 | |
| 8790 | // 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] | 8791 | NotifyMotionArgs notifyArgs = |
| 8792 | generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 8793 | ui::LogicalDisplayId::DEFAULT, {PointF{50, 50}}); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8794 | mDispatcher->notifyMotion(notifyArgs); |
| 8795 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8796 | std::unique_ptr<MotionEvent> leftEnter = left->consumeMotionEvent( |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8797 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), Not(WithEventId(notifyArgs.id)), |
| 8798 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8799 | ASSERT_NE(nullptr, leftEnter); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8800 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 8801 | Not(WithEventId(notifyArgs.id)), |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8802 | Not(WithEventId(leftEnter->getId())), |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8803 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 8804 | |
| 8805 | // 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] | 8806 | notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 8807 | ui::LogicalDisplayId::DEFAULT, {PointF{150, 50}}); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8808 | mDispatcher->notifyMotion(notifyArgs); |
| 8809 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8810 | std::unique_ptr<MotionEvent> leftExit = left->consumeMotionEvent( |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8811 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), Not(WithEventId(notifyArgs.id)), |
| 8812 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8813 | ASSERT_NE(nullptr, leftExit); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8814 | right->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 8815 | Not(WithEventId(notifyArgs.id)), |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8816 | Not(WithEventId(leftExit->getId())), |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8817 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 8818 | |
| 8819 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithEventId(notifyArgs.id))); |
| 8820 | } |
| 8821 | |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 8822 | /** |
| 8823 | * When a device reports a DOWN event, which lands in a window that supports splits, and then the |
| 8824 | * device then reports a POINTER_DOWN, which lands in the location of a non-existing window, then |
| 8825 | * the previous window should receive this event and not be dropped. |
| 8826 | */ |
| 8827 | TEST_F(InputDispatcherMultiDeviceTest, SingleDevicePointerDownEventRetentionWithoutWindowTarget) { |
Siarhei Vishniakou | a3fe664 | 2024-07-18 11:58:25 -0700 | [diff] [blame] | 8828 | SCOPED_FLAG_OVERRIDE(split_all_touches, false); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 8829 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8830 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 8831 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 8832 | window->setFrame(Rect(0, 0, 100, 100)); |
| 8833 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 8834 | |
| 8835 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8836 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8837 | .build()); |
| 8838 | |
| 8839 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 8840 | |
| 8841 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8842 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8843 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 8844 | .build()); |
| 8845 | |
| 8846 | window->consumeMotionEvent(AllOf(WithMotionAction(POINTER_1_DOWN))); |
| 8847 | } |
| 8848 | |
| 8849 | /** |
| 8850 | * When deviceA reports a DOWN event, which lands in a window that supports splits, and then deviceB |
| 8851 | * also reports a DOWN event, which lands in the location of a non-existing window, then the |
| 8852 | * previous window should receive deviceB's event and it should be dropped. |
| 8853 | */ |
| 8854 | TEST_F(InputDispatcherMultiDeviceTest, SecondDeviceDownEventDroppedWithoutWindowTarget) { |
| 8855 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8856 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 8857 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 8858 | window->setFrame(Rect(0, 0, 100, 100)); |
| 8859 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 8860 | |
| 8861 | const DeviceId deviceA = 9; |
| 8862 | const DeviceId deviceB = 3; |
| 8863 | |
| 8864 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8865 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8866 | .deviceId(deviceA) |
| 8867 | .build()); |
| 8868 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 8869 | |
| 8870 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8871 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
| 8872 | .deviceId(deviceB) |
| 8873 | .build()); |
| 8874 | window->assertNoEvents(); |
| 8875 | } |
| 8876 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 8877 | class InputDispatcherFallbackKeyTest : public InputDispatcherTest { |
| 8878 | protected: |
| 8879 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 8880 | sp<FakeWindowHandle> mWindow; |
| 8881 | |
| 8882 | virtual void SetUp() override { |
| 8883 | InputDispatcherTest::SetUp(); |
| 8884 | |
| 8885 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 8886 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8887 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Window", |
| 8888 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 8889 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
| 8890 | |
| 8891 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 8892 | setFocusedWindow(mWindow); |
| 8893 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeFocusEvent(/*hasFocus=*/true)); |
| 8894 | } |
| 8895 | |
| 8896 | void setFallback(int32_t keycode) { |
| 8897 | mFakePolicy->setUnhandledKeyHandler([keycode](const KeyEvent& event) { |
| 8898 | return KeyEventBuilder(event).keyCode(keycode).build(); |
| 8899 | }); |
| 8900 | } |
| 8901 | |
| 8902 | void consumeKey(bool handled, const ::testing::Matcher<KeyEvent>& matcher) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8903 | std::unique_ptr<KeyEvent> event = mWindow->consumeKey(handled); |
| 8904 | ASSERT_NE(nullptr, event); |
| 8905 | ASSERT_THAT(*event, matcher); |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 8906 | } |
| 8907 | }; |
| 8908 | |
| 8909 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotNotifiedForHandledKey) { |
| 8910 | mDispatcher->notifyKey( |
| 8911 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8912 | consumeKey(/*handled=*/true, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 8913 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8914 | } |
| 8915 | |
| 8916 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotifiedForUnhandledKey) { |
| 8917 | mDispatcher->notifyKey( |
| 8918 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8919 | consumeKey(/*handled=*/false, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 8920 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8921 | } |
| 8922 | |
| 8923 | TEST_F(InputDispatcherFallbackKeyTest, NoFallbackRequestedByPolicy) { |
| 8924 | mDispatcher->notifyKey( |
| 8925 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8926 | |
| 8927 | // Do not handle this key event. |
| 8928 | consumeKey(/*handled=*/false, |
| 8929 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8930 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8931 | |
| 8932 | // Since the policy did not request any fallback to be generated, ensure there are no events. |
| 8933 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8934 | } |
| 8935 | |
| 8936 | TEST_F(InputDispatcherFallbackKeyTest, FallbackDispatchForUnhandledKey) { |
| 8937 | setFallback(AKEYCODE_B); |
| 8938 | mDispatcher->notifyKey( |
| 8939 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8940 | |
| 8941 | // Do not handle this key event. |
| 8942 | consumeKey(/*handled=*/false, |
| 8943 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8944 | |
| 8945 | // Since the key was not handled, ensure the fallback event was dispatched instead. |
| 8946 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8947 | consumeKey(/*handled=*/true, |
| 8948 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8949 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8950 | |
| 8951 | // Release the original key, and ensure the fallback key is also released. |
| 8952 | mDispatcher->notifyKey( |
| 8953 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8954 | consumeKey(/*handled=*/false, |
| 8955 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8956 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8957 | consumeKey(/*handled=*/true, |
| 8958 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8959 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8960 | |
| 8961 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8962 | mWindow->assertNoEvents(); |
| 8963 | } |
| 8964 | |
| 8965 | TEST_F(InputDispatcherFallbackKeyTest, AppHandlesPreviouslyUnhandledKey) { |
| 8966 | setFallback(AKEYCODE_B); |
| 8967 | mDispatcher->notifyKey( |
| 8968 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8969 | |
| 8970 | // Do not handle this key event, but handle the fallback. |
| 8971 | consumeKey(/*handled=*/false, |
| 8972 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8973 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8974 | consumeKey(/*handled=*/true, |
| 8975 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8976 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8977 | |
| 8978 | // Release the original key, and ensure the fallback key is also released. |
| 8979 | mDispatcher->notifyKey( |
| 8980 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8981 | // But this time, the app handles the original key. |
| 8982 | consumeKey(/*handled=*/true, |
| 8983 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8984 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8985 | // Ensure the fallback key is canceled. |
| 8986 | consumeKey(/*handled=*/true, |
| 8987 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8988 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 8989 | |
| 8990 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8991 | mWindow->assertNoEvents(); |
| 8992 | } |
| 8993 | |
| 8994 | TEST_F(InputDispatcherFallbackKeyTest, AppDoesNotHandleFallback) { |
| 8995 | setFallback(AKEYCODE_B); |
| 8996 | mDispatcher->notifyKey( |
| 8997 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8998 | |
| 8999 | // Do not handle this key event. |
| 9000 | consumeKey(/*handled=*/false, |
| 9001 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 9002 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 9003 | // App does not handle the fallback either, so ensure another fallback is not generated. |
| 9004 | setFallback(AKEYCODE_C); |
| 9005 | consumeKey(/*handled=*/false, |
| 9006 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 9007 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 9008 | |
| 9009 | // Release the original key, and ensure the fallback key is also released. |
| 9010 | setFallback(AKEYCODE_B); |
| 9011 | mDispatcher->notifyKey( |
| 9012 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 9013 | consumeKey(/*handled=*/false, |
| 9014 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 9015 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 9016 | consumeKey(/*handled=*/false, |
| 9017 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 9018 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 9019 | |
| 9020 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 9021 | mWindow->assertNoEvents(); |
| 9022 | } |
| 9023 | |
| 9024 | TEST_F(InputDispatcherFallbackKeyTest, InconsistentPolicyCancelsFallback) { |
| 9025 | setFallback(AKEYCODE_B); |
| 9026 | mDispatcher->notifyKey( |
| 9027 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 9028 | |
| 9029 | // Do not handle this key event, so fallback is generated. |
| 9030 | consumeKey(/*handled=*/false, |
| 9031 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 9032 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 9033 | consumeKey(/*handled=*/true, |
| 9034 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 9035 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 9036 | |
| 9037 | // Release the original key, but assume the policy is misbehaving and it |
| 9038 | // generates an inconsistent fallback to the one from the DOWN event. |
| 9039 | setFallback(AKEYCODE_C); |
| 9040 | mDispatcher->notifyKey( |
| 9041 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 9042 | consumeKey(/*handled=*/false, |
| 9043 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 9044 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 9045 | // Ensure the fallback key reported before as DOWN is canceled due to the inconsistency. |
| 9046 | consumeKey(/*handled=*/true, |
| 9047 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 9048 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 9049 | |
| 9050 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 9051 | mWindow->assertNoEvents(); |
| 9052 | } |
| 9053 | |
| 9054 | TEST_F(InputDispatcherFallbackKeyTest, CanceledKeyCancelsFallback) { |
| 9055 | setFallback(AKEYCODE_B); |
| 9056 | mDispatcher->notifyKey( |
| 9057 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 9058 | |
| 9059 | // Do not handle this key event, so fallback is generated. |
| 9060 | consumeKey(/*handled=*/false, |
| 9061 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 9062 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 9063 | consumeKey(/*handled=*/true, |
| 9064 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 9065 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 9066 | |
| 9067 | // The original key is canceled. |
| 9068 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
| 9069 | .keyCode(AKEYCODE_A) |
| 9070 | .addFlag(AKEY_EVENT_FLAG_CANCELED) |
| 9071 | .build()); |
| 9072 | consumeKey(/*handled=*/false, |
| 9073 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), |
| 9074 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 9075 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 9076 | // Ensure the fallback key is also canceled due to the original key being canceled. |
| 9077 | consumeKey(/*handled=*/true, |
| 9078 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 9079 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 9080 | |
| 9081 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 9082 | mWindow->assertNoEvents(); |
| 9083 | } |
| 9084 | |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 9085 | TEST_F(InputDispatcherFallbackKeyTest, InputChannelRemovedDuringPolicyCall) { |
Prabir Pradhan | b13da8f | 2024-01-09 23:10:13 +0000 | [diff] [blame] | 9086 | setFallback(AKEYCODE_B); |
| 9087 | mDispatcher->notifyKey( |
| 9088 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 9089 | |
| 9090 | // Do not handle this key event. |
| 9091 | consumeKey(/*handled=*/false, |
| 9092 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 9093 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 9094 | consumeKey(/*handled=*/true, |
| 9095 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 9096 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 9097 | |
| 9098 | mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) { |
| 9099 | // When the unhandled key is reported to the policy next, remove the input channel. |
| 9100 | mDispatcher->removeInputChannel(mWindow->getToken()); |
| 9101 | return KeyEventBuilder(event).keyCode(AKEYCODE_B).build(); |
| 9102 | }); |
| 9103 | // Release the original key, and let the app now handle the previously unhandled key. |
| 9104 | // This should result in the previously generated fallback key to be cancelled. |
| 9105 | // Since the policy was notified of the unhandled DOWN event earlier, it will also be notified |
| 9106 | // of the UP event for consistency. The Dispatcher calls into the policy from its own thread |
| 9107 | // without holding the lock, because it need to synchronously fetch the fallback key. While in |
| 9108 | // the policy call, we will now remove the input channel. Once the policy call returns, the |
| 9109 | // Dispatcher will no longer have a channel to send cancellation events to. Ensure this does |
| 9110 | // not cause any crashes. |
| 9111 | mDispatcher->notifyKey( |
| 9112 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 9113 | consumeKey(/*handled=*/true, |
| 9114 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 9115 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 9116 | } |
| 9117 | |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 9118 | TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedDuringPolicyCall) { |
| 9119 | setFallback(AKEYCODE_B); |
| 9120 | mDispatcher->notifyKey( |
| 9121 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 9122 | |
| 9123 | // Do not handle this key event. |
| 9124 | consumeKey(/*handled=*/false, |
| 9125 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 9126 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 9127 | consumeKey(/*handled=*/true, |
| 9128 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 9129 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 9130 | |
| 9131 | mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) { |
| 9132 | // When the unhandled key is reported to the policy next, remove the window. |
| 9133 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 9134 | return KeyEventBuilder(event).keyCode(AKEYCODE_B).build(); |
| 9135 | }); |
| 9136 | // Release the original key, which the app will not handle. When this unhandled key is reported |
| 9137 | // to the policy, the window will be removed. |
| 9138 | mDispatcher->notifyKey( |
| 9139 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 9140 | consumeKey(/*handled=*/false, |
| 9141 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 9142 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 9143 | |
| 9144 | // Since the window was removed, it loses focus, and the channel state will be reset. |
| 9145 | consumeKey(/*handled=*/true, |
| 9146 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 9147 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 9148 | mWindow->consumeFocusEvent(false); |
| 9149 | mWindow->assertNoEvents(); |
| 9150 | } |
| 9151 | |
| 9152 | TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedWhileAwaitingFinishedSignal) { |
| 9153 | setFallback(AKEYCODE_B); |
| 9154 | mDispatcher->notifyKey( |
| 9155 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 9156 | |
| 9157 | // Do not handle this key event. |
| 9158 | consumeKey(/*handled=*/false, |
| 9159 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 9160 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 9161 | const auto [seq, event] = mWindow->receiveEvent(); |
| 9162 | ASSERT_TRUE(seq.has_value() && event != nullptr) << "Failed to receive fallback event"; |
| 9163 | ASSERT_EQ(event->getType(), InputEventType::KEY); |
| 9164 | ASSERT_THAT(static_cast<const KeyEvent&>(*event), |
| 9165 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 9166 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 9167 | |
| 9168 | // Remove the window now, which should generate a cancellations and make the window lose focus. |
| 9169 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 9170 | consumeKey(/*handled=*/true, |
| 9171 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), |
| 9172 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 9173 | consumeKey(/*handled=*/true, |
| 9174 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 9175 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 9176 | mWindow->consumeFocusEvent(false); |
| 9177 | |
| 9178 | // Finish the event by reporting it as handled. |
| 9179 | mWindow->finishEvent(*seq); |
| 9180 | mWindow->assertNoEvents(); |
| 9181 | } |
| 9182 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9183 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 9184 | protected: |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 9185 | static constexpr std::chrono::nanoseconds KEY_REPEAT_TIMEOUT = 40ms; |
| 9186 | static constexpr std::chrono::nanoseconds KEY_REPEAT_DELAY = 40ms; |
Asmita Poddar | 2808d7e | 2024-09-12 13:15:38 +0000 | [diff] [blame] | 9187 | static constexpr bool KEY_REPEAT_ENABLED = true; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9188 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9189 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9190 | sp<FakeWindowHandle> mWindow; |
| 9191 | |
| 9192 | virtual void SetUp() override { |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 9193 | InputDispatcherTest::SetUp(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9194 | |
Asmita Poddar | 2808d7e | 2024-09-12 13:15:38 +0000 | [diff] [blame] | 9195 | mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY, |
| 9196 | KEY_REPEAT_ENABLED); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9197 | setUpWindow(); |
| 9198 | } |
| 9199 | |
| 9200 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9201 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9202 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", |
| 9203 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9204 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9205 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9206 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9207 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9208 | mWindow->consumeFocusEvent(true); |
| 9209 | } |
| 9210 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 9211 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9212 | NotifyKeyArgs keyArgs = |
| 9213 | generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 9214 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9215 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9216 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9217 | |
| 9218 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9219 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9220 | } |
| 9221 | |
| 9222 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 9223 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9224 | mWindow->consumeKeyEvent( |
| 9225 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount))); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9226 | } |
| 9227 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 9228 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9229 | NotifyKeyArgs keyArgs = |
| 9230 | generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 9231 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9232 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9233 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9234 | |
| 9235 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9236 | mWindow->consumeKeyUp(ui::LogicalDisplayId::DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9237 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9238 | } |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 9239 | |
| 9240 | void injectKeyRepeat(int32_t repeatCount) { |
| 9241 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9242 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, repeatCount, |
| 9243 | ui::LogicalDisplayId::DEFAULT)) |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 9244 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 9245 | } |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9246 | }; |
| 9247 | |
| 9248 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9249 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 9250 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 9251 | expectKeyRepeatOnce(repeatCount); |
| 9252 | } |
| 9253 | } |
| 9254 | |
| 9255 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9256 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 9257 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 9258 | expectKeyRepeatOnce(repeatCount); |
| 9259 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9260 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 9261 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9262 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 9263 | expectKeyRepeatOnce(repeatCount); |
| 9264 | } |
| 9265 | } |
| 9266 | |
| 9267 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9268 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 9269 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 9270 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 9271 | mWindow->assertNoEvents(); |
| 9272 | } |
| 9273 | |
| 9274 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9275 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 9276 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 9277 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 9278 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 9279 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9280 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 9281 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9282 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 9283 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 9284 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9285 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 9286 | mWindow->assertNoEvents(); |
| 9287 | } |
| 9288 | |
| 9289 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9290 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 9291 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 9292 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 9293 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 9294 | // 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] | 9295 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 9296 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9297 | mWindow->assertNoEvents(); |
| 9298 | } |
| 9299 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 9300 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 9301 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9302 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9303 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9304 | mWindow->consumeKeyUp(ui::LogicalDisplayId::DEFAULT, |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 9305 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 9306 | mWindow->assertNoEvents(); |
| 9307 | } |
| 9308 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9309 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 9310 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9311 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9312 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9313 | std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey(); |
| 9314 | ASSERT_NE(nullptr, repeatEvent); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9315 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9316 | IdGenerator::getSource(repeatEvent->getId())); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9317 | } |
| 9318 | } |
| 9319 | |
| 9320 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 9321 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9322 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9323 | |
| 9324 | std::unordered_set<int32_t> idSet; |
| 9325 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9326 | std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey(); |
| 9327 | ASSERT_NE(nullptr, repeatEvent); |
| 9328 | int32_t id = repeatEvent->getId(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 9329 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 9330 | idSet.insert(id); |
| 9331 | } |
| 9332 | } |
| 9333 | |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 9334 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_CorrectRepeatCountWhenInjectKeyRepeat) { |
| 9335 | injectKeyRepeat(0); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9336 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 9337 | for (int32_t repeatCount = 1; repeatCount <= 2; ++repeatCount) { |
| 9338 | expectKeyRepeatOnce(repeatCount); |
| 9339 | } |
| 9340 | injectKeyRepeat(1); |
| 9341 | // Expect repeatCount to be 3 instead of 1 |
| 9342 | expectKeyRepeatOnce(3); |
| 9343 | } |
| 9344 | |
Asmita Poddar | 2808d7e | 2024-09-12 13:15:38 +0000 | [diff] [blame] | 9345 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_NoRepeatWhenKeyRepeatDisabled) { |
| 9346 | SCOPED_FLAG_OVERRIDE(keyboard_repeat_keys, true); |
| 9347 | static constexpr std::chrono::milliseconds KEY_NO_REPEAT_ASSERTION_TIMEOUT = 100ms; |
| 9348 | |
| 9349 | mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY, |
| 9350 | /*repeatKeyEnabled=*/false); |
| 9351 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 9352 | |
| 9353 | ASSERT_GT(KEY_NO_REPEAT_ASSERTION_TIMEOUT, KEY_REPEAT_TIMEOUT) |
| 9354 | << "Ensure the check for no key repeats extends beyond the repeat timeout duration."; |
| 9355 | ASSERT_GT(KEY_NO_REPEAT_ASSERTION_TIMEOUT, KEY_REPEAT_DELAY) |
| 9356 | << "Ensure the check for no key repeats extends beyond the repeat delay duration."; |
| 9357 | |
| 9358 | // No events should be returned if key repeat is turned off. |
| 9359 | // Wait for KEY_NO_REPEAT_ASSERTION_TIMEOUT to return no events to ensure key repeat disabled. |
| 9360 | mWindow->assertNoEvents(KEY_NO_REPEAT_ASSERTION_TIMEOUT); |
| 9361 | } |
| 9362 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9363 | /* Test InputDispatcher for MultiDisplay */ |
| 9364 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 9365 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 9366 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9367 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 9368 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9369 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9370 | windowInPrimary = sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", |
| 9371 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 9372 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9373 | // 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] | 9374 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9375 | windowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9376 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
| 9377 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9378 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 9379 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 9380 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9381 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 9382 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9383 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9384 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9385 | // Set focus display to second one. |
| 9386 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
Arpit Singh | b65e2bd | 2024-06-03 09:48:16 +0000 | [diff] [blame] | 9387 | mFakePolicy->assertFocusedDisplayNotified(SECOND_DISPLAY_ID); |
| 9388 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9389 | // Set focus window for second display. |
| 9390 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9391 | windowInSecondary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9392 | mDispatcher->onWindowInfosChanged( |
| 9393 | {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9394 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 9395 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9396 | } |
| 9397 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 9398 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9399 | InputDispatcherTest::TearDown(); |
| 9400 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9401 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9402 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9403 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9404 | windowInSecondary.clear(); |
| 9405 | } |
| 9406 | |
| 9407 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9408 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9409 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9410 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9411 | sp<FakeWindowHandle> windowInSecondary; |
| 9412 | }; |
| 9413 | |
| 9414 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 9415 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9416 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9417 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9418 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9419 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9420 | windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 9421 | windowInSecondary->assertNoEvents(); |
| 9422 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9423 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9424 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9425 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9426 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 9427 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 9428 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 9429 | } |
| 9430 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9431 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 9432 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 9433 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9434 | injectKeyDownNoRepeat(*mDispatcher, ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9435 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9436 | windowInPrimary->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 9437 | windowInSecondary->assertNoEvents(); |
| 9438 | |
| 9439 | // Test inject a key down without display id specified. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9440 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9441 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 9442 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9443 | windowInSecondary->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 9444 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 9445 | // Remove all windows in secondary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9446 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 9447 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9448 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9449 | windowInSecondary->consumeKeyUp(ui::LogicalDisplayId::INVALID, AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 9450 | |
| 9451 | // Test inject a key down, should timeout because of no target window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9452 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 9453 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 9454 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 9455 | windowInSecondary->assertNoEvents(); |
| 9456 | } |
| 9457 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9458 | // Test per-display input monitors for motion event. |
| 9459 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 9460 | FakeMonitorReceiver monitorInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9461 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 9462 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 9463 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9464 | |
| 9465 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9466 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9467 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9468 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9469 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9470 | windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 9471 | monitorInPrimary.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9472 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 9473 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9474 | |
| 9475 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9476 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9477 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9478 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9479 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 9480 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 9481 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 9482 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9483 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 9484 | // Lift up the touch from the second display |
| 9485 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9486 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 9487 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9488 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 9489 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 9490 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9491 | // Test inject a non-pointer motion event. |
| 9492 | // If specific a display, it will dispatch to the focused window of particular display, |
| 9493 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9494 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9495 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, |
| 9496 | ui::LogicalDisplayId::INVALID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9497 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9498 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 9499 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9500 | windowInSecondary->consumeMotionDown(ui::LogicalDisplayId::INVALID); |
| 9501 | monitorInSecondary.consumeMotionDown(ui::LogicalDisplayId::INVALID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9502 | } |
| 9503 | |
| 9504 | // Test per-display input monitors for key event. |
| 9505 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 9506 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 9507 | FakeMonitorReceiver monitorInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9508 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 9509 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 9510 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9511 | |
| 9512 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9513 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9514 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9515 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 9516 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9517 | windowInSecondary->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
| 9518 | monitorInSecondary.consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 9519 | } |
| 9520 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9521 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 9522 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9523 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", |
| 9524 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9525 | secondWindowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9526 | mDispatcher->onWindowInfosChanged( |
| 9527 | {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(), |
| 9528 | *windowInSecondary->getInfo()}, |
| 9529 | {}, |
| 9530 | 0, |
| 9531 | 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9532 | setFocusedWindow(secondWindowInPrimary); |
| 9533 | windowInPrimary->consumeFocusEvent(false); |
| 9534 | secondWindowInPrimary->consumeFocusEvent(true); |
| 9535 | |
| 9536 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9537 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9538 | injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9539 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9540 | windowInPrimary->assertNoEvents(); |
| 9541 | windowInSecondary->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9542 | secondWindowInPrimary->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9543 | } |
| 9544 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 9545 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 9546 | FakeMonitorReceiver monitorInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9547 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 9548 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 9549 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 9550 | |
| 9551 | // Test touch down on primary display. |
| 9552 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9553 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9554 | ui::LogicalDisplayId::DEFAULT)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 9555 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9556 | windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 9557 | monitorInPrimary.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 9558 | |
| 9559 | // Test touch down on second display. |
| 9560 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9561 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 9562 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 9563 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 9564 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 9565 | |
| 9566 | // Trigger cancel touch. |
| 9567 | mDispatcher->cancelCurrentTouch(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9568 | windowInPrimary->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT); |
| 9569 | monitorInPrimary.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 9570 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 9571 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 9572 | |
| 9573 | // Test inject a move motion event, no window/monitor should receive the event. |
| 9574 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9575 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9576 | ui::LogicalDisplayId::DEFAULT, {110, 200})) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 9577 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 9578 | windowInPrimary->assertNoEvents(); |
| 9579 | monitorInPrimary.assertNoEvents(); |
| 9580 | |
| 9581 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9582 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 9583 | SECOND_DISPLAY_ID, {110, 200})) |
| 9584 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 9585 | windowInSecondary->assertNoEvents(); |
| 9586 | monitorInSecondary.assertNoEvents(); |
| 9587 | } |
| 9588 | |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 9589 | /** |
| 9590 | * Send a key to the primary display and to the secondary display. |
| 9591 | * Then cause the key on the primary display to be canceled by sending in a stale key. |
| 9592 | * Ensure that the key on the primary display is canceled, and that the key on the secondary display |
| 9593 | * does not get canceled. |
| 9594 | */ |
| 9595 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture) { |
| 9596 | // Send a key down on primary display |
| 9597 | mDispatcher->notifyKey( |
| 9598 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9599 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 9600 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 9601 | .build()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9602 | windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), |
| 9603 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 9604 | windowInSecondary->assertNoEvents(); |
| 9605 | |
| 9606 | // Send a key down on second display |
| 9607 | mDispatcher->notifyKey( |
| 9608 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 9609 | .displayId(SECOND_DISPLAY_ID) |
| 9610 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 9611 | .build()); |
| 9612 | windowInSecondary->consumeKeyEvent( |
| 9613 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID))); |
| 9614 | windowInPrimary->assertNoEvents(); |
| 9615 | |
| 9616 | // Send a valid key up event on primary display that will be dropped because it is stale |
| 9617 | NotifyKeyArgs staleKeyUp = |
| 9618 | KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9619 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 9620 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 9621 | .build(); |
| 9622 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms; |
| 9623 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
| 9624 | std::this_thread::sleep_for(STALE_EVENT_TIMEOUT); |
| 9625 | mDispatcher->notifyKey(staleKeyUp); |
| 9626 | |
| 9627 | // Only the key gesture corresponding to the dropped event should receive the cancel event. |
| 9628 | // Therefore, windowInPrimary should get the cancel event and windowInSecondary should not |
| 9629 | // receive any events. |
| 9630 | windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9631 | WithDisplayId(ui::LogicalDisplayId::DEFAULT), |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 9632 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 9633 | windowInSecondary->assertNoEvents(); |
| 9634 | } |
| 9635 | |
| 9636 | /** |
| 9637 | * Similar to 'WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture' but for motion events. |
| 9638 | */ |
| 9639 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropMotionEvent_OnlyCancelCorrespondingGesture) { |
| 9640 | // Send touch down on primary display. |
| 9641 | mDispatcher->notifyMotion( |
| 9642 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9643 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9644 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 9645 | .build()); |
| 9646 | windowInPrimary->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9647 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 9648 | windowInSecondary->assertNoEvents(); |
| 9649 | |
| 9650 | // Send touch down on second display. |
| 9651 | mDispatcher->notifyMotion( |
| 9652 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9653 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 9654 | .displayId(SECOND_DISPLAY_ID) |
| 9655 | .build()); |
| 9656 | windowInPrimary->assertNoEvents(); |
| 9657 | windowInSecondary->consumeMotionEvent( |
| 9658 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID))); |
| 9659 | |
| 9660 | // inject a valid MotionEvent on primary display that will be stale when it arrives. |
| 9661 | NotifyMotionArgs staleMotionUp = |
| 9662 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9663 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 9664 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 9665 | .build(); |
| 9666 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms; |
| 9667 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
| 9668 | std::this_thread::sleep_for(STALE_EVENT_TIMEOUT); |
| 9669 | mDispatcher->notifyMotion(staleMotionUp); |
| 9670 | |
| 9671 | // For stale motion events, we let the gesture to complete. This behaviour is different from key |
| 9672 | // events, where we would cancel the current keys instead. |
| 9673 | windowInPrimary->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 9674 | windowInSecondary->assertNoEvents(); |
| 9675 | } |
| 9676 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9677 | class InputFilterTest : public InputDispatcherTest { |
| 9678 | protected: |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 9679 | void testNotifyMotion(ui::LogicalDisplayId displayId, bool expectToBeFiltered, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 9680 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9681 | NotifyMotionArgs motionArgs; |
| 9682 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 9683 | motionArgs = |
| 9684 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9685 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 9686 | motionArgs = |
| 9687 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9688 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 9689 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9690 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 9691 | const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue()); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 9692 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9693 | } else { |
| 9694 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 9695 | } |
| 9696 | } |
| 9697 | |
| 9698 | void testNotifyKey(bool expectToBeFiltered) { |
| 9699 | NotifyKeyArgs keyArgs; |
| 9700 | |
| 9701 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9702 | mDispatcher->notifyKey(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9703 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9704 | mDispatcher->notifyKey(keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 9705 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9706 | |
| 9707 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 9708 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9709 | } else { |
| 9710 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 9711 | } |
| 9712 | } |
| 9713 | }; |
| 9714 | |
| 9715 | // Test InputFilter for MotionEvent |
| 9716 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 9717 | // 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] | 9718 | testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/false); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 9719 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9720 | |
| 9721 | // Enable InputFilter |
| 9722 | mDispatcher->setInputFilterEnabled(true); |
| 9723 | // 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] | 9724 | testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/true); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 9725 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9726 | |
| 9727 | // Disable InputFilter |
| 9728 | mDispatcher->setInputFilterEnabled(false); |
| 9729 | // 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] | 9730 | testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/false); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 9731 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9732 | } |
| 9733 | |
| 9734 | // Test InputFilter for KeyEvent |
| 9735 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 9736 | // 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] | 9737 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9738 | |
| 9739 | // Enable InputFilter |
| 9740 | mDispatcher->setInputFilterEnabled(true); |
| 9741 | // Send a key event, and check if it is filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 9742 | testNotifyKey(/*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9743 | |
| 9744 | // Disable InputFilter |
| 9745 | mDispatcher->setInputFilterEnabled(false); |
| 9746 | // Send a key event, and check if it isn't filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 9747 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9748 | } |
| 9749 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 9750 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 9751 | // logical display coordinate space. |
| 9752 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 9753 | ui::Transform firstDisplayTransform; |
| 9754 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 9755 | ui::Transform secondDisplayTransform; |
| 9756 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 9757 | |
| 9758 | std::vector<gui::DisplayInfo> displayInfos(2); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9759 | displayInfos[0].displayId = ui::LogicalDisplayId::DEFAULT; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 9760 | displayInfos[0].transform = firstDisplayTransform; |
| 9761 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 9762 | displayInfos[1].transform = secondDisplayTransform; |
| 9763 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 9764 | mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0}); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 9765 | |
| 9766 | // Enable InputFilter |
| 9767 | mDispatcher->setInputFilterEnabled(true); |
| 9768 | |
| 9769 | // Ensure the correct transforms are used for the displays. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9770 | testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/true, |
| 9771 | firstDisplayTransform); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 9772 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 9773 | } |
| 9774 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9775 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 9776 | protected: |
| 9777 | virtual void SetUp() override { |
| 9778 | InputDispatcherTest::SetUp(); |
| 9779 | |
| 9780 | /** |
| 9781 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 9782 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 9783 | * on. |
| 9784 | */ |
| 9785 | mDispatcher->setInputFilterEnabled(true); |
| 9786 | |
| 9787 | std::shared_ptr<InputApplicationHandle> application = |
| 9788 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9789 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9790 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9791 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9792 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9793 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9794 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9795 | setFocusedWindow(mWindow); |
| 9796 | mWindow->consumeFocusEvent(true); |
| 9797 | } |
| 9798 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9799 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 9800 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9801 | KeyEvent event; |
| 9802 | |
| 9803 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 9804 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9805 | ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, |
| 9806 | AKEYCODE_A, KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9807 | const int32_t additionalPolicyFlags = |
| 9808 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 9809 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9810 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 9811 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9812 | policyFlags | additionalPolicyFlags)); |
| 9813 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9814 | mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags))); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9815 | } |
| 9816 | |
| 9817 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 9818 | int32_t flags) { |
| 9819 | MotionEvent event; |
| 9820 | PointerProperties pointerProperties[1]; |
| 9821 | PointerCoords pointerCoords[1]; |
| 9822 | pointerProperties[0].clear(); |
| 9823 | pointerProperties[0].id = 0; |
| 9824 | pointerCoords[0].clear(); |
| 9825 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 9826 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 9827 | |
| 9828 | ui::Transform identityTransform; |
| 9829 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 9830 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 9831 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 9832 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 9833 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 9834 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 9835 | eventTime, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 9836 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9837 | |
| 9838 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 9839 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9840 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 9841 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9842 | policyFlags | additionalPolicyFlags)); |
| 9843 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9844 | mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId))); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9845 | } |
| 9846 | |
| 9847 | private: |
| 9848 | sp<FakeWindowHandle> mWindow; |
| 9849 | }; |
| 9850 | |
| 9851 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9852 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 9853 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 9854 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9855 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 9856 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9857 | } |
| 9858 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9859 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9860 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9861 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9862 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 9863 | } |
| 9864 | |
| 9865 | TEST_F(InputFilterInjectionPolicyTest, |
| 9866 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 9867 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9868 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9869 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9870 | } |
| 9871 | |
| 9872 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9873 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 9874 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9875 | } |
| 9876 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9877 | class InputDispatcherUserActivityPokeTests : public InputDispatcherTest { |
| 9878 | protected: |
| 9879 | virtual void SetUp() override { |
| 9880 | InputDispatcherTest::SetUp(); |
| 9881 | |
| 9882 | std::shared_ptr<FakeApplicationHandle> application = |
| 9883 | std::make_shared<FakeApplicationHandle>(); |
| 9884 | application->setDispatchingTimeout(100ms); |
| 9885 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9886 | ui::LogicalDisplayId::DEFAULT); |
Yeabkal Wubshit | 222d83d | 2024-01-24 18:00:09 +0000 | [diff] [blame] | 9887 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9888 | mWindow->setDispatchingTimeout(100ms); |
| 9889 | mWindow->setFocusable(true); |
| 9890 | |
| 9891 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9892 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9893 | |
| 9894 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 9895 | setFocusedWindow(mWindow); |
| 9896 | mWindow->consumeFocusEvent(true); |
| 9897 | } |
| 9898 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 9899 | void notifyAndConsumeMotion(int32_t action, uint32_t source, ui::LogicalDisplayId displayId, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9900 | nsecs_t eventTime) { |
| 9901 | mDispatcher->notifyMotion(MotionArgsBuilder(action, source) |
| 9902 | .displayId(displayId) |
| 9903 | .eventTime(eventTime) |
| 9904 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 9905 | .build()); |
| 9906 | mWindow->consumeMotionEvent(WithMotionAction(action)); |
| 9907 | } |
| 9908 | |
| 9909 | private: |
| 9910 | sp<FakeWindowHandle> mWindow; |
| 9911 | }; |
| 9912 | |
| 9913 | TEST_F_WITH_FLAGS( |
| 9914 | InputDispatcherUserActivityPokeTests, MinPokeTimeObserved, |
| 9915 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 9916 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 9917 | mDispatcher->setMinTimeBetweenUserActivityPokes(50ms); |
| 9918 | |
| 9919 | // First event of type TOUCH. Should poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9920 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9921 | milliseconds_to_nanoseconds(50)); |
| 9922 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9923 | {{milliseconds_to_nanoseconds(50), USER_ACTIVITY_EVENT_TOUCH, |
| 9924 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9925 | |
| 9926 | // 80ns > 50ns has passed since previous TOUCH event. Should poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9927 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9928 | milliseconds_to_nanoseconds(130)); |
| 9929 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9930 | {{milliseconds_to_nanoseconds(130), USER_ACTIVITY_EVENT_TOUCH, |
| 9931 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9932 | |
| 9933 | // 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] | 9934 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, |
| 9935 | ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(135)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9936 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9937 | {{milliseconds_to_nanoseconds(135), USER_ACTIVITY_EVENT_OTHER, |
| 9938 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9939 | |
| 9940 | // Within 50ns of previous TOUCH event. Should NOT poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9941 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9942 | milliseconds_to_nanoseconds(140)); |
| 9943 | mFakePolicy->assertUserActivityNotPoked(); |
| 9944 | |
| 9945 | // Within 50ns of previous OTHER event. Should NOT poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9946 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, |
| 9947 | ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(150)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9948 | mFakePolicy->assertUserActivityNotPoked(); |
| 9949 | |
| 9950 | // Within 50ns of previous TOUCH event (which was at time 130). Should NOT poke. |
| 9951 | // 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] | 9952 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_STYLUS, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9953 | milliseconds_to_nanoseconds(160)); |
| 9954 | mFakePolicy->assertUserActivityNotPoked(); |
| 9955 | |
| 9956 | // 65ns > 50ns has passed since previous OTHER event. Should poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9957 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, |
| 9958 | ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(200)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9959 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9960 | {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_OTHER, |
| 9961 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9962 | |
| 9963 | // 170ns > 50ns has passed since previous TOUCH event. Should poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9964 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_STYLUS, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9965 | milliseconds_to_nanoseconds(300)); |
| 9966 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9967 | {{milliseconds_to_nanoseconds(300), USER_ACTIVITY_EVENT_TOUCH, |
| 9968 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9969 | |
| 9970 | // Assert that there's no more user activity poke event. |
| 9971 | mFakePolicy->assertUserActivityNotPoked(); |
| 9972 | } |
| 9973 | |
| 9974 | TEST_F_WITH_FLAGS( |
| 9975 | InputDispatcherUserActivityPokeTests, DefaultMinPokeTimeOf100MsUsed, |
| 9976 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 9977 | rate_limit_user_activity_poke_in_dispatcher))) { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9978 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9979 | milliseconds_to_nanoseconds(200)); |
| 9980 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9981 | {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_TOUCH, |
| 9982 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9983 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9984 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9985 | milliseconds_to_nanoseconds(280)); |
| 9986 | mFakePolicy->assertUserActivityNotPoked(); |
| 9987 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9988 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9989 | milliseconds_to_nanoseconds(340)); |
| 9990 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9991 | {{milliseconds_to_nanoseconds(340), USER_ACTIVITY_EVENT_TOUCH, |
| 9992 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9993 | } |
| 9994 | |
| 9995 | TEST_F_WITH_FLAGS( |
| 9996 | InputDispatcherUserActivityPokeTests, ZeroMinPokeTimeDisablesRateLimiting, |
| 9997 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 9998 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 9999 | mDispatcher->setMinTimeBetweenUserActivityPokes(0ms); |
| 10000 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10001 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
| 10002 | 20); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 10003 | mFakePolicy->assertUserActivityPoked(); |
| 10004 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10005 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
| 10006 | 30); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 10007 | mFakePolicy->assertUserActivityPoked(); |
| 10008 | } |
| 10009 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 10010 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 10011 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 10012 | InputDispatcherTest::SetUp(); |
| 10013 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 10014 | std::shared_ptr<FakeApplicationHandle> application = |
| 10015 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10016 | mUnfocusedWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 10017 | ui::LogicalDisplayId::DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 10018 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 10019 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10020 | mFocusedWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 10021 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 10022 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 10023 | |
| 10024 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10025 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 10026 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 10027 | |
| 10028 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10029 | mDispatcher->onWindowInfosChanged( |
| 10030 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10031 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 10032 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 10033 | } |
| 10034 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 10035 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 10036 | InputDispatcherTest::TearDown(); |
| 10037 | |
| 10038 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 10039 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 10040 | } |
| 10041 | |
| 10042 | protected: |
| 10043 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 10044 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 10045 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 10046 | }; |
| 10047 | |
| 10048 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 10049 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 10050 | // the onPointerDownOutsideFocus callback. |
| 10051 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10052 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10053 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10054 | ui::LogicalDisplayId::DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10055 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 10056 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 10057 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 10058 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 10059 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 10060 | } |
| 10061 | |
| 10062 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 10063 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 10064 | // onPointerDownOutsideFocus callback. |
| 10065 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10066 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10067 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10068 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10069 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 10070 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 10071 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 10072 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10073 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 10074 | } |
| 10075 | |
| 10076 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 10077 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 10078 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 10079 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10080 | injectKeyDownNoRepeat(*mDispatcher, ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10081 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10082 | mFocusedWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 10083 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 10084 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10085 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 10086 | } |
| 10087 | |
| 10088 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 10089 | // DOWN on the window that already has focus. Ensure no window received the |
| 10090 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 10091 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10092 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10093 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10094 | ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10095 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 10096 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 10097 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 10098 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10099 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 10100 | } |
| 10101 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 10102 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 10103 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 10104 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 10105 | const MotionEvent event = |
| 10106 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 10107 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10108 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 10109 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 10110 | .build(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10111 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 10112 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10113 | mUnfocusedWindow->consumeAnyMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 10114 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 10115 | |
| 10116 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10117 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 10118 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 10119 | mUnfocusedWindow->assertNoEvents(); |
| 10120 | } |
| 10121 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 10122 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 10123 | // windows that point to the same client token. |
| 10124 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 10125 | virtual void SetUp() override { |
| 10126 | InputDispatcherTest::SetUp(); |
| 10127 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 10128 | std::shared_ptr<FakeApplicationHandle> application = |
| 10129 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10130 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10131 | ui::LogicalDisplayId::DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 10132 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 10133 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10134 | mWindow2 = mWindow1->clone(ui::LogicalDisplayId::DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 10135 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 10136 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10137 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 10138 | } |
| 10139 | |
| 10140 | protected: |
| 10141 | sp<FakeWindowHandle> mWindow1; |
| 10142 | sp<FakeWindowHandle> mWindow2; |
| 10143 | |
| 10144 | // 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] | 10145 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 10146 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 10147 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 10148 | } |
| 10149 | |
| 10150 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 10151 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 10152 | const std::string name = window->getName(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10153 | std::unique_ptr<MotionEvent> motionEvent = |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 10154 | window->consumeMotionEvent(WithMotionAction(expectedAction)); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10155 | ASSERT_NE(nullptr, motionEvent); |
| 10156 | ASSERT_EQ(points.size(), motionEvent->getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 10157 | |
| 10158 | for (size_t i = 0; i < points.size(); i++) { |
| 10159 | float expectedX = points[i].x; |
| 10160 | float expectedY = points[i].y; |
| 10161 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10162 | EXPECT_EQ(expectedX, motionEvent->getX(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 10163 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10164 | << ", got " << motionEvent->getX(i); |
| 10165 | EXPECT_EQ(expectedY, motionEvent->getY(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 10166 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10167 | << ", got " << motionEvent->getY(i); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 10168 | } |
| 10169 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10170 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10171 | void touchAndAssertPositions(sp<FakeWindowHandle> touchedWindow, int32_t action, |
| 10172 | const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10173 | std::vector<PointF> expectedPoints) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10174 | mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10175 | ui::LogicalDisplayId::DEFAULT, touchedPoints)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10176 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10177 | consumeMotionEvent(touchedWindow, action, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10178 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 10179 | }; |
| 10180 | |
| 10181 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 10182 | // Touch Window 1 |
| 10183 | PointF touchedPoint = {10, 10}; |
| 10184 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10185 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 10186 | |
| 10187 | // Release touch on Window 1 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10188 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 10189 | |
| 10190 | // Touch Window 2 |
| 10191 | touchedPoint = {150, 150}; |
| 10192 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10193 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 10194 | } |
| 10195 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10196 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 10197 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 10198 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10199 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 10200 | |
| 10201 | // Touch Window 1 |
| 10202 | PointF touchedPoint = {10, 10}; |
| 10203 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10204 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 10205 | // Release touch on Window 1 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10206 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 10207 | |
| 10208 | // Touch Window 2 |
| 10209 | touchedPoint = {150, 150}; |
| 10210 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10211 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 10212 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 10213 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10214 | // Update the transform so rotation is set |
| 10215 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10216 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10217 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10218 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 10219 | } |
| 10220 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10221 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 10222 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10223 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 10224 | |
| 10225 | // Touch Window 1 |
| 10226 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 10227 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10228 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 10229 | |
| 10230 | // Touch Window 2 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10231 | // Since this is part of the same touch gesture that has already been dispatched to Window 1, |
| 10232 | // the touch stream from Window 2 will be merged with the stream in Window 1. The merged stream |
| 10233 | // will continue to be dispatched through Window 1. |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10234 | touchedPoints.push_back(PointF{150, 150}); |
| 10235 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10236 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 10237 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10238 | // Release Window 2 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10239 | touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10240 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 10241 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10242 | // Update the transform so rotation is set for Window 2 |
| 10243 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10244 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10245 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10246 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 10247 | } |
| 10248 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10249 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 10250 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10251 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 10252 | |
| 10253 | // Touch Window 1 |
| 10254 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 10255 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10256 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 10257 | |
| 10258 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10259 | touchedPoints.push_back(PointF{150, 150}); |
| 10260 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 10261 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10262 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 10263 | |
| 10264 | // Move both windows |
| 10265 | touchedPoints = {{20, 20}, {175, 175}}; |
| 10266 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 10267 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 10268 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10269 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 10270 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10271 | // Release Window 2 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10272 | touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10273 | expectedPoints.pop_back(); |
| 10274 | |
| 10275 | // Touch Window 2 |
| 10276 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10277 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10278 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10279 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10280 | |
| 10281 | // Move both windows |
| 10282 | touchedPoints = {{20, 20}, {175, 175}}; |
| 10283 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 10284 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 10285 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10286 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 10287 | } |
| 10288 | |
| 10289 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 10290 | mWindow1->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10291 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 10292 | |
| 10293 | // Touch Window 1 |
| 10294 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 10295 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10296 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 10297 | |
| 10298 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 10299 | touchedPoints.push_back(PointF{150, 150}); |
| 10300 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 10301 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10302 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 10303 | |
| 10304 | // Move both windows |
| 10305 | touchedPoints = {{20, 20}, {175, 175}}; |
| 10306 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 10307 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 10308 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10309 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 10310 | } |
| 10311 | |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 10312 | /** |
| 10313 | * When one of the windows is slippery, the touch should not slip into the other window with the |
| 10314 | * same input channel. |
| 10315 | */ |
| 10316 | TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) { |
| 10317 | mWindow1->setSlippery(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10318 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 10319 | |
| 10320 | // Touch down in window 1 |
| 10321 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10322 | ui::LogicalDisplayId::DEFAULT, {{50, 50}})); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 10323 | consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}}); |
| 10324 | |
| 10325 | // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip. |
| 10326 | // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN |
| 10327 | // getting generated. |
| 10328 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10329 | ui::LogicalDisplayId::DEFAULT, {{150, 150}})); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 10330 | |
| 10331 | consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}}); |
| 10332 | } |
| 10333 | |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 10334 | /** |
| 10335 | * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and |
| 10336 | * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window |
| 10337 | * that the pointer is hovering over may have a different transform. |
| 10338 | */ |
| 10339 | TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) { |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10340 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 10341 | |
| 10342 | // Start hover in window 1 |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 10343 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN) |
| 10344 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 10345 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 10346 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 10347 | {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 10348 | // Move hover to window 2. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 10349 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 10350 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 10351 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 10352 | consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}}); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10353 | consumeMotionEvent(mWindow2, ACTION_HOVER_ENTER, |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 10354 | {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})}); |
| 10355 | } |
| 10356 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 10357 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 10358 | virtual void SetUp() override { |
| 10359 | InputDispatcherTest::SetUp(); |
| 10360 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 10361 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 10362 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10363 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10364 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 10365 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 10366 | mWindow->setDispatchingTimeout(100ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 10367 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 10368 | |
| 10369 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10370 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 10371 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10372 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10373 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 10374 | mWindow->consumeFocusEvent(true); |
| 10375 | } |
| 10376 | |
| 10377 | virtual void TearDown() override { |
| 10378 | InputDispatcherTest::TearDown(); |
| 10379 | mWindow.clear(); |
| 10380 | } |
| 10381 | |
| 10382 | protected: |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10383 | static constexpr std::chrono::duration SPY_TIMEOUT = 200ms; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 10384 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 10385 | sp<FakeWindowHandle> mWindow; |
| 10386 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 10387 | |
| 10388 | void tapOnWindow() { |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 10389 | const auto touchingPointer = PointerBuilder(/*id=*/0, ToolType::FINGER) |
| 10390 | .x(WINDOW_LOCATION.x) |
| 10391 | .y(WINDOW_LOCATION.y); |
| 10392 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10393 | .pointer(touchingPointer) |
| 10394 | .build()); |
| 10395 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 10396 | .pointer(touchingPointer) |
| 10397 | .build()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 10398 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10399 | |
| 10400 | sp<FakeWindowHandle> addSpyWindow() { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10401 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", |
| 10402 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10403 | spy->setTrustedOverlay(true); |
| 10404 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10405 | spy->setSpy(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10406 | spy->setDispatchingTimeout(SPY_TIMEOUT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10407 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10408 | return spy; |
| 10409 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 10410 | }; |
| 10411 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10412 | // Send a tap and respond, which should not cause an ANR. |
| 10413 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 10414 | tapOnWindow(); |
| 10415 | mWindow->consumeMotionDown(); |
| 10416 | mWindow->consumeMotionUp(); |
| 10417 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10418 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 10419 | } |
| 10420 | |
| 10421 | // Send a regular key and respond, which should not cause an ANR. |
| 10422 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10423 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10424 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10425 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10426 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 10427 | } |
| 10428 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 10429 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 10430 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10431 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 10432 | mWindow->consumeFocusEvent(false); |
| 10433 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10434 | InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10435 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 10436 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
| 10437 | CONSUME_TIMEOUT_EVENT_EXPECTED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10438 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10439 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 10440 | // Key will not go to window because we have no focused window. |
| 10441 | // The 'no focused window' ANR timer should start instead. |
| 10442 | |
| 10443 | // Now, the focused application goes away. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10444 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, nullptr); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 10445 | // The key should get dropped and there should be no ANR. |
| 10446 | |
| 10447 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10448 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 10449 | } |
| 10450 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 10451 | // 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] | 10452 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 10453 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 10454 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10455 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10456 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10457 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 10458 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10459 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 10460 | ASSERT_TRUE(sequenceNum); |
| 10461 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10462 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10463 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10464 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 10465 | mWindow->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10466 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 10467 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10468 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 10469 | } |
| 10470 | |
| 10471 | // Send a key to the app and have the app not respond right away. |
| 10472 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 10473 | // Inject a key, and don't respond - expect that ANR is called. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10474 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10475 | const auto [sequenceNum, _] = mWindow->receiveEvent(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 10476 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 10477 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10478 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 10479 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10480 | } |
| 10481 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10482 | // We have a focused application, but no focused window |
| 10483 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 10484 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10485 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10486 | mWindow->consumeFocusEvent(false); |
| 10487 | |
| 10488 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10489 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10490 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10491 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10492 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 10493 | mDispatcher->waitForIdle(); |
| 10494 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 10495 | |
| 10496 | // Once a focused event arrives, we get an ANR for this application |
| 10497 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 10498 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10499 | const InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10500 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 10501 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 10502 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10503 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10504 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 10505 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10506 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10507 | } |
| 10508 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 10509 | /** |
| 10510 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 10511 | * there will not be an ANR. |
| 10512 | */ |
| 10513 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 10514 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10515 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 10516 | mWindow->consumeFocusEvent(false); |
| 10517 | |
| 10518 | KeyEvent event; |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 10519 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 10520 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 10521 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 10522 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 10523 | |
| 10524 | // Define a valid key down event that is stale (too old). |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10525 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 10526 | ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, |
| 10527 | /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, |
| 10528 | eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 10529 | |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 10530 | const int32_t policyFlags = |
| 10531 | 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] | 10532 | |
| 10533 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10534 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 10535 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 10536 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 10537 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 10538 | << "Injection should fail because the event is stale"; |
| 10539 | |
| 10540 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10541 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 10542 | mWindow->assertNoEvents(); |
| 10543 | } |
| 10544 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10545 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10546 | // Make sure that we don't notify policy twice about the same ANR. |
| 10547 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 10548 | const std::chrono::duration appTimeout = 400ms; |
| 10549 | mApplication->setDispatchingTimeout(appTimeout); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10550 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication); |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 10551 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 10552 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10553 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10554 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10555 | |
| 10556 | // Once a focused event arrives, we get an ANR for this application |
| 10557 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 10558 | // injection times out (instead of failing). |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 10559 | const std::chrono::duration eventInjectionTimeout = 100ms; |
| 10560 | ASSERT_LT(eventInjectionTimeout, appTimeout); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10561 | const InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10562 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 10563 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 10564 | eventInjectionTimeout, |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 10565 | /*allowKeyRepeat=*/false); |
| 10566 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result) |
| 10567 | << "result=" << ftl::enum_string(result); |
| 10568 | // We already waited for 'eventInjectionTimeout`, because the countdown started when the event |
| 10569 | // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait. |
| 10570 | std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout; |
| 10571 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10572 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 10573 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10574 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 10575 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10576 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10577 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 10578 | // '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] | 10579 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10580 | } |
| 10581 | |
| 10582 | // We have a focused application, but no focused window |
| 10583 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 10584 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10585 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10586 | mWindow->consumeFocusEvent(false); |
| 10587 | |
| 10588 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10589 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10590 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 10591 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 10592 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10593 | |
| 10594 | // Future focused events get dropped right away |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10595 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10596 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10597 | mWindow->assertNoEvents(); |
| 10598 | } |
| 10599 | |
| 10600 | /** |
| 10601 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 10602 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 10603 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 10604 | * the ANR mechanism should still work. |
| 10605 | * |
| 10606 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 10607 | * DOWN event, while not responding on the second one. |
| 10608 | */ |
| 10609 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 10610 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10611 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10612 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10613 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 10614 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10615 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10616 | |
| 10617 | // Now send ACTION_UP, with identical timestamp |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10618 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10619 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10620 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 10621 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10622 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10623 | |
| 10624 | // 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] | 10625 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10626 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10627 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10628 | } |
| 10629 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10630 | // A spy window can receive an ANR |
| 10631 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 10632 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 10633 | |
| 10634 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10635 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10636 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10637 | mWindow->consumeMotionDown(); |
| 10638 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10639 | const auto [sequenceNum, _] = spy->receiveEvent(); // ACTION_DOWN |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10640 | ASSERT_TRUE(sequenceNum); |
| 10641 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10642 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10643 | |
| 10644 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 10645 | spy->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10646 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10647 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10648 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10649 | } |
| 10650 | |
| 10651 | // 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] | 10652 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10653 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 10654 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10655 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10656 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10657 | injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT)); |
| 10658 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
| 10659 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10660 | injectKeyUp(*mDispatcher, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10661 | |
| 10662 | // Stuck on the ACTION_UP |
| 10663 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10664 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10665 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10666 | // 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] | 10667 | tapOnWindow(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10668 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 10669 | spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10670 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10671 | mWindow->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); // still the previous motion |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10672 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10673 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10674 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10675 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10676 | } |
| 10677 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10678 | // 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] | 10679 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10680 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 10681 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10682 | |
| 10683 | tapOnWindow(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10684 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 10685 | spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10686 | |
| 10687 | mWindow->consumeMotionDown(); |
| 10688 | // Stuck on the ACTION_UP |
| 10689 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10690 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10691 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10692 | // 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] | 10693 | tapOnWindow(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10694 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 10695 | spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10696 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10697 | mWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); // still the previous motion |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10698 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10699 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10700 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10701 | spy->assertNoEvents(); |
| 10702 | } |
| 10703 | |
| 10704 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10705 | mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10706 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10707 | FakeMonitorReceiver monitor = |
| 10708 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10709 | |
| 10710 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10711 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10712 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10713 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10714 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10715 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 10716 | ASSERT_TRUE(consumeSeq); |
| 10717 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10718 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(), |
| 10719 | MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10720 | |
| 10721 | monitor.finishEvent(*consumeSeq); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10722 | monitor.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10723 | |
| 10724 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10725 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10726 | } |
| 10727 | |
| 10728 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 10729 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 10730 | // get an ANR again. |
| 10731 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 10732 | // 2. consume all pending events (= queue becomes healthy again) |
| 10733 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 10734 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 10735 | tapOnWindow(); |
| 10736 | |
| 10737 | mWindow->consumeMotionDown(); |
| 10738 | // Block on ACTION_UP |
| 10739 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10740 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10741 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 10742 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10743 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10744 | mWindow->assertNoEvents(); |
| 10745 | |
| 10746 | tapOnWindow(); |
| 10747 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10748 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10749 | mWindow->consumeMotionUp(); |
| 10750 | |
| 10751 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10752 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10753 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10754 | mWindow->assertNoEvents(); |
| 10755 | } |
| 10756 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10757 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 10758 | // it. |
| 10759 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10760 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10761 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10762 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10763 | |
| 10764 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10765 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10766 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10767 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 10768 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 10769 | // 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] | 10770 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 10771 | mWindow->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10772 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10773 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10774 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10775 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10776 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10777 | } |
| 10778 | |
| 10779 | /** |
| 10780 | * 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] | 10781 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10782 | */ |
| 10783 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 10784 | // The timeouts in this test are established by relying on the fact that the "key waiting for |
| 10785 | // events timeout" is equal to 500ms. |
| 10786 | ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10787 | 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] | 10788 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10789 | |
| 10790 | tapOnWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10791 | const auto& [downSequenceNum, downEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10792 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10793 | const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10794 | ASSERT_TRUE(upSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10795 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 10796 | // Don't finish the events yet, and send a key |
| 10797 | mDispatcher->notifyKey( |
| 10798 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 10799 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 10800 | .build()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10801 | // Key will not be sent to the window, yet, because the window is still processing events |
| 10802 | // 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] | 10803 | // 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] | 10804 | mWindow->assertNoEvents(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10805 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 10806 | std::this_thread::sleep_for(400ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10807 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 10808 | // 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] | 10809 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10810 | mWindow->finishEvent(*downSequenceNum); |
| 10811 | mWindow->finishEvent(*upSequenceNum); |
| 10812 | } |
| 10813 | |
| 10814 | /** |
| 10815 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 10816 | * not go to the focused window until the motion is processed. |
| 10817 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 10818 | * focused window right away. |
| 10819 | */ |
| 10820 | TEST_F(InputDispatcherSingleWindowAnr, |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 10821 | PendingKey_IsDeliveredWhileMotionIsProcessingAndNewTouchComesIn) { |
| 10822 | // The timeouts in this test are established by relying on the fact that the "key waiting for |
| 10823 | // events timeout" is equal to 500ms. |
| 10824 | ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10825 | 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] | 10826 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10827 | |
| 10828 | tapOnWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10829 | const auto& [downSequenceNum, _] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10830 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10831 | const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10832 | ASSERT_TRUE(upSequenceNum); |
| 10833 | // Don't finish the events yet, and send a key |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 10834 | mDispatcher->notifyKey( |
| 10835 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 10836 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 10837 | .build()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10838 | // 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] | 10839 | mWindow->assertNoEvents(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10840 | |
| 10841 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 10842 | tapOnWindow(); |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 10843 | // Now that we tapped, we should receive the key immediately. |
| 10844 | // Since there's still room for slowness, we use 200ms, which is much less than |
| 10845 | // the "key waiting for events' timeout of 500ms minus the already waited 100ms duration. |
| 10846 | std::unique_ptr<InputEvent> keyEvent = mWindow->consume(200ms); |
| 10847 | ASSERT_NE(nullptr, keyEvent); |
| 10848 | ASSERT_EQ(InputEventType::KEY, keyEvent->getType()); |
| 10849 | ASSERT_THAT(static_cast<KeyEvent&>(*keyEvent), WithKeyAction(AKEY_EVENT_ACTION_DOWN)); |
| 10850 | // it doesn't matter that we haven't ack'd the other events yet. We can finish events in any |
| 10851 | // order. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10852 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 10853 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 10854 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 10855 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10856 | mWindow->assertNoEvents(); |
| 10857 | } |
| 10858 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 10859 | /** |
| 10860 | * Send an event to the app and have the app not respond right away. |
| 10861 | * When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 10862 | * So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 10863 | * At some point, the window becomes responsive again. |
| 10864 | * Ensure that subsequent events get dropped, and the next gesture is delivered. |
| 10865 | */ |
| 10866 | TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) { |
| 10867 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10868 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 10869 | .build()); |
| 10870 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10871 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 10872 | ASSERT_TRUE(sequenceNum); |
| 10873 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 10874 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 10875 | |
| 10876 | mWindow->finishEvent(*sequenceNum); |
| 10877 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 10878 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10879 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
| 10880 | |
| 10881 | // Now that the window is responsive, let's continue the gesture. |
| 10882 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 10883 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 10884 | .build()); |
| 10885 | |
| 10886 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10887 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 10888 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 10889 | .build()); |
| 10890 | |
| 10891 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 10892 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 10893 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 10894 | .build()); |
| 10895 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 10896 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 10897 | .build()); |
| 10898 | // We already canceled this pointer, so the window shouldn't get any new events. |
| 10899 | mWindow->assertNoEvents(); |
| 10900 | |
| 10901 | // Start another one. |
| 10902 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10903 | .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15)) |
| 10904 | .build()); |
| 10905 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 10906 | } |
| 10907 | |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10908 | // Send an event to the app and have the app not respond right away. Then remove the app window. |
| 10909 | // When the window is removed, the dispatcher will cancel the events for that window. |
| 10910 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 10911 | TEST_F(InputDispatcherSingleWindowAnr, AnrAfterWindowRemoval) { |
| 10912 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10913 | AINPUT_SOURCE_TOUCHSCREEN, |
| 10914 | ui::LogicalDisplayId::DEFAULT, {WINDOW_LOCATION})); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10915 | |
| 10916 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
| 10917 | ASSERT_TRUE(sequenceNum); |
| 10918 | |
| 10919 | // Remove the window, but the input channel should remain alive. |
| 10920 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 10921 | |
| 10922 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 10923 | // Since the window was removed, Dispatcher does not know the PID associated with the window |
| 10924 | // anymore, so the policy is notified without the PID. |
| 10925 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken(), |
| 10926 | /*pid=*/std::nullopt); |
| 10927 | |
| 10928 | mWindow->finishEvent(*sequenceNum); |
| 10929 | // The cancellation was generated when the window was removed, along with the focus event. |
| 10930 | mWindow->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10931 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10932 | mWindow->consumeFocusEvent(false); |
| 10933 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10934 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt); |
| 10935 | } |
| 10936 | |
| 10937 | // Send an event to the app and have the app not respond right away. Wait for the policy to be |
| 10938 | // notified of the unresponsive window, then remove the app window. |
| 10939 | TEST_F(InputDispatcherSingleWindowAnr, AnrFollowedByWindowRemoval) { |
| 10940 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10941 | AINPUT_SOURCE_TOUCHSCREEN, |
| 10942 | ui::LogicalDisplayId::DEFAULT, {WINDOW_LOCATION})); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10943 | |
| 10944 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
| 10945 | ASSERT_TRUE(sequenceNum); |
| 10946 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 10947 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 10948 | |
| 10949 | // Remove the window, but the input channel should remain alive. |
| 10950 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 10951 | |
| 10952 | mWindow->finishEvent(*sequenceNum); |
| 10953 | // The cancellation was generated during the ANR, and the window lost focus when it was removed. |
| 10954 | mWindow->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10955 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10956 | mWindow->consumeFocusEvent(false); |
| 10957 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10958 | // Since the window was removed, Dispatcher does not know the PID associated with the window |
| 10959 | // becoming responsive, so the policy is notified without the PID. |
| 10960 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt); |
| 10961 | } |
| 10962 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10963 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 10964 | virtual void SetUp() override { |
| 10965 | InputDispatcherTest::SetUp(); |
| 10966 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 10967 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 10968 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10969 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10970 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10971 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10972 | // 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] | 10973 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10974 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10975 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10976 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 10977 | mFocusedWindow->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10978 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10979 | |
| 10980 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10981 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 10982 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10983 | |
| 10984 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10985 | mDispatcher->onWindowInfosChanged( |
| 10986 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10987 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10988 | mFocusedWindow->consumeFocusEvent(true); |
| 10989 | } |
| 10990 | |
| 10991 | virtual void TearDown() override { |
| 10992 | InputDispatcherTest::TearDown(); |
| 10993 | |
| 10994 | mUnfocusedWindow.clear(); |
| 10995 | mFocusedWindow.clear(); |
| 10996 | } |
| 10997 | |
| 10998 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 10999 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11000 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 11001 | sp<FakeWindowHandle> mFocusedWindow; |
| 11002 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 11003 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 11004 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 11005 | |
| 11006 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 11007 | |
| 11008 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 11009 | |
| 11010 | private: |
| 11011 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11012 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11013 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 11014 | ui::LogicalDisplayId::DEFAULT, location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11015 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11016 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 11017 | ui::LogicalDisplayId::DEFAULT, location)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11018 | } |
| 11019 | }; |
| 11020 | |
| 11021 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 11022 | // should be ANR'd first. |
| 11023 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11024 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11025 | injectMotionEvent(*mDispatcher, |
| 11026 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 11027 | AINPUT_SOURCE_TOUCHSCREEN) |
| 11028 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 11029 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 11030 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 11031 | .build())); |
| 11032 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11033 | injectMotionEvent(*mDispatcher, |
| 11034 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 11035 | AINPUT_SOURCE_TOUCHSCREEN) |
| 11036 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 11037 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 11038 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 11039 | .build())); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11040 | mFocusedWindow->consumeMotionDown(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11041 | mFocusedWindow->consumeMotionUp(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11042 | mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11043 | // We consumed all events, so no ANR |
| 11044 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 11045 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 11046 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11047 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11048 | injectMotionEvent(*mDispatcher, |
| 11049 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 11050 | AINPUT_SOURCE_TOUCHSCREEN) |
| 11051 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 11052 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 11053 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 11054 | .build())); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 11055 | const auto [unfocusedSequenceNum, _] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11056 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11057 | |
| 11058 | const std::chrono::duration timeout = |
| 11059 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 11060 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11061 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11062 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 11063 | mFocusedWindow->consumeMotionDown(); |
| 11064 | // This cancel is generated because the connection was unresponsive |
| 11065 | mFocusedWindow->consumeMotionCancel(); |
| 11066 | mFocusedWindow->assertNoEvents(); |
| 11067 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11068 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 11069 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 11070 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 11071 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11072 | } |
| 11073 | |
| 11074 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 11075 | // it doesn't matter which order they should have ANR. |
| 11076 | // But we should receive ANR for both. |
| 11077 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 11078 | // Set the timeout for unfocused window to match the focused window |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 11079 | mUnfocusedWindow->setDispatchingTimeout( |
| 11080 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11081 | mDispatcher->onWindowInfosChanged( |
| 11082 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11083 | |
| 11084 | tapOnFocusedWindow(); |
| 11085 | // 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] | 11086 | // 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] | 11087 | std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken( |
| 11088 | mFocusedWindow->getDispatchingTimeout( |
| 11089 | DISPATCHING_TIMEOUT)), |
| 11090 | mFakePolicy->getUnresponsiveWindowToken(0ms)}; |
| 11091 | |
| 11092 | ASSERT_THAT(anrConnectionTokens, |
| 11093 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 11094 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11095 | |
| 11096 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 11097 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 11098 | |
| 11099 | mFocusedWindow->consumeMotionDown(); |
| 11100 | mFocusedWindow->consumeMotionUp(); |
| 11101 | mUnfocusedWindow->consumeMotionOutside(); |
| 11102 | |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 11103 | std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(), |
| 11104 | mFakePolicy->getResponsiveWindowToken()}; |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 11105 | |
| 11106 | // Both applications should be marked as responsive, in any order |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 11107 | ASSERT_THAT(responsiveTokens, |
| 11108 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 11109 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 11110 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11111 | } |
| 11112 | |
| 11113 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 11114 | // We should also log an error to account for the dropped event (not tested here). |
| 11115 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 11116 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 11117 | tapOnFocusedWindow(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11118 | mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11119 | // Receive the events, but don't respond |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 11120 | const auto [downEventSequenceNum, downEvent] = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11121 | ASSERT_TRUE(downEventSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 11122 | const auto [upEventSequenceNum, upEvent] = mFocusedWindow->receiveEvent(); // ACTION_UP |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11123 | ASSERT_TRUE(upEventSequenceNum); |
| 11124 | const std::chrono::duration timeout = |
| 11125 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 11126 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11127 | |
| 11128 | // Tap once again |
| 11129 | // 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] | 11130 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11131 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 11132 | ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11133 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11134 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11135 | FOCUSED_WINDOW_LOCATION)); |
| 11136 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 11137 | // valid touch target |
| 11138 | mUnfocusedWindow->assertNoEvents(); |
| 11139 | |
| 11140 | // Consume the first tap |
| 11141 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 11142 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 11143 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 11144 | // The second tap did not go to the focused window |
| 11145 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 11146 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 11147 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 11148 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11149 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 11150 | } |
| 11151 | |
| 11152 | // If you tap outside of all windows, there will not be ANR |
| 11153 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11154 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11155 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 11156 | ui::LogicalDisplayId::DEFAULT, LOCATION_OUTSIDE_ALL_WINDOWS)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11157 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 11158 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 11159 | } |
| 11160 | |
| 11161 | // Since the focused window is paused, tapping on it should not produce any events |
| 11162 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 11163 | mFocusedWindow->setPaused(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11164 | mDispatcher->onWindowInfosChanged( |
| 11165 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11166 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11167 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11168 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 11169 | ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11170 | |
| 11171 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 11172 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 11173 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 11174 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 11175 | |
| 11176 | mFocusedWindow->assertNoEvents(); |
| 11177 | mUnfocusedWindow->assertNoEvents(); |
| 11178 | } |
| 11179 | |
| 11180 | /** |
| 11181 | * 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] | 11182 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11183 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 11184 | * |
| 11185 | * Warning!!! |
| 11186 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 11187 | * and the injection timeout that we specify when injecting the key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11188 | * We must have the injection timeout (100ms) be smaller than |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11189 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 11190 | * |
| 11191 | * If that value changes, this test should also change. |
| 11192 | */ |
| 11193 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 11194 | // Set a long ANR timeout to prevent it from triggering |
| 11195 | mFocusedWindow->setDispatchingTimeout(2s); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11196 | mDispatcher->onWindowInfosChanged( |
| 11197 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11198 | |
| 11199 | tapOnUnfocusedWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 11200 | const auto [downSequenceNum, downEvent] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11201 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 11202 | const auto [upSequenceNum, upEvent] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11203 | ASSERT_TRUE(upSequenceNum); |
| 11204 | // Don't finish the events yet, and send a key |
| 11205 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 11206 | // window even if motions are still being processed. |
| 11207 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11208 | InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11209 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 11210 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 11211 | /*injectionTimeout=*/100ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11212 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11213 | // 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] | 11214 | // and the key remains pending, waiting for the touch events to be processed. |
| 11215 | // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED |
| 11216 | // under the hood. |
| 11217 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 11218 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11219 | |
| 11220 | // 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] | 11221 | mFocusedWindow->setFocusable(false); |
| 11222 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11223 | mDispatcher->onWindowInfosChanged( |
| 11224 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11225 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11226 | |
| 11227 | // Focus events should precede the key events |
| 11228 | mUnfocusedWindow->consumeFocusEvent(true); |
| 11229 | mFocusedWindow->consumeFocusEvent(false); |
| 11230 | |
| 11231 | // Finish the tap events, which should unblock dispatcher |
| 11232 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 11233 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 11234 | |
| 11235 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 11236 | // can finally go to the newly focused "mUnfocusedWindow". |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11237 | mUnfocusedWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11238 | mFocusedWindow->assertNoEvents(); |
| 11239 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 11240 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11241 | } |
| 11242 | |
| 11243 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 11244 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 11245 | // The other window should not be affected by that. |
| 11246 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 11247 | // Touch Window 1 |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11248 | mDispatcher->notifyMotion( |
| 11249 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 11250 | ui::LogicalDisplayId::DEFAULT, {FOCUSED_WINDOW_LOCATION})); |
| 11251 | mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11252 | |
| 11253 | // Touch Window 2 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11254 | mDispatcher->notifyMotion( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11255 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 11256 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11257 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11258 | |
| 11259 | const std::chrono::duration timeout = |
| 11260 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 11261 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11262 | |
| 11263 | mUnfocusedWindow->consumeMotionDown(); |
| 11264 | mFocusedWindow->consumeMotionDown(); |
| 11265 | // Focused window may or may not receive ACTION_MOVE |
| 11266 | // But it should definitely receive ACTION_CANCEL due to the ANR |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 11267 | const auto [moveOrCancelSequenceNum, event] = mFocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11268 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 11269 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 11270 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 11271 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11272 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 11273 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 11274 | mFocusedWindow->consumeMotionCancel(); |
| 11275 | } else { |
| 11276 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 11277 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11278 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 11279 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 11280 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 11281 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11282 | mUnfocusedWindow->assertNoEvents(); |
| 11283 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 11284 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 11285 | } |
| 11286 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 11287 | /** |
| 11288 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 11289 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 11290 | * |
| 11291 | * If the user touches another application during this time, the key should be dropped. |
| 11292 | * Next, if a new focused window comes in, without toggling the focused application, |
| 11293 | * then no ANR should occur. |
| 11294 | * |
| 11295 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 11296 | * but in some cases the policy may not update the focused application. |
| 11297 | */ |
| 11298 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 11299 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 11300 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | c033dfb | 2023-10-03 10:45:16 -0700 | [diff] [blame] | 11301 | focusedApplication->setDispatchingTimeout(300ms); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11302 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, focusedApplication); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 11303 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 11304 | mFocusedWindow->setFocusable(false); |
| 11305 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11306 | mDispatcher->onWindowInfosChanged( |
| 11307 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 11308 | mFocusedWindow->consumeFocusEvent(false); |
| 11309 | |
| 11310 | // Send a key. The ANR timer should start because there is no focused window. |
| 11311 | // 'focusedApplication' will get blamed if this timer completes. |
| 11312 | // 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] | 11313 | InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11314 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 11315 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 11316 | /*injectionTimeout=*/100ms, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11317 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11318 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 11319 | |
| 11320 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 11321 | // then the injected touches won't cause the focused event to get dropped. |
| 11322 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 11323 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 11324 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 11325 | // For this test, it means that the key would get delivered to the window once it becomes |
| 11326 | // focused. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11327 | std::this_thread::sleep_for(100ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 11328 | |
| 11329 | // Touch unfocused window. This should force the pending key to get dropped. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11330 | mDispatcher->notifyMotion( |
| 11331 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 11332 | ui::LogicalDisplayId::DEFAULT, {UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 11333 | |
| 11334 | // We do not consume the motion right away, because that would require dispatcher to first |
| 11335 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 11336 | // Set the focused window first. |
| 11337 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11338 | mDispatcher->onWindowInfosChanged( |
| 11339 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 11340 | setFocusedWindow(mFocusedWindow); |
| 11341 | mFocusedWindow->consumeFocusEvent(true); |
| 11342 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 11343 | // to another application. This could be a bug / behaviour in the policy. |
| 11344 | |
| 11345 | mUnfocusedWindow->consumeMotionDown(); |
| 11346 | |
| 11347 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 11348 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 11349 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 11350 | } |
| 11351 | |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 11352 | /** |
| 11353 | * If we are pruning input queue, we should never drop pointer events. Otherwise, we risk having |
| 11354 | * an inconsistent event stream inside the dispatcher. In this test, we make sure that the |
| 11355 | * dispatcher doesn't prune pointer events incorrectly. |
| 11356 | * |
| 11357 | * This test reproduces a crash in InputDispatcher. |
| 11358 | * To reproduce the crash, we need to simulate the conditions for "pruning input queue" to occur. |
| 11359 | * |
| 11360 | * Keep the currently focused application (mApplication), and have no focused window. |
| 11361 | * We set up two additional windows: |
| 11362 | * 1) The navigation bar window. This simulates the system "NavigationBar", which is used in the |
| 11363 | * 3-button navigation mode. This window injects a BACK button when it's touched. 2) The application |
| 11364 | * window. This window is not focusable, but is touchable. |
| 11365 | * |
| 11366 | * We first touch the navigation bar, which causes it to inject a key. Since there's no focused |
| 11367 | * window, the dispatcher doesn't process this key, and all other events inside dispatcher are now |
| 11368 | * blocked. The dispatcher is waiting for 'mApplication' to add a focused window. |
| 11369 | * |
| 11370 | * Now, we touch "Another window". This window is owned by a different application than |
| 11371 | * 'mApplication'. This causes the dispatcher to stop waiting for 'mApplication' to add a focused |
| 11372 | * window. Now, the "pruning input queue" behaviour should kick in, and the dispatcher should start |
| 11373 | * dropping the events from its queue. Ensure that no crash occurs. |
| 11374 | * |
| 11375 | * In this test, we are setting long timeouts to prevent ANRs and events dropped due to being stale. |
| 11376 | * This does not affect the test running time. |
| 11377 | */ |
| 11378 | TEST_F(InputDispatcherMultiWindowAnr, PruningInputQueueShouldNotDropPointerEvents) { |
| 11379 | std::shared_ptr<FakeApplicationHandle> systemUiApplication = |
| 11380 | std::make_shared<FakeApplicationHandle>(); |
| 11381 | systemUiApplication->setDispatchingTimeout(3000ms); |
| 11382 | mFakePolicy->setStaleEventTimeout(3000ms); |
| 11383 | sp<FakeWindowHandle> navigationBar = |
| 11384 | sp<FakeWindowHandle>::make(systemUiApplication, mDispatcher, "NavigationBar", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11385 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 11386 | navigationBar->setFocusable(false); |
| 11387 | navigationBar->setWatchOutsideTouch(true); |
| 11388 | navigationBar->setFrame(Rect(0, 0, 100, 100)); |
| 11389 | |
| 11390 | mApplication->setDispatchingTimeout(3000ms); |
| 11391 | // '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] | 11392 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication); |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 11393 | |
| 11394 | std::shared_ptr<FakeApplicationHandle> anotherApplication = |
| 11395 | std::make_shared<FakeApplicationHandle>(); |
| 11396 | sp<FakeWindowHandle> appWindow = |
| 11397 | sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Another window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11398 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 11399 | appWindow->setFocusable(false); |
| 11400 | appWindow->setFrame(Rect(100, 100, 200, 200)); |
| 11401 | |
| 11402 | mDispatcher->onWindowInfosChanged( |
| 11403 | {{*navigationBar->getInfo(), *appWindow->getInfo()}, {}, 0, 0}); |
| 11404 | // 'mFocusedWindow' is no longer in the dispatcher window list, and therefore loses focus |
| 11405 | mFocusedWindow->consumeFocusEvent(false); |
| 11406 | |
| 11407 | // Touch down the navigation bar. It consumes the touch and injects a key into the dispatcher |
| 11408 | // in response. |
| 11409 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11410 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 11411 | .build()); |
| 11412 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 11413 | |
| 11414 | // Key will not be sent anywhere because we have no focused window. It will remain pending. |
| 11415 | // Pretend we are injecting KEYCODE_BACK, but it doesn't actually matter what key it is. |
| 11416 | InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11417 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 11418 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 11419 | /*injectionTimeout=*/100ms, |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 11420 | /*allowKeyRepeat=*/false); |
| 11421 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
| 11422 | |
| 11423 | // Finish the gesture - lift up finger and inject ACTION_UP key event |
| 11424 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 11425 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 11426 | .build()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11427 | result = injectKey(*mDispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, |
| 11428 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 11429 | /*injectionTimeout=*/100ms, |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 11430 | /*allowKeyRepeat=*/false); |
| 11431 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
| 11432 | // The key that was injected is blocking the dispatcher, so the navigation bar shouldn't be |
| 11433 | // getting any events yet. |
| 11434 | navigationBar->assertNoEvents(); |
| 11435 | |
| 11436 | // Now touch "Another window". This touch is going to a different application than the one we |
| 11437 | // are waiting for (which is 'mApplication'). |
| 11438 | // This should cause the dispatcher to drop the pending focus-dispatched events (like the key |
| 11439 | // trying to be injected) and to continue processing the rest of the events in the original |
| 11440 | // order. |
| 11441 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11442 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 11443 | .build()); |
| 11444 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 11445 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_OUTSIDE)); |
| 11446 | appWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 11447 | |
| 11448 | appWindow->assertNoEvents(); |
| 11449 | navigationBar->assertNoEvents(); |
| 11450 | } |
| 11451 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 11452 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 11453 | // that has feature NO_INPUT_CHANNEL. |
| 11454 | // Layout: |
| 11455 | // Top (closest to user) |
| 11456 | // mNoInputWindow (above all windows) |
| 11457 | // mBottomWindow |
| 11458 | // Bottom (furthest from user) |
| 11459 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 11460 | virtual void SetUp() override { |
| 11461 | InputDispatcherTest::SetUp(); |
| 11462 | |
| 11463 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11464 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 11465 | "Window without input channel", |
| 11466 | ui::LogicalDisplayId::DEFAULT, |
| 11467 | /*createInputChannel=*/false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11468 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 11469 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 11470 | // It's perfectly valid for this window to not have an associated input channel |
| 11471 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11472 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11473 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 11474 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 11475 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11476 | mDispatcher->onWindowInfosChanged( |
| 11477 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 11478 | } |
| 11479 | |
| 11480 | protected: |
| 11481 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 11482 | sp<FakeWindowHandle> mNoInputWindow; |
| 11483 | sp<FakeWindowHandle> mBottomWindow; |
| 11484 | }; |
| 11485 | |
| 11486 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 11487 | PointF touchedPoint = {10, 10}; |
| 11488 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11489 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11490 | AINPUT_SOURCE_TOUCHSCREEN, |
| 11491 | ui::LogicalDisplayId::DEFAULT, {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 11492 | |
| 11493 | mNoInputWindow->assertNoEvents(); |
| 11494 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 11495 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 11496 | // and therefore should prevent mBottomWindow from receiving touches |
| 11497 | mBottomWindow->assertNoEvents(); |
| 11498 | } |
| 11499 | |
| 11500 | /** |
| 11501 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 11502 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 11503 | */ |
| 11504 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 11505 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11506 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 11507 | "Window with input channel and NO_INPUT_CHANNEL", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11508 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 11509 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11510 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 11511 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11512 | mDispatcher->onWindowInfosChanged( |
| 11513 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 11514 | |
| 11515 | PointF touchedPoint = {10, 10}; |
| 11516 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11517 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11518 | AINPUT_SOURCE_TOUCHSCREEN, |
| 11519 | ui::LogicalDisplayId::DEFAULT, {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 11520 | |
| 11521 | mNoInputWindow->assertNoEvents(); |
| 11522 | mBottomWindow->assertNoEvents(); |
| 11523 | } |
| 11524 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11525 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 11526 | protected: |
| 11527 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 11528 | sp<FakeWindowHandle> mWindow; |
| 11529 | sp<FakeWindowHandle> mMirror; |
| 11530 | |
| 11531 | virtual void SetUp() override { |
| 11532 | InputDispatcherTest::SetUp(); |
| 11533 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11534 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", |
| 11535 | ui::LogicalDisplayId::DEFAULT); |
| 11536 | mMirror = mWindow->clone(ui::LogicalDisplayId::DEFAULT); |
| 11537 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11538 | mWindow->setFocusable(true); |
| 11539 | mMirror->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11540 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11541 | } |
| 11542 | }; |
| 11543 | |
| 11544 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 11545 | // Request focus on a mirrored window |
| 11546 | setFocusedWindow(mMirror); |
| 11547 | |
| 11548 | // window gets focused |
| 11549 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11550 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11551 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11552 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11553 | } |
| 11554 | |
| 11555 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 11556 | // focusable. |
| 11557 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 11558 | setFocusedWindow(mMirror); |
| 11559 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 11560 | // window gets focused because it is above the mirror |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11561 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11562 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11563 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11564 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11565 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11566 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11567 | mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11568 | |
| 11569 | mMirror->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11570 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11571 | |
| 11572 | // window loses focus since one of the windows associated with the token in not focusable |
| 11573 | mWindow->consumeFocusEvent(false); |
| 11574 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11575 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11576 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11577 | mWindow->assertNoEvents(); |
| 11578 | } |
| 11579 | |
| 11580 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 11581 | // invisible. |
| 11582 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 11583 | setFocusedWindow(mMirror); |
| 11584 | |
| 11585 | // window gets focused |
| 11586 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11587 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11588 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11589 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11590 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11591 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11592 | mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11593 | |
| 11594 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11595 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11596 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11597 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11598 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11599 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11600 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11601 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11602 | mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11603 | |
| 11604 | mWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11605 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11606 | |
| 11607 | // window loses focus only after all windows associated with the token become invisible. |
| 11608 | mWindow->consumeFocusEvent(false); |
| 11609 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11610 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11611 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11612 | mWindow->assertNoEvents(); |
| 11613 | } |
| 11614 | |
| 11615 | // A focused & mirrored window remains focused until both windows are removed. |
| 11616 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 11617 | setFocusedWindow(mMirror); |
| 11618 | |
| 11619 | // window gets focused |
| 11620 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11621 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11622 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11623 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11624 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11625 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11626 | mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11627 | |
| 11628 | // single window is removed but the window token remains focused |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11629 | mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11630 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11631 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11632 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11633 | mMirror->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11634 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11635 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11636 | mMirror->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11637 | |
| 11638 | // Both windows are removed |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11639 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11640 | mWindow->consumeFocusEvent(false); |
| 11641 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11642 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11643 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11644 | mWindow->assertNoEvents(); |
| 11645 | } |
| 11646 | |
| 11647 | // Focus request can be pending until one window becomes visible. |
| 11648 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 11649 | // Request focus on an invisible mirror. |
| 11650 | mWindow->setVisible(false); |
| 11651 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11652 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11653 | setFocusedWindow(mMirror); |
| 11654 | |
| 11655 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 11656 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11657 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11658 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11659 | |
| 11660 | mMirror->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11661 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11662 | |
| 11663 | // window gets focused |
| 11664 | mWindow->consumeFocusEvent(true); |
| 11665 | // window gets the pending key event |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11666 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 11667 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11668 | |
| 11669 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 11670 | protected: |
| 11671 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 11672 | sp<FakeWindowHandle> mWindow; |
| 11673 | sp<FakeWindowHandle> mSecondWindow; |
| 11674 | |
| 11675 | void SetUp() override { |
| 11676 | InputDispatcherTest::SetUp(); |
| 11677 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11678 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", |
| 11679 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11680 | mWindow->setFocusable(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11681 | mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", |
| 11682 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11683 | mSecondWindow->setFocusable(true); |
| 11684 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11685 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11686 | mDispatcher->onWindowInfosChanged( |
| 11687 | {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11688 | |
| 11689 | setFocusedWindow(mWindow); |
| 11690 | mWindow->consumeFocusEvent(true); |
| 11691 | } |
| 11692 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11693 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11694 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11695 | } |
| 11696 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11697 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 11698 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11699 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11700 | auto request = mFakePolicy->assertSetPointerCaptureCalled(window, enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11701 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11702 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11703 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11704 | } |
| 11705 | }; |
| 11706 | |
| 11707 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 11708 | // Ensure that capture cannot be obtained for unfocused windows. |
| 11709 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 11710 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 11711 | mSecondWindow->assertNoEvents(); |
| 11712 | |
| 11713 | // Ensure that capture can be enabled from the focus window. |
| 11714 | requestAndVerifyPointerCapture(mWindow, true); |
| 11715 | |
| 11716 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 11717 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 11718 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 11719 | |
| 11720 | // Ensure that capture can be disabled from the window with capture. |
| 11721 | requestAndVerifyPointerCapture(mWindow, false); |
| 11722 | } |
| 11723 | |
| 11724 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11725 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11726 | |
| 11727 | setFocusedWindow(mSecondWindow); |
| 11728 | |
| 11729 | // Ensure that the capture disabled event was sent first. |
| 11730 | mWindow->consumeCaptureEvent(false); |
| 11731 | mWindow->consumeFocusEvent(false); |
| 11732 | mSecondWindow->consumeFocusEvent(true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11733 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11734 | |
| 11735 | // 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] | 11736 | notifyPointerCaptureChanged({}); |
| 11737 | notifyPointerCaptureChanged(request); |
| 11738 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11739 | mWindow->assertNoEvents(); |
| 11740 | mSecondWindow->assertNoEvents(); |
| 11741 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 11742 | } |
| 11743 | |
| 11744 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11745 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11746 | |
| 11747 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11748 | notifyPointerCaptureChanged({}); |
| 11749 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11750 | |
| 11751 | // Ensure that Pointer Capture is disabled. |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11752 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11753 | mWindow->consumeCaptureEvent(false); |
| 11754 | mWindow->assertNoEvents(); |
| 11755 | } |
| 11756 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 11757 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 11758 | requestAndVerifyPointerCapture(mWindow, true); |
| 11759 | |
| 11760 | // The first window loses focus. |
| 11761 | setFocusedWindow(mSecondWindow); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11762 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 11763 | mWindow->consumeCaptureEvent(false); |
| 11764 | |
| 11765 | // Request Pointer Capture from the second window before the notification from InputReader |
| 11766 | // arrives. |
| 11767 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11768 | auto request = mFakePolicy->assertSetPointerCaptureCalled(mSecondWindow, true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 11769 | |
| 11770 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11771 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 11772 | |
| 11773 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11774 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 11775 | |
| 11776 | mSecondWindow->consumeFocusEvent(true); |
| 11777 | mSecondWindow->consumeCaptureEvent(true); |
| 11778 | } |
| 11779 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11780 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 11781 | // App repeatedly enables and disables capture. |
| 11782 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11783 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11784 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11785 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11786 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11787 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11788 | |
| 11789 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 11790 | // first request is now stale, this should do nothing. |
| 11791 | notifyPointerCaptureChanged(firstRequest); |
| 11792 | mWindow->assertNoEvents(); |
| 11793 | |
| 11794 | // InputReader notifies that the second request was enabled. |
| 11795 | notifyPointerCaptureChanged(secondRequest); |
| 11796 | mWindow->consumeCaptureEvent(true); |
| 11797 | } |
| 11798 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 11799 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 11800 | requestAndVerifyPointerCapture(mWindow, true); |
| 11801 | |
| 11802 | // App toggles pointer capture off and on. |
| 11803 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11804 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 11805 | |
| 11806 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11807 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 11808 | |
| 11809 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 11810 | // preceding "disable" request. |
| 11811 | notifyPointerCaptureChanged(enableRequest); |
| 11812 | |
| 11813 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 11814 | // any notifications. |
| 11815 | mWindow->assertNoEvents(); |
| 11816 | } |
| 11817 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11818 | /** |
| 11819 | * One window. Hover mouse in the window, and then start capture. Make sure that the relative |
| 11820 | * mouse movements don't affect the previous mouse hovering state. |
| 11821 | * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no |
| 11822 | * HOVER_MOVE events). |
| 11823 | */ |
| 11824 | TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) { |
| 11825 | // Mouse hover on the window |
| 11826 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 11827 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 11828 | .build()); |
| 11829 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 11830 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 11831 | .build()); |
| 11832 | |
| 11833 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER))); |
| 11834 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE))); |
| 11835 | |
| 11836 | // Start pointer capture |
| 11837 | requestAndVerifyPointerCapture(mWindow, true); |
| 11838 | |
| 11839 | // Send some relative mouse movements and receive them in the window. |
| 11840 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE) |
| 11841 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11)) |
| 11842 | .build()); |
| 11843 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11), |
| 11844 | WithSource(AINPUT_SOURCE_MOUSE_RELATIVE))); |
| 11845 | |
| 11846 | // Stop pointer capture |
| 11847 | requestAndVerifyPointerCapture(mWindow, false); |
| 11848 | |
| 11849 | // Continue hovering on the window |
| 11850 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 11851 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115)) |
| 11852 | .build()); |
| 11853 | mWindow->consumeMotionEvent( |
| 11854 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 11855 | |
| 11856 | mWindow->assertNoEvents(); |
| 11857 | } |
| 11858 | |
Prabir Pradhan | 9d3d561 | 2024-06-06 20:34:26 +0000 | [diff] [blame] | 11859 | TEST_F(InputDispatcherPointerCaptureTests, MultiDisplayPointerCapture) { |
| 11860 | // The default display is the focused display to begin with. |
| 11861 | requestAndVerifyPointerCapture(mWindow, true); |
| 11862 | |
| 11863 | // Move the second window to a second display, make it the focused window on that display. |
| 11864 | mSecondWindow->editInfo()->displayId = SECOND_DISPLAY_ID; |
| 11865 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
| 11866 | setFocusedWindow(mSecondWindow); |
| 11867 | mSecondWindow->consumeFocusEvent(true); |
| 11868 | |
| 11869 | mWindow->assertNoEvents(); |
| 11870 | |
| 11871 | // The second window cannot gain capture because it is not on the focused display. |
| 11872 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 11873 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 11874 | mSecondWindow->assertNoEvents(); |
| 11875 | |
| 11876 | // Make the second display the focused display. |
| 11877 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
Arpit Singh | b65e2bd | 2024-06-03 09:48:16 +0000 | [diff] [blame] | 11878 | mFakePolicy->assertFocusedDisplayNotified(SECOND_DISPLAY_ID); |
Prabir Pradhan | 9d3d561 | 2024-06-06 20:34:26 +0000 | [diff] [blame] | 11879 | |
| 11880 | // This causes the first window to lose pointer capture, and it's unable to request capture. |
| 11881 | mWindow->consumeCaptureEvent(false); |
| 11882 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
| 11883 | |
| 11884 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 11885 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 11886 | |
| 11887 | // The second window is now able to gain pointer capture successfully. |
| 11888 | requestAndVerifyPointerCapture(mSecondWindow, true); |
| 11889 | } |
| 11890 | |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11891 | using InputDispatcherPointerCaptureDeathTest = InputDispatcherPointerCaptureTests; |
| 11892 | |
| 11893 | TEST_F(InputDispatcherPointerCaptureDeathTest, |
| 11894 | NotifyPointerCaptureChangedWithWrongTokenAbortsDispatcher) { |
| 11895 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
| 11896 | ScopedSilentDeath _silentDeath; |
| 11897 | |
| 11898 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 11899 | auto request = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
| 11900 | |
| 11901 | // Dispatch a pointer changed event with a wrong token. |
| 11902 | request.window = mSecondWindow->getToken(); |
| 11903 | ASSERT_DEATH( |
| 11904 | { |
| 11905 | notifyPointerCaptureChanged(request); |
| 11906 | mSecondWindow->consumeCaptureEvent(true); |
| 11907 | }, |
| 11908 | "Unexpected requested window for Pointer Capture."); |
| 11909 | } |
| 11910 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11911 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 11912 | protected: |
| 11913 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11914 | |
| 11915 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 11916 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 11917 | |
| 11918 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 11919 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 11920 | |
| 11921 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 11922 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 11923 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 11924 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 11925 | MAXIMUM_OBSCURING_OPACITY); |
| 11926 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11927 | static constexpr gui::Uid TOUCHED_APP_UID{10001}; |
| 11928 | static constexpr gui::Uid APP_B_UID{10002}; |
| 11929 | static constexpr gui::Uid APP_C_UID{10003}; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11930 | |
| 11931 | sp<FakeWindowHandle> mTouchWindow; |
| 11932 | |
| 11933 | virtual void SetUp() override { |
| 11934 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11935 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11936 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 11937 | } |
| 11938 | |
| 11939 | virtual void TearDown() override { |
| 11940 | InputDispatcherTest::TearDown(); |
| 11941 | mTouchWindow.clear(); |
| 11942 | } |
| 11943 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11944 | sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 11945 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11946 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11947 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11948 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11949 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11950 | return window; |
| 11951 | } |
| 11952 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11953 | sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11954 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 11955 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11956 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ui::LogicalDisplayId::DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11957 | // Generate an arbitrary PID based on the UID |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11958 | 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] | 11959 | return window; |
| 11960 | } |
| 11961 | |
| 11962 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11963 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11964 | AINPUT_SOURCE_TOUCHSCREEN, |
| 11965 | ui::LogicalDisplayId::DEFAULT, points)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11966 | } |
| 11967 | }; |
| 11968 | |
| 11969 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11970 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11971 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11972 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11973 | |
| 11974 | touch(); |
| 11975 | |
| 11976 | mTouchWindow->assertNoEvents(); |
| 11977 | } |
| 11978 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11979 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11980 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 11981 | const sp<FakeWindowHandle>& w = |
| 11982 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11983 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11984 | |
| 11985 | touch(); |
| 11986 | |
| 11987 | mTouchWindow->assertNoEvents(); |
| 11988 | } |
| 11989 | |
| 11990 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11991 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 11992 | const sp<FakeWindowHandle>& w = |
| 11993 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11994 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11995 | |
| 11996 | touch(); |
| 11997 | |
| 11998 | w->assertNoEvents(); |
| 11999 | } |
| 12000 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 12001 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 12002 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12003 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 12004 | |
| 12005 | touch(); |
| 12006 | |
| 12007 | mTouchWindow->consumeAnyMotionDown(); |
| 12008 | } |
| 12009 | |
| 12010 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 12011 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 12012 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 12013 | w->setFrame(Rect(0, 0, 50, 50)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12014 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 12015 | |
| 12016 | touch({PointF{100, 100}}); |
| 12017 | |
| 12018 | mTouchWindow->consumeAnyMotionDown(); |
| 12019 | } |
| 12020 | |
| 12021 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 12022 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 12023 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12024 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 12025 | |
| 12026 | touch(); |
| 12027 | |
| 12028 | mTouchWindow->consumeAnyMotionDown(); |
| 12029 | } |
| 12030 | |
| 12031 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 12032 | const sp<FakeWindowHandle>& w = |
| 12033 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12034 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 12035 | |
| 12036 | touch(); |
| 12037 | |
| 12038 | mTouchWindow->consumeAnyMotionDown(); |
| 12039 | } |
| 12040 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 12041 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 12042 | const sp<FakeWindowHandle>& w = |
| 12043 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12044 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 12045 | |
| 12046 | touch(); |
| 12047 | |
| 12048 | w->assertNoEvents(); |
| 12049 | } |
| 12050 | |
| 12051 | /** |
| 12052 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 12053 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 12054 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 12055 | */ |
| 12056 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 12057 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 12058 | const sp<FakeWindowHandle>& w = |
| 12059 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12060 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12061 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 12062 | |
| 12063 | touch(); |
| 12064 | |
| 12065 | w->consumeMotionOutside(); |
| 12066 | } |
| 12067 | |
| 12068 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 12069 | const sp<FakeWindowHandle>& w = |
| 12070 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12071 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12072 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 12073 | |
| 12074 | touch(); |
| 12075 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 12076 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 12077 | } |
| 12078 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 12079 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 12080 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 12081 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 12082 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12083 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 12084 | |
| 12085 | touch(); |
| 12086 | |
| 12087 | mTouchWindow->consumeAnyMotionDown(); |
| 12088 | } |
| 12089 | |
| 12090 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 12091 | const sp<FakeWindowHandle>& w = |
| 12092 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 12093 | MAXIMUM_OBSCURING_OPACITY); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12094 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 12095 | |
| 12096 | touch(); |
| 12097 | |
| 12098 | mTouchWindow->consumeAnyMotionDown(); |
| 12099 | } |
| 12100 | |
| 12101 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 12102 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 12103 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 12104 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12105 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 12106 | |
| 12107 | touch(); |
| 12108 | |
| 12109 | mTouchWindow->assertNoEvents(); |
| 12110 | } |
| 12111 | |
| 12112 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 12113 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 12114 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 12115 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 12116 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 12117 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 12118 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 12119 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12120 | mDispatcher->onWindowInfosChanged( |
| 12121 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 12122 | |
| 12123 | touch(); |
| 12124 | |
| 12125 | mTouchWindow->assertNoEvents(); |
| 12126 | } |
| 12127 | |
| 12128 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 12129 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 12130 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 12131 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 12132 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 12133 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 12134 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 12135 | OPACITY_FAR_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12136 | mDispatcher->onWindowInfosChanged( |
| 12137 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 12138 | |
| 12139 | touch(); |
| 12140 | |
| 12141 | mTouchWindow->consumeAnyMotionDown(); |
| 12142 | } |
| 12143 | |
| 12144 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 12145 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 12146 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 12147 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 12148 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 12149 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 12150 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 12151 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12152 | mDispatcher->onWindowInfosChanged( |
| 12153 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 12154 | |
| 12155 | touch(); |
| 12156 | |
| 12157 | mTouchWindow->consumeAnyMotionDown(); |
| 12158 | } |
| 12159 | |
| 12160 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 12161 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 12162 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 12163 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 12164 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 12165 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 12166 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12167 | mDispatcher->onWindowInfosChanged( |
| 12168 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 12169 | |
| 12170 | touch(); |
| 12171 | |
| 12172 | mTouchWindow->assertNoEvents(); |
| 12173 | } |
| 12174 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 12175 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 12176 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 12177 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 12178 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 12179 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 12180 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 12181 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 12182 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12183 | mDispatcher->onWindowInfosChanged( |
| 12184 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 12185 | |
| 12186 | touch(); |
| 12187 | |
| 12188 | mTouchWindow->assertNoEvents(); |
| 12189 | } |
| 12190 | |
| 12191 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 12192 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 12193 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 12194 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 12195 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 12196 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 12197 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 12198 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12199 | mDispatcher->onWindowInfosChanged( |
| 12200 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 12201 | |
| 12202 | touch(); |
| 12203 | |
| 12204 | mTouchWindow->consumeAnyMotionDown(); |
| 12205 | } |
| 12206 | |
| 12207 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 12208 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 12209 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 12210 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12211 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 12212 | |
| 12213 | touch(); |
| 12214 | |
| 12215 | mTouchWindow->consumeAnyMotionDown(); |
| 12216 | } |
| 12217 | |
| 12218 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 12219 | const sp<FakeWindowHandle>& w = |
| 12220 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12221 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 12222 | |
| 12223 | touch(); |
| 12224 | |
| 12225 | mTouchWindow->consumeAnyMotionDown(); |
| 12226 | } |
| 12227 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 12228 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 12229 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 12230 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 12231 | const sp<FakeWindowHandle>& w = |
| 12232 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12233 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 12234 | |
| 12235 | touch(); |
| 12236 | |
| 12237 | mTouchWindow->assertNoEvents(); |
| 12238 | } |
| 12239 | |
| 12240 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 12241 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 12242 | const sp<FakeWindowHandle>& w = |
| 12243 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12244 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 12245 | |
| 12246 | touch(); |
| 12247 | |
| 12248 | mTouchWindow->consumeAnyMotionDown(); |
| 12249 | } |
| 12250 | |
| 12251 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 12252 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 12253 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 12254 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 12255 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 12256 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12257 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 12258 | |
| 12259 | touch(); |
| 12260 | |
| 12261 | mTouchWindow->consumeAnyMotionDown(); |
| 12262 | } |
| 12263 | |
| 12264 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 12265 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 12266 | const sp<FakeWindowHandle>& w1 = |
| 12267 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 12268 | OPACITY_BELOW_THRESHOLD); |
| 12269 | const sp<FakeWindowHandle>& w2 = |
| 12270 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 12271 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12272 | mDispatcher->onWindowInfosChanged( |
| 12273 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 12274 | |
| 12275 | touch(); |
| 12276 | |
| 12277 | mTouchWindow->assertNoEvents(); |
| 12278 | } |
| 12279 | |
| 12280 | /** |
| 12281 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 12282 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 12283 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 12284 | */ |
| 12285 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 12286 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 12287 | const sp<FakeWindowHandle>& wB = |
| 12288 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 12289 | OPACITY_BELOW_THRESHOLD); |
| 12290 | const sp<FakeWindowHandle>& wC = |
| 12291 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 12292 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12293 | mDispatcher->onWindowInfosChanged( |
| 12294 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 12295 | |
| 12296 | touch(); |
| 12297 | |
| 12298 | mTouchWindow->assertNoEvents(); |
| 12299 | } |
| 12300 | |
| 12301 | /** |
| 12302 | * This test is testing that a window from a different UID but with same application token doesn't |
| 12303 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 12304 | */ |
| 12305 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 12306 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 12307 | const sp<FakeWindowHandle>& w = |
| 12308 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 12309 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12310 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 12311 | |
| 12312 | touch(); |
| 12313 | |
| 12314 | mTouchWindow->consumeAnyMotionDown(); |
| 12315 | } |
| 12316 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12317 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 12318 | protected: |
| 12319 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 12320 | sp<FakeWindowHandle> mWindow; |
| 12321 | sp<FakeWindowHandle> mSecondWindow; |
| 12322 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 12323 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12324 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 12325 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12326 | |
| 12327 | void SetUp() override { |
| 12328 | InputDispatcherTest::SetUp(); |
| 12329 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12330 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", |
| 12331 | ui::LogicalDisplayId::DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12332 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12333 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12334 | mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", |
| 12335 | ui::LogicalDisplayId::DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12336 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12337 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12338 | mSpyWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", |
| 12339 | ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 12340 | mSpyWindow->setSpy(true); |
| 12341 | mSpyWindow->setTrustedOverlay(true); |
| 12342 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 12343 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12344 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12345 | mDispatcher->onWindowInfosChanged( |
| 12346 | {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 12347 | {}, |
| 12348 | 0, |
| 12349 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12350 | } |
| 12351 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12352 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 12353 | switch (fromSource) { |
| 12354 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 12355 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12356 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12357 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12358 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12359 | break; |
| 12360 | case AINPUT_SOURCE_STYLUS: |
| 12361 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12362 | injectMotionEvent(*mDispatcher, |
| 12363 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 12364 | AINPUT_SOURCE_STYLUS) |
| 12365 | .buttonState( |
| 12366 | AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 12367 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
| 12368 | .x(50) |
| 12369 | .y(50)) |
| 12370 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12371 | break; |
| 12372 | case AINPUT_SOURCE_MOUSE: |
| 12373 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12374 | injectMotionEvent(*mDispatcher, |
| 12375 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 12376 | AINPUT_SOURCE_MOUSE) |
| 12377 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 12378 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 12379 | ToolType::MOUSE) |
| 12380 | .x(50) |
| 12381 | .y(50)) |
| 12382 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12383 | break; |
| 12384 | default: |
| 12385 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 12386 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12387 | |
| 12388 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12389 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 12390 | // Spy window should also receive motion event |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12391 | mSpyWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12392 | } |
| 12393 | |
| 12394 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 12395 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 12396 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12397 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12398 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12399 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12400 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12401 | |
| 12402 | // The drag window covers the entire display |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12403 | mDragWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", |
| 12404 | ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 12405 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12406 | mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), |
| 12407 | *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 12408 | {}, |
| 12409 | 0, |
| 12410 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12411 | |
| 12412 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12413 | bool transferred = |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 12414 | mDispatcher->transferTouchGesture(mWindow->getToken(), mDragWindow->getToken(), |
| 12415 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12416 | if (transferred) { |
| 12417 | mWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12418 | mDragWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 12419 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12420 | } |
| 12421 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12422 | } |
| 12423 | }; |
| 12424 | |
| 12425 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12426 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12427 | |
| 12428 | // Move on window. |
| 12429 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12430 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12431 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12432 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12433 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12434 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12435 | mWindow->consumeDragEvent(false, 50, 50); |
| 12436 | mSecondWindow->assertNoEvents(); |
| 12437 | |
| 12438 | // Move to another window. |
| 12439 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12440 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12441 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12442 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12443 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12444 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12445 | mWindow->consumeDragEvent(true, 150, 50); |
| 12446 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 12447 | |
| 12448 | // Move back to original window. |
| 12449 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12450 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12451 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12452 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12453 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12454 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12455 | mWindow->consumeDragEvent(false, 50, 50); |
| 12456 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 12457 | |
| 12458 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12459 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12460 | {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12461 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12462 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 12463 | mWindow->assertNoEvents(); |
| 12464 | mSecondWindow->assertNoEvents(); |
| 12465 | } |
| 12466 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 12467 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12468 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 12469 | |
| 12470 | // No cancel event after drag start |
| 12471 | mSpyWindow->assertNoEvents(); |
| 12472 | |
| 12473 | const MotionEvent secondFingerDownEvent = |
| 12474 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 12475 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12476 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12477 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 12478 | .build(); |
| 12479 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12480 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 12481 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12482 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12483 | |
| 12484 | // Receives cancel for first pointer after next pointer down |
| 12485 | mSpyWindow->consumeMotionCancel(); |
| 12486 | mSpyWindow->consumeMotionDown(); |
| 12487 | |
| 12488 | mSpyWindow->assertNoEvents(); |
| 12489 | } |
| 12490 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 12491 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12492 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 12493 | |
| 12494 | // Move on window. |
| 12495 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12496 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12497 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 12498 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12499 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12500 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 12501 | mWindow->consumeDragEvent(false, 50, 50); |
| 12502 | mSecondWindow->assertNoEvents(); |
| 12503 | |
| 12504 | // Move to another window. |
| 12505 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12506 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12507 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 12508 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12509 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12510 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 12511 | mWindow->consumeDragEvent(true, 150, 50); |
| 12512 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 12513 | |
| 12514 | // drop to another window. |
| 12515 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12516 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 12517 | {150, 50})) |
| 12518 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12519 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 12520 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 12521 | mWindow->assertNoEvents(); |
| 12522 | mSecondWindow->assertNoEvents(); |
| 12523 | } |
| 12524 | |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 12525 | TEST_F(InputDispatcherDragTests, DragAndDropNotCancelledIfSomeOtherPointerIsPilfered) { |
| 12526 | startDrag(); |
| 12527 | |
| 12528 | // No cancel event after drag start |
| 12529 | mSpyWindow->assertNoEvents(); |
| 12530 | |
| 12531 | const MotionEvent secondFingerDownEvent = |
| 12532 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 12533 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 12534 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12535 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 12536 | .build(); |
| 12537 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12538 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 12539 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12540 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12541 | |
| 12542 | // Receives cancel for first pointer after next pointer down |
| 12543 | mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 12544 | mSpyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithPointerIds({1}))); |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 12545 | mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 12546 | |
| 12547 | mSpyWindow->assertNoEvents(); |
| 12548 | |
| 12549 | // Spy window calls pilfer pointers |
| 12550 | EXPECT_EQ(OK, mDispatcher->pilferPointers(mSpyWindow->getToken())); |
| 12551 | mDragWindow->assertNoEvents(); |
| 12552 | |
| 12553 | const MotionEvent firstFingerMoveEvent = |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 12554 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 12555 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 12556 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(60).y(60)) |
| 12557 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 12558 | .build(); |
| 12559 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 12560 | injectMotionEvent(*mDispatcher, firstFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 12561 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12562 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12563 | |
| 12564 | // Drag window should still receive the new event |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 12565 | mDragWindow->consumeMotionEvent( |
| 12566 | AllOf(WithMotionAction(ACTION_MOVE), WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 12567 | mDragWindow->assertNoEvents(); |
| 12568 | } |
| 12569 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 12570 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12571 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 12572 | |
| 12573 | // Move on window and keep button pressed. |
| 12574 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12575 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 12576 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 12577 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12578 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 12579 | .build())) |
| 12580 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12581 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12582 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 12583 | mWindow->consumeDragEvent(false, 50, 50); |
| 12584 | mSecondWindow->assertNoEvents(); |
| 12585 | |
| 12586 | // Move to another window and release button, expect to drop item. |
| 12587 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12588 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 12589 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 12590 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12591 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 12592 | .build())) |
| 12593 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12594 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12595 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 12596 | mWindow->assertNoEvents(); |
| 12597 | mSecondWindow->assertNoEvents(); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 12598 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 12599 | |
| 12600 | // nothing to the window. |
| 12601 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12602 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 12603 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 12604 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12605 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 12606 | .build())) |
| 12607 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12608 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 12609 | mWindow->assertNoEvents(); |
| 12610 | mSecondWindow->assertNoEvents(); |
| 12611 | } |
| 12612 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12613 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12614 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 12615 | |
| 12616 | // Set second window invisible. |
| 12617 | mSecondWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12618 | mDispatcher->onWindowInfosChanged( |
| 12619 | {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 12620 | |
| 12621 | // Move on window. |
| 12622 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12623 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12624 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 12625 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12626 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12627 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 12628 | mWindow->consumeDragEvent(false, 50, 50); |
| 12629 | mSecondWindow->assertNoEvents(); |
| 12630 | |
| 12631 | // Move to another window. |
| 12632 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12633 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12634 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 12635 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12636 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12637 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 12638 | mWindow->consumeDragEvent(true, 150, 50); |
| 12639 | mSecondWindow->assertNoEvents(); |
| 12640 | |
| 12641 | // drop to another window. |
| 12642 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12643 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 12644 | {150, 50})) |
| 12645 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12646 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 12647 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 12648 | mWindow->assertNoEvents(); |
| 12649 | mSecondWindow->assertNoEvents(); |
| 12650 | } |
| 12651 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12652 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12653 | // Ensure window could track pointerIds if it didn't support split touch. |
| 12654 | mWindow->setPreventSplitting(true); |
| 12655 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12656 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12657 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12658 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12659 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12660 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12661 | |
| 12662 | const MotionEvent secondFingerDownEvent = |
| 12663 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12664 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12665 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12666 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12667 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12668 | .build(); |
| 12669 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12670 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12671 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12672 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12673 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12674 | |
| 12675 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12676 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12677 | } |
| 12678 | |
| 12679 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 12680 | // First down on second window. |
| 12681 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12682 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12683 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12684 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12685 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12686 | mSecondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12687 | |
| 12688 | // Second down on first window. |
| 12689 | const MotionEvent secondFingerDownEvent = |
| 12690 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12691 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12692 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12693 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 12694 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12695 | .build(); |
| 12696 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12697 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12698 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12699 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12700 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 12701 | mSecondWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12702 | |
| 12703 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12704 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12705 | |
| 12706 | // Move on window. |
| 12707 | const MotionEvent secondFingerMoveEvent = |
| 12708 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 12709 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12710 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 12711 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12712 | .build(); |
| 12713 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12714 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12715 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12716 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12717 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12718 | mWindow->consumeDragEvent(false, 50, 50); |
| 12719 | mSecondWindow->consumeMotionMove(); |
| 12720 | |
| 12721 | // Release the drag pointer should perform drop. |
| 12722 | const MotionEvent secondFingerUpEvent = |
| 12723 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 12724 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12725 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 12726 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12727 | .build(); |
| 12728 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12729 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12730 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12731 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 12732 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken()); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12733 | mWindow->assertNoEvents(); |
| 12734 | mSecondWindow->consumeMotionMove(); |
| 12735 | } |
| 12736 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12737 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12738 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12739 | |
| 12740 | // Update window of second display. |
| 12741 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 12742 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12743 | mDispatcher->onWindowInfosChanged( |
| 12744 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 12745 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 12746 | {}, |
| 12747 | 0, |
| 12748 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12749 | |
| 12750 | // Let second display has a touch state. |
| 12751 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12752 | injectMotionEvent(*mDispatcher, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12753 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 12754 | AINPUT_SOURCE_TOUCHSCREEN) |
| 12755 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12756 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12757 | .build())); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 12758 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12759 | // Update window again. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12760 | mDispatcher->onWindowInfosChanged( |
| 12761 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 12762 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 12763 | {}, |
| 12764 | 0, |
| 12765 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12766 | |
| 12767 | // Move on window. |
| 12768 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12769 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12770 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12771 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12772 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12773 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12774 | mWindow->consumeDragEvent(false, 50, 50); |
| 12775 | mSecondWindow->assertNoEvents(); |
| 12776 | |
| 12777 | // Move to another window. |
| 12778 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12779 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12780 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12781 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12782 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12783 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12784 | mWindow->consumeDragEvent(true, 150, 50); |
| 12785 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 12786 | |
| 12787 | // drop to another window. |
| 12788 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12789 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12790 | {150, 50})) |
| 12791 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12792 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 12793 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12794 | mWindow->assertNoEvents(); |
| 12795 | mSecondWindow->assertNoEvents(); |
| 12796 | } |
| 12797 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12798 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 12799 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 12800 | // Move on window. |
| 12801 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12802 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12803 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 12804 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12805 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12806 | .x(50) |
| 12807 | .y(50)) |
| 12808 | .build())) |
| 12809 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12810 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12811 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12812 | mWindow->consumeDragEvent(false, 50, 50); |
| 12813 | mSecondWindow->assertNoEvents(); |
| 12814 | |
| 12815 | // Move to another window. |
| 12816 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12817 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12818 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 12819 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12820 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12821 | .x(150) |
| 12822 | .y(50)) |
| 12823 | .build())) |
| 12824 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12825 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12826 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12827 | mWindow->consumeDragEvent(true, 150, 50); |
| 12828 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 12829 | |
| 12830 | // drop to another window. |
| 12831 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12832 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12833 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 12834 | .buttonState(0) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12835 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12836 | .x(150) |
| 12837 | .y(50)) |
| 12838 | .build())) |
| 12839 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12840 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 12841 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12842 | mWindow->assertNoEvents(); |
| 12843 | mSecondWindow->assertNoEvents(); |
| 12844 | } |
| 12845 | |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12846 | /** |
| 12847 | * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag |
| 12848 | * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash. |
| 12849 | */ |
| 12850 | TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) { |
| 12851 | // Down on second window |
| 12852 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12853 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12854 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12855 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12856 | |
| 12857 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown()); |
| 12858 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown()); |
| 12859 | |
| 12860 | // Down on first window |
| 12861 | const MotionEvent secondFingerDownEvent = |
| 12862 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12863 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12864 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 12865 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 12866 | .build(); |
| 12867 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12868 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 12869 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12870 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12871 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 12872 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove()); |
| 12873 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1)); |
| 12874 | |
| 12875 | // Start drag on first window |
| 12876 | ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN)); |
| 12877 | |
| 12878 | // Trigger cancel |
| 12879 | mDispatcher->cancelCurrentTouch(); |
| 12880 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12881 | ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 12882 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)); |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12883 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel()); |
| 12884 | |
| 12885 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 12886 | // The D&D finished with nullptr |
| 12887 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
| 12888 | |
| 12889 | // Remove drag window |
| 12890 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
| 12891 | |
| 12892 | // Inject a simple gesture, ensure dispatcher not crashed |
| 12893 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12894 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12895 | ui::LogicalDisplayId::DEFAULT, PointF{50, 50})) |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12896 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12897 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 12898 | |
| 12899 | const MotionEvent moveEvent = |
| 12900 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12901 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12902 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12903 | .build(); |
| 12904 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12905 | injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT, |
| 12906 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12907 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12908 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove()); |
| 12909 | |
| 12910 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12911 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12912 | {50, 50})) |
| 12913 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12914 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp()); |
| 12915 | } |
| 12916 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 12917 | TEST_F(InputDispatcherDragTests, NoDragAndDropWithHoveringPointer) { |
| 12918 | // Start hovering over the window. |
| 12919 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12920 | injectMotionEvent(*mDispatcher, ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12921 | ui::LogicalDisplayId::DEFAULT, {50, 50})); |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 12922 | |
| 12923 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 12924 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 12925 | |
| 12926 | ASSERT_FALSE(startDrag(/*sendDown=*/false)) |
| 12927 | << "Drag and drop should not work with a hovering pointer"; |
| 12928 | } |
| 12929 | |
Linnan Li | a512a53 | 2024-10-08 16:19:16 +0800 | [diff] [blame] | 12930 | /** |
| 12931 | * Two devices, we use the second pointer of Device A to start the drag, during the drag process, if |
| 12932 | * we perform a click using Device B, the dispatcher should work well. |
| 12933 | */ |
| 12934 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouchAndMultiDevice) { |
| 12935 | const DeviceId deviceA = 1; |
| 12936 | const DeviceId deviceB = 2; |
| 12937 | // First down on second window with deviceA. |
| 12938 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 12939 | .deviceId(deviceA) |
| 12940 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 12941 | .build()); |
| 12942 | mSecondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA), |
| 12943 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
| 12944 | |
| 12945 | // Second down on first window with deviceA |
| 12946 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 12947 | .deviceId(deviceA) |
| 12948 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 12949 | .pointer(PointerBuilder(1, ToolType::FINGER).x(50).y(50)) |
| 12950 | .build()); |
| 12951 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA), |
| 12952 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
| 12953 | mSecondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceA), |
| 12954 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
| 12955 | |
| 12956 | // Perform drag and drop from first window. |
| 12957 | ASSERT_TRUE(startDrag(/*sendDown=*/false)); |
| 12958 | |
| 12959 | // Click first window with device B, we should ensure dispatcher work well. |
| 12960 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 12961 | .deviceId(deviceB) |
| 12962 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 12963 | .build()); |
| 12964 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB), |
| 12965 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
| 12966 | |
| 12967 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 12968 | .deviceId(deviceB) |
| 12969 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 12970 | .build()); |
| 12971 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDeviceId(deviceB), |
| 12972 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
| 12973 | |
| 12974 | // Move with device A. |
| 12975 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 12976 | .deviceId(deviceA) |
| 12977 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(51)) |
| 12978 | .pointer(PointerBuilder(1, ToolType::FINGER).x(51).y(51)) |
| 12979 | .build()); |
| 12980 | |
| 12981 | mDragWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceA), |
| 12982 | WithDisplayId(ui::LogicalDisplayId::DEFAULT), |
| 12983 | WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
| 12984 | mWindow->consumeDragEvent(false, 51, 51); |
| 12985 | mSecondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceA), |
| 12986 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
| 12987 | |
| 12988 | // Releasing the drag pointer should cause drop. |
| 12989 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 12990 | .deviceId(deviceA) |
| 12991 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(51)) |
| 12992 | .pointer(PointerBuilder(1, ToolType::FINGER).x(51).y(51)) |
| 12993 | .build()); |
| 12994 | mDragWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDeviceId(deviceA), |
| 12995 | WithDisplayId(ui::LogicalDisplayId::DEFAULT), |
| 12996 | WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
| 12997 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken()); |
| 12998 | mSecondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceA), |
| 12999 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
| 13000 | |
| 13001 | // Release all pointers. |
| 13002 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 13003 | .deviceId(deviceA) |
| 13004 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(51)) |
| 13005 | .build()); |
| 13006 | mSecondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDeviceId(deviceA), |
| 13007 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
| 13008 | mWindow->assertNoEvents(); |
| 13009 | } |
| 13010 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13011 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 13012 | |
| 13013 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 13014 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13015 | sp<FakeWindowHandle> window = |
| 13016 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 13017 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 13018 | window->setDropInput(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13019 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13020 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13021 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13022 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 13023 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13024 | |
| 13025 | // With the flag set, window should not get any input |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13026 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13027 | window->assertNoEvents(); |
| 13028 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 13029 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13030 | AINPUT_SOURCE_TOUCHSCREEN, |
| 13031 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 13032 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13033 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 13034 | mDispatcher->waitForIdle(); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13035 | window->assertNoEvents(); |
| 13036 | |
| 13037 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 13038 | window->setDropInput(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13039 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13040 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13041 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT)); |
| 13042 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13043 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 13044 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13045 | AINPUT_SOURCE_TOUCHSCREEN, |
| 13046 | ui::LogicalDisplayId::DEFAULT)); |
| 13047 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13048 | window->assertNoEvents(); |
| 13049 | } |
| 13050 | |
| 13051 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 13052 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 13053 | std::make_shared<FakeApplicationHandle>(); |
| 13054 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 13055 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13056 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13057 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13058 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 13059 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13060 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13061 | sp<FakeWindowHandle> window = |
| 13062 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 13063 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 13064 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13065 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13066 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13067 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13068 | mDispatcher->onWindowInfosChanged( |
| 13069 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13070 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 13071 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13072 | |
| 13073 | // With the flag set, window should not get any input |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13074 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13075 | window->assertNoEvents(); |
| 13076 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 13077 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13078 | AINPUT_SOURCE_TOUCHSCREEN, |
| 13079 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 13080 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13081 | ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13082 | window->assertNoEvents(); |
| 13083 | |
| 13084 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 13085 | window->setDropInputIfObscured(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13086 | mDispatcher->onWindowInfosChanged( |
| 13087 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13088 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13089 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT)); |
| 13090 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13091 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 13092 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13093 | AINPUT_SOURCE_TOUCHSCREEN, |
| 13094 | ui::LogicalDisplayId::DEFAULT)); |
| 13095 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 13096 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13097 | window->assertNoEvents(); |
| 13098 | } |
| 13099 | |
| 13100 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 13101 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 13102 | std::make_shared<FakeApplicationHandle>(); |
| 13103 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 13104 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13105 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13106 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13107 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 13108 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13109 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13110 | sp<FakeWindowHandle> window = |
| 13111 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 13112 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 13113 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13114 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13115 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13116 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13117 | mDispatcher->onWindowInfosChanged( |
| 13118 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13119 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 13120 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13121 | |
| 13122 | // With the flag set, window should not get any input |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13123 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13124 | window->assertNoEvents(); |
| 13125 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 13126 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13127 | AINPUT_SOURCE_TOUCHSCREEN, |
| 13128 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 13129 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13130 | ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13131 | window->assertNoEvents(); |
| 13132 | |
| 13133 | // 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] | 13134 | mDispatcher->onWindowInfosChanged( |
| 13135 | {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13136 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13137 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT)); |
| 13138 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13139 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 13140 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13141 | AINPUT_SOURCE_TOUCHSCREEN, |
| 13142 | ui::LogicalDisplayId::DEFAULT)); |
| 13143 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 13144 | window->assertNoEvents(); |
| 13145 | } |
| 13146 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 13147 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 13148 | protected: |
| 13149 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 13150 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 13151 | sp<FakeWindowHandle> mWindow; |
| 13152 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 13153 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 13154 | |
| 13155 | void SetUp() override { |
| 13156 | InputDispatcherTest::SetUp(); |
| 13157 | |
| 13158 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 13159 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13160 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", |
| 13161 | ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 13162 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 13163 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13164 | mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", |
| 13165 | ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 13166 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 13167 | mThirdWindow = |
| 13168 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 13169 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 13170 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 13171 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13172 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13173 | mDispatcher->onWindowInfosChanged( |
| 13174 | {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()}, |
| 13175 | {}, |
| 13176 | 0, |
| 13177 | 0}); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 13178 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 13179 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 13180 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 13181 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13182 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13183 | WINDOW_UID, /*hasPermission=*/true, |
| 13184 | ui::LogicalDisplayId::DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 13185 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 13186 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 13187 | mThirdWindow->assertNoEvents(); |
| 13188 | } |
| 13189 | |
| 13190 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 13191 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 13192 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 13193 | SECOND_DISPLAY_ID)) { |
| 13194 | mWindow->assertNoEvents(); |
| 13195 | mSecondWindow->assertNoEvents(); |
| 13196 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 13197 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 13198 | } |
| 13199 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13200 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 13201 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 13202 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13203 | ui::LogicalDisplayId::DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 13204 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 13205 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 13206 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 13207 | } |
| 13208 | }; |
| 13209 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 13210 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 13211 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 13212 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 13213 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 13214 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 13215 | } |
| 13216 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 13217 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 13218 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13219 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 13220 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13221 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 13222 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 13223 | ownerUid, /*hasPermission=*/false, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13224 | ui::LogicalDisplayId::DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 13225 | mWindow->assertNoEvents(); |
| 13226 | mSecondWindow->assertNoEvents(); |
| 13227 | } |
| 13228 | |
| 13229 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 13230 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13231 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 13232 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13233 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 13234 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 13235 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 13236 | } |
| 13237 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 13238 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 13239 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 13240 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 13241 | windowInfo.ownerPid, windowInfo.ownerUid, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13242 | /*hasPermission=*/true, |
| 13243 | ui::LogicalDisplayId::DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 13244 | mWindow->assertNoEvents(); |
| 13245 | mSecondWindow->assertNoEvents(); |
| 13246 | } |
| 13247 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 13248 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 13249 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 13250 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 13251 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 13252 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 13253 | mWindow->assertNoEvents(); |
| 13254 | mSecondWindow->assertNoEvents(); |
| 13255 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 13256 | } |
| 13257 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 13258 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 13259 | // Interact with the window first. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13260 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13261 | injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT)) |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 13262 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13263 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 13264 | |
| 13265 | // Then remove focus. |
| 13266 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13267 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 13268 | |
| 13269 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 13270 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 13271 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 13272 | windowInfo.ownerPid, windowInfo.ownerUid, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13273 | /*hasPermission=*/false, |
| 13274 | ui::LogicalDisplayId::DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 13275 | } |
| 13276 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13277 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 13278 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 13279 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13280 | std::shared_ptr<FakeApplicationHandle> application = |
| 13281 | std::make_shared<FakeApplicationHandle>(); |
| 13282 | std::string name = "Fake Spy "; |
| 13283 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13284 | sp<FakeWindowHandle> spy = |
| 13285 | sp<FakeWindowHandle>::make(application, mDispatcher, name.c_str(), |
| 13286 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 13287 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 13288 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13289 | return spy; |
| 13290 | } |
| 13291 | |
| 13292 | sp<FakeWindowHandle> createForeground() { |
| 13293 | std::shared_ptr<FakeApplicationHandle> application = |
| 13294 | std::make_shared<FakeApplicationHandle>(); |
| 13295 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 13296 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13297 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 13298 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13299 | return window; |
| 13300 | } |
| 13301 | |
| 13302 | private: |
| 13303 | int mSpyCount{0}; |
| 13304 | }; |
| 13305 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 13306 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13307 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 13308 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 13309 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 13310 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
Siarhei Vishniakou | 21f77bd | 2023-07-18 17:51:35 -0700 | [diff] [blame] | 13311 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 13312 | ScopedSilentDeath _silentDeath; |
| 13313 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 13314 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 13315 | spy->setTrustedOverlay(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13316 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 13317 | ".* not a trusted overlay"); |
| 13318 | } |
| 13319 | |
| 13320 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13321 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 13322 | */ |
| 13323 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 13324 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13325 | mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13326 | |
| 13327 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13328 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 13329 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13330 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13331 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13332 | } |
| 13333 | |
| 13334 | /** |
| 13335 | * Verify the order in which different input windows receive events. The touched foreground window |
| 13336 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 13337 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 13338 | * receive events before ones belows it. |
| 13339 | * |
| 13340 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 13341 | * spy1, spy2, window, spy3. |
| 13342 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 13343 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 13344 | * window. |
| 13345 | */ |
| 13346 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 13347 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 13348 | auto spy1 = createSpy(); |
| 13349 | auto spy2 = createSpy(); |
| 13350 | auto spy3 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13351 | mDispatcher->onWindowInfosChanged( |
| 13352 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13353 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 13354 | const size_t numChannels = channels.size(); |
| 13355 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 13356 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13357 | if (!epollFd.ok()) { |
| 13358 | FAIL() << "Failed to create epoll fd"; |
| 13359 | } |
| 13360 | |
| 13361 | for (size_t i = 0; i < numChannels; i++) { |
| 13362 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 13363 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 13364 | FAIL() << "Failed to add fd to epoll"; |
| 13365 | } |
| 13366 | } |
| 13367 | |
| 13368 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13369 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 13370 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13371 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13372 | |
| 13373 | std::vector<size_t> eventOrder; |
| 13374 | std::vector<struct epoll_event> events(numChannels); |
| 13375 | for (;;) { |
| 13376 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 13377 | (100ms).count()); |
| 13378 | if (nFds < 0) { |
| 13379 | FAIL() << "Failed to call epoll_wait"; |
| 13380 | } |
| 13381 | if (nFds == 0) { |
| 13382 | break; // epoll_wait timed out |
| 13383 | } |
| 13384 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 13385 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 13386 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13387 | channels[i]->consumeMotionDown(); |
| 13388 | } |
| 13389 | } |
| 13390 | |
| 13391 | // Verify the order in which the events were received. |
| 13392 | EXPECT_EQ(3u, eventOrder.size()); |
| 13393 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 13394 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 13395 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 13396 | } |
| 13397 | |
| 13398 | /** |
| 13399 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 13400 | */ |
| 13401 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 13402 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 13403 | auto spy = createSpy(); |
| 13404 | spy->setTouchable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13405 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13406 | |
| 13407 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13408 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 13409 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13410 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13411 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13412 | spy->assertNoEvents(); |
| 13413 | } |
| 13414 | |
| 13415 | /** |
| 13416 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 13417 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 13418 | * to the window. |
| 13419 | */ |
| 13420 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 13421 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 13422 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13423 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13424 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13425 | |
| 13426 | // Inject an event outside the spy window's touchable region. |
| 13427 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13428 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 13429 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13430 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13431 | window->consumeMotionDown(); |
| 13432 | spy->assertNoEvents(); |
| 13433 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13434 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 13435 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13436 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13437 | window->consumeMotionUp(); |
| 13438 | spy->assertNoEvents(); |
| 13439 | |
| 13440 | // Inject an event inside the spy window's touchable region. |
| 13441 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13442 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 13443 | ui::LogicalDisplayId::DEFAULT, {5, 10})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13444 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13445 | window->consumeMotionDown(); |
| 13446 | spy->consumeMotionDown(); |
| 13447 | } |
| 13448 | |
| 13449 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13450 | * 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] | 13451 | * 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] | 13452 | */ |
| 13453 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 13454 | auto window = createForeground(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13455 | window->setOwnerInfo(gui::Pid{12}, gui::Uid{34}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 13456 | auto spy = createSpy(); |
| 13457 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13458 | spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13459 | spy->setFrame(Rect{0, 0, 20, 20}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13460 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13461 | |
| 13462 | // Inject an event outside the spy window's frame and touchable region. |
| 13463 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13464 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 13465 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13466 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13467 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 13468 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13469 | } |
| 13470 | |
| 13471 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13472 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 13473 | * pointers that are down within its bounds. |
| 13474 | */ |
| 13475 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 13476 | auto windowLeft = createForeground(); |
| 13477 | windowLeft->setFrame({0, 0, 100, 200}); |
| 13478 | auto windowRight = createForeground(); |
| 13479 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 13480 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13481 | spy->setFrame({0, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13482 | mDispatcher->onWindowInfosChanged( |
| 13483 | {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13484 | |
| 13485 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13486 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 13487 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13488 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13489 | windowLeft->consumeMotionDown(); |
| 13490 | spy->consumeMotionDown(); |
| 13491 | |
| 13492 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 13493 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13494 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 13495 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 13496 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13497 | .build(); |
| 13498 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13499 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13500 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 13501 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13502 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 13503 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13504 | } |
| 13505 | |
| 13506 | /** |
| 13507 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 13508 | * the spy should receive the second pointer with ACTION_DOWN. |
| 13509 | */ |
| 13510 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 13511 | auto window = createForeground(); |
| 13512 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 13513 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13514 | spyRight->setFrame({100, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13515 | mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13516 | |
| 13517 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13518 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 13519 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13520 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13521 | window->consumeMotionDown(); |
| 13522 | spyRight->assertNoEvents(); |
| 13523 | |
| 13524 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 13525 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13526 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 13527 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 13528 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13529 | .build(); |
| 13530 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13531 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13532 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 13533 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 13534 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 13535 | spyRight->consumeMotionDown(); |
| 13536 | } |
| 13537 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 13538 | /** |
| 13539 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 13540 | * windows should be allowed to control split touch. |
| 13541 | */ |
| 13542 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 13543 | // 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] | 13544 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 13545 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 13546 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 13547 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 13548 | auto window = createForeground(); |
| 13549 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 13550 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13551 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 13552 | |
| 13553 | // First finger down, no window touched. |
| 13554 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13555 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 13556 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 13557 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13558 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 13559 | window->assertNoEvents(); |
| 13560 | |
| 13561 | // Second finger down on window, the window should receive touch down. |
| 13562 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 13563 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13564 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 13565 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 13566 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 13567 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 13568 | .build(); |
| 13569 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13570 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 13571 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 13572 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13573 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13574 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 13575 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 13576 | } |
| 13577 | |
| 13578 | /** |
| 13579 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 13580 | * do not receive key events. |
| 13581 | */ |
| 13582 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 13583 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 13584 | spy->setFocusable(false); |
| 13585 | |
| 13586 | auto window = createForeground(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13587 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 13588 | setFocusedWindow(window); |
| 13589 | window->consumeFocusEvent(true); |
| 13590 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13591 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 13592 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13593 | window->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 13594 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13595 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 13596 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13597 | window->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 13598 | |
| 13599 | spy->assertNoEvents(); |
| 13600 | } |
| 13601 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13602 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 13603 | |
| 13604 | /** |
| 13605 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 13606 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 13607 | */ |
| 13608 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 13609 | auto window = createForeground(); |
| 13610 | auto spy1 = createSpy(); |
| 13611 | auto spy2 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13612 | mDispatcher->onWindowInfosChanged( |
| 13613 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13614 | |
| 13615 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13616 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 13617 | ui::LogicalDisplayId::DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13618 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13619 | window->consumeMotionDown(); |
| 13620 | spy1->consumeMotionDown(); |
| 13621 | spy2->consumeMotionDown(); |
| 13622 | |
| 13623 | // Pilfer pointers from the second spy window. |
| 13624 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 13625 | spy2->assertNoEvents(); |
| 13626 | spy1->consumeMotionCancel(); |
| 13627 | window->consumeMotionCancel(); |
| 13628 | |
| 13629 | // The rest of the gesture should only be sent to the second spy window. |
| 13630 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13631 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13632 | ui::LogicalDisplayId::DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13633 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13634 | spy2->consumeMotionMove(); |
| 13635 | spy1->assertNoEvents(); |
| 13636 | window->assertNoEvents(); |
| 13637 | } |
| 13638 | |
| 13639 | /** |
| 13640 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 13641 | * in the middle of the gesture. |
| 13642 | */ |
| 13643 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 13644 | auto window = createForeground(); |
| 13645 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13646 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13647 | |
| 13648 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13649 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 13650 | ui::LogicalDisplayId::DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13651 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13652 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 13653 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13654 | |
| 13655 | window->releaseChannel(); |
| 13656 | |
| 13657 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 13658 | |
| 13659 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13660 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 13661 | ui::LogicalDisplayId::DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13662 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13663 | spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13664 | } |
| 13665 | |
| 13666 | /** |
| 13667 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 13668 | * the spy, but not to any other windows. |
| 13669 | */ |
| 13670 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 13671 | auto spy = createSpy(); |
| 13672 | auto window = createForeground(); |
| 13673 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13674 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13675 | |
| 13676 | // First finger down on the window and the spy. |
| 13677 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13678 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 13679 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13680 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13681 | spy->consumeMotionDown(); |
| 13682 | window->consumeMotionDown(); |
| 13683 | |
| 13684 | // Spy window pilfers the pointers. |
| 13685 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 13686 | window->consumeMotionCancel(); |
| 13687 | |
| 13688 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 13689 | const MotionEvent secondFingerDownEvent = |
| 13690 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13691 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13692 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 13693 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 13694 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13695 | .build(); |
| 13696 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13697 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13698 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 13699 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13700 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 13701 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13702 | |
| 13703 | // Third finger goes down outside all windows, so injection should fail. |
| 13704 | const MotionEvent thirdFingerDownEvent = |
| 13705 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13706 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13707 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 13708 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 13709 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 13710 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13711 | .build(); |
| 13712 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13713 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13714 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 13715 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13716 | |
| 13717 | spy->assertNoEvents(); |
| 13718 | window->assertNoEvents(); |
| 13719 | } |
| 13720 | |
| 13721 | /** |
| 13722 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 13723 | */ |
| 13724 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 13725 | auto spy = createSpy(); |
| 13726 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 13727 | auto window = createForeground(); |
| 13728 | window->setFrame(Rect(0, 0, 200, 200)); |
| 13729 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13730 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13731 | |
| 13732 | // First finger down on the window only |
| 13733 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13734 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 13735 | ui::LogicalDisplayId::DEFAULT, {150, 150})) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13736 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13737 | window->consumeMotionDown(); |
| 13738 | |
| 13739 | // Second finger down on the spy and window |
| 13740 | const MotionEvent secondFingerDownEvent = |
| 13741 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13742 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13743 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 13744 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 13745 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13746 | .build(); |
| 13747 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13748 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13749 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 13750 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13751 | spy->consumeMotionDown(); |
| 13752 | window->consumeMotionPointerDown(1); |
| 13753 | |
| 13754 | // Third finger down on the spy and window |
| 13755 | const MotionEvent thirdFingerDownEvent = |
| 13756 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13757 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13758 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 13759 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 13760 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 13761 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13762 | .build(); |
| 13763 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13764 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13765 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 13766 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13767 | spy->consumeMotionPointerDown(1); |
| 13768 | window->consumeMotionPointerDown(2); |
| 13769 | |
| 13770 | // Spy window pilfers the pointers. |
| 13771 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Prabir Pradhan | 33cfc6d | 2024-06-11 20:17:44 +0000 | [diff] [blame] | 13772 | window->consumeMotionPointerUp(/*pointerIdx=*/2, |
| 13773 | AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT), |
| 13774 | WithFlags(AMOTION_EVENT_FLAG_CANCELED), |
| 13775 | WithPointerCount(3))); |
| 13776 | window->consumeMotionPointerUp(/*pointerIdx=*/1, |
| 13777 | AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT), |
| 13778 | WithFlags(AMOTION_EVENT_FLAG_CANCELED), |
| 13779 | WithPointerCount(2))); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13780 | |
| 13781 | spy->assertNoEvents(); |
| 13782 | window->assertNoEvents(); |
| 13783 | } |
| 13784 | |
| 13785 | /** |
| 13786 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 13787 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 13788 | * window, then that window should receive ACTION_CANCEL. |
| 13789 | */ |
| 13790 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 13791 | auto spy = createSpy(); |
| 13792 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 13793 | auto window = createForeground(); |
| 13794 | window->setFrame(Rect(0, 0, 200, 200)); |
| 13795 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13796 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13797 | |
| 13798 | // First finger down on both spy and window |
| 13799 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13800 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 13801 | ui::LogicalDisplayId::DEFAULT, {10, 10})) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13802 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13803 | window->consumeMotionDown(); |
| 13804 | spy->consumeMotionDown(); |
| 13805 | |
| 13806 | // Second finger down on the spy and window |
| 13807 | const MotionEvent secondFingerDownEvent = |
| 13808 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13809 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13810 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 13811 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 13812 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13813 | .build(); |
| 13814 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13815 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13816 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 13817 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13818 | spy->consumeMotionPointerDown(1); |
| 13819 | window->consumeMotionPointerDown(1); |
| 13820 | |
| 13821 | // Spy window pilfers the pointers. |
| 13822 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 13823 | window->consumeMotionCancel(); |
| 13824 | |
| 13825 | spy->assertNoEvents(); |
| 13826 | window->assertNoEvents(); |
| 13827 | } |
| 13828 | |
| 13829 | /** |
| 13830 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 13831 | * be sent to other windows |
| 13832 | */ |
| 13833 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 13834 | auto spy = createSpy(); |
| 13835 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 13836 | auto window = createForeground(); |
| 13837 | window->setFrame(Rect(0, 0, 200, 200)); |
| 13838 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13839 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13840 | |
| 13841 | // First finger down on both window and spy |
| 13842 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13843 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 13844 | ui::LogicalDisplayId::DEFAULT, {10, 10})) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13845 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13846 | window->consumeMotionDown(); |
| 13847 | spy->consumeMotionDown(); |
| 13848 | |
| 13849 | // Spy window pilfers the pointers. |
| 13850 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 13851 | window->consumeMotionCancel(); |
| 13852 | |
| 13853 | // Second finger down on the window only |
| 13854 | const MotionEvent secondFingerDownEvent = |
| 13855 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13856 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13857 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 13858 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 13859 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13860 | .build(); |
| 13861 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13862 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13863 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 13864 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13865 | window->consumeMotionDown(); |
| 13866 | window->assertNoEvents(); |
| 13867 | |
| 13868 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 13869 | spy->consumeMotionMove(); |
| 13870 | spy->assertNoEvents(); |
| 13871 | } |
| 13872 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 13873 | /** |
| 13874 | * A window on the left and a window on the right. Also, a spy window that's above all of the |
| 13875 | * windows, and spanning both left and right windows. |
| 13876 | * Send simultaneous motion streams from two different devices, one to the left window, and another |
| 13877 | * to the right window. |
| 13878 | * Pilfer from spy window. |
| 13879 | * Check that the pilfering only affects the pointers that are actually being received by the spy. |
| 13880 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13881 | TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer_legacy) { |
| 13882 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 13883 | sp<FakeWindowHandle> spy = createSpy(); |
| 13884 | spy->setFrame(Rect(0, 0, 200, 200)); |
| 13885 | sp<FakeWindowHandle> leftWindow = createForeground(); |
| 13886 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 13887 | |
| 13888 | sp<FakeWindowHandle> rightWindow = createForeground(); |
| 13889 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 13890 | |
| 13891 | constexpr int32_t stylusDeviceId = 1; |
| 13892 | constexpr int32_t touchDeviceId = 2; |
| 13893 | |
| 13894 | mDispatcher->onWindowInfosChanged( |
| 13895 | {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 13896 | |
| 13897 | // Stylus down on left window and spy |
| 13898 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 13899 | .deviceId(stylusDeviceId) |
| 13900 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 13901 | .build()); |
| 13902 | leftWindow->consumeMotionEvent( |
| 13903 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 13904 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 13905 | |
| 13906 | // Finger down on right window and spy - but spy already has stylus |
| 13907 | mDispatcher->notifyMotion( |
| 13908 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 13909 | .deviceId(touchDeviceId) |
| 13910 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 13911 | .build()); |
| 13912 | rightWindow->consumeMotionEvent( |
| 13913 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 13914 | spy->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 13915 | |
| 13916 | // Act: pilfer from spy. Spy is currently receiving touch events. |
| 13917 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 13918 | leftWindow->consumeMotionEvent( |
| 13919 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 13920 | rightWindow->consumeMotionEvent( |
| 13921 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 13922 | |
| 13923 | // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus |
| 13924 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 13925 | .deviceId(stylusDeviceId) |
| 13926 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52)) |
| 13927 | .build()); |
| 13928 | mDispatcher->notifyMotion( |
| 13929 | MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 13930 | .deviceId(touchDeviceId) |
| 13931 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52)) |
| 13932 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 13933 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 13934 | |
| 13935 | spy->assertNoEvents(); |
| 13936 | leftWindow->assertNoEvents(); |
| 13937 | rightWindow->assertNoEvents(); |
| 13938 | } |
| 13939 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13940 | /** |
| 13941 | * A window on the left and a window on the right. Also, a spy window that's above all of the |
| 13942 | * windows, and spanning both left and right windows. |
| 13943 | * Send simultaneous motion streams from two different devices, one to the left window, and another |
| 13944 | * to the right window. |
| 13945 | * Pilfer from spy window. |
| 13946 | * Check that the pilfering affects all of the pointers that are actually being received by the spy. |
| 13947 | * The spy should receive both the touch and the stylus events after pilfer. |
| 13948 | */ |
| 13949 | TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) { |
| 13950 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 13951 | sp<FakeWindowHandle> spy = createSpy(); |
| 13952 | spy->setFrame(Rect(0, 0, 200, 200)); |
| 13953 | sp<FakeWindowHandle> leftWindow = createForeground(); |
| 13954 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 13955 | |
| 13956 | sp<FakeWindowHandle> rightWindow = createForeground(); |
| 13957 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 13958 | |
| 13959 | constexpr int32_t stylusDeviceId = 1; |
| 13960 | constexpr int32_t touchDeviceId = 2; |
| 13961 | |
| 13962 | mDispatcher->onWindowInfosChanged( |
| 13963 | {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 13964 | |
| 13965 | // Stylus down on left window and spy |
| 13966 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 13967 | .deviceId(stylusDeviceId) |
| 13968 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 13969 | .build()); |
| 13970 | leftWindow->consumeMotionEvent( |
| 13971 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 13972 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 13973 | |
| 13974 | // Finger down on right window and spy |
| 13975 | mDispatcher->notifyMotion( |
| 13976 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 13977 | .deviceId(touchDeviceId) |
| 13978 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 13979 | .build()); |
| 13980 | rightWindow->consumeMotionEvent( |
| 13981 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 13982 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 13983 | |
| 13984 | // Act: pilfer from spy. Spy is currently receiving touch events. |
| 13985 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 13986 | leftWindow->consumeMotionEvent( |
| 13987 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId))); |
| 13988 | rightWindow->consumeMotionEvent( |
| 13989 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 13990 | |
| 13991 | // 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] | 13992 | // Instead of sending the two MOVE events for each input device together, and then receiving |
| 13993 | // them both, process them one at at time. InputConsumer is always in the batching mode, which |
| 13994 | // means that the two MOVE events will be initially put into a batch. Once the events are |
| 13995 | // batched, the 'consume' call may result in any of the MOVE events to be sent first (depending |
| 13996 | // on the implementation of InputConsumer), which would mean that the order of the received |
| 13997 | // events could be different depending on whether there are 1 or 2 events pending in the |
| 13998 | // InputChannel at the time the test calls 'consume'. To make assertions simpler here, and to |
| 13999 | // avoid this confusing behaviour, send and receive each MOVE event separately. |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 14000 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 14001 | .deviceId(stylusDeviceId) |
| 14002 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52)) |
| 14003 | .build()); |
Siarhei Vishniakou | 92bca1c | 2024-04-01 14:06:59 -0700 | [diff] [blame] | 14004 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 14005 | mDispatcher->notifyMotion( |
| 14006 | MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 14007 | .deviceId(touchDeviceId) |
| 14008 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52)) |
| 14009 | .build()); |
Siarhei Vishniakou | 92bca1c | 2024-04-01 14:06:59 -0700 | [diff] [blame] | 14010 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 14011 | |
| 14012 | spy->assertNoEvents(); |
| 14013 | leftWindow->assertNoEvents(); |
| 14014 | rightWindow->assertNoEvents(); |
| 14015 | } |
| 14016 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 14017 | TEST_F(InputDispatcherPilferPointersTest, NoPilferingWithHoveringPointers) { |
| 14018 | auto window = createForeground(); |
| 14019 | auto spy = createSpy(); |
| 14020 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 14021 | |
| 14022 | mDispatcher->notifyMotion( |
| 14023 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 14024 | .deviceId(1) |
| 14025 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(100).y(200)) |
| 14026 | .build()); |
| 14027 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 14028 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 14029 | |
| 14030 | // Pilfer pointers from the spy window should fail. |
| 14031 | EXPECT_NE(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 14032 | spy->assertNoEvents(); |
| 14033 | window->assertNoEvents(); |
| 14034 | } |
| 14035 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 14036 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 14037 | public: |
| 14038 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 14039 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 14040 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14041 | sp<FakeWindowHandle> overlay = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 14042 | "Stylus interceptor window", |
| 14043 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 14044 | overlay->setFocusable(false); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 14045 | overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 14046 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 14047 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 14048 | overlay->setTrustedOverlay(true); |
| 14049 | |
| 14050 | std::shared_ptr<FakeApplicationHandle> application = |
| 14051 | std::make_shared<FakeApplicationHandle>(); |
| 14052 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 14053 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14054 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 14055 | window->setFocusable(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 14056 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 14057 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14058 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 14059 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 14060 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 14061 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 14062 | return {std::move(overlay), std::move(window)}; |
| 14063 | } |
| 14064 | |
| 14065 | void sendFingerEvent(int32_t action) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 14066 | mDispatcher->notifyMotion( |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 14067 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14068 | ui::LogicalDisplayId::DEFAULT, {PointF{20, 20}})); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 14069 | } |
| 14070 | |
| 14071 | void sendStylusEvent(int32_t action) { |
| 14072 | NotifyMotionArgs motionArgs = |
| 14073 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14074 | ui::LogicalDisplayId::DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 14075 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 14076 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 14077 | } |
| 14078 | }; |
| 14079 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 14080 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 14081 | |
| 14082 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
Siarhei Vishniakou | ad3b682 | 2023-06-22 14:17:35 -0700 | [diff] [blame] | 14083 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 14084 | ScopedSilentDeath _silentDeath; |
| 14085 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 14086 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 14087 | overlay->setTrustedOverlay(false); |
| 14088 | // 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] | 14089 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 14090 | {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 14091 | ".* not a trusted overlay"); |
| 14092 | } |
| 14093 | |
| 14094 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 14095 | auto [overlay, window] = setupStylusOverlayScenario(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 14096 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 14097 | |
| 14098 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 14099 | overlay->consumeMotionDown(); |
| 14100 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 14101 | overlay->consumeMotionUp(); |
| 14102 | |
| 14103 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 14104 | window->consumeMotionDown(); |
| 14105 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 14106 | window->consumeMotionUp(); |
| 14107 | |
| 14108 | overlay->assertNoEvents(); |
| 14109 | window->assertNoEvents(); |
| 14110 | } |
| 14111 | |
| 14112 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 14113 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 14114 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 14115 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 14116 | |
| 14117 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 14118 | overlay->consumeMotionDown(); |
| 14119 | window->consumeMotionDown(); |
| 14120 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 14121 | overlay->consumeMotionUp(); |
| 14122 | window->consumeMotionUp(); |
| 14123 | |
| 14124 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 14125 | window->consumeMotionDown(); |
| 14126 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 14127 | window->consumeMotionUp(); |
| 14128 | |
| 14129 | overlay->assertNoEvents(); |
| 14130 | window->assertNoEvents(); |
| 14131 | } |
| 14132 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 14133 | /** |
| 14134 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 14135 | * The scenario is as follows: |
| 14136 | * - The stylus interceptor overlay is configured as a spy window. |
| 14137 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 14138 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 14139 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 14140 | */ |
| 14141 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 14142 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 14143 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 14144 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 14145 | |
| 14146 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 14147 | overlay->consumeMotionDown(); |
| 14148 | window->consumeMotionDown(); |
| 14149 | |
| 14150 | // The interceptor pilfers the pointers. |
| 14151 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 14152 | window->consumeMotionCancel(); |
| 14153 | |
| 14154 | // The interceptor configures itself so that it is no longer a spy. |
| 14155 | overlay->setSpy(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 14156 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 14157 | |
| 14158 | // It continues to receive the rest of the stylus gesture. |
| 14159 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 14160 | overlay->consumeMotionMove(); |
| 14161 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 14162 | overlay->consumeMotionUp(); |
| 14163 | |
| 14164 | window->assertNoEvents(); |
| 14165 | } |
| 14166 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14167 | struct User { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 14168 | gui::Pid mPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 14169 | gui::Uid mUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14170 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 14171 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 14172 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 14173 | User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid) |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14174 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 14175 | |
| 14176 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 14177 | return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14178 | ui::LogicalDisplayId::DEFAULT, {100, 200}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14179 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 14180 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 14181 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 14182 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 14183 | } |
| 14184 | |
| 14185 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 14186 | return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14187 | ui::LogicalDisplayId::INVALID, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14188 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 14189 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14190 | mPolicyFlags); |
| 14191 | } |
| 14192 | |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 14193 | sp<FakeWindowHandle> createWindow(const char* name) const { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14194 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 14195 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14196 | sp<FakeWindowHandle> window = |
| 14197 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, name, |
| 14198 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14199 | window->setOwnerInfo(mPid, mUid); |
| 14200 | return window; |
| 14201 | } |
| 14202 | }; |
| 14203 | |
| 14204 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 14205 | |
| 14206 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 14207 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 14208 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 14209 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14210 | |
| 14211 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 14212 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 14213 | window->consumeMotionDown(); |
| 14214 | |
| 14215 | setFocusedWindow(window); |
| 14216 | window->consumeFocusEvent(true); |
| 14217 | |
| 14218 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 14219 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14220 | window->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14221 | } |
| 14222 | |
| 14223 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 14224 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 14225 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 14226 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14227 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 14228 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14229 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 14230 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 14231 | |
| 14232 | setFocusedWindow(window); |
| 14233 | window->consumeFocusEvent(true); |
| 14234 | |
| 14235 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 14236 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 14237 | window->assertNoEvents(); |
| 14238 | } |
| 14239 | |
| 14240 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 14241 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 14242 | auto window = owner.createWindow("Owned window"); |
| 14243 | auto spy = owner.createWindow("Owned spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14244 | spy->setSpy(true); |
| 14245 | spy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 14246 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14247 | |
| 14248 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 14249 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 14250 | spy->consumeMotionDown(); |
| 14251 | window->consumeMotionDown(); |
| 14252 | } |
| 14253 | |
| 14254 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 14255 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 14256 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14257 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 14258 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 14259 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14260 | randosSpy->setSpy(true); |
| 14261 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 14262 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14263 | |
| 14264 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 14265 | // not receive the event. |
| 14266 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 14267 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 14268 | randosSpy->assertNoEvents(); |
| 14269 | window->consumeMotionDown(); |
| 14270 | } |
| 14271 | |
| 14272 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 14273 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 14274 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14275 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 14276 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 14277 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14278 | randosSpy->setSpy(true); |
| 14279 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 14280 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14281 | |
| 14282 | // A user that has injection permission can inject into any window. |
| 14283 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 14284 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14285 | ui::LogicalDisplayId::DEFAULT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14286 | randosSpy->consumeMotionDown(); |
| 14287 | window->consumeMotionDown(); |
| 14288 | |
| 14289 | setFocusedWindow(randosSpy); |
| 14290 | randosSpy->consumeFocusEvent(true); |
| 14291 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 14292 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14293 | randosSpy->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14294 | window->assertNoEvents(); |
| 14295 | } |
| 14296 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 14297 | TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 14298 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 14299 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14300 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 14301 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 14302 | auto randosWindow = rando.createWindow("Rando's window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14303 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 14304 | randosWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 14305 | mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14306 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 14307 | // 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] | 14308 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 14309 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 14310 | window->consumeMotionDown(); |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 14311 | randosWindow->assertNoEvents(); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 14312 | } |
| 14313 | |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14314 | using InputDispatcherPointerInWindowTest = InputDispatcherTest; |
| 14315 | |
| 14316 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWhenHovering) { |
| 14317 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 14318 | |
| 14319 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14320 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14321 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14322 | sp<FakeWindowHandle> right = |
| 14323 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 14324 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14325 | right->setFrame(Rect(100, 0, 200, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14326 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", |
| 14327 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14328 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 14329 | spy->setTrustedOverlay(true); |
| 14330 | spy->setSpy(true); |
| 14331 | |
| 14332 | mDispatcher->onWindowInfosChanged( |
| 14333 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 14334 | |
| 14335 | // Hover into the left window. |
| 14336 | mDispatcher->notifyMotion( |
| 14337 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 14338 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(50).y(50)) |
| 14339 | .build()); |
| 14340 | |
| 14341 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 14342 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 14343 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14344 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14345 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14346 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14347 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14348 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14349 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14350 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14351 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14352 | /*pointerId=*/0)); |
| 14353 | |
| 14354 | // Hover move to the right window. |
| 14355 | mDispatcher->notifyMotion( |
| 14356 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 14357 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 14358 | .build()); |
| 14359 | |
| 14360 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 14361 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 14362 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 14363 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14364 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14365 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14366 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14367 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14368 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14369 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14370 | ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14371 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14372 | /*pointerId=*/0)); |
| 14373 | |
| 14374 | // Stop hovering. |
| 14375 | mDispatcher->notifyMotion( |
| 14376 | MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 14377 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 14378 | .build()); |
| 14379 | |
| 14380 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 14381 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 14382 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14383 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14384 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14385 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14386 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14387 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14388 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14389 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14390 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14391 | /*pointerId=*/0)); |
| 14392 | } |
| 14393 | |
| 14394 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWithSplitTouch) { |
| 14395 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 14396 | |
| 14397 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14398 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14399 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14400 | sp<FakeWindowHandle> right = |
| 14401 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 14402 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14403 | right->setFrame(Rect(100, 0, 200, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14404 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", |
| 14405 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14406 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 14407 | spy->setTrustedOverlay(true); |
| 14408 | spy->setSpy(true); |
| 14409 | |
| 14410 | mDispatcher->onWindowInfosChanged( |
| 14411 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 14412 | |
| 14413 | // First pointer down on left window. |
| 14414 | mDispatcher->notifyMotion( |
| 14415 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 14416 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 14417 | .build()); |
| 14418 | |
| 14419 | left->consumeMotionDown(); |
| 14420 | spy->consumeMotionDown(); |
| 14421 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14422 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14423 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14424 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14425 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14426 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14427 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14428 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14429 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14430 | /*pointerId=*/0)); |
| 14431 | |
| 14432 | // Second pointer down on right window. |
| 14433 | mDispatcher->notifyMotion( |
| 14434 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 14435 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 14436 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 14437 | .build()); |
| 14438 | |
| 14439 | left->consumeMotionMove(); |
| 14440 | right->consumeMotionDown(); |
| 14441 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 14442 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14443 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14444 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14445 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14446 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14447 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14448 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14449 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14450 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14451 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14452 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14453 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14454 | /*pointerId=*/1)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14455 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14456 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14457 | /*pointerId=*/1)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14458 | ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14459 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14460 | /*pointerId=*/1)); |
| 14461 | |
| 14462 | // Second pointer up. |
| 14463 | mDispatcher->notifyMotion( |
| 14464 | MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 14465 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 14466 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 14467 | .build()); |
| 14468 | |
| 14469 | left->consumeMotionMove(); |
| 14470 | right->consumeMotionUp(); |
| 14471 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 14472 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14473 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14474 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14475 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14476 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14477 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14478 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14479 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14480 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14481 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14482 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14483 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14484 | /*pointerId=*/1)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14485 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14486 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14487 | /*pointerId=*/1)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14488 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14489 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14490 | /*pointerId=*/1)); |
| 14491 | |
| 14492 | // First pointer up. |
| 14493 | mDispatcher->notifyMotion( |
| 14494 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 14495 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 14496 | .build()); |
| 14497 | |
| 14498 | left->consumeMotionUp(); |
| 14499 | spy->consumeMotionUp(); |
| 14500 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14501 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14502 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14503 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14504 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14505 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14506 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14507 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14508 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14509 | /*pointerId=*/0)); |
| 14510 | } |
| 14511 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 14512 | TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse_legacy) { |
| 14513 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14514 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 14515 | |
| 14516 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14517 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14518 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14519 | sp<FakeWindowHandle> right = |
| 14520 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 14521 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14522 | right->setFrame(Rect(100, 0, 200, 100)); |
| 14523 | |
| 14524 | mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 14525 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14526 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14527 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14528 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14529 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14530 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14531 | /*pointerId=*/0)); |
| 14532 | |
| 14533 | // Hover move into the window. |
| 14534 | mDispatcher->notifyMotion( |
| 14535 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 14536 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50)) |
| 14537 | .rawXCursorPosition(50) |
| 14538 | .rawYCursorPosition(50) |
| 14539 | .deviceId(DEVICE_ID) |
| 14540 | .build()); |
| 14541 | |
| 14542 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 14543 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14544 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14545 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14546 | /*pointerId=*/0)); |
| 14547 | |
| 14548 | // Move the mouse with another device. This cancels the hovering pointer from the first device. |
| 14549 | mDispatcher->notifyMotion( |
| 14550 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 14551 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50)) |
| 14552 | .rawXCursorPosition(51) |
| 14553 | .rawYCursorPosition(50) |
| 14554 | .deviceId(SECOND_DEVICE_ID) |
| 14555 | .build()); |
| 14556 | |
| 14557 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 14558 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 14559 | |
| 14560 | // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets |
| 14561 | // a HOVER_EXIT from the first device. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14562 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14563 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14564 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14565 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14566 | SECOND_DEVICE_ID, |
| 14567 | /*pointerId=*/0)); |
| 14568 | |
| 14569 | // Move the mouse outside the window. Document the current behavior, where the window does not |
| 14570 | // receive HOVER_EXIT even though the mouse left the window. |
| 14571 | mDispatcher->notifyMotion( |
| 14572 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 14573 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50)) |
| 14574 | .rawXCursorPosition(150) |
| 14575 | .rawYCursorPosition(50) |
| 14576 | .deviceId(SECOND_DEVICE_ID) |
| 14577 | .build()); |
| 14578 | |
| 14579 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 14580 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14581 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14582 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14583 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14584 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 14585 | SECOND_DEVICE_ID, |
| 14586 | /*pointerId=*/0)); |
| 14587 | } |
| 14588 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 14589 | /** |
| 14590 | * TODO(b/313689709) - correctly support multiple mouse devices, because they should be controlling |
| 14591 | * the same cursor, and therefore have a shared motion event stream. |
| 14592 | */ |
| 14593 | TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse) { |
| 14594 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 14595 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 14596 | |
| 14597 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14598 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 14599 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14600 | sp<FakeWindowHandle> right = |
| 14601 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 14602 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 14603 | right->setFrame(Rect(100, 0, 200, 100)); |
| 14604 | |
| 14605 | mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 14606 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14607 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14608 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 14609 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14610 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14611 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 14612 | /*pointerId=*/0)); |
| 14613 | |
| 14614 | // Hover move into the window. |
| 14615 | mDispatcher->notifyMotion( |
| 14616 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 14617 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50)) |
| 14618 | .rawXCursorPosition(50) |
| 14619 | .rawYCursorPosition(50) |
| 14620 | .deviceId(DEVICE_ID) |
| 14621 | .build()); |
| 14622 | |
| 14623 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 14624 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14625 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14626 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 14627 | /*pointerId=*/0)); |
| 14628 | |
| 14629 | // Move the mouse with another device |
| 14630 | mDispatcher->notifyMotion( |
| 14631 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 14632 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50)) |
| 14633 | .rawXCursorPosition(51) |
| 14634 | .rawYCursorPosition(50) |
| 14635 | .deviceId(SECOND_DEVICE_ID) |
| 14636 | .build()); |
| 14637 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 14638 | |
| 14639 | // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets |
| 14640 | // a HOVER_EXIT from the first device. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14641 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14642 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 14643 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14644 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 14645 | SECOND_DEVICE_ID, |
| 14646 | /*pointerId=*/0)); |
| 14647 | |
| 14648 | // Move the mouse outside the window. Document the current behavior, where the window does not |
| 14649 | // receive HOVER_EXIT even though the mouse left the window. |
| 14650 | mDispatcher->notifyMotion( |
| 14651 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 14652 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50)) |
| 14653 | .rawXCursorPosition(150) |
| 14654 | .rawYCursorPosition(50) |
| 14655 | .deviceId(SECOND_DEVICE_ID) |
| 14656 | .build()); |
| 14657 | |
| 14658 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14659 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 14660 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 14661 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 14662 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 14663 | SECOND_DEVICE_ID, |
| 14664 | /*pointerId=*/0)); |
| 14665 | } |
| 14666 | |
Arpit Singh | b65e2bd | 2024-06-03 09:48:16 +0000 | [diff] [blame] | 14667 | TEST_F(InputDispatcherTest, FocusedDisplayChangeIsNotified) { |
| 14668 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 14669 | mFakePolicy->assertFocusedDisplayNotified(SECOND_DISPLAY_ID); |
| 14670 | } |
| 14671 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 14672 | } // namespace android::inputdispatcher |