Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Garfield Tan | 0fc2fa7 | 2019-08-29 17:22:15 -0700 | [diff] [blame] | 17 | #include "../dispatcher/InputDispatcher.h" |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 18 | #include "FakeApplicationHandle.h" |
Prabir Pradhan | 81e89fe | 2024-03-20 21:17:09 +0000 | [diff] [blame] | 19 | #include "FakeInputDispatcherPolicy.h" |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 20 | #include "FakeInputTracingBackend.h" |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 21 | #include "FakeWindows.h" |
Prabir Pradhan | e3b28dd | 2023-10-06 04:19:29 +0000 | [diff] [blame] | 22 | #include "TestEventMatchers.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 23 | |
Cody Heiner | 166a5af | 2023-07-07 12:25:00 -0700 | [diff] [blame] | 24 | #include <NotifyArgsBuilders.h> |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 25 | #include <android-base/logging.h> |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 26 | #include <android-base/properties.h> |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 27 | #include <android-base/silent_death_test.h> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 28 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 29 | #include <android-base/thread_annotations.h> |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 30 | #include <binder/Binder.h> |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 31 | #include <com_android_input_flags.h> |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 32 | #include <fcntl.h> |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 33 | #include <flag_macros.h> |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 34 | #include <gmock/gmock.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 35 | #include <gtest/gtest.h> |
Siarhei Vishniakou | 3782af6 | 2024-03-07 21:56:39 -0800 | [diff] [blame] | 36 | #include <input/BlockingQueue.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 37 | #include <input/Input.h> |
Siarhei Vishniakou | 0438ca8 | 2024-03-12 14:27:25 -0700 | [diff] [blame] | 38 | #include <input/InputConsumer.h> |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 39 | #include <input/PrintTools.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 40 | #include <linux/input.h> |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 41 | #include <sys/epoll.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 42 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 43 | #include <cinttypes> |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 44 | #include <compare> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 45 | #include <thread> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 46 | #include <unordered_set> |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 47 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 48 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 49 | using android::base::StringPrintf; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 50 | using android::gui::FocusRequest; |
| 51 | using android::gui::TouchOcclusionMode; |
| 52 | using android::gui::WindowInfo; |
| 53 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 54 | using android::os::InputEventInjectionResult; |
| 55 | using android::os::InputEventInjectionSync; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 56 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 57 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 58 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 59 | using namespace ftl::flag_operators; |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 60 | using testing::AllOf; |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 61 | using testing::Not; |
Siarhei Vishniakou | 85eb580 | 2024-04-01 11:40:59 -0700 | [diff] [blame] | 62 | using testing::Pointee; |
| 63 | using testing::UnorderedElementsAre; |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 64 | |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 65 | namespace { |
| 66 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 67 | // An arbitrary time value. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 68 | static constexpr nsecs_t ARBITRARY_TIME = 1234; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 69 | |
| 70 | // An arbitrary device id. |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 71 | static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 72 | static constexpr int32_t SECOND_DEVICE_ID = 2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 73 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 74 | // An arbitrary display id. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 75 | constexpr ui::LogicalDisplayId DISPLAY_ID = ui::LogicalDisplayId::DEFAULT; |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 76 | constexpr ui::LogicalDisplayId SECOND_DISPLAY_ID = ui::LogicalDisplayId{1}; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 77 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 78 | // Ensure common actions are interchangeable between keys and motions for convenience. |
| 79 | static_assert(AMOTION_EVENT_ACTION_DOWN == AKEY_EVENT_ACTION_DOWN); |
| 80 | static_assert(AMOTION_EVENT_ACTION_UP == AKEY_EVENT_ACTION_UP); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 81 | static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN; |
| 82 | static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE; |
| 83 | static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP; |
| 84 | static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 85 | static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 86 | static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 87 | static constexpr int32_t ACTION_SCROLL = AMOTION_EVENT_ACTION_SCROLL; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 88 | static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 89 | static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 90 | /** |
| 91 | * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the |
| 92 | * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at |
| 93 | * index 0) is the new pointer going down. The same pointer could have been placed at a different |
| 94 | * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In |
| 95 | * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if |
| 96 | * pointer id=0 leaves but the pointer id=1 remains. |
| 97 | */ |
| 98 | static constexpr int32_t POINTER_0_DOWN = |
| 99 | AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 100 | static constexpr int32_t POINTER_1_DOWN = |
| 101 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 102 | static constexpr int32_t POINTER_2_DOWN = |
| 103 | AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 104 | static constexpr int32_t POINTER_3_DOWN = |
| 105 | AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 106 | static constexpr int32_t POINTER_0_UP = |
| 107 | AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 108 | static constexpr int32_t POINTER_1_UP = |
| 109 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 110 | static constexpr int32_t POINTER_2_UP = |
| 111 | AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 112 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 113 | // The default pid and uid for the windows created on the secondary display by the test. |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 114 | static constexpr gui::Pid SECONDARY_WINDOW_PID{1010}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 115 | static constexpr gui::Uid SECONDARY_WINDOW_UID{1012}; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 116 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 117 | // An arbitrary pid of the gesture monitor window |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 118 | static constexpr gui::Pid MONITOR_PID{2001}; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 119 | |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 120 | static constexpr int EXPECTED_WALLPAPER_FLAGS = |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 121 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 122 | |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 123 | using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID; |
| 124 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 125 | /** |
| 126 | * Return a DOWN key event with KEYCODE_A. |
| 127 | */ |
| 128 | static KeyEvent getTestKeyEvent() { |
| 129 | KeyEvent event; |
| 130 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 131 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 132 | ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, |
| 133 | AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 134 | return event; |
| 135 | } |
| 136 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 137 | /** |
| 138 | * Provide a local override for a flag value. The value is restored when the object of this class |
| 139 | * goes out of scope. |
| 140 | * This class is not intended to be used directly, because its usage is cumbersome. |
| 141 | * Instead, a wrapper macro SCOPED_FLAG_OVERRIDE is provided. |
| 142 | */ |
| 143 | class ScopedFlagOverride { |
| 144 | public: |
| 145 | ScopedFlagOverride(std::function<bool()> read, std::function<void(bool)> write, bool value) |
| 146 | : mInitialValue(read()), mWriteValue(write) { |
| 147 | mWriteValue(value); |
| 148 | } |
| 149 | ~ScopedFlagOverride() { mWriteValue(mInitialValue); } |
| 150 | |
| 151 | private: |
| 152 | const bool mInitialValue; |
| 153 | std::function<void(bool)> mWriteValue; |
| 154 | }; |
| 155 | |
| 156 | typedef bool (*readFlagValueFunction)(); |
| 157 | typedef void (*writeFlagValueFunction)(bool); |
| 158 | |
| 159 | /** |
| 160 | * Use this macro to locally override a flag value. |
| 161 | * Example usage: |
| 162 | * SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
| 163 | * Note: this works by creating a local variable in your current scope. Don't call this twice for |
| 164 | * the same flag, because the variable names will clash! |
| 165 | */ |
| 166 | #define SCOPED_FLAG_OVERRIDE(NAME, VALUE) \ |
| 167 | readFlagValueFunction read##NAME = com::android::input::flags::NAME; \ |
| 168 | writeFlagValueFunction write##NAME = com::android::input::flags::NAME; \ |
| 169 | ScopedFlagOverride override##NAME(read##NAME, write##NAME, (VALUE)) |
| 170 | |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 171 | } // namespace |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 172 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 173 | // --- InputDispatcherTest --- |
| 174 | |
| 175 | class InputDispatcherTest : public testing::Test { |
| 176 | protected: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 177 | std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 178 | std::unique_ptr<InputDispatcher> mDispatcher; |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 179 | std::shared_ptr<VerifyingTrace> mVerifyingTrace; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 180 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 181 | void SetUp() override { |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 182 | mVerifyingTrace = std::make_shared<VerifyingTrace>(); |
| 183 | FakeWindowHandle::sOnEventReceivedCallback = [this](const auto& _1, const auto& _2) { |
| 184 | handleEventReceivedByWindow(_1, _2); |
| 185 | }; |
| 186 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 187 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 188 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, |
| 189 | std::make_unique<FakeInputTracingBackend>( |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 190 | mVerifyingTrace)); |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 191 | |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 192 | mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 193 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 194 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 197 | void TearDown() override { |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 198 | ASSERT_NO_FATAL_FAILURE(mVerifyingTrace->verifyExpectedEventsTraced()); |
| 199 | FakeWindowHandle::sOnEventReceivedCallback = nullptr; |
| 200 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 201 | ASSERT_EQ(OK, mDispatcher->stop()); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 202 | mFakePolicy.reset(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 203 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 204 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 205 | |
Prabir Pradhan | c534073 | 2024-03-20 22:53:52 +0000 | [diff] [blame] | 206 | void handleEventReceivedByWindow(const std::unique_ptr<InputEvent>& event, |
| 207 | const gui::WindowInfo& info) { |
| 208 | if (!event) { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | switch (event->getType()) { |
| 213 | case InputEventType::KEY: { |
| 214 | mVerifyingTrace->expectKeyDispatchTraced(static_cast<KeyEvent&>(*event), info.id); |
| 215 | break; |
| 216 | } |
| 217 | case InputEventType::MOTION: { |
| 218 | mVerifyingTrace->expectMotionDispatchTraced(static_cast<MotionEvent&>(*event), |
| 219 | info.id); |
| 220 | break; |
| 221 | } |
| 222 | default: |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 227 | /** |
| 228 | * Used for debugging when writing the test |
| 229 | */ |
| 230 | void dumpDispatcherState() { |
| 231 | std::string dump; |
| 232 | mDispatcher->dump(dump); |
| 233 | std::stringstream ss(dump); |
| 234 | std::string to; |
| 235 | |
| 236 | while (std::getline(ss, to, '\n')) { |
| 237 | ALOGE("%s", to.c_str()); |
| 238 | } |
| 239 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 240 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 241 | void setFocusedWindow(const sp<WindowInfoHandle>& window) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 242 | FocusRequest request; |
| 243 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 244 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 245 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 246 | request.displayId = window->getInfo()->displayId.val(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 247 | mDispatcher->setFocusedWindow(request); |
| 248 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 249 | }; |
| 250 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 251 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 252 | KeyEvent event; |
| 253 | |
| 254 | // Rejects undefined key actions. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 255 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 256 | ui::LogicalDisplayId::INVALID, INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 257 | /*action=*/-1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 258 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 259 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 260 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 261 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 262 | << "Should reject key events with undefined action."; |
| 263 | |
| 264 | // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 265 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 266 | ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, |
| 267 | AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 268 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 269 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 270 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 271 | << "Should reject key events with ACTION_MULTIPLE."; |
| 272 | } |
| 273 | |
| 274 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 275 | MotionEvent event; |
| 276 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 277 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 278 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 279 | pointerProperties[i].clear(); |
| 280 | pointerProperties[i].id = i; |
| 281 | pointerCoords[i].clear(); |
| 282 | } |
| 283 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 284 | // Some constants commonly used below |
| 285 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 286 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 287 | constexpr int32_t metaState = AMETA_NONE; |
| 288 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 289 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 290 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 291 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 292 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 293 | /*action=*/-1, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 294 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 295 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 296 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 297 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 298 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 299 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 300 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 301 | << "Should reject motion events with undefined action."; |
| 302 | |
| 303 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 304 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 305 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 306 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 307 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 308 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 309 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 310 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 311 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 312 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 313 | << "Should reject motion events with pointer down index too large."; |
| 314 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 315 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 316 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 317 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 318 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 319 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 320 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 321 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 322 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 323 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 324 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 325 | << "Should reject motion events with pointer down index too small."; |
| 326 | |
| 327 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 328 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 329 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 330 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 331 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 332 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 333 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 334 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 335 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 336 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 337 | << "Should reject motion events with pointer up index too large."; |
| 338 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 339 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 340 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 341 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 342 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 343 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 344 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 345 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 346 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 347 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 348 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 349 | << "Should reject motion events with pointer up index too small."; |
| 350 | |
| 351 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 352 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 353 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 354 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 355 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 356 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 357 | /*pointerCount=*/0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 358 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 359 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 360 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 361 | << "Should reject motion events with 0 pointers."; |
| 362 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 363 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 364 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 365 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 366 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 367 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 368 | /*pointerCount=*/MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 369 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 370 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 371 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 372 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 373 | |
| 374 | // Rejects motion events with invalid pointer ids. |
| 375 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 376 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 377 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 378 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 379 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 380 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 381 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 382 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 383 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 384 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 385 | << "Should reject motion events with pointer ids less than 0."; |
| 386 | |
| 387 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 388 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 389 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 390 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 391 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 392 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 393 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 394 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 395 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 396 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 397 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 398 | |
| 399 | // Rejects motion events with duplicate pointer ids. |
| 400 | pointerProperties[0].id = 1; |
| 401 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 402 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 403 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 404 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 405 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 406 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 407 | /*pointerCount=*/2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 408 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 409 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 410 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 411 | << "Should reject motion events with duplicate pointer ids."; |
| 412 | } |
| 413 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 414 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 415 | |
| 416 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 417 | constexpr nsecs_t eventTime = 20; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 418 | mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 419 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 420 | |
| 421 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 422 | } |
| 423 | |
| 424 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 425 | NotifySwitchArgs args(InputEvent::nextId(), /*eventTime=*/20, /*policyFlags=*/0, |
| 426 | /*switchValues=*/1, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 427 | /*switchMask=*/2); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 428 | mDispatcher->notifySwitch(args); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 429 | |
| 430 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 431 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 432 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 433 | } |
| 434 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 435 | namespace { |
| 436 | |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 437 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 438 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 439 | class FakeMonitorReceiver { |
| 440 | public: |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 441 | FakeMonitorReceiver(InputDispatcher& dispatcher, const std::string name, |
| 442 | ui::LogicalDisplayId displayId) |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 443 | : mInputReceiver(*dispatcher.createInputMonitor(displayId, name, MONITOR_PID), name) {} |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 444 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 445 | sp<IBinder> getToken() { return mInputReceiver.getToken(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 446 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 447 | void consumeKeyDown(ui::LogicalDisplayId expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 448 | mInputReceiver.consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 449 | expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | std::optional<int32_t> receiveEvent() { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 453 | const auto [sequenceNum, _] = mInputReceiver.receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED); |
| 454 | return sequenceNum; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 457 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver.finishEvent(consumeSeq); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 458 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 459 | void consumeMotionDown(ui::LogicalDisplayId expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 460 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 461 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 464 | void consumeMotionMove(ui::LogicalDisplayId expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 465 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 466 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 469 | void consumeMotionUp(ui::LogicalDisplayId expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 470 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, |
| 471 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 474 | void consumeMotionCancel(ui::LogicalDisplayId expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 475 | mInputReceiver.consumeMotionEvent( |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 476 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 477 | WithDisplayId(expectedDisplayId), |
| 478 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
| 479 | } |
| 480 | |
| 481 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 482 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 483 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 484 | mInputReceiver.consumeEvent(InputEventType::MOTION, action, ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 485 | /*expectedFlags=*/0); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 489 | mInputReceiver.consumeMotionEvent(matcher); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 492 | std::unique_ptr<MotionEvent> consumeMotion() { return mInputReceiver.consumeMotion(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 493 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 494 | void assertNoEvents() { mInputReceiver.assertNoEvents(CONSUME_TIMEOUT_NO_EVENT_EXPECTED); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 495 | |
| 496 | private: |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 497 | FakeInputReceiver mInputReceiver; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 498 | }; |
| 499 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 500 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 501 | InputDispatcher& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 502 | ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 503 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 504 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 505 | bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 506 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 507 | KeyEvent event; |
| 508 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 509 | |
| 510 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 511 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 512 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 513 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 514 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 515 | if (!allowKeyRepeat) { |
| 516 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 517 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 518 | // Inject event until dispatch out. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 519 | return dispatcher.injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 520 | } |
| 521 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 522 | static void assertInjectedKeyTimesOut(InputDispatcher& dispatcher) { |
| 523 | InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 524 | injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 525 | ui::LogicalDisplayId::INVALID, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 526 | CONSUME_TIMEOUT_NO_EVENT_EXPECTED); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 527 | if (result != InputEventInjectionResult::TIMED_OUT) { |
| 528 | FAIL() << "Injection should have timed out, but got " << ftl::enum_string(result); |
| 529 | } |
| 530 | } |
| 531 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 532 | static InputEventInjectionResult injectKeyDown( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 533 | InputDispatcher& dispatcher, |
| 534 | ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 535 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 538 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 539 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 540 | // has to be woken up to process the repeating key. |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 541 | static InputEventInjectionResult injectKeyDownNoRepeat( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 542 | InputDispatcher& dispatcher, |
| 543 | ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 544 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 545 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 546 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 547 | } |
| 548 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 549 | static InputEventInjectionResult injectKeyUp( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 550 | InputDispatcher& dispatcher, |
| 551 | ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 552 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 555 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 556 | InputDispatcher& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 557 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 558 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 559 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 560 | return dispatcher.injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 561 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 564 | static InputEventInjectionResult injectMotionEvent( |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 565 | InputDispatcher& dispatcher, int32_t action, int32_t source, ui::LogicalDisplayId displayId, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 566 | const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 567 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 568 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 569 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 570 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 571 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 572 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 573 | MotionEventBuilder motionBuilder = |
| 574 | MotionEventBuilder(action, source) |
| 575 | .displayId(displayId) |
| 576 | .eventTime(eventTime) |
| 577 | .rawXCursorPosition(cursorPosition.x) |
| 578 | .rawYCursorPosition(cursorPosition.y) |
| 579 | .pointer( |
| 580 | PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y)); |
| 581 | if (MotionEvent::getActionMasked(action) == ACTION_DOWN) { |
| 582 | motionBuilder.downTime(eventTime); |
| 583 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 584 | |
| 585 | // Inject event until dispatch out. |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 586 | return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode, |
| 587 | targetUid, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 588 | } |
| 589 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 590 | static InputEventInjectionResult injectMotionDown(InputDispatcher& dispatcher, int32_t source, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 591 | ui::LogicalDisplayId displayId, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 592 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 593 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 596 | static InputEventInjectionResult injectMotionUp(InputDispatcher& dispatcher, int32_t source, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 597 | ui::LogicalDisplayId displayId, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 598 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 599 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 602 | static NotifyKeyArgs generateKeyArgs( |
| 603 | int32_t action, ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 604 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 605 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 606 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 607 | AINPUT_SOURCE_KEYBOARD, displayId, POLICY_FLAG_PASS_TO_USER, action, |
| 608 | /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 609 | |
| 610 | return args; |
| 611 | } |
| 612 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 613 | [[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 614 | ui::LogicalDisplayId displayId, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 615 | const std::vector<PointF>& points) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 616 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 617 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 618 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 619 | } |
| 620 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 621 | PointerProperties pointerProperties[pointerCount]; |
| 622 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 623 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 624 | for (size_t i = 0; i < pointerCount; i++) { |
| 625 | pointerProperties[i].clear(); |
| 626 | pointerProperties[i].id = i; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 627 | pointerProperties[i].toolType = ToolType::FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 628 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 629 | pointerCoords[i].clear(); |
| 630 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 631 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 632 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 633 | |
| 634 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 635 | // Define a valid motion event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 636 | NotifyMotionArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, source, |
| 637 | displayId, POLICY_FLAG_PASS_TO_USER, action, /*actionButton=*/0, |
| 638 | /*flags=*/0, AMETA_NONE, /*buttonState=*/0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 639 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 640 | pointerCoords, /*xPrecision=*/0, /*yPrecision=*/0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 641 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 642 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /*videoFrames=*/{}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 643 | |
| 644 | return args; |
| 645 | } |
| 646 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 647 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 648 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 649 | } |
| 650 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 651 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
| 652 | ui::LogicalDisplayId displayId) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 653 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 654 | } |
| 655 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 656 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 657 | const PointerCaptureRequest& request) { |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 658 | return NotifyPointerCaptureChangedArgs(InputEvent::nextId(), systemTime(SYSTEM_TIME_MONOTONIC), |
| 659 | request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 660 | } |
| 661 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 662 | } // namespace |
| 663 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 664 | /** |
| 665 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 666 | * broken channel. |
| 667 | */ |
| 668 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 669 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 670 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 671 | "Window that breaks its input channel", |
| 672 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 673 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 674 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 675 | |
| 676 | // Window closes its channel, but the window remains. |
| 677 | window->destroyReceiver(); |
| 678 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 679 | } |
| 680 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 681 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 682 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 683 | sp<FakeWindowHandle> window = |
| 684 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 685 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 686 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 687 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 688 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 689 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 690 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 691 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 692 | |
| 693 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 694 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 695 | } |
| 696 | |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 697 | using InputDispatcherDeathTest = InputDispatcherTest; |
| 698 | |
| 699 | /** |
| 700 | * When 'onWindowInfosChanged' arguments contain a duplicate entry for the same window, dispatcher |
| 701 | * should crash. |
| 702 | */ |
| 703 | TEST_F(InputDispatcherDeathTest, DuplicateWindowInfosAbortDispatcher) { |
| 704 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
| 705 | ScopedSilentDeath _silentDeath; |
| 706 | |
| 707 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 708 | sp<FakeWindowHandle> window = |
| 709 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 710 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 711 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 712 | {{*window->getInfo(), *window->getInfo()}, {}, 0, 0}), |
| 713 | "Incorrect WindowInfosUpdate provided"); |
| 714 | } |
| 715 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 716 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 717 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 718 | sp<FakeWindowHandle> window = |
| 719 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 720 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 721 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 722 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 723 | // Inject a MotionEvent to an unknown display. |
| 724 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 725 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 726 | ui::LogicalDisplayId::INVALID)) |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 727 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 728 | |
| 729 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 730 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 733 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 734 | * Calling onWindowInfosChanged once should not cause any issues. |
| 735 | * This test serves as a sanity check for the next test, where onWindowInfosChanged is |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 736 | * called twice. |
| 737 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 738 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 739 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 740 | sp<FakeWindowHandle> window = |
| 741 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 742 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 743 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 744 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 745 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 746 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 747 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 748 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 749 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 750 | |
| 751 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 752 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 756 | * Calling onWindowInfosChanged twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 757 | */ |
| 758 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 759 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 760 | sp<FakeWindowHandle> window = |
| 761 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 762 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 763 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 764 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 765 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 766 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 767 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 768 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 769 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 770 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 771 | |
| 772 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 773 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 776 | // The foreground window should receive the first touch down event. |
| 777 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 778 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 779 | sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 780 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 781 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 782 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 783 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 784 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 785 | mDispatcher->onWindowInfosChanged( |
| 786 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 787 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 788 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 789 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 790 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 791 | |
| 792 | // Top window should receive the touch down event. Second window should not receive anything. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 793 | windowTop->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 794 | windowSecond->assertNoEvents(); |
| 795 | } |
| 796 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 797 | /** |
| 798 | * Two windows: A top window, and a wallpaper behind the window. |
| 799 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 800 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 801 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 802 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 803 | */ |
| 804 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 805 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 806 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 807 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 808 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 809 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 810 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 811 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 812 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 813 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 814 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 815 | mDispatcher->onWindowInfosChanged( |
| 816 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 817 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 818 | injectMotionEvent(*mDispatcher, |
| 819 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 820 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(200)) |
| 821 | .build())) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 822 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 823 | |
| 824 | // Both foreground window and its wallpaper should receive the touch down |
| 825 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 826 | wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 827 | |
| 828 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 829 | injectMotionEvent(*mDispatcher, |
| 830 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 831 | .pointer(PointerBuilder(0, ToolType::FINGER).x(110).y(200)) |
| 832 | .build())) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 833 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 834 | |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 835 | foregroundWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 836 | wallpaperWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 837 | |
| 838 | // Now the foreground window goes away, but the wallpaper stays |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 839 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 840 | foregroundWindow->consumeMotionCancel(); |
| 841 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 842 | wallpaperWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | /** |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 846 | * Two fingers down on the window, and lift off the first finger. |
| 847 | * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event |
| 848 | * contains a single pointer. |
| 849 | */ |
| 850 | TEST_F(InputDispatcherTest, CancelAfterPointer0Up) { |
| 851 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 852 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 853 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 854 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 855 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 856 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 857 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 858 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 859 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 860 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 861 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 862 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 863 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 864 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 865 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 866 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 867 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 868 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 869 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 870 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 871 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 872 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 873 | |
| 874 | // Remove the window. The gesture should be canceled |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 875 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 876 | const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}}; |
| 877 | window->consumeMotionEvent( |
| 878 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers))); |
| 879 | } |
| 880 | |
| 881 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 882 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 883 | * with the following differences: |
| 884 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 885 | * clean up the connection. |
| 886 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 887 | * Ensure that there's no crash in the dispatcher. |
| 888 | */ |
| 889 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 890 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 891 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 892 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 893 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 894 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 895 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 896 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 897 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 898 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 899 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 900 | mDispatcher->onWindowInfosChanged( |
| 901 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 902 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 903 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 904 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 905 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 906 | |
| 907 | // Both foreground window and its wallpaper should receive the touch down |
| 908 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 909 | wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 910 | |
| 911 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 912 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 913 | ui::LogicalDisplayId::DEFAULT, {110, 200})) |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 914 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 915 | |
| 916 | foregroundWindow->consumeMotionMove(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 917 | wallpaperWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 918 | |
| 919 | // Wallpaper closes its channel, but the window remains. |
| 920 | wallpaperWindow->destroyReceiver(); |
| 921 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 922 | |
| 923 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 924 | // is no longer valid. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 925 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 926 | foregroundWindow->consumeMotionCancel(); |
| 927 | } |
| 928 | |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 929 | /** |
| 930 | * Two windows: left and right, and a separate wallpaper window underneath each. Device A sends a |
| 931 | * down event to the left window. Device B sends a down event to the right window. Next, the right |
| 932 | * window disappears. Both the right window and its wallpaper window should receive cancel event. |
| 933 | * The left window and its wallpaper window should not receive any events. |
| 934 | */ |
| 935 | TEST_F(InputDispatcherTest, MultiDeviceDisappearingWindowWithWallpaperWindows) { |
| 936 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 937 | sp<FakeWindowHandle> leftForegroundWindow = |
| 938 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 939 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 940 | leftForegroundWindow->setFrame(Rect(0, 0, 100, 100)); |
| 941 | leftForegroundWindow->setDupTouchToWallpaper(true); |
| 942 | sp<FakeWindowHandle> leftWallpaperWindow = |
| 943 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 944 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 945 | leftWallpaperWindow->setFrame(Rect(0, 0, 100, 100)); |
| 946 | leftWallpaperWindow->setIsWallpaper(true); |
| 947 | |
| 948 | sp<FakeWindowHandle> rightForegroundWindow = |
| 949 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 950 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 951 | rightForegroundWindow->setFrame(Rect(100, 0, 200, 100)); |
| 952 | rightForegroundWindow->setDupTouchToWallpaper(true); |
| 953 | sp<FakeWindowHandle> rightWallpaperWindow = |
| 954 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 955 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 956 | rightWallpaperWindow->setFrame(Rect(100, 0, 200, 100)); |
| 957 | rightWallpaperWindow->setIsWallpaper(true); |
| 958 | |
| 959 | mDispatcher->onWindowInfosChanged( |
| 960 | {{*leftForegroundWindow->getInfo(), *leftWallpaperWindow->getInfo(), |
| 961 | *rightForegroundWindow->getInfo(), *rightWallpaperWindow->getInfo()}, |
| 962 | {}, |
| 963 | 0, |
| 964 | 0}); |
| 965 | |
| 966 | const DeviceId deviceA = 9; |
| 967 | const DeviceId deviceB = 3; |
| 968 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 969 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 970 | .deviceId(deviceA) |
| 971 | .build()); |
| 972 | leftForegroundWindow->consumeMotionEvent( |
| 973 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 974 | leftWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 975 | WithDeviceId(deviceA), |
| 976 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 977 | |
| 978 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 979 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 980 | .deviceId(deviceB) |
| 981 | .build()); |
| 982 | rightForegroundWindow->consumeMotionEvent( |
| 983 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 984 | rightWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 985 | WithDeviceId(deviceB), |
| 986 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 987 | |
| 988 | // Now right foreground window disappears, but right wallpaper window remains. |
| 989 | mDispatcher->onWindowInfosChanged( |
| 990 | {{*leftForegroundWindow->getInfo(), *leftWallpaperWindow->getInfo(), |
| 991 | *rightWallpaperWindow->getInfo()}, |
| 992 | {}, |
| 993 | 0, |
| 994 | 0}); |
| 995 | |
| 996 | // Left foreground window and left wallpaper window still exist, and should not receive any |
| 997 | // events. |
| 998 | leftForegroundWindow->assertNoEvents(); |
| 999 | leftWallpaperWindow->assertNoEvents(); |
| 1000 | // Since right foreground window disappeared, right wallpaper window and right foreground window |
| 1001 | // should receive cancel events. |
| 1002 | rightForegroundWindow->consumeMotionEvent( |
| 1003 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB))); |
| 1004 | rightWallpaperWindow->consumeMotionEvent( |
| 1005 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB), |
| 1006 | WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_CANCELED))); |
| 1007 | } |
| 1008 | |
| 1009 | /** |
| 1010 | * Three windows arranged horizontally and without any overlap. Every window has a |
| 1011 | * wallpaper window underneath. The middle window also has SLIPPERY flag. |
| 1012 | * Device A sends a down event to the left window. Device B sends a down event to the middle window. |
| 1013 | * Next, device B sends move event to the right window. Touch for device B should slip from the |
| 1014 | * middle window to the right window. Also, the right wallpaper window should receive a down event. |
| 1015 | * The middle window and its wallpaper window should receive a cancel event. The left window should |
| 1016 | * not receive any events. If device B continues to report events, the right window and its |
| 1017 | * wallpaper window should receive remaining events. |
| 1018 | */ |
| 1019 | TEST_F(InputDispatcherTest, MultiDeviceSlipperyTouchWithWallpaperWindow) { |
| 1020 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1021 | sp<FakeWindowHandle> leftForegroundWindow = |
| 1022 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1023 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1024 | leftForegroundWindow->setFrame(Rect(0, 0, 100, 100)); |
| 1025 | leftForegroundWindow->setDupTouchToWallpaper(true); |
| 1026 | sp<FakeWindowHandle> leftWallpaperWindow = |
| 1027 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1028 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1029 | leftWallpaperWindow->setFrame(Rect(0, 0, 100, 100)); |
| 1030 | leftWallpaperWindow->setIsWallpaper(true); |
| 1031 | |
| 1032 | sp<FakeWindowHandle> middleForegroundWindow = |
| 1033 | sp<FakeWindowHandle>::make(application, mDispatcher, "Middle foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1034 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1035 | middleForegroundWindow->setFrame(Rect(100, 0, 200, 100)); |
| 1036 | middleForegroundWindow->setDupTouchToWallpaper(true); |
| 1037 | middleForegroundWindow->setSlippery(true); |
| 1038 | sp<FakeWindowHandle> middleWallpaperWindow = |
| 1039 | sp<FakeWindowHandle>::make(application, mDispatcher, "Middle wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1040 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1041 | middleWallpaperWindow->setFrame(Rect(100, 0, 200, 100)); |
| 1042 | middleWallpaperWindow->setIsWallpaper(true); |
| 1043 | |
| 1044 | sp<FakeWindowHandle> rightForegroundWindow = |
| 1045 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1046 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1047 | rightForegroundWindow->setFrame(Rect(200, 0, 300, 100)); |
| 1048 | rightForegroundWindow->setDupTouchToWallpaper(true); |
| 1049 | sp<FakeWindowHandle> rightWallpaperWindow = |
| 1050 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1051 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1052 | rightWallpaperWindow->setFrame(Rect(200, 0, 300, 100)); |
| 1053 | rightWallpaperWindow->setIsWallpaper(true); |
| 1054 | |
| 1055 | mDispatcher->onWindowInfosChanged( |
| 1056 | {{*leftForegroundWindow->getInfo(), *leftWallpaperWindow->getInfo(), |
| 1057 | *middleForegroundWindow->getInfo(), *middleWallpaperWindow->getInfo(), |
| 1058 | *rightForegroundWindow->getInfo(), *rightWallpaperWindow->getInfo()}, |
| 1059 | {}, |
| 1060 | 0, |
| 1061 | 0}); |
| 1062 | |
| 1063 | const DeviceId deviceA = 9; |
| 1064 | const DeviceId deviceB = 3; |
| 1065 | // Device A sends a DOWN event to the left window |
| 1066 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1067 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 1068 | .deviceId(deviceA) |
| 1069 | .build()); |
| 1070 | leftForegroundWindow->consumeMotionEvent( |
| 1071 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 1072 | leftWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 1073 | WithDeviceId(deviceA), |
| 1074 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 1075 | // Device B sends a DOWN event to the middle window |
| 1076 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1077 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 1078 | .deviceId(deviceB) |
| 1079 | .build()); |
| 1080 | middleForegroundWindow->consumeMotionEvent( |
| 1081 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 1082 | middleWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 1083 | WithDeviceId(deviceB), |
| 1084 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 1085 | // Move the events of device B to the top of the right window. |
| 1086 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1087 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(50)) |
| 1088 | .deviceId(deviceB) |
| 1089 | .build()); |
| 1090 | middleForegroundWindow->consumeMotionEvent( |
| 1091 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB))); |
| 1092 | middleWallpaperWindow->consumeMotionEvent( |
| 1093 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB), |
| 1094 | WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_CANCELED))); |
| 1095 | rightForegroundWindow->consumeMotionEvent( |
| 1096 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 1097 | rightWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 1098 | WithDeviceId(deviceB), |
| 1099 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 1100 | // Make sure the window on the right can receive the remaining events. |
| 1101 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1102 | .pointer(PointerBuilder(0, ToolType::FINGER).x(251).y(51)) |
| 1103 | .deviceId(deviceB) |
| 1104 | .build()); |
| 1105 | leftForegroundWindow->assertNoEvents(); |
| 1106 | leftWallpaperWindow->assertNoEvents(); |
| 1107 | middleForegroundWindow->assertNoEvents(); |
| 1108 | middleWallpaperWindow->assertNoEvents(); |
| 1109 | rightForegroundWindow->consumeMotionEvent( |
| 1110 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB))); |
| 1111 | rightWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), |
| 1112 | WithDeviceId(deviceB), |
| 1113 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 1114 | } |
| 1115 | |
| 1116 | /** |
| 1117 | * Similar to the test above, we have three windows, they are arranged horizontally and without any |
| 1118 | * overlap, and every window has a wallpaper window. The middle window is a simple window, without |
| 1119 | * any special flags. Device A reports a down event that lands in left window. Device B sends a down |
| 1120 | * event to the middle window and then touch is transferred from the middle window to the right |
| 1121 | * window. The right window and its wallpaper window should receive a down event. The middle window |
| 1122 | * and its wallpaper window should receive a cancel event. The left window should not receive any |
| 1123 | * events. Subsequent events reported by device B should go to the right window and its wallpaper. |
| 1124 | */ |
| 1125 | TEST_F(InputDispatcherTest, MultiDeviceTouchTransferWithWallpaperWindows) { |
| 1126 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1127 | sp<FakeWindowHandle> leftForegroundWindow = |
| 1128 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1129 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1130 | leftForegroundWindow->setFrame(Rect(0, 0, 100, 100)); |
| 1131 | leftForegroundWindow->setDupTouchToWallpaper(true); |
| 1132 | sp<FakeWindowHandle> leftWallpaperWindow = |
| 1133 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1134 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1135 | leftWallpaperWindow->setFrame(Rect(0, 0, 100, 100)); |
| 1136 | leftWallpaperWindow->setIsWallpaper(true); |
| 1137 | |
| 1138 | sp<FakeWindowHandle> middleForegroundWindow = |
| 1139 | sp<FakeWindowHandle>::make(application, mDispatcher, "Middle foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1140 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1141 | middleForegroundWindow->setFrame(Rect(100, 0, 200, 100)); |
| 1142 | middleForegroundWindow->setDupTouchToWallpaper(true); |
| 1143 | sp<FakeWindowHandle> middleWallpaperWindow = |
| 1144 | sp<FakeWindowHandle>::make(application, mDispatcher, "Middle wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1145 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1146 | middleWallpaperWindow->setFrame(Rect(100, 0, 200, 100)); |
| 1147 | middleWallpaperWindow->setIsWallpaper(true); |
| 1148 | |
| 1149 | sp<FakeWindowHandle> rightForegroundWindow = |
| 1150 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right foreground window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1151 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1152 | rightForegroundWindow->setFrame(Rect(200, 0, 300, 100)); |
| 1153 | rightForegroundWindow->setDupTouchToWallpaper(true); |
| 1154 | sp<FakeWindowHandle> rightWallpaperWindow = |
| 1155 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right wallpaper window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1156 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1157 | rightWallpaperWindow->setFrame(Rect(200, 0, 300, 100)); |
| 1158 | rightWallpaperWindow->setIsWallpaper(true); |
| 1159 | |
| 1160 | mDispatcher->onWindowInfosChanged( |
| 1161 | {{*leftForegroundWindow->getInfo(), *leftWallpaperWindow->getInfo(), |
| 1162 | *middleForegroundWindow->getInfo(), *middleWallpaperWindow->getInfo(), |
| 1163 | *rightForegroundWindow->getInfo(), *rightWallpaperWindow->getInfo()}, |
| 1164 | {}, |
| 1165 | 0, |
| 1166 | 0}); |
| 1167 | |
| 1168 | const DeviceId deviceA = 9; |
| 1169 | const DeviceId deviceB = 3; |
| 1170 | // Device A touch down on the left window |
| 1171 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1172 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 1173 | .deviceId(deviceA) |
| 1174 | .build()); |
| 1175 | leftForegroundWindow->consumeMotionEvent( |
| 1176 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 1177 | leftWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 1178 | WithDeviceId(deviceA), |
| 1179 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 1180 | // Device B touch down on the middle window |
| 1181 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1182 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 1183 | .deviceId(deviceB) |
| 1184 | .build()); |
| 1185 | middleForegroundWindow->consumeMotionEvent( |
| 1186 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 1187 | middleWallpaperWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 1188 | WithDeviceId(deviceB), |
| 1189 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
| 1190 | |
| 1191 | // Transfer touch from the middle window to the right window. |
| 1192 | ASSERT_TRUE(mDispatcher->transferTouchGesture(middleForegroundWindow->getToken(), |
| 1193 | rightForegroundWindow->getToken())); |
| 1194 | |
| 1195 | middleForegroundWindow->consumeMotionEvent( |
| 1196 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB))); |
| 1197 | middleWallpaperWindow->consumeMotionEvent( |
| 1198 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB), |
| 1199 | WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_CANCELED))); |
| 1200 | rightForegroundWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), |
| 1201 | WithDeviceId(deviceB), |
| 1202 | WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
| 1203 | rightWallpaperWindow->consumeMotionEvent( |
| 1204 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB), |
| 1205 | WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
| 1206 | |
| 1207 | // Make sure the right window can receive the remaining events. |
| 1208 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1209 | .pointer(PointerBuilder(0, ToolType::FINGER).x(251).y(51)) |
| 1210 | .deviceId(deviceB) |
| 1211 | .build()); |
| 1212 | leftForegroundWindow->assertNoEvents(); |
| 1213 | leftWallpaperWindow->assertNoEvents(); |
| 1214 | middleForegroundWindow->assertNoEvents(); |
| 1215 | middleWallpaperWindow->assertNoEvents(); |
| 1216 | rightForegroundWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), |
| 1217 | WithDeviceId(deviceB), |
| 1218 | WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
| 1219 | rightWallpaperWindow->consumeMotionEvent( |
| 1220 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB), |
| 1221 | WithFlags(EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
| 1222 | } |
| 1223 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1224 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 1225 | public ::testing::WithParamInterface<bool> {}; |
| 1226 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 1227 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1228 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1229 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1230 | * The top window gets a multitouch gesture. |
| 1231 | * Ensure that wallpaper gets the same gesture. |
| 1232 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1233 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1234 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1235 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1236 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 1237 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1238 | foregroundWindow->setDupTouchToWallpaper(true); |
| 1239 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1240 | |
| 1241 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1242 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 1243 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1244 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1245 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1246 | mDispatcher->onWindowInfosChanged( |
| 1247 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1248 | |
| 1249 | // Touch down on top window |
| 1250 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1251 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 1252 | ui::LogicalDisplayId::DEFAULT, {100, 100})) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1253 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1254 | |
| 1255 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1256 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1257 | wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1258 | |
| 1259 | // Second finger down on the top window |
| 1260 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1261 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1262 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1263 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1264 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1265 | .build(); |
| 1266 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1267 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1268 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1269 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1270 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1271 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1272 | EXPECTED_WALLPAPER_FLAGS); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1273 | |
| 1274 | const MotionEvent secondFingerUpEvent = |
| 1275 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1276 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1277 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1278 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1279 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1280 | .build(); |
| 1281 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1282 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1283 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1284 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1285 | foregroundWindow->consumeMotionPointerUp(0); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1286 | wallpaperWindow->consumeMotionPointerUp(0, ui::LogicalDisplayId::DEFAULT, |
| 1287 | EXPECTED_WALLPAPER_FLAGS); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1288 | |
| 1289 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1290 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1291 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1292 | AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1293 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1294 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1295 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1296 | .x(100) |
| 1297 | .y(100)) |
| 1298 | .build(), |
| 1299 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1300 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1301 | foregroundWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
| 1302 | wallpaperWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | /** |
| 1306 | * Two windows: a window on the left and window on the right. |
| 1307 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1308 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1309 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1310 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1311 | * ACTION_POINTER_DOWN(1). |
| 1312 | */ |
| 1313 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1314 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1315 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 1316 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1317 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1318 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1319 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1320 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 1321 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1322 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1323 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1324 | |
| 1325 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1326 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 1327 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1328 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1329 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1330 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1331 | mDispatcher->onWindowInfosChanged( |
| 1332 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 1333 | {}, |
| 1334 | 0, |
| 1335 | 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1336 | |
| 1337 | // Touch down on left window |
| 1338 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1339 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 1340 | ui::LogicalDisplayId::DEFAULT, {100, 100})) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1341 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1342 | |
| 1343 | // Both foreground window and its wallpaper should receive the touch down |
| 1344 | leftWindow->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1345 | wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1346 | |
| 1347 | // Second finger down on the right window |
| 1348 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1349 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1350 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1351 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1352 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1353 | .build(); |
| 1354 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1355 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1356 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1357 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1358 | |
| 1359 | leftWindow->consumeMotionMove(); |
| 1360 | // Since the touch is split, right window gets ACTION_DOWN |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1361 | rightWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 1362 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1363 | EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1364 | |
| 1365 | // Now, leftWindow, which received the first finger, disappears. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1366 | mDispatcher->onWindowInfosChanged( |
| 1367 | {{*rightWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1368 | leftWindow->consumeMotionCancel(); |
| 1369 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1370 | wallpaperWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1371 | |
| 1372 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 1373 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 1374 | const MotionEvent secondFingerMoveEvent = |
| 1375 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1376 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1377 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1378 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1379 | .build(); |
| 1380 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1381 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1382 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 1383 | rightWindow->consumeMotionMove(); |
| 1384 | |
| 1385 | leftWindow->assertNoEvents(); |
| 1386 | rightWindow->assertNoEvents(); |
| 1387 | wallpaperWindow->assertNoEvents(); |
| 1388 | } |
| 1389 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1390 | /** |
| 1391 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 1392 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 1393 | * The right window should receive ACTION_DOWN. |
| 1394 | */ |
| 1395 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1396 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1397 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 1398 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1399 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1400 | leftWindow->setDupTouchToWallpaper(true); |
| 1401 | leftWindow->setSlippery(true); |
| 1402 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1403 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 1404 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1405 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1406 | |
| 1407 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1408 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 1409 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1410 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1411 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1412 | mDispatcher->onWindowInfosChanged( |
| 1413 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 1414 | {}, |
| 1415 | 0, |
| 1416 | 0}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1417 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1418 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1419 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1420 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 1421 | ui::LogicalDisplayId::DEFAULT, {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1422 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1423 | |
| 1424 | // Both foreground window and its wallpaper should receive the touch down |
| 1425 | leftWindow->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1426 | wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1427 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1428 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1429 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1430 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1431 | ui::LogicalDisplayId::DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1432 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1433 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1434 | leftWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1435 | rightWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 1436 | wallpaperWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1439 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1440 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 1441 | * interactive, it might stop sending this flag. |
| 1442 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 1443 | * to have a consistent input stream. |
| 1444 | * |
| 1445 | * Test procedure: |
| 1446 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 1447 | * DOWN (new gesture). |
| 1448 | * |
| 1449 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 1450 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 1451 | * |
| 1452 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 1453 | * regular window below) to emulate the actual situation where it happens on the device. |
| 1454 | */ |
| 1455 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 1456 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1457 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 1458 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1459 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1460 | spyWindow->setTrustedOverlay(true); |
| 1461 | spyWindow->setSpy(true); |
| 1462 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1463 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1464 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1465 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1466 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1467 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1468 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1469 | |
| 1470 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1471 | mDispatcher->notifyMotion( |
| 1472 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1473 | .deviceId(touchDeviceId) |
| 1474 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1475 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1476 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1477 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1478 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1479 | .deviceId(touchDeviceId) |
| 1480 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1481 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1482 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 1483 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1484 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1485 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1486 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1487 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1488 | |
| 1489 | // Cancel the current gesture. Send the cancel without the default policy flags. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1490 | mDispatcher->notifyMotion( |
| 1491 | MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 1492 | .deviceId(touchDeviceId) |
| 1493 | .policyFlags(0) |
| 1494 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1495 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 1496 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1497 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 1498 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 1499 | |
| 1500 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 1501 | // follows, so we keep it here to model the actual listener behaviour more closely. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1502 | mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1503 | |
| 1504 | // Start new gesture |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1505 | mDispatcher->notifyMotion( |
| 1506 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1507 | .deviceId(touchDeviceId) |
| 1508 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1509 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1510 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1511 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1512 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1513 | |
| 1514 | // No more events |
| 1515 | spyWindow->assertNoEvents(); |
| 1516 | window->assertNoEvents(); |
| 1517 | } |
| 1518 | |
| 1519 | /** |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 1520 | * Same as the above 'TwoPointerCancelInconsistentPolicy' test, but for hovers. |
| 1521 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 1522 | * interactive, it might stop sending this flag. |
| 1523 | * We've already ensured the consistency of the touch event in this case, and we should also ensure |
| 1524 | * the consistency of the hover event in this case. |
| 1525 | * |
| 1526 | * Test procedure: |
| 1527 | * HOVER_ENTER -> HOVER_MOVE -> (stop sending POLICY_FLAG_PASS_TO_USER) -> HOVER_EXIT |
| 1528 | * HOVER_ENTER -> HOVER_MOVE -> HOVER_EXIT |
| 1529 | * |
| 1530 | * We expect to receive two full streams of hover events. |
| 1531 | */ |
| 1532 | TEST_F(InputDispatcherTest, HoverEventInconsistentPolicy) { |
| 1533 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1534 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1535 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1536 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 1537 | window->setFrame(Rect(0, 0, 300, 300)); |
| 1538 | |
| 1539 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1540 | |
| 1541 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1542 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1543 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 1544 | .build()); |
| 1545 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1546 | |
| 1547 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1548 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1549 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 1550 | .build()); |
| 1551 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 1552 | |
| 1553 | // Send hover exit without the default policy flags. |
| 1554 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 1555 | .policyFlags(0) |
| 1556 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 1557 | .build()); |
| 1558 | |
| 1559 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1560 | |
| 1561 | // Send a simple hover event stream, ensure dispatcher not crashed and window can receive |
| 1562 | // right event. |
| 1563 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1564 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1565 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(200).y(201)) |
| 1566 | .build()); |
| 1567 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1568 | |
| 1569 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1570 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1571 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 1572 | .build()); |
| 1573 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 1574 | |
| 1575 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 1576 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1577 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 1578 | .build()); |
| 1579 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1580 | } |
| 1581 | |
| 1582 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1583 | * Two windows: a window on the left and a window on the right. |
| 1584 | * Mouse is hovered from the right window into the left window. |
| 1585 | * Next, we tap on the left window, where the cursor was last seen. |
| 1586 | * The second tap is done onto the right window. |
| 1587 | * The mouse and tap are from two different devices. |
| 1588 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 1589 | * explicitly helps during debugging. |
| 1590 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 1591 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 1592 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1593 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap_legacy) { |
| 1594 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
| 1595 | |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1596 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1597 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 1598 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1599 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1600 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1601 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 1602 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1603 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 1604 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1605 | mDispatcher->onWindowInfosChanged( |
| 1606 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1607 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 1608 | // stale. |
| 1609 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1610 | const int32_t mouseDeviceId = 6; |
| 1611 | const int32_t touchDeviceId = 4; |
| 1612 | // Move the cursor from right |
| 1613 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1614 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1615 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1616 | AINPUT_SOURCE_MOUSE) |
| 1617 | .deviceId(mouseDeviceId) |
| 1618 | .downTime(baseTime + 10) |
| 1619 | .eventTime(baseTime + 20) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1620 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1621 | .build())); |
| 1622 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 1623 | |
| 1624 | // .. to the left window |
| 1625 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1626 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1627 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1628 | AINPUT_SOURCE_MOUSE) |
| 1629 | .deviceId(mouseDeviceId) |
| 1630 | .downTime(baseTime + 10) |
| 1631 | .eventTime(baseTime + 30) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1632 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1633 | .build())); |
| 1634 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 1635 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 1636 | // Now tap the left window |
| 1637 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1638 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1639 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 1640 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1641 | .deviceId(touchDeviceId) |
| 1642 | .downTime(baseTime + 40) |
| 1643 | .eventTime(baseTime + 40) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1644 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1645 | .build())); |
| 1646 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 1647 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1648 | |
| 1649 | // release tap |
| 1650 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1651 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1652 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1653 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1654 | .deviceId(touchDeviceId) |
| 1655 | .downTime(baseTime + 40) |
| 1656 | .eventTime(baseTime + 50) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1657 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1658 | .build())); |
| 1659 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 1660 | |
| 1661 | // Tap the window on the right |
| 1662 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1663 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1664 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 1665 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1666 | .deviceId(touchDeviceId) |
| 1667 | .downTime(baseTime + 60) |
| 1668 | .eventTime(baseTime + 60) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1669 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1670 | .build())); |
| 1671 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1672 | |
| 1673 | // release tap |
| 1674 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1675 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1676 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1677 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1678 | .deviceId(touchDeviceId) |
| 1679 | .downTime(baseTime + 60) |
| 1680 | .eventTime(baseTime + 70) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1681 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1682 | .build())); |
| 1683 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 1684 | |
| 1685 | // No more events |
| 1686 | leftWindow->assertNoEvents(); |
| 1687 | rightWindow->assertNoEvents(); |
| 1688 | } |
| 1689 | |
| 1690 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1691 | * Two windows: a window on the left and a window on the right. |
| 1692 | * Mouse is hovered from the right window into the left window. |
| 1693 | * Next, we tap on the left window, where the cursor was last seen. |
| 1694 | * The second tap is done onto the right window. |
| 1695 | * The mouse and tap are from two different devices. |
| 1696 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 1697 | * explicitly helps during debugging. |
| 1698 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 1699 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 1700 | */ |
| 1701 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 1702 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 1703 | |
| 1704 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1705 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 1706 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1707 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1708 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1709 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 1710 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1711 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 1712 | |
| 1713 | mDispatcher->onWindowInfosChanged( |
| 1714 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 1715 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 1716 | // stale. |
| 1717 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1718 | const int32_t mouseDeviceId = 6; |
| 1719 | const int32_t touchDeviceId = 4; |
| 1720 | // Move the cursor from right |
| 1721 | mDispatcher->notifyMotion( |
| 1722 | MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 1723 | .deviceId(mouseDeviceId) |
| 1724 | .downTime(baseTime + 10) |
| 1725 | .eventTime(baseTime + 20) |
| 1726 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100)) |
| 1727 | .build()); |
| 1728 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 1729 | |
| 1730 | // .. to the left window |
| 1731 | mDispatcher->notifyMotion( |
| 1732 | MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 1733 | .deviceId(mouseDeviceId) |
| 1734 | .downTime(baseTime + 10) |
| 1735 | .eventTime(baseTime + 30) |
| 1736 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100)) |
| 1737 | .build()); |
| 1738 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 1739 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 1740 | // Now tap the left window |
| 1741 | mDispatcher->notifyMotion( |
| 1742 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1743 | .deviceId(touchDeviceId) |
| 1744 | .downTime(baseTime + 40) |
| 1745 | .eventTime(baseTime + 40) |
| 1746 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1747 | .build()); |
| 1748 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1749 | |
| 1750 | // release tap |
| 1751 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1752 | .deviceId(touchDeviceId) |
| 1753 | .downTime(baseTime + 40) |
| 1754 | .eventTime(baseTime + 50) |
| 1755 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1756 | .build()); |
| 1757 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 1758 | |
| 1759 | // Tap the window on the right |
| 1760 | mDispatcher->notifyMotion( |
| 1761 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1762 | .deviceId(touchDeviceId) |
| 1763 | .downTime(baseTime + 60) |
| 1764 | .eventTime(baseTime + 60) |
| 1765 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 1766 | .build()); |
| 1767 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1768 | |
| 1769 | // release tap |
| 1770 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1771 | .deviceId(touchDeviceId) |
| 1772 | .downTime(baseTime + 60) |
| 1773 | .eventTime(baseTime + 70) |
| 1774 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 1775 | .build()); |
| 1776 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 1777 | |
| 1778 | // No more events |
| 1779 | leftWindow->assertNoEvents(); |
| 1780 | rightWindow->assertNoEvents(); |
| 1781 | } |
| 1782 | |
| 1783 | /** |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1784 | * Start hovering in a window. While this hover is still active, make another window appear on top. |
| 1785 | * The top, obstructing window has no input channel, so it's not supposed to receive input. |
| 1786 | * While the top window is present, the hovering is stopped. |
| 1787 | * Later, hovering gets resumed again. |
| 1788 | * Ensure that new hover gesture is handled correctly. |
| 1789 | * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly |
| 1790 | * to the window that's currently being hovered over. |
| 1791 | */ |
| 1792 | TEST_F(InputDispatcherTest, HoverWhileWindowAppears) { |
| 1793 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1794 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1795 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1796 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1797 | |
| 1798 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1799 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1800 | |
| 1801 | // Start hovering in the window |
| 1802 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1803 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1804 | .build()); |
| 1805 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1806 | |
| 1807 | // Now, an obscuring window appears! |
| 1808 | sp<FakeWindowHandle> obscuringWindow = |
| 1809 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1810 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1811 | /*createInputChannel=*/false); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1812 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1813 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 1814 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 1815 | obscuringWindow->setNoInputChannel(true); |
| 1816 | obscuringWindow->setFocusable(false); |
| 1817 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1818 | mDispatcher->onWindowInfosChanged( |
| 1819 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1820 | |
| 1821 | // While this new obscuring window is present, the hovering is stopped |
| 1822 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 1823 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1824 | .build()); |
| 1825 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1826 | |
| 1827 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1828 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1829 | |
| 1830 | // And a new hover gesture starts. |
| 1831 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1832 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1833 | .build()); |
| 1834 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1835 | } |
| 1836 | |
| 1837 | /** |
| 1838 | * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to |
| 1839 | * the obscuring window. |
| 1840 | */ |
| 1841 | TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) { |
| 1842 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1843 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1844 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1845 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1846 | |
| 1847 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1848 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1849 | |
| 1850 | // Start hovering in the window |
| 1851 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1852 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1853 | .build()); |
| 1854 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1855 | |
| 1856 | // Now, an obscuring window appears! |
| 1857 | sp<FakeWindowHandle> obscuringWindow = |
| 1858 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1859 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1860 | /*createInputChannel=*/false); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1861 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1862 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 1863 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 1864 | obscuringWindow->setNoInputChannel(true); |
| 1865 | obscuringWindow->setFocusable(false); |
| 1866 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1867 | mDispatcher->onWindowInfosChanged( |
| 1868 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1869 | |
| 1870 | // While this new obscuring window is present, the hovering continues. The event can't go to the |
| 1871 | // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window. |
| 1872 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1873 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1874 | .build()); |
| 1875 | obscuringWindow->assertNoEvents(); |
| 1876 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1877 | |
| 1878 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1879 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1880 | |
| 1881 | // Hovering continues in the same position. The hovering pointer re-enters the bottom window, |
| 1882 | // so it should generate a HOVER_ENTER |
| 1883 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1884 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1885 | .build()); |
| 1886 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1887 | |
| 1888 | // Now the MOVE should be getting dispatched normally |
| 1889 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1890 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 1891 | .build()); |
| 1892 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 1893 | } |
| 1894 | |
| 1895 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1896 | * Hover mouse over a window, and then send ACTION_SCROLL. Ensure both the hover and the scroll |
| 1897 | * events are delivered to the window. |
| 1898 | */ |
| 1899 | TEST_F(InputDispatcherTest, HoverMoveAndScroll) { |
| 1900 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1901 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1902 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1903 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1904 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1905 | |
| 1906 | // Start hovering in the window |
| 1907 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 1908 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 1909 | .build()); |
| 1910 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1911 | |
| 1912 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 1913 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 1914 | .build()); |
| 1915 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 1916 | |
| 1917 | // Scroll with the mouse |
| 1918 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_SCROLL, AINPUT_SOURCE_MOUSE) |
| 1919 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 1920 | .build()); |
| 1921 | window->consumeMotionEvent(WithMotionAction(ACTION_SCROLL)); |
| 1922 | } |
| 1923 | |
| 1924 | using InputDispatcherMultiDeviceTest = InputDispatcherTest; |
| 1925 | |
| 1926 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1927 | * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that |
| 1928 | * touch is dropped, because stylus should be preferred over touch. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1929 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1930 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownBlocksTouchDown) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1931 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1932 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1933 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1934 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1935 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1936 | |
| 1937 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1938 | |
| 1939 | constexpr int32_t touchDeviceId = 4; |
| 1940 | constexpr int32_t stylusDeviceId = 2; |
| 1941 | |
| 1942 | // Stylus down |
| 1943 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 1944 | .deviceId(stylusDeviceId) |
| 1945 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 1946 | .build()); |
| 1947 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 1948 | |
| 1949 | // Touch down |
| 1950 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1951 | .deviceId(touchDeviceId) |
| 1952 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 1953 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1954 | |
| 1955 | // Touch move |
| 1956 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1957 | .deviceId(touchDeviceId) |
| 1958 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 1959 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1960 | // Touch is ignored because stylus is already down |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1961 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1962 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1963 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 1964 | .deviceId(stylusDeviceId) |
| 1965 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 1966 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1967 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 1968 | WithCoords(101, 111))); |
| 1969 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1970 | window->assertNoEvents(); |
| 1971 | } |
| 1972 | |
| 1973 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1974 | * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that |
| 1975 | * touch is not dropped, because multiple devices are allowed to be active in the same window. |
| 1976 | */ |
| 1977 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownDoesNotBlockTouchDown) { |
| 1978 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 1979 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1980 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1981 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1982 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1983 | |
| 1984 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1985 | |
| 1986 | constexpr int32_t touchDeviceId = 4; |
| 1987 | constexpr int32_t stylusDeviceId = 2; |
| 1988 | |
| 1989 | // Stylus down |
| 1990 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 1991 | .deviceId(stylusDeviceId) |
| 1992 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 1993 | .build()); |
| 1994 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 1995 | |
| 1996 | // Touch down |
| 1997 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1998 | .deviceId(touchDeviceId) |
| 1999 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2000 | .build()); |
| 2001 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2002 | |
| 2003 | // Touch move |
| 2004 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2005 | .deviceId(touchDeviceId) |
| 2006 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2007 | .build()); |
| 2008 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2009 | |
| 2010 | // Stylus move |
| 2011 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2012 | .deviceId(stylusDeviceId) |
| 2013 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2014 | .build()); |
| 2015 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2016 | WithCoords(101, 111))); |
| 2017 | |
| 2018 | window->assertNoEvents(); |
| 2019 | } |
| 2020 | |
| 2021 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2022 | * One window and one spy window. Stylus down on the window. Next, touch from another device goes |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2023 | * down. Ensure that touch is dropped, because stylus should be preferred over touch. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2024 | * Similar test as above, but with added SPY window. |
| 2025 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2026 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyBlocksTouchDown) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2027 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2028 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2029 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2030 | ui::LogicalDisplayId::DEFAULT); |
| 2031 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 2032 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2033 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2034 | spyWindow->setTrustedOverlay(true); |
| 2035 | spyWindow->setSpy(true); |
| 2036 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2037 | |
| 2038 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 2039 | |
| 2040 | constexpr int32_t touchDeviceId = 4; |
| 2041 | constexpr int32_t stylusDeviceId = 2; |
| 2042 | |
| 2043 | // Stylus down |
| 2044 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2045 | .deviceId(stylusDeviceId) |
| 2046 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2047 | .build()); |
| 2048 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2049 | spyWindow->consumeMotionEvent( |
| 2050 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2051 | |
| 2052 | // Touch down |
| 2053 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2054 | .deviceId(touchDeviceId) |
| 2055 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2056 | .build()); |
| 2057 | |
| 2058 | // Touch move |
| 2059 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2060 | .deviceId(touchDeviceId) |
| 2061 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2062 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2063 | |
| 2064 | // Touch is ignored because stylus is already down |
| 2065 | |
| 2066 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2067 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2068 | .deviceId(stylusDeviceId) |
| 2069 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2070 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2071 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2072 | WithCoords(101, 111))); |
| 2073 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2074 | WithCoords(101, 111))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2075 | |
| 2076 | window->assertNoEvents(); |
| 2077 | spyWindow->assertNoEvents(); |
| 2078 | } |
| 2079 | |
| 2080 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2081 | * One window and one spy window. Stylus down on the window. Next, touch from another device goes |
| 2082 | * down. Ensure that touch is not dropped, because multiple devices can be active at the same time. |
| 2083 | * Similar test as above, but with added SPY window. |
| 2084 | */ |
| 2085 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyDoesNotBlockTouchDown) { |
| 2086 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2087 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2088 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2089 | ui::LogicalDisplayId::DEFAULT); |
| 2090 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 2091 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2092 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2093 | spyWindow->setTrustedOverlay(true); |
| 2094 | spyWindow->setSpy(true); |
| 2095 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2096 | |
| 2097 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 2098 | |
| 2099 | constexpr int32_t touchDeviceId = 4; |
| 2100 | constexpr int32_t stylusDeviceId = 2; |
| 2101 | |
| 2102 | // Stylus down |
| 2103 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2104 | .deviceId(stylusDeviceId) |
| 2105 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2106 | .build()); |
| 2107 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2108 | spyWindow->consumeMotionEvent( |
| 2109 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2110 | |
| 2111 | // Touch down |
| 2112 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2113 | .deviceId(touchDeviceId) |
| 2114 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2115 | .build()); |
| 2116 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2117 | spyWindow->consumeMotionEvent( |
| 2118 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2119 | |
| 2120 | // Touch move |
| 2121 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2122 | .deviceId(touchDeviceId) |
| 2123 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2124 | .build()); |
| 2125 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2126 | spyWindow->consumeMotionEvent( |
| 2127 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2128 | |
| 2129 | // Subsequent stylus movements are delivered correctly |
| 2130 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2131 | .deviceId(stylusDeviceId) |
| 2132 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2133 | .build()); |
| 2134 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2135 | WithCoords(101, 111))); |
| 2136 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2137 | WithCoords(101, 111))); |
| 2138 | |
| 2139 | window->assertNoEvents(); |
| 2140 | spyWindow->assertNoEvents(); |
| 2141 | } |
| 2142 | |
| 2143 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2144 | * One window. Stylus hover on the window. Next, touch from another device goes down. Ensure that |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2145 | * touch is dropped, because stylus hover takes precedence. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2146 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2147 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverBlocksTouchDown) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2148 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2149 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2150 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2151 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2152 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2153 | |
| 2154 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2155 | |
| 2156 | constexpr int32_t touchDeviceId = 4; |
| 2157 | constexpr int32_t stylusDeviceId = 2; |
| 2158 | |
| 2159 | // Stylus down on the window |
| 2160 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2161 | .deviceId(stylusDeviceId) |
| 2162 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2163 | .build()); |
| 2164 | window->consumeMotionEvent( |
| 2165 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2166 | |
| 2167 | // Touch down on window |
| 2168 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2169 | .deviceId(touchDeviceId) |
| 2170 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2171 | .build()); |
| 2172 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2173 | .deviceId(touchDeviceId) |
| 2174 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2175 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2176 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2177 | // Touch is ignored because stylus is hovering |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2178 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2179 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2180 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2181 | .deviceId(stylusDeviceId) |
| 2182 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2183 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2184 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2185 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2186 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2187 | // and subsequent touches continue to be ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2188 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2189 | .deviceId(touchDeviceId) |
| 2190 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2191 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2192 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2193 | } |
| 2194 | |
| 2195 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2196 | * One window. Stylus hover on the window. Next, touch from another device goes down. Ensure that |
| 2197 | * touch is not dropped, because stylus hover and touch can be both active at the same time. |
| 2198 | */ |
| 2199 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverDoesNotBlockTouchDown) { |
| 2200 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2201 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2202 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2203 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2204 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2205 | |
| 2206 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2207 | |
| 2208 | constexpr int32_t touchDeviceId = 4; |
| 2209 | constexpr int32_t stylusDeviceId = 2; |
| 2210 | |
| 2211 | // Stylus down on the window |
| 2212 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2213 | .deviceId(stylusDeviceId) |
| 2214 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2215 | .build()); |
| 2216 | window->consumeMotionEvent( |
| 2217 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2218 | |
| 2219 | // Touch down on window |
| 2220 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2221 | .deviceId(touchDeviceId) |
| 2222 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2223 | .build()); |
| 2224 | // Touch move on window |
| 2225 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2226 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2227 | .deviceId(touchDeviceId) |
| 2228 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2229 | .build()); |
| 2230 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2231 | |
| 2232 | // Subsequent stylus movements are delivered correctly |
| 2233 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2234 | .deviceId(stylusDeviceId) |
| 2235 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2236 | .build()); |
| 2237 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2238 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
| 2239 | |
| 2240 | // and subsequent touches continue to work |
| 2241 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2242 | .deviceId(touchDeviceId) |
| 2243 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2244 | .build()); |
| 2245 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2246 | window->assertNoEvents(); |
| 2247 | } |
| 2248 | |
| 2249 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2250 | * One window. Touch down on the window. Then, stylus hover on the window from another device. |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2251 | * Ensure that touch is canceled, because stylus hover should take precedence. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2252 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2253 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusHover) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2254 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2255 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2256 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2257 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2258 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2259 | |
| 2260 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2261 | |
| 2262 | constexpr int32_t touchDeviceId = 4; |
| 2263 | constexpr int32_t stylusDeviceId = 2; |
| 2264 | |
| 2265 | // Touch down on window |
| 2266 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2267 | .deviceId(touchDeviceId) |
| 2268 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2269 | .build()); |
| 2270 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2271 | .deviceId(touchDeviceId) |
| 2272 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2273 | .build()); |
| 2274 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2275 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2276 | |
| 2277 | // Stylus hover on the window |
| 2278 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2279 | .deviceId(stylusDeviceId) |
| 2280 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2281 | .build()); |
| 2282 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2283 | .deviceId(stylusDeviceId) |
| 2284 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2285 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2286 | // Stylus hover movement causes touch to be canceled |
| 2287 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 2288 | WithCoords(141, 146))); |
| 2289 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 2290 | WithDeviceId(stylusDeviceId), WithCoords(100, 110))); |
| 2291 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2292 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2293 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2294 | // Subsequent touch movements are ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2295 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2296 | .deviceId(touchDeviceId) |
| 2297 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2298 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2299 | |
| 2300 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2304 | * One window. Touch down on the window. Then, stylus hover on the window from another device. |
| 2305 | * Ensure that touch is not canceled, because stylus hover can be active at the same time as touch. |
| 2306 | */ |
| 2307 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsNotCanceledByStylusHover) { |
| 2308 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2309 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2310 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2311 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2312 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2313 | |
| 2314 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2315 | |
| 2316 | constexpr int32_t touchDeviceId = 4; |
| 2317 | constexpr int32_t stylusDeviceId = 2; |
| 2318 | |
| 2319 | // Touch down on window |
| 2320 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2321 | .deviceId(touchDeviceId) |
| 2322 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2323 | .build()); |
| 2324 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2325 | .deviceId(touchDeviceId) |
| 2326 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2327 | .build()); |
| 2328 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2329 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2330 | |
| 2331 | // Stylus hover on the window |
| 2332 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2333 | .deviceId(stylusDeviceId) |
| 2334 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2335 | .build()); |
| 2336 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2337 | .deviceId(stylusDeviceId) |
| 2338 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2339 | .build()); |
| 2340 | // Stylus hover movement is received normally |
| 2341 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 2342 | WithDeviceId(stylusDeviceId), WithCoords(100, 110))); |
| 2343 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2344 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
| 2345 | |
| 2346 | // Subsequent touch movements also work |
| 2347 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2348 | .deviceId(touchDeviceId) |
| 2349 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2350 | .build()); |
| 2351 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId), |
| 2352 | WithCoords(142, 147))); |
| 2353 | |
| 2354 | window->assertNoEvents(); |
| 2355 | } |
| 2356 | |
| 2357 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2358 | * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that |
| 2359 | * the latest stylus takes over. That is, old stylus should be canceled and the new stylus should |
| 2360 | * become active. |
| 2361 | */ |
| 2362 | TEST_F(InputDispatcherMultiDeviceTest, LatestStylusWins) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2363 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2364 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2365 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2366 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2367 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2368 | |
| 2369 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2370 | |
| 2371 | constexpr int32_t stylusDeviceId1 = 3; |
| 2372 | constexpr int32_t stylusDeviceId2 = 5; |
| 2373 | |
| 2374 | // Touch down on window |
| 2375 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2376 | .deviceId(stylusDeviceId1) |
| 2377 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100)) |
| 2378 | .build()); |
| 2379 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2380 | .deviceId(stylusDeviceId1) |
| 2381 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2382 | .build()); |
| 2383 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1))); |
| 2384 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2385 | |
| 2386 | // Second stylus down |
| 2387 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2388 | .deviceId(stylusDeviceId2) |
| 2389 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10)) |
| 2390 | .build()); |
| 2391 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2392 | .deviceId(stylusDeviceId2) |
| 2393 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11)) |
| 2394 | .build()); |
| 2395 | |
| 2396 | // First stylus is canceled, second one takes over. |
| 2397 | window->consumeMotionEvent( |
| 2398 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId1))); |
| 2399 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2))); |
| 2400 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2))); |
| 2401 | |
| 2402 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2403 | .deviceId(stylusDeviceId1) |
| 2404 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2405 | .build()); |
| 2406 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2407 | window->assertNoEvents(); |
| 2408 | } |
| 2409 | |
| 2410 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2411 | * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that |
| 2412 | * both stylus devices can function simultaneously. |
| 2413 | */ |
| 2414 | TEST_F(InputDispatcherMultiDeviceTest, TwoStylusDevicesActiveAtTheSameTime) { |
| 2415 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2416 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2417 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2418 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2419 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2420 | |
| 2421 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2422 | |
| 2423 | constexpr int32_t stylusDeviceId1 = 3; |
| 2424 | constexpr int32_t stylusDeviceId2 = 5; |
| 2425 | |
| 2426 | // Touch down on window |
| 2427 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2428 | .deviceId(stylusDeviceId1) |
| 2429 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100)) |
| 2430 | .build()); |
| 2431 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2432 | .deviceId(stylusDeviceId1) |
| 2433 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2434 | .build()); |
| 2435 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1))); |
| 2436 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2437 | |
| 2438 | // Second stylus down |
| 2439 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2440 | .deviceId(stylusDeviceId2) |
| 2441 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10)) |
| 2442 | .build()); |
| 2443 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2444 | .deviceId(stylusDeviceId2) |
| 2445 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11)) |
| 2446 | .build()); |
| 2447 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2))); |
| 2448 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2))); |
| 2449 | |
| 2450 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2451 | .deviceId(stylusDeviceId1) |
| 2452 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2453 | .build()); |
| 2454 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2455 | window->assertNoEvents(); |
| 2456 | } |
| 2457 | |
| 2458 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2459 | * One window. Touch down on the window. Then, stylus down on the window from another device. |
| 2460 | * Ensure that is canceled, because stylus down should be preferred over touch. |
| 2461 | */ |
| 2462 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusDown) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2463 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2464 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2465 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2466 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2467 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2468 | |
| 2469 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2470 | |
| 2471 | constexpr int32_t touchDeviceId = 4; |
| 2472 | constexpr int32_t stylusDeviceId = 2; |
| 2473 | |
| 2474 | // Touch down on window |
| 2475 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2476 | .deviceId(touchDeviceId) |
| 2477 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2478 | .build()); |
| 2479 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2480 | .deviceId(touchDeviceId) |
| 2481 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2482 | .build()); |
| 2483 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2484 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2485 | |
| 2486 | // Stylus down on the window |
| 2487 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2488 | .deviceId(stylusDeviceId) |
| 2489 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2490 | .build()); |
| 2491 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 2492 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2493 | |
| 2494 | // Subsequent stylus movements are delivered correctly |
| 2495 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2496 | .deviceId(stylusDeviceId) |
| 2497 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2498 | .build()); |
| 2499 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2500 | WithCoords(101, 111))); |
| 2501 | } |
| 2502 | |
| 2503 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2504 | * One window. Touch down on the window. Then, stylus down on the window from another device. |
| 2505 | * Ensure that both touch and stylus are functioning independently. |
| 2506 | */ |
| 2507 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsNotCanceledByStylusDown) { |
| 2508 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2509 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2510 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2511 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2512 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2513 | |
| 2514 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2515 | |
| 2516 | constexpr int32_t touchDeviceId = 4; |
| 2517 | constexpr int32_t stylusDeviceId = 2; |
| 2518 | |
| 2519 | // Touch down on window |
| 2520 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2521 | .deviceId(touchDeviceId) |
| 2522 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2523 | .build()); |
| 2524 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2525 | .deviceId(touchDeviceId) |
| 2526 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2527 | .build()); |
| 2528 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2529 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2530 | |
| 2531 | // Stylus down on the window |
| 2532 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2533 | .deviceId(stylusDeviceId) |
| 2534 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2535 | .build()); |
| 2536 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2537 | |
| 2538 | // Subsequent stylus movements are delivered correctly |
| 2539 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2540 | .deviceId(stylusDeviceId) |
| 2541 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2542 | .build()); |
| 2543 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2544 | WithCoords(101, 111))); |
| 2545 | |
| 2546 | // Touch continues to work too |
| 2547 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2548 | .deviceId(touchDeviceId) |
| 2549 | .pointer(PointerBuilder(0, ToolType::FINGER).x(148).y(149)) |
| 2550 | .build()); |
| 2551 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2552 | } |
| 2553 | |
| 2554 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2555 | * Two windows: a window on the left and a window on the right. |
| 2556 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2557 | * down. Then, on the left window, also place second touch pointer down. |
| 2558 | * This test tries to reproduce a crash. |
| 2559 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2560 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2561 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch_legacy) { |
| 2562 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2563 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2564 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2565 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2566 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2567 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2568 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2569 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2570 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2571 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2572 | mDispatcher->onWindowInfosChanged( |
| 2573 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2574 | |
| 2575 | const int32_t touchDeviceId = 4; |
| 2576 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2577 | |
| 2578 | // Start hovering over the left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2579 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2580 | .deviceId(mouseDeviceId) |
| 2581 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2582 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2583 | leftWindow->consumeMotionEvent( |
| 2584 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2585 | |
| 2586 | // Mouse down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2587 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2588 | .deviceId(mouseDeviceId) |
| 2589 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2590 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2591 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2592 | |
| 2593 | leftWindow->consumeMotionEvent( |
| 2594 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2595 | leftWindow->consumeMotionEvent( |
| 2596 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2597 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2598 | mDispatcher->notifyMotion( |
| 2599 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2600 | .deviceId(mouseDeviceId) |
| 2601 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2602 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2603 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2604 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2605 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2606 | |
| 2607 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2608 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2609 | .deviceId(touchDeviceId) |
| 2610 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2611 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2612 | leftWindow->assertNoEvents(); |
| 2613 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2614 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2615 | |
| 2616 | // Second touch pointer down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2617 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2618 | .deviceId(touchDeviceId) |
| 2619 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2620 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2621 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2622 | // Since this is now a new splittable pointer going down on the left window, and it's coming |
| 2623 | // from a different device, the current gesture in the left window (pointer down) should first |
| 2624 | // be canceled. |
| 2625 | leftWindow->consumeMotionEvent( |
| 2626 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2627 | leftWindow->consumeMotionEvent( |
| 2628 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2629 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2630 | // current implementation. |
| 2631 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2632 | rightWindow->consumeMotionEvent( |
| 2633 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2634 | |
| 2635 | leftWindow->assertNoEvents(); |
| 2636 | rightWindow->assertNoEvents(); |
| 2637 | } |
| 2638 | |
| 2639 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2640 | * Two windows: a window on the left and a window on the right. |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2641 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2642 | * down. Then, on the left window, also place second touch pointer down. |
| 2643 | * This test tries to reproduce a crash. |
| 2644 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2645 | */ |
| 2646 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch) { |
| 2647 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2648 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2649 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2650 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2651 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2652 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2653 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2654 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2655 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2656 | |
| 2657 | mDispatcher->onWindowInfosChanged( |
| 2658 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2659 | |
| 2660 | const int32_t touchDeviceId = 4; |
| 2661 | const int32_t mouseDeviceId = 6; |
| 2662 | |
| 2663 | // Start hovering over the left window |
| 2664 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2665 | .deviceId(mouseDeviceId) |
| 2666 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2667 | .build()); |
| 2668 | leftWindow->consumeMotionEvent( |
| 2669 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2670 | |
| 2671 | // Mouse down on left window |
| 2672 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2673 | .deviceId(mouseDeviceId) |
| 2674 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2675 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2676 | .build()); |
| 2677 | |
| 2678 | leftWindow->consumeMotionEvent( |
| 2679 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2680 | leftWindow->consumeMotionEvent( |
| 2681 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2682 | |
| 2683 | mDispatcher->notifyMotion( |
| 2684 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2685 | .deviceId(mouseDeviceId) |
| 2686 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2687 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2688 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2689 | .build()); |
| 2690 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2691 | |
| 2692 | // First touch pointer down on right window |
| 2693 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2694 | .deviceId(touchDeviceId) |
| 2695 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2696 | .build()); |
| 2697 | leftWindow->assertNoEvents(); |
| 2698 | |
| 2699 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2700 | |
| 2701 | // Second touch pointer down on left window |
| 2702 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2703 | .deviceId(touchDeviceId) |
| 2704 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2705 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2706 | .build()); |
| 2707 | // Since this is now a new splittable pointer going down on the left window, and it's coming |
| 2708 | // from a different device, it will be split and delivered to left window separately. |
| 2709 | leftWindow->consumeMotionEvent( |
| 2710 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2711 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2712 | // current implementation. |
| 2713 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2714 | rightWindow->consumeMotionEvent( |
| 2715 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2716 | |
| 2717 | leftWindow->assertNoEvents(); |
| 2718 | rightWindow->assertNoEvents(); |
| 2719 | } |
| 2720 | |
| 2721 | /** |
| 2722 | * Two windows: a window on the left and a window on the right. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2723 | * Mouse is hovered on the left window and stylus is hovered on the right window. |
| 2724 | */ |
| 2725 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHover) { |
| 2726 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2727 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2728 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2729 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2730 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2731 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2732 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2733 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2734 | |
| 2735 | mDispatcher->onWindowInfosChanged( |
| 2736 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2737 | |
| 2738 | const int32_t stylusDeviceId = 3; |
| 2739 | const int32_t mouseDeviceId = 6; |
| 2740 | |
| 2741 | // Start hovering over the left window |
| 2742 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2743 | .deviceId(mouseDeviceId) |
| 2744 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 2745 | .build()); |
| 2746 | leftWindow->consumeMotionEvent( |
| 2747 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2748 | |
| 2749 | // Stylus hovered on right window |
| 2750 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2751 | .deviceId(stylusDeviceId) |
| 2752 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(300).y(100)) |
| 2753 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2754 | rightWindow->consumeMotionEvent( |
| 2755 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2756 | |
| 2757 | // Subsequent HOVER_MOVE events are dispatched correctly. |
| 2758 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 2759 | .deviceId(mouseDeviceId) |
| 2760 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2761 | .build()); |
| 2762 | leftWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2763 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2764 | |
| 2765 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2766 | .deviceId(stylusDeviceId) |
| 2767 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(310).y(110)) |
| 2768 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2769 | rightWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2770 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2771 | |
| 2772 | leftWindow->assertNoEvents(); |
| 2773 | rightWindow->assertNoEvents(); |
| 2774 | } |
| 2775 | |
| 2776 | /** |
| 2777 | * Three windows: a window on the left and a window on the right. |
| 2778 | * And a spy window that's positioned above all of them. |
| 2779 | * Stylus down on the left window and remains down. Touch goes down on the right and remains down. |
| 2780 | * Check the stream that's received by the spy. |
| 2781 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2782 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy_legacy) { |
| 2783 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2784 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2785 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2786 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 2787 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2788 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 2789 | spyWindow->setTrustedOverlay(true); |
| 2790 | spyWindow->setSpy(true); |
| 2791 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2792 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2793 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2794 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2795 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2796 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2797 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2798 | |
| 2799 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2800 | |
| 2801 | mDispatcher->onWindowInfosChanged( |
| 2802 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2803 | |
| 2804 | const int32_t stylusDeviceId = 1; |
| 2805 | const int32_t touchDeviceId = 2; |
| 2806 | |
| 2807 | // Stylus down on the left window |
| 2808 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2809 | .deviceId(stylusDeviceId) |
| 2810 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2811 | .build()); |
| 2812 | leftWindow->consumeMotionEvent( |
| 2813 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2814 | spyWindow->consumeMotionEvent( |
| 2815 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2816 | |
| 2817 | // Touch down on the right window |
| 2818 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2819 | .deviceId(touchDeviceId) |
| 2820 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2821 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2822 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2823 | rightWindow->consumeMotionEvent( |
| 2824 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2825 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2826 | // Spy window does not receive touch events, because stylus events take precedence, and it |
| 2827 | // already has an active stylus gesture. |
| 2828 | |
| 2829 | // Stylus movements continue. They should be delivered to the left window and to the spy window |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2830 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2831 | .deviceId(stylusDeviceId) |
| 2832 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2833 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2834 | leftWindow->consumeMotionEvent( |
| 2835 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 2836 | spyWindow->consumeMotionEvent( |
| 2837 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2838 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2839 | // Further MOVE events keep going to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2840 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2841 | .deviceId(touchDeviceId) |
| 2842 | .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110)) |
| 2843 | .build()); |
| 2844 | rightWindow->consumeMotionEvent( |
| 2845 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2846 | |
| 2847 | spyWindow->assertNoEvents(); |
| 2848 | leftWindow->assertNoEvents(); |
| 2849 | rightWindow->assertNoEvents(); |
| 2850 | } |
| 2851 | |
| 2852 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2853 | * Three windows: a window on the left and a window on the right. |
| 2854 | * And a spy window that's positioned above all of them. |
| 2855 | * Stylus down on the left window and remains down. Touch goes down on the right and remains down. |
| 2856 | * Check the stream that's received by the spy. |
| 2857 | */ |
| 2858 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy) { |
| 2859 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2860 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2861 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2862 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 2863 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2864 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 2865 | spyWindow->setTrustedOverlay(true); |
| 2866 | spyWindow->setSpy(true); |
| 2867 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2868 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2869 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2870 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2871 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2872 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2873 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2874 | |
| 2875 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2876 | |
| 2877 | mDispatcher->onWindowInfosChanged( |
| 2878 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2879 | |
| 2880 | const int32_t stylusDeviceId = 1; |
| 2881 | const int32_t touchDeviceId = 2; |
| 2882 | |
| 2883 | // Stylus down on the left window |
| 2884 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2885 | .deviceId(stylusDeviceId) |
| 2886 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2887 | .build()); |
| 2888 | leftWindow->consumeMotionEvent( |
| 2889 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2890 | spyWindow->consumeMotionEvent( |
| 2891 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2892 | |
| 2893 | // Touch down on the right window |
| 2894 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2895 | .deviceId(touchDeviceId) |
| 2896 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2897 | .build()); |
| 2898 | leftWindow->assertNoEvents(); |
| 2899 | rightWindow->consumeMotionEvent( |
| 2900 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2901 | spyWindow->consumeMotionEvent( |
| 2902 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2903 | |
| 2904 | // Stylus movements continue. They should be delivered to the left window and to the spy window |
| 2905 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2906 | .deviceId(stylusDeviceId) |
| 2907 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2908 | .build()); |
| 2909 | leftWindow->consumeMotionEvent( |
| 2910 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 2911 | spyWindow->consumeMotionEvent( |
| 2912 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 2913 | |
| 2914 | // Further touch MOVE events keep going to the right window and to the spy |
| 2915 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2916 | .deviceId(touchDeviceId) |
| 2917 | .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110)) |
| 2918 | .build()); |
| 2919 | rightWindow->consumeMotionEvent( |
| 2920 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2921 | spyWindow->consumeMotionEvent( |
| 2922 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2923 | |
| 2924 | spyWindow->assertNoEvents(); |
| 2925 | leftWindow->assertNoEvents(); |
| 2926 | rightWindow->assertNoEvents(); |
| 2927 | } |
| 2928 | |
| 2929 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2930 | * Three windows: a window on the left, a window on the right, and a spy window positioned above |
| 2931 | * both. |
| 2932 | * Check hover in left window and touch down in the right window. |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2933 | * At first, spy should receive hover. Spy shouldn't receive touch while stylus is hovering. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2934 | * At the same time, left and right should be getting independent streams of hovering and touch, |
| 2935 | * respectively. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2936 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2937 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverBlocksTouchWithSpy) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2938 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2939 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2940 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2941 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 2942 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2943 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 2944 | spyWindow->setTrustedOverlay(true); |
| 2945 | spyWindow->setSpy(true); |
| 2946 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2947 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2948 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2949 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2950 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2951 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2952 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2953 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2954 | |
| 2955 | mDispatcher->onWindowInfosChanged( |
| 2956 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2957 | |
| 2958 | const int32_t stylusDeviceId = 1; |
| 2959 | const int32_t touchDeviceId = 2; |
| 2960 | |
| 2961 | // Stylus hover on the left window |
| 2962 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2963 | .deviceId(stylusDeviceId) |
| 2964 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2965 | .build()); |
| 2966 | leftWindow->consumeMotionEvent( |
| 2967 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2968 | spyWindow->consumeMotionEvent( |
| 2969 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2970 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2971 | // Touch down on the right window. Spy doesn't receive this touch because it already has |
| 2972 | // stylus hovering there. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2973 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2974 | .deviceId(touchDeviceId) |
| 2975 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2976 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2977 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2978 | spyWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2979 | rightWindow->consumeMotionEvent( |
| 2980 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2981 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2982 | // Stylus movements continue. They should be delivered to the left window and the spy. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2983 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2984 | .deviceId(stylusDeviceId) |
| 2985 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2986 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2987 | leftWindow->consumeMotionEvent( |
| 2988 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2989 | spyWindow->consumeMotionEvent( |
| 2990 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2991 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2992 | // Touch movements continue. They should be delivered to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2993 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2994 | .deviceId(touchDeviceId) |
| 2995 | .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101)) |
| 2996 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2997 | rightWindow->consumeMotionEvent( |
| 2998 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2999 | |
| 3000 | spyWindow->assertNoEvents(); |
| 3001 | leftWindow->assertNoEvents(); |
| 3002 | rightWindow->assertNoEvents(); |
| 3003 | } |
| 3004 | |
| 3005 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3006 | * Three windows: a window on the left, a window on the right, and a spy window positioned above |
| 3007 | * both. |
| 3008 | * Check hover in left window and touch down in the right window. |
| 3009 | * At first, spy should receive hover. Next, spy should receive touch. |
| 3010 | * At the same time, left and right should be getting independent streams of hovering and touch, |
| 3011 | * respectively. |
| 3012 | */ |
| 3013 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverDoesNotBlockTouchWithSpy) { |
| 3014 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3015 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3016 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3017 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 3018 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3019 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 3020 | spyWindow->setTrustedOverlay(true); |
| 3021 | spyWindow->setSpy(true); |
| 3022 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3023 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 3024 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3025 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3026 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3027 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 3028 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3029 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3030 | |
| 3031 | mDispatcher->onWindowInfosChanged( |
| 3032 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3033 | |
| 3034 | const int32_t stylusDeviceId = 1; |
| 3035 | const int32_t touchDeviceId = 2; |
| 3036 | |
| 3037 | // Stylus hover on the left window |
| 3038 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3039 | .deviceId(stylusDeviceId) |
| 3040 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 3041 | .build()); |
| 3042 | leftWindow->consumeMotionEvent( |
| 3043 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3044 | spyWindow->consumeMotionEvent( |
| 3045 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3046 | |
| 3047 | // Touch down on the right window. |
| 3048 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3049 | .deviceId(touchDeviceId) |
| 3050 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3051 | .build()); |
| 3052 | leftWindow->assertNoEvents(); |
| 3053 | spyWindow->consumeMotionEvent( |
| 3054 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3055 | rightWindow->consumeMotionEvent( |
| 3056 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3057 | |
| 3058 | // Stylus movements continue. They should be delivered to the left window and the spy. |
| 3059 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3060 | .deviceId(stylusDeviceId) |
| 3061 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 3062 | .build()); |
| 3063 | leftWindow->consumeMotionEvent( |
| 3064 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3065 | spyWindow->consumeMotionEvent( |
| 3066 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3067 | |
| 3068 | // Touch movements continue. They should be delivered to the right window and the spy |
| 3069 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3070 | .deviceId(touchDeviceId) |
| 3071 | .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101)) |
| 3072 | .build()); |
| 3073 | rightWindow->consumeMotionEvent( |
| 3074 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3075 | spyWindow->consumeMotionEvent( |
| 3076 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3077 | |
| 3078 | spyWindow->assertNoEvents(); |
| 3079 | leftWindow->assertNoEvents(); |
| 3080 | rightWindow->assertNoEvents(); |
| 3081 | } |
| 3082 | |
| 3083 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3084 | * On a single window, use two different devices: mouse and touch. |
| 3085 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 3086 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 3087 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 3088 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 3089 | * represent a new gesture. |
| 3090 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3091 | TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown_legacy) { |
| 3092 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3093 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3094 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3095 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3096 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3097 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3098 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3099 | |
| 3100 | const int32_t touchDeviceId = 4; |
| 3101 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3102 | |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3103 | // First touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3104 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3105 | .deviceId(touchDeviceId) |
| 3106 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3107 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3108 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3109 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3110 | .deviceId(touchDeviceId) |
| 3111 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3112 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3113 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3114 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3115 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3116 | .deviceId(touchDeviceId) |
| 3117 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3118 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3119 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3120 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3121 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3122 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 3123 | |
| 3124 | // Mouse down. The touch should be canceled |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3125 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3126 | .deviceId(mouseDeviceId) |
| 3127 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3128 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3129 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3130 | |
| 3131 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 3132 | WithPointerCount(1u))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3133 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3134 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3135 | mDispatcher->notifyMotion( |
| 3136 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3137 | .deviceId(mouseDeviceId) |
| 3138 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3139 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3140 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3141 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3142 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3143 | |
| 3144 | // Second touch pointer down. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3145 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3146 | .deviceId(touchDeviceId) |
| 3147 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3148 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3149 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3150 | // Since we already canceled this touch gesture, it will be ignored until a completely new |
| 3151 | // gesture is started. This is easier to implement than trying to keep track of the new pointer |
| 3152 | // and generating an ACTION_DOWN instead of ACTION_POINTER_DOWN. |
| 3153 | // However, mouse movements should continue to work. |
| 3154 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3155 | .deviceId(mouseDeviceId) |
| 3156 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3157 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110)) |
| 3158 | .build()); |
| 3159 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 3160 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3161 | window->assertNoEvents(); |
| 3162 | } |
| 3163 | |
| 3164 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3165 | * On a single window, use two different devices: mouse and touch. |
| 3166 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 3167 | * Mouse is clicked next, which should not interfere with the touch stream. |
| 3168 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is also |
| 3169 | * delivered correctly. |
| 3170 | */ |
| 3171 | TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown) { |
| 3172 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3173 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3174 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3175 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3176 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3177 | |
| 3178 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3179 | |
| 3180 | const int32_t touchDeviceId = 4; |
| 3181 | const int32_t mouseDeviceId = 6; |
| 3182 | |
| 3183 | // First touch pointer down |
| 3184 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3185 | .deviceId(touchDeviceId) |
| 3186 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3187 | .build()); |
| 3188 | // Second touch pointer down |
| 3189 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3190 | .deviceId(touchDeviceId) |
| 3191 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3192 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3193 | .build()); |
| 3194 | // First touch pointer lifts. The second one remains down |
| 3195 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3196 | .deviceId(touchDeviceId) |
| 3197 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3198 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3199 | .build()); |
| 3200 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3201 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3202 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 3203 | |
| 3204 | // Mouse down |
| 3205 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3206 | .deviceId(mouseDeviceId) |
| 3207 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3208 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3209 | .build()); |
| 3210 | |
| 3211 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3212 | |
| 3213 | mDispatcher->notifyMotion( |
| 3214 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3215 | .deviceId(mouseDeviceId) |
| 3216 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3217 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3218 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3219 | .build()); |
| 3220 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3221 | |
| 3222 | // Second touch pointer down. |
| 3223 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3224 | .deviceId(touchDeviceId) |
| 3225 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3226 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3227 | .build()); |
| 3228 | window->consumeMotionEvent(AllOf(WithMotionAction(POINTER_0_DOWN), WithDeviceId(touchDeviceId), |
| 3229 | WithPointerCount(2u))); |
| 3230 | |
| 3231 | // Mouse movements should continue to work |
| 3232 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3233 | .deviceId(mouseDeviceId) |
| 3234 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3235 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110)) |
| 3236 | .build()); |
| 3237 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 3238 | |
| 3239 | window->assertNoEvents(); |
| 3240 | } |
| 3241 | |
| 3242 | /** |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3243 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels |
| 3244 | * the injected event. |
| 3245 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3246 | TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent_legacy) { |
| 3247 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3248 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3249 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3250 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3251 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3252 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3253 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3254 | |
| 3255 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3256 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 3257 | // completion. |
| 3258 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3259 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3260 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3261 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3262 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3263 | .build())); |
| 3264 | window->consumeMotionEvent( |
| 3265 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3266 | |
| 3267 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer |
| 3268 | // should be canceled and the new gesture should take over. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3269 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3270 | .deviceId(touchDeviceId) |
| 3271 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3272 | .build()); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3273 | |
| 3274 | window->consumeMotionEvent( |
| 3275 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3276 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3277 | } |
| 3278 | |
| 3279 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3280 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event runs |
| 3281 | * parallel to the injected event. |
| 3282 | */ |
| 3283 | TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent) { |
| 3284 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3285 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3286 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3287 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3288 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3289 | |
| 3290 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3291 | |
| 3292 | const int32_t touchDeviceId = 4; |
| 3293 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 3294 | // completion. |
| 3295 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3296 | injectMotionEvent(*mDispatcher, |
| 3297 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3298 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
| 3299 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 3300 | .build())); |
| 3301 | window->consumeMotionEvent( |
| 3302 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3303 | |
| 3304 | // Now a real touch comes. The injected pointer will remain, and the new gesture will also be |
| 3305 | // allowed through. |
| 3306 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3307 | .deviceId(touchDeviceId) |
| 3308 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3309 | .build()); |
| 3310 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3311 | } |
| 3312 | |
| 3313 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3314 | * This test is similar to the test above, but the sequence of injected events is different. |
| 3315 | * |
| 3316 | * Two windows: a window on the left and a window on the right. |
| 3317 | * Mouse is hovered over the left window. |
| 3318 | * Next, we tap on the left window, where the cursor was last seen. |
| 3319 | * |
| 3320 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 3321 | * the left window. |
| 3322 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 3323 | * window (first), and then another on the left window (second). |
| 3324 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 3325 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 3326 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3327 | TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch_legacy) { |
| 3328 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3329 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3330 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 3331 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3332 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3333 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3334 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 3335 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3336 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3337 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3338 | mDispatcher->onWindowInfosChanged( |
| 3339 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3340 | |
| 3341 | const int32_t mouseDeviceId = 6; |
| 3342 | const int32_t touchDeviceId = 4; |
| 3343 | // Hover over the left window. Keep the cursor there. |
| 3344 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3345 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3346 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3347 | AINPUT_SOURCE_MOUSE) |
| 3348 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3349 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3350 | .build())); |
| 3351 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3352 | |
| 3353 | // Tap on left window |
| 3354 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3355 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3356 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3357 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3358 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3359 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3360 | .build())); |
| 3361 | |
| 3362 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3363 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3364 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3365 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3366 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3367 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3368 | .build())); |
| 3369 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3370 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3371 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 3372 | |
| 3373 | // First finger down on right window |
| 3374 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3375 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3376 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3377 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3378 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3379 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3380 | .build())); |
| 3381 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3382 | |
| 3383 | // Second finger down on the left window |
| 3384 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3385 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3386 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3387 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3388 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3389 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3390 | .build())); |
| 3391 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3392 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 3393 | |
| 3394 | // No more events |
| 3395 | leftWindow->assertNoEvents(); |
| 3396 | rightWindow->assertNoEvents(); |
| 3397 | } |
| 3398 | |
| 3399 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3400 | * This test is similar to the test above, but the sequence of injected events is different. |
| 3401 | * |
| 3402 | * Two windows: a window on the left and a window on the right. |
| 3403 | * Mouse is hovered over the left window. |
| 3404 | * Next, we tap on the left window, where the cursor was last seen. |
| 3405 | * |
| 3406 | * After that, we send one finger down onto the right window, and then a second finger down onto |
| 3407 | * the left window. |
| 3408 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 3409 | * window (first), and then another on the left window (second). |
| 3410 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 3411 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 3412 | */ |
| 3413 | TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch) { |
| 3414 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3415 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3416 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 3417 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3418 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3419 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3420 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 3421 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3422 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3423 | |
| 3424 | mDispatcher->onWindowInfosChanged( |
| 3425 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3426 | |
| 3427 | const int32_t mouseDeviceId = 6; |
| 3428 | const int32_t touchDeviceId = 4; |
| 3429 | // Hover over the left window. Keep the cursor there. |
| 3430 | mDispatcher->notifyMotion( |
| 3431 | MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3432 | .deviceId(mouseDeviceId) |
| 3433 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 3434 | .build()); |
| 3435 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3436 | |
| 3437 | // Tap on left window |
| 3438 | mDispatcher->notifyMotion( |
| 3439 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3440 | .deviceId(touchDeviceId) |
| 3441 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3442 | .build()); |
| 3443 | |
| 3444 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3445 | .deviceId(touchDeviceId) |
| 3446 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3447 | .build()); |
| 3448 | leftWindow->consumeMotionEvent( |
| 3449 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3450 | leftWindow->consumeMotionEvent( |
| 3451 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithDeviceId(touchDeviceId))); |
| 3452 | |
| 3453 | // First finger down on right window |
| 3454 | mDispatcher->notifyMotion( |
| 3455 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3456 | .deviceId(touchDeviceId) |
| 3457 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3458 | .build()); |
| 3459 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3460 | |
| 3461 | // Second finger down on the left window |
| 3462 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3463 | .deviceId(touchDeviceId) |
| 3464 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3465 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 3466 | .build()); |
| 3467 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3468 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 3469 | |
| 3470 | // No more events |
| 3471 | leftWindow->assertNoEvents(); |
| 3472 | rightWindow->assertNoEvents(); |
| 3473 | } |
| 3474 | |
| 3475 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3476 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 3477 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 3478 | * touch is gone, stylus hovering should start working again. |
| 3479 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3480 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverIgnoresTouchTap) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3481 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3482 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3483 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3484 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3485 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3486 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3487 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3488 | |
| 3489 | const int32_t stylusDeviceId = 5; |
| 3490 | const int32_t touchDeviceId = 4; |
| 3491 | // Start hovering with stylus |
| 3492 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3493 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3494 | MotionEventBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3495 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3496 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3497 | .build())); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3498 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3499 | |
| 3500 | // Finger down on the window |
| 3501 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3502 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3503 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3504 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3505 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3506 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3507 | // The touch device should be ignored! |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3508 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3509 | // Continue hovering with stylus. |
| 3510 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3511 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3512 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3513 | AINPUT_SOURCE_STYLUS) |
| 3514 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3515 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3516 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3517 | // Hovers continue to work |
| 3518 | window->consumeMotionEvent( |
| 3519 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3520 | |
| 3521 | // Lift up the finger |
| 3522 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3523 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3524 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3525 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3526 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3527 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3528 | .build())); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3529 | |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3530 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3531 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3532 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3533 | AINPUT_SOURCE_STYLUS) |
| 3534 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3535 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3536 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3537 | window->consumeMotionEvent( |
| 3538 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3539 | window->assertNoEvents(); |
| 3540 | } |
| 3541 | |
| 3542 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3543 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 3544 | * While the touch is down, hovering from the stylus is not affected. After the touch is gone, |
| 3545 | * check that the stylus hovering continues to work. |
| 3546 | */ |
| 3547 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverWithTouchTap) { |
| 3548 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3549 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3550 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3551 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3552 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3553 | |
| 3554 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3555 | |
| 3556 | const int32_t stylusDeviceId = 5; |
| 3557 | const int32_t touchDeviceId = 4; |
| 3558 | // Start hovering with stylus |
| 3559 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3560 | .deviceId(stylusDeviceId) |
| 3561 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3562 | .build()); |
| 3563 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3564 | |
| 3565 | // Finger down on the window |
| 3566 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3567 | .deviceId(touchDeviceId) |
| 3568 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3569 | .build()); |
| 3570 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3571 | |
| 3572 | // Continue hovering with stylus. |
| 3573 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3574 | .deviceId(stylusDeviceId) |
| 3575 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60)) |
| 3576 | .build()); |
| 3577 | // Hovers continue to work |
| 3578 | window->consumeMotionEvent( |
| 3579 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3580 | |
| 3581 | // Lift up the finger |
| 3582 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3583 | .deviceId(touchDeviceId) |
| 3584 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3585 | .build()); |
| 3586 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDeviceId(touchDeviceId))); |
| 3587 | |
| 3588 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3589 | .deviceId(stylusDeviceId) |
| 3590 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70)) |
| 3591 | .build()); |
| 3592 | window->consumeMotionEvent( |
| 3593 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3594 | window->assertNoEvents(); |
| 3595 | } |
| 3596 | |
| 3597 | /** |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3598 | * If stylus is down anywhere on the screen, then touches should not be delivered to windows that |
| 3599 | * have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 3600 | * |
| 3601 | * Two windows: one on the left and one on the right. |
| 3602 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 3603 | * Stylus down on the left window, and then touch down on the right window. |
| 3604 | * Check that the right window doesn't get touches while the stylus is down on the left window. |
| 3605 | */ |
| 3606 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusDownBlocksTouch) { |
| 3607 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3608 | sp<FakeWindowHandle> leftWindow = |
| 3609 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3610 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3611 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3612 | |
| 3613 | sp<FakeWindowHandle> sbtRightWindow = |
| 3614 | sp<FakeWindowHandle>::make(application, mDispatcher, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3615 | "Stylus blocks touch (right) window", |
| 3616 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3617 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3618 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 3619 | |
| 3620 | mDispatcher->onWindowInfosChanged( |
| 3621 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 3622 | |
| 3623 | const int32_t stylusDeviceId = 5; |
| 3624 | const int32_t touchDeviceId = 4; |
| 3625 | |
| 3626 | // Stylus down in the left window |
| 3627 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3628 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 3629 | .deviceId(stylusDeviceId) |
| 3630 | .build()); |
| 3631 | leftWindow->consumeMotionEvent( |
| 3632 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 3633 | |
| 3634 | // Finger tap on the right window |
| 3635 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3636 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3637 | .deviceId(touchDeviceId) |
| 3638 | .build()); |
| 3639 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3640 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3641 | .deviceId(touchDeviceId) |
| 3642 | .build()); |
| 3643 | |
| 3644 | // The touch should be blocked, because stylus is down somewhere else on screen! |
| 3645 | sbtRightWindow->assertNoEvents(); |
| 3646 | |
| 3647 | // Continue stylus motion, and ensure it's not impacted. |
| 3648 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 3649 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3650 | .deviceId(stylusDeviceId) |
| 3651 | .build()); |
| 3652 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3653 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3654 | .deviceId(stylusDeviceId) |
| 3655 | .build()); |
| 3656 | leftWindow->consumeMotionEvent( |
| 3657 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 3658 | leftWindow->consumeMotionEvent( |
| 3659 | AllOf(WithMotionAction(ACTION_UP), WithDeviceId(stylusDeviceId))); |
| 3660 | |
| 3661 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3662 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3663 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3664 | .deviceId(touchDeviceId) |
| 3665 | .build()); |
| 3666 | sbtRightWindow->consumeMotionEvent( |
| 3667 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3668 | } |
| 3669 | |
| 3670 | /** |
| 3671 | * If stylus is hovering anywhere on the screen, then touches should not be delivered to windows |
| 3672 | * that have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 3673 | * |
| 3674 | * Two windows: one on the left and one on the right. |
| 3675 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 3676 | * Stylus hover on the left window, and then touch down on the right window. |
| 3677 | * Check that the right window doesn't get touches while the stylus is hovering on the left window. |
| 3678 | */ |
| 3679 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusHoverBlocksTouch) { |
| 3680 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3681 | sp<FakeWindowHandle> leftWindow = |
| 3682 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3683 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3684 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3685 | |
| 3686 | sp<FakeWindowHandle> sbtRightWindow = |
| 3687 | sp<FakeWindowHandle>::make(application, mDispatcher, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3688 | "Stylus blocks touch (right) window", |
| 3689 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3690 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3691 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 3692 | |
| 3693 | mDispatcher->onWindowInfosChanged( |
| 3694 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 3695 | |
| 3696 | const int32_t stylusDeviceId = 5; |
| 3697 | const int32_t touchDeviceId = 4; |
| 3698 | |
| 3699 | // Stylus hover in the left window |
| 3700 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3701 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 3702 | .deviceId(stylusDeviceId) |
| 3703 | .build()); |
| 3704 | leftWindow->consumeMotionEvent( |
| 3705 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3706 | |
| 3707 | // Finger tap on the right window |
| 3708 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3709 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3710 | .deviceId(touchDeviceId) |
| 3711 | .build()); |
| 3712 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3713 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3714 | .deviceId(touchDeviceId) |
| 3715 | .build()); |
| 3716 | |
| 3717 | // The touch should be blocked, because stylus is hovering somewhere else on screen! |
| 3718 | sbtRightWindow->assertNoEvents(); |
| 3719 | |
| 3720 | // Continue stylus motion, and ensure it's not impacted. |
| 3721 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3722 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3723 | .deviceId(stylusDeviceId) |
| 3724 | .build()); |
| 3725 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3726 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3727 | .deviceId(stylusDeviceId) |
| 3728 | .build()); |
| 3729 | leftWindow->consumeMotionEvent( |
| 3730 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3731 | leftWindow->consumeMotionEvent( |
| 3732 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId))); |
| 3733 | |
| 3734 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3735 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3736 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3737 | .deviceId(touchDeviceId) |
| 3738 | .build()); |
| 3739 | sbtRightWindow->consumeMotionEvent( |
| 3740 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3741 | } |
| 3742 | |
| 3743 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3744 | * A spy window above a window with no input channel. |
| 3745 | * Start hovering with a stylus device, and then tap with it. |
| 3746 | * Ensure spy window receives the entire sequence. |
| 3747 | */ |
| 3748 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 3749 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3750 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 3751 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3752 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3753 | spyWindow->setTrustedOverlay(true); |
| 3754 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3755 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3756 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3757 | window->setNoInputChannel(true); |
| 3758 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3759 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3760 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3761 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3762 | // Start hovering with stylus |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3763 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3764 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3765 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3766 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3767 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3768 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3769 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3770 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3771 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3772 | |
| 3773 | // Stylus touches down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3774 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3775 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3776 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3777 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3778 | |
| 3779 | // Stylus goes up |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3780 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3781 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3782 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3783 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 3784 | |
| 3785 | // Again hover |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3786 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3787 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3788 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3789 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3790 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3791 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3792 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3793 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3794 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3795 | |
| 3796 | // No more events |
| 3797 | spyWindow->assertNoEvents(); |
| 3798 | window->assertNoEvents(); |
| 3799 | } |
| 3800 | |
| 3801 | /** |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 3802 | * A stale stylus HOVER_EXIT event is injected. Since it's a stale event, it should generally be |
| 3803 | * rejected. But since we already have an ongoing gesture, this event should be processed. |
| 3804 | * This prevents inconsistent events being handled inside the dispatcher. |
| 3805 | */ |
| 3806 | TEST_F(InputDispatcherTest, StaleStylusHoverGestureIsComplete) { |
| 3807 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3808 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3809 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3810 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 3811 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3812 | |
| 3813 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3814 | |
| 3815 | // Start hovering with stylus |
| 3816 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3817 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3818 | .build()); |
| 3819 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3820 | |
| 3821 | NotifyMotionArgs hoverExit = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3822 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3823 | .build(); |
| 3824 | // Make this 'hoverExit' event stale |
| 3825 | mFakePolicy->setStaleEventTimeout(100ms); |
| 3826 | std::this_thread::sleep_for(100ms); |
| 3827 | |
| 3828 | // It shouldn't be dropped by the dispatcher, even though it's stale. |
| 3829 | mDispatcher->notifyMotion(hoverExit); |
| 3830 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3831 | |
| 3832 | // Stylus starts hovering again! There should be no crash. |
| 3833 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3834 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(51)) |
| 3835 | .build()); |
| 3836 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3837 | } |
| 3838 | |
| 3839 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3840 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 3841 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 3842 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 3843 | * While the mouse is down, new move events from the touch device should be ignored. |
| 3844 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3845 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove_legacy) { |
| 3846 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3847 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3848 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 3849 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3850 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3851 | spyWindow->setTrustedOverlay(true); |
| 3852 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3853 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3854 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3855 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3856 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3857 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3858 | |
| 3859 | const int32_t mouseDeviceId = 7; |
| 3860 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3861 | |
| 3862 | // Hover a bit with mouse first |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3863 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3864 | .deviceId(mouseDeviceId) |
| 3865 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3866 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3867 | spyWindow->consumeMotionEvent( |
| 3868 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3869 | window->consumeMotionEvent( |
| 3870 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3871 | |
| 3872 | // Start touching |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3873 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3874 | .deviceId(touchDeviceId) |
| 3875 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3876 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3877 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3878 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3879 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3880 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3881 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3882 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3883 | .deviceId(touchDeviceId) |
| 3884 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 3885 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3886 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3887 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3888 | |
| 3889 | // Pilfer the stream |
| 3890 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3891 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 3892 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3893 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3894 | .deviceId(touchDeviceId) |
| 3895 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 3896 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3897 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3898 | |
| 3899 | // Mouse down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3900 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3901 | .deviceId(mouseDeviceId) |
| 3902 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3903 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3904 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3905 | |
| 3906 | spyWindow->consumeMotionEvent( |
| 3907 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 3908 | spyWindow->consumeMotionEvent( |
| 3909 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3910 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3911 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3912 | mDispatcher->notifyMotion( |
| 3913 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3914 | .deviceId(mouseDeviceId) |
| 3915 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3916 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3917 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3918 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3919 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3920 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3921 | |
| 3922 | // Mouse move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3923 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3924 | .deviceId(mouseDeviceId) |
| 3925 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3926 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 3927 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3928 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3929 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3930 | |
| 3931 | // Touch move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3932 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3933 | .deviceId(touchDeviceId) |
| 3934 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 3935 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3936 | |
| 3937 | // No more events |
| 3938 | spyWindow->assertNoEvents(); |
| 3939 | window->assertNoEvents(); |
| 3940 | } |
| 3941 | |
| 3942 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3943 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 3944 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 3945 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 3946 | * While the mouse is down, new move events from the touch device should continue to work. |
| 3947 | */ |
| 3948 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 3949 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3950 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3951 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 3952 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3953 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3954 | spyWindow->setTrustedOverlay(true); |
| 3955 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3956 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3957 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3958 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3959 | |
| 3960 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 3961 | |
| 3962 | const int32_t mouseDeviceId = 7; |
| 3963 | const int32_t touchDeviceId = 4; |
| 3964 | |
| 3965 | // Hover a bit with mouse first |
| 3966 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3967 | .deviceId(mouseDeviceId) |
| 3968 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3969 | .build()); |
| 3970 | spyWindow->consumeMotionEvent( |
| 3971 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3972 | window->consumeMotionEvent( |
| 3973 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3974 | |
| 3975 | // Start touching |
| 3976 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3977 | .deviceId(touchDeviceId) |
| 3978 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3979 | .build()); |
| 3980 | |
| 3981 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3982 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3983 | |
| 3984 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3985 | .deviceId(touchDeviceId) |
| 3986 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 3987 | .build()); |
| 3988 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3989 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3990 | |
| 3991 | // Pilfer the stream |
| 3992 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3993 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 3994 | // Hover is not pilfered! Only touch. |
| 3995 | |
| 3996 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3997 | .deviceId(touchDeviceId) |
| 3998 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 3999 | .build()); |
| 4000 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 4001 | |
| 4002 | // Mouse down |
| 4003 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4004 | .deviceId(mouseDeviceId) |
| 4005 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4006 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 4007 | .build()); |
| 4008 | |
| 4009 | spyWindow->consumeMotionEvent( |
| 4010 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 4011 | spyWindow->consumeMotionEvent( |
| 4012 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 4013 | window->consumeMotionEvent( |
| 4014 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 4015 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 4016 | |
| 4017 | mDispatcher->notifyMotion( |
| 4018 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 4019 | .deviceId(mouseDeviceId) |
| 4020 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4021 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4022 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 4023 | .build()); |
| 4024 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4025 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4026 | |
| 4027 | // Mouse move! |
| 4028 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4029 | .deviceId(mouseDeviceId) |
| 4030 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4031 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 4032 | .build()); |
| 4033 | spyWindow->consumeMotionEvent( |
| 4034 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 4035 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 4036 | |
| 4037 | // Touch move! |
| 4038 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 4039 | .deviceId(touchDeviceId) |
| 4040 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 4041 | .build()); |
| 4042 | spyWindow->consumeMotionEvent( |
| 4043 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 4044 | |
| 4045 | // No more events |
| 4046 | spyWindow->assertNoEvents(); |
| 4047 | window->assertNoEvents(); |
| 4048 | } |
| 4049 | |
| 4050 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4051 | * On the display, have a single window, and also an area where there's no window. |
| 4052 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 4053 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 4054 | */ |
| 4055 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 4056 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4057 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4058 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4059 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4060 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4061 | |
| 4062 | // Touch down on the empty space |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4063 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4064 | |
| 4065 | mDispatcher->waitForIdle(); |
| 4066 | window->assertNoEvents(); |
| 4067 | |
| 4068 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4069 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4070 | mDispatcher->waitForIdle(); |
| 4071 | window->consumeMotionDown(); |
| 4072 | } |
| 4073 | |
| 4074 | /** |
| 4075 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 4076 | * non-touchable window. |
| 4077 | */ |
| 4078 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 4079 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4080 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4081 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4082 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 4083 | window1->setTouchable(false); |
| 4084 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4085 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4086 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 4087 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4088 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4089 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4090 | // Touch down on the non-touchable window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4091 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4092 | |
| 4093 | mDispatcher->waitForIdle(); |
| 4094 | window1->assertNoEvents(); |
| 4095 | window2->assertNoEvents(); |
| 4096 | |
| 4097 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4098 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4099 | mDispatcher->waitForIdle(); |
| 4100 | window2->consumeMotionDown(); |
| 4101 | } |
| 4102 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4103 | /** |
| 4104 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 4105 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 4106 | */ |
| 4107 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 4108 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4109 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4110 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4111 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 4112 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4113 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4114 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 4115 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4116 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4117 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4118 | // Touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4119 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4120 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4121 | |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4122 | const std::unique_ptr<MotionEvent> firstDown = |
| 4123 | window1->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 4124 | ASSERT_EQ(firstDown->getDownTime(), firstDown->getEventTime()); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4125 | window2->assertNoEvents(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4126 | |
| 4127 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4128 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4129 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4130 | |
| 4131 | const std::unique_ptr<MotionEvent> secondDown = |
| 4132 | window2->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 4133 | ASSERT_EQ(secondDown->getDownTime(), secondDown->getEventTime()); |
| 4134 | ASSERT_NE(firstDown->getDownTime(), secondDown->getDownTime()); |
| 4135 | // We currently send MOVE events to all windows receiving a split touch when there is any change |
| 4136 | // in the touch state, even when none of the pointers in the split window actually moved. |
| 4137 | // Document this behavior in the test. |
| 4138 | window1->consumeMotionMove(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4139 | |
| 4140 | // Now move the pointer on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4141 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4142 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4143 | |
| 4144 | window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime())); |
| 4145 | window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime())); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4146 | |
| 4147 | // Now add new touch down on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4148 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4149 | mDispatcher->waitForIdle(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4150 | |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4151 | window2->consumeMotionEvent( |
| 4152 | AllOf(WithMotionAction(POINTER_1_DOWN), WithDownTime(secondDown->getDownTime()))); |
| 4153 | window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime())); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4154 | |
| 4155 | // Now move the pointer on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4156 | mDispatcher->notifyMotion( |
| 4157 | generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4158 | mDispatcher->waitForIdle(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4159 | |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4160 | window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime())); |
| 4161 | window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime())); |
| 4162 | |
| 4163 | // Now add new touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4164 | mDispatcher->notifyMotion( |
| 4165 | generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4166 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4167 | |
| 4168 | window1->consumeMotionEvent( |
| 4169 | AllOf(WithMotionAction(POINTER_1_DOWN), WithDownTime(firstDown->getDownTime()))); |
| 4170 | window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime())); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4171 | } |
| 4172 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4173 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4174 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4175 | sp<FakeWindowHandle> windowLeft = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 4176 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4177 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4178 | sp<FakeWindowHandle> windowRight = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 4179 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4180 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4181 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4182 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4183 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4184 | mDispatcher->onWindowInfosChanged( |
| 4185 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4186 | |
| 4187 | // Start cursor position in right window so that we can move the cursor to left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4188 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4189 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4190 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4191 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4192 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4193 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4194 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4195 | |
| 4196 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4197 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4198 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4199 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4200 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4201 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4202 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4203 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 4204 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4205 | |
| 4206 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4207 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4208 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4209 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4210 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4211 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4212 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4213 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4214 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4215 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4216 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4217 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4218 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 4219 | AINPUT_SOURCE_MOUSE) |
| 4220 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4221 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4222 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4223 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4224 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4225 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4226 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4227 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4228 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 4229 | AINPUT_SOURCE_MOUSE) |
| 4230 | .buttonState(0) |
| 4231 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4232 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4233 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4234 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4235 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4236 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4237 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4238 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4239 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4240 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4241 | .build())); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4242 | windowLeft->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4243 | |
| 4244 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4245 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4246 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4247 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4248 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4249 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4250 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4251 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4252 | |
| 4253 | // No more events |
| 4254 | windowLeft->assertNoEvents(); |
| 4255 | windowRight->assertNoEvents(); |
| 4256 | } |
| 4257 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4258 | /** |
| 4259 | * Put two fingers down (and don't release them) and click the mouse button. |
| 4260 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 4261 | * currently active gesture should be canceled, and the new one should proceed. |
| 4262 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4263 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick_legacy) { |
| 4264 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4265 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4266 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4267 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4268 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4269 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4270 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4271 | |
| 4272 | const int32_t touchDeviceId = 4; |
| 4273 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4274 | |
| 4275 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4276 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4277 | .deviceId(touchDeviceId) |
| 4278 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4279 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4280 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4281 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4282 | .deviceId(touchDeviceId) |
| 4283 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4284 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 4285 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4286 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4287 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4288 | |
| 4289 | // Inject a series of mouse events for a mouse click |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4290 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4291 | .deviceId(mouseDeviceId) |
| 4292 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4293 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4294 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4295 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 4296 | WithPointerCount(2u))); |
| 4297 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 4298 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4299 | mDispatcher->notifyMotion( |
| 4300 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 4301 | .deviceId(mouseDeviceId) |
| 4302 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4303 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4304 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4305 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4306 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4307 | |
| 4308 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 4309 | // already canceled gesture, it should be ignored. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4310 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 4311 | .deviceId(touchDeviceId) |
| 4312 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 4313 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 4314 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4315 | window->assertNoEvents(); |
| 4316 | } |
| 4317 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4318 | /** |
| 4319 | * Put two fingers down (and don't release them) and click the mouse button. |
| 4320 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 4321 | * currently active gesture should not be canceled, and the new one should proceed in parallel. |
| 4322 | */ |
| 4323 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 4324 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 4325 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4326 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4327 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4328 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4329 | |
| 4330 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 4331 | |
| 4332 | const int32_t touchDeviceId = 4; |
| 4333 | const int32_t mouseDeviceId = 6; |
| 4334 | |
| 4335 | // Two pointers down |
| 4336 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4337 | .deviceId(touchDeviceId) |
| 4338 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4339 | .build()); |
| 4340 | |
| 4341 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4342 | .deviceId(touchDeviceId) |
| 4343 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4344 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 4345 | .build()); |
| 4346 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4347 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4348 | |
| 4349 | // Send a series of mouse events for a mouse click |
| 4350 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4351 | .deviceId(mouseDeviceId) |
| 4352 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4353 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4354 | .build()); |
| 4355 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 4356 | |
| 4357 | mDispatcher->notifyMotion( |
| 4358 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 4359 | .deviceId(mouseDeviceId) |
| 4360 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4361 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4362 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4363 | .build()); |
| 4364 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4365 | |
| 4366 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 4367 | // already active gesture, it should be sent normally. |
| 4368 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 4369 | .deviceId(touchDeviceId) |
| 4370 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 4371 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 4372 | .build()); |
| 4373 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 4374 | window->assertNoEvents(); |
| 4375 | } |
| 4376 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4377 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 4378 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4379 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4380 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 4381 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4382 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4383 | spyWindow->setTrustedOverlay(true); |
| 4384 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4385 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4386 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4387 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4388 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4389 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4390 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4391 | |
| 4392 | // Send mouse cursor to the window |
| 4393 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4394 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4395 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4396 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4397 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4398 | .build())); |
| 4399 | |
| 4400 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4401 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4402 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4403 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4404 | |
| 4405 | window->assertNoEvents(); |
| 4406 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4407 | } |
| 4408 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4409 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows_legacy) { |
| 4410 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4411 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4412 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4413 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 4414 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4415 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4416 | spyWindow->setTrustedOverlay(true); |
| 4417 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4418 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4419 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4420 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4421 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4422 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4423 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4424 | |
| 4425 | // Send mouse cursor to the window |
| 4426 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4427 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4428 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4429 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4430 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4431 | .build())); |
| 4432 | |
| 4433 | // Move mouse cursor |
| 4434 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4435 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4436 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4437 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4438 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4439 | .build())); |
| 4440 | |
| 4441 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4442 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4443 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4444 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4445 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4446 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4447 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4448 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4449 | // Touch down on the window |
| 4450 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4451 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4452 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 4453 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4454 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4455 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4456 | .build())); |
| 4457 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4458 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4459 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4460 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4461 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4462 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4463 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4464 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4465 | |
| 4466 | // pilfer the motion, retaining the gesture on the spy window. |
| 4467 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 4468 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 4469 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4470 | |
| 4471 | // Touch UP on the window |
| 4472 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4473 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4474 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 4475 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4476 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4477 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4478 | .build())); |
| 4479 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4480 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4481 | |
| 4482 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 4483 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 4484 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 4485 | |
| 4486 | // One more tap - DOWN |
| 4487 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4488 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4489 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 4490 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4491 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4492 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4493 | .build())); |
| 4494 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4495 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4496 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4497 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4498 | |
| 4499 | // Touch UP on the window |
| 4500 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4501 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4502 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 4503 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4504 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4505 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4506 | .build())); |
| 4507 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4508 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4509 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4510 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4511 | |
| 4512 | window->assertNoEvents(); |
| 4513 | spyWindow->assertNoEvents(); |
| 4514 | } |
| 4515 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4516 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 4517 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 4518 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4519 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4520 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 4521 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4522 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4523 | spyWindow->setTrustedOverlay(true); |
| 4524 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4525 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4526 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4527 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4528 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4529 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4530 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 4531 | |
| 4532 | // Send mouse cursor to the window |
| 4533 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4534 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 4535 | .build()); |
| 4536 | |
| 4537 | // Move mouse cursor |
| 4538 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4539 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 4540 | .build()); |
| 4541 | |
| 4542 | window->consumeMotionEvent( |
| 4543 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4544 | spyWindow->consumeMotionEvent( |
| 4545 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4546 | window->consumeMotionEvent( |
| 4547 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4548 | spyWindow->consumeMotionEvent( |
| 4549 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4550 | // Touch down on the window |
| 4551 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4552 | .deviceId(SECOND_DEVICE_ID) |
| 4553 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
| 4554 | .build()); |
| 4555 | window->consumeMotionEvent( |
| 4556 | AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4557 | spyWindow->consumeMotionEvent( |
| 4558 | AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4559 | |
| 4560 | // pilfer the motion, retaining the gesture on the spy window. |
| 4561 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 4562 | window->consumeMotionEvent( |
| 4563 | AllOf(WithMotionAction(ACTION_CANCEL), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4564 | // Mouse hover is not pilfered |
| 4565 | |
| 4566 | // Touch UP on the window |
| 4567 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4568 | .deviceId(SECOND_DEVICE_ID) |
| 4569 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
| 4570 | .build()); |
| 4571 | spyWindow->consumeMotionEvent( |
| 4572 | AllOf(WithMotionAction(ACTION_UP), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4573 | |
| 4574 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 4575 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 4576 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 4577 | |
| 4578 | // One more tap - DOWN |
| 4579 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4580 | .deviceId(SECOND_DEVICE_ID) |
| 4581 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
| 4582 | .build()); |
| 4583 | window->consumeMotionEvent( |
| 4584 | AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4585 | spyWindow->consumeMotionEvent( |
| 4586 | AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4587 | |
| 4588 | // Touch UP on the window |
| 4589 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4590 | .deviceId(SECOND_DEVICE_ID) |
| 4591 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
| 4592 | .build()); |
| 4593 | window->consumeMotionEvent( |
| 4594 | AllOf(WithMotionAction(ACTION_UP), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4595 | spyWindow->consumeMotionEvent( |
| 4596 | AllOf(WithMotionAction(ACTION_UP), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4597 | |
| 4598 | // Mouse movement continues normally as well |
| 4599 | // Move mouse cursor |
| 4600 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4601 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(120).y(130)) |
| 4602 | .build()); |
| 4603 | window->consumeMotionEvent( |
| 4604 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4605 | spyWindow->consumeMotionEvent( |
| 4606 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4607 | |
| 4608 | window->assertNoEvents(); |
| 4609 | spyWindow->assertNoEvents(); |
| 4610 | } |
| 4611 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4612 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 4613 | // directly in this test. |
| 4614 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4615 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4616 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4617 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4618 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4619 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4620 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4621 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4622 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4623 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4624 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4625 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4626 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4627 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4628 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4629 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4630 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4631 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4632 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4633 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4634 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4635 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4636 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4637 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4638 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4639 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4640 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4641 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4642 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4643 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 4644 | AINPUT_SOURCE_MOUSE) |
| 4645 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4646 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4647 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4648 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4649 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4650 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4651 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4652 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4653 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 4654 | AINPUT_SOURCE_MOUSE) |
| 4655 | .buttonState(0) |
| 4656 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4657 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4658 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4659 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4660 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4661 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4662 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4663 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4664 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4665 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4666 | .build())); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4667 | window->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4668 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 4669 | // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the |
| 4670 | // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail. |
| 4671 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4672 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4673 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 4674 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4675 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4676 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4677 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4678 | } |
| 4679 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4680 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4681 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 4682 | * is generated. |
| 4683 | */ |
| 4684 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 4685 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4686 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4687 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4688 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 4689 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4690 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4691 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4692 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4693 | |
| 4694 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4695 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4696 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4697 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4698 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4699 | .build())); |
| 4700 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4701 | |
| 4702 | // Remove the window, but keep the channel. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4703 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4704 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 4705 | } |
| 4706 | |
| 4707 | /** |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4708 | * Test that invalid HOVER events sent by accessibility do not cause a fatal crash. |
| 4709 | */ |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 4710 | TEST_F_WITH_FLAGS(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash, |
| 4711 | REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(com::android::input::flags, |
| 4712 | a11y_crash_on_inconsistent_event_stream))) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4713 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4714 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4715 | ui::LogicalDisplayId::DEFAULT); |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4716 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 4717 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4718 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4719 | |
| 4720 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 4721 | |
| 4722 | MotionEventBuilder hoverEnterBuilder = |
| 4723 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4724 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4725 | .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 4726 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4727 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 4728 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4729 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 4730 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4731 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4732 | } |
| 4733 | |
| 4734 | /** |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4735 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. |
| 4736 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4737 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover_legacy) { |
| 4738 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4739 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4740 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4741 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4742 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4743 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4744 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4745 | |
| 4746 | const int32_t mouseDeviceId = 7; |
| 4747 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4748 | |
| 4749 | // Start hovering with the mouse |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4750 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4751 | .deviceId(mouseDeviceId) |
| 4752 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 4753 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4754 | window->consumeMotionEvent( |
| 4755 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 4756 | |
| 4757 | // Touch goes down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4758 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4759 | .deviceId(touchDeviceId) |
| 4760 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4761 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4762 | |
| 4763 | window->consumeMotionEvent( |
| 4764 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 4765 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 4766 | } |
| 4767 | |
| 4768 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4769 | * If mouse is hovering when the touch goes down, the hovering should not be stopped. |
| 4770 | */ |
| 4771 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { |
| 4772 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 4773 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4774 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4775 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4776 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4777 | |
| 4778 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 4779 | |
| 4780 | const int32_t mouseDeviceId = 7; |
| 4781 | const int32_t touchDeviceId = 4; |
| 4782 | |
| 4783 | // Start hovering with the mouse |
| 4784 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4785 | .deviceId(mouseDeviceId) |
| 4786 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 4787 | .build()); |
| 4788 | window->consumeMotionEvent( |
| 4789 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 4790 | |
| 4791 | // Touch goes down |
| 4792 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4793 | .deviceId(touchDeviceId) |
| 4794 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4795 | .build()); |
| 4796 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 4797 | } |
| 4798 | |
| 4799 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4800 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4801 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 4802 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4803 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4804 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap_legacy) { |
| 4805 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4806 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4807 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4808 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4809 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4810 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4811 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4812 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4813 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 4814 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4815 | ASSERT_NO_FATAL_FAILURE( |
| 4816 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4817 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4818 | |
| 4819 | // Tap on the window |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4820 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4821 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4822 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4823 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4824 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4825 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 4826 | |
| 4827 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4828 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4829 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4830 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4831 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4832 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4833 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4834 | ASSERT_NO_FATAL_FAILURE( |
| 4835 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4836 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4837 | } |
| 4838 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4839 | /** |
| 4840 | * Send a mouse hover event followed by a tap from touchscreen. |
| 4841 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 4842 | * stream's source has been switched. |
| 4843 | */ |
| 4844 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 4845 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 4846 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4847 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4848 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4849 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4850 | |
| 4851 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 4852 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4853 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 4854 | .build()); |
| 4855 | |
| 4856 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4857 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4858 | |
| 4859 | // Tap on the window |
| 4860 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4861 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4862 | .build()); |
| 4863 | |
| 4864 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4865 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4866 | |
| 4867 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4868 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4869 | |
| 4870 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4871 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4872 | .build()); |
| 4873 | |
| 4874 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4875 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4876 | } |
| 4877 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4878 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 4879 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4880 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 4881 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4882 | ui::LogicalDisplayId::DEFAULT); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4883 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 4884 | sp<FakeWindowHandle> windowSecondDisplay = |
| 4885 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 4886 | SECOND_DISPLAY_ID); |
| 4887 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 4888 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4889 | mDispatcher->onWindowInfosChanged( |
| 4890 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4891 | |
| 4892 | // Set cursor position in window in default display and check that hover enter and move |
| 4893 | // events are generated. |
| 4894 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4895 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4896 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4897 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4898 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4899 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4900 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4901 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4902 | |
| 4903 | // Remove all windows in secondary display and check that no event happens on window in |
| 4904 | // primary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4905 | mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0}); |
| 4906 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4907 | windowDefaultDisplay->assertNoEvents(); |
| 4908 | |
| 4909 | // Move cursor position in window in default display and check that only hover move |
| 4910 | // event is generated and not hover enter event. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4911 | mDispatcher->onWindowInfosChanged( |
| 4912 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4913 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4914 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4915 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4916 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4917 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4918 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4919 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4920 | windowDefaultDisplay->consumeMotionEvent( |
| 4921 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4922 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4923 | windowDefaultDisplay->assertNoEvents(); |
| 4924 | } |
| 4925 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4926 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4927 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4928 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4929 | sp<FakeWindowHandle> windowLeft = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 4930 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4931 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4932 | sp<FakeWindowHandle> windowRight = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 4933 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4934 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4935 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4936 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4937 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4938 | mDispatcher->onWindowInfosChanged( |
| 4939 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4940 | |
| 4941 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 4942 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4943 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4944 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4945 | ui::LogicalDisplayId::DEFAULT, {610, 400}, {599, 400})); |
| 4946 | windowLeft->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4947 | windowRight->assertNoEvents(); |
| 4948 | } |
| 4949 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4950 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4951 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4952 | sp<FakeWindowHandle> window = |
| 4953 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 4954 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4955 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4956 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4957 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4958 | setFocusedWindow(window); |
| 4959 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4960 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4961 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4962 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4963 | |
| 4964 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4965 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4966 | |
| 4967 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 4968 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4969 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4970 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT, AKEY_EVENT_FLAG_CANCELED); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4971 | } |
| 4972 | |
| 4973 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4974 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4975 | sp<FakeWindowHandle> window = |
| 4976 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 4977 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4978 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4979 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4980 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4981 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4982 | AINPUT_SOURCE_TOUCHSCREEN, |
| 4983 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4984 | |
| 4985 | // Window should receive motion down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4986 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4987 | |
| 4988 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 4989 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4990 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 4991 | window->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4992 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4993 | } |
| 4994 | |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 4995 | TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) { |
| 4996 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4997 | sp<FakeWindowHandle> window = |
| 4998 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 4999 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 5000 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5001 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 5002 | |
| 5003 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 5004 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10)) |
| 5005 | .build()); |
| 5006 | |
| 5007 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 5008 | |
| 5009 | // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT |
| 5010 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
| 5011 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 5012 | |
| 5013 | // After the device has been reset, a new hovering stream can be sent to the window |
| 5014 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 5015 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15)) |
| 5016 | .build()); |
| 5017 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 5018 | } |
| 5019 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5020 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 5021 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5022 | sp<FakeWindowHandle> window = |
| 5023 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5024 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5025 | window->setFocusable(true); |
| 5026 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5027 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5028 | setFocusedWindow(window); |
| 5029 | |
| 5030 | window->consumeFocusEvent(true); |
| 5031 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5032 | const NotifyKeyArgs keyArgs = |
| 5033 | generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5034 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 5035 | const nsecs_t injectTime = keyArgs.eventTime; |
| 5036 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5037 | mDispatcher->notifyKey(keyArgs); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5038 | // The dispatching time should be always greater than or equal to intercept key timeout. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5039 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5040 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 5041 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 5042 | } |
| 5043 | |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 5044 | /** |
| 5045 | * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set. |
| 5046 | */ |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5047 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 5048 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5049 | sp<FakeWindowHandle> window = |
| 5050 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5051 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5052 | window->setFocusable(true); |
| 5053 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5054 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5055 | setFocusedWindow(window); |
| 5056 | |
| 5057 | window->consumeFocusEvent(true); |
| 5058 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5059 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
| 5060 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 5061 | |
| 5062 | // Set a value that's significantly larger than the default consumption timeout. If the |
| 5063 | // implementation is correct, the actual value doesn't matter; it won't slow down the test. |
| 5064 | mFakePolicy->setInterceptKeyTimeout(600ms); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5065 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 5066 | // Window should receive key event immediately when same key up. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5067 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5068 | } |
| 5069 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5070 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5071 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 5072 | * WATCH_OUTSIDE_TOUCH. |
| 5073 | * Both windows are owned by the same UID. |
| 5074 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 5075 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 5076 | */ |
| 5077 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 5078 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5079 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5080 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5081 | window->setFrame(Rect{0, 0, 100, 100}); |
| 5082 | |
| 5083 | sp<FakeWindowHandle> outsideWindow = |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 5084 | sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5085 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5086 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 5087 | outsideWindow->setWatchOutsideTouch(true); |
| 5088 | // outsideWindow must be above 'window' to receive ACTION_OUTSIDE events when 'window' is tapped |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5089 | mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5090 | |
| 5091 | // Tap on first window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5092 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5093 | AINPUT_SOURCE_TOUCHSCREEN, |
| 5094 | ui::LogicalDisplayId::DEFAULT, {PointF{50, 50}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5095 | window->consumeMotionDown(); |
| 5096 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 5097 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 5098 | outsideWindow->consumeMotionEvent( |
| 5099 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 5100 | |
| 5101 | // Ensure outsideWindow doesn't get any more events for the gesture. |
| 5102 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5103 | ui::LogicalDisplayId::DEFAULT, {PointF{51, 51}})); |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 5104 | window->consumeMotionMove(); |
| 5105 | outsideWindow->assertNoEvents(); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5106 | } |
| 5107 | |
| 5108 | /** |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 5109 | * Three windows: |
| 5110 | * - Left window |
| 5111 | * - Right window |
| 5112 | * - Outside window(watch for ACTION_OUTSIDE events) |
| 5113 | * The windows "left" and "outside" share the same owner, the window "right" has a different owner, |
| 5114 | * In order to allow the outside window can receive the ACTION_OUTSIDE events, the outside window is |
| 5115 | * positioned above the "left" and "right" windows, and it doesn't overlap with them. |
| 5116 | * |
| 5117 | * First, device A report a down event landed in the right window, the outside window can receive |
| 5118 | * an ACTION_OUTSIDE event that with zeroed coordinates, the device B report a down event landed |
| 5119 | * in the left window, the outside window can receive an ACTION_OUTSIDE event the with valid |
| 5120 | * coordinates, after these, device A and device B continue report MOVE event, the right and left |
| 5121 | * window can receive it, but outside window event can't receive it. |
| 5122 | */ |
| 5123 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinatesWhenMultiDevice) { |
| 5124 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5125 | sp<FakeWindowHandle> leftWindow = |
| 5126 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5127 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 5128 | leftWindow->setFrame(Rect{0, 0, 100, 100}); |
| 5129 | leftWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
| 5130 | |
| 5131 | sp<FakeWindowHandle> outsideWindow = |
| 5132 | sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5133 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 5134 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 5135 | outsideWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
| 5136 | outsideWindow->setWatchOutsideTouch(true); |
| 5137 | |
| 5138 | std::shared_ptr<FakeApplicationHandle> anotherApplication = |
| 5139 | std::make_shared<FakeApplicationHandle>(); |
| 5140 | sp<FakeWindowHandle> rightWindow = |
| 5141 | sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Right Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5142 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 5143 | rightWindow->setFrame(Rect{100, 0, 200, 100}); |
| 5144 | rightWindow->setOwnerInfo(gui::Pid{2}, gui::Uid{202}); |
| 5145 | |
| 5146 | // OutsideWindow must be above left window and right window to receive ACTION_OUTSIDE events |
| 5147 | // when left window or right window is tapped |
| 5148 | mDispatcher->onWindowInfosChanged( |
| 5149 | {{*outsideWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, |
| 5150 | {}, |
| 5151 | 0, |
| 5152 | 0}); |
| 5153 | |
| 5154 | const DeviceId deviceA = 9; |
| 5155 | const DeviceId deviceB = 3; |
| 5156 | |
| 5157 | // Tap on right window use device A |
| 5158 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5159 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 5160 | .deviceId(deviceA) |
| 5161 | .build()); |
| 5162 | leftWindow->assertNoEvents(); |
| 5163 | rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 5164 | // Right window is belonged to another owner, so outsideWindow should receive ACTION_OUTSIDE |
| 5165 | // with zeroed coords. |
| 5166 | outsideWindow->consumeMotionEvent( |
| 5167 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithDeviceId(deviceA), WithCoords(0, 0))); |
| 5168 | |
| 5169 | // Tap on left window use device B |
| 5170 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5171 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5172 | .deviceId(deviceB) |
| 5173 | .build()); |
| 5174 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 5175 | rightWindow->assertNoEvents(); |
| 5176 | // Because new gesture down on the left window that has the same owner with outside Window, the |
| 5177 | // outside Window should receive the ACTION_OUTSIDE with coords. |
| 5178 | outsideWindow->consumeMotionEvent( |
| 5179 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithDeviceId(deviceB), WithCoords(-50, -50))); |
| 5180 | |
| 5181 | // Ensure that windows that can only accept outside do not receive remaining gestures |
| 5182 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5183 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(51)) |
| 5184 | .deviceId(deviceA) |
| 5185 | .build()); |
| 5186 | leftWindow->assertNoEvents(); |
| 5187 | rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceA))); |
| 5188 | |
| 5189 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5190 | .pointer(PointerBuilder(0, ToolType::FINGER).x(51).y(51)) |
| 5191 | .deviceId(deviceB) |
| 5192 | .build()); |
| 5193 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB))); |
| 5194 | rightWindow->assertNoEvents(); |
| 5195 | outsideWindow->assertNoEvents(); |
| 5196 | } |
| 5197 | |
| 5198 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5199 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 5200 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 5201 | * ACTION_OUTSIDE event is sent per gesture. |
| 5202 | */ |
| 5203 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 5204 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 5205 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5206 | sp<FakeWindowHandle> window = |
| 5207 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5208 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5209 | window->setWatchOutsideTouch(true); |
| 5210 | window->setFrame(Rect{0, 0, 100, 100}); |
| 5211 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5212 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5213 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5214 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 5215 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5216 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5217 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5218 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5219 | mDispatcher->onWindowInfosChanged( |
| 5220 | {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5221 | |
| 5222 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5223 | mDispatcher->notifyMotion( |
| 5224 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5225 | ui::LogicalDisplayId::DEFAULT, {PointF{-10, -10}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5226 | window->assertNoEvents(); |
| 5227 | secondWindow->assertNoEvents(); |
| 5228 | |
| 5229 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 5230 | // Now, `window` should get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5231 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5232 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5233 | {PointF{-10, -10}, PointF{105, 105}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5234 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 5235 | window->consumeMotionEvent( |
| 5236 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5237 | secondWindow->consumeMotionDown(); |
| 5238 | thirdWindow->assertNoEvents(); |
| 5239 | |
| 5240 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 5241 | // no ACTION_OUTSIDE sent to `window` because one has already been sent for this gesture. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5242 | mDispatcher->notifyMotion( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5243 | generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5244 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5245 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5246 | window->assertNoEvents(); |
| 5247 | secondWindow->consumeMotionMove(); |
| 5248 | thirdWindow->consumeMotionDown(); |
| 5249 | } |
| 5250 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5251 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 5252 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5253 | sp<FakeWindowHandle> window = |
| 5254 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5255 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5256 | window->setFocusable(true); |
| 5257 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5258 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5259 | setFocusedWindow(window); |
| 5260 | |
| 5261 | window->consumeFocusEvent(true); |
| 5262 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5263 | const NotifyKeyArgs keyDown = |
| 5264 | generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT); |
| 5265 | const NotifyKeyArgs keyUp = |
| 5266 | generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5267 | mDispatcher->notifyKey(keyDown); |
| 5268 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5269 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5270 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
| 5271 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5272 | |
| 5273 | // All windows are removed from the display. Ensure that we can no longer dispatch to it. |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5274 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5275 | |
| 5276 | window->consumeFocusEvent(false); |
| 5277 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5278 | mDispatcher->notifyKey(keyDown); |
| 5279 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5280 | window->assertNoEvents(); |
| 5281 | } |
| 5282 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5283 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 5284 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5285 | sp<FakeWindowHandle> window = |
| 5286 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5287 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5288 | // Ensure window is non-split and have some transform. |
| 5289 | window->setPreventSplitting(true); |
| 5290 | window->setWindowOffset(20, 40); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5291 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5292 | |
| 5293 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5294 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 5295 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5296 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5297 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5298 | |
| 5299 | const MotionEvent secondFingerDownEvent = |
| 5300 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5301 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5302 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 5303 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 5304 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5305 | .build(); |
| 5306 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5307 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5308 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 5309 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5310 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5311 | std::unique_ptr<MotionEvent> event = window->consumeMotionEvent(); |
| 5312 | ASSERT_NE(nullptr, event); |
| 5313 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 5314 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 5315 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 5316 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 5317 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5318 | } |
| 5319 | |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 5320 | /** |
| 5321 | * Two windows: a splittable and a non-splittable. |
| 5322 | * The non-splittable window shouldn't receive any "incomplete" gestures. |
| 5323 | * Send the first pointer to the splittable window, and then touch the non-splittable window. |
| 5324 | * The second pointer should be dropped because the initial window is splittable, so it won't get |
| 5325 | * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any |
| 5326 | * "incomplete" gestures. |
| 5327 | */ |
| 5328 | TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) { |
| 5329 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5330 | sp<FakeWindowHandle> leftWindow = |
| 5331 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5332 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 5333 | leftWindow->setPreventSplitting(false); |
| 5334 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5335 | sp<FakeWindowHandle> rightWindow = |
| 5336 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5337 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 5338 | rightWindow->setPreventSplitting(true); |
| 5339 | rightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 5340 | mDispatcher->onWindowInfosChanged( |
| 5341 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 5342 | |
| 5343 | // Touch down on left, splittable window |
| 5344 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5345 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5346 | .build()); |
| 5347 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 5348 | |
| 5349 | mDispatcher->notifyMotion( |
| 5350 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5351 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 5352 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
| 5353 | .build()); |
| 5354 | leftWindow->assertNoEvents(); |
| 5355 | rightWindow->assertNoEvents(); |
| 5356 | } |
| 5357 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 5358 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) { |
| 5359 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5360 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5361 | ui::LogicalDisplayId::DEFAULT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 5362 | window->setFrame(Rect(0, 0, 400, 400)); |
| 5363 | sp<FakeWindowHandle> trustedOverlay = |
| 5364 | sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5365 | ui::LogicalDisplayId::DEFAULT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 5366 | trustedOverlay->setSpy(true); |
| 5367 | trustedOverlay->setTrustedOverlay(true); |
| 5368 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5369 | mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 5370 | |
| 5371 | // Start a three-finger touchpad swipe |
| 5372 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 5373 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 5374 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5375 | .build()); |
| 5376 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 5377 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 5378 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 5379 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5380 | .build()); |
| 5381 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 5382 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 5383 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 5384 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 5385 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5386 | .build()); |
| 5387 | |
| 5388 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 5389 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 5390 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN)); |
| 5391 | |
| 5392 | // Move the swipe a bit |
| 5393 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 5394 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5395 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 5396 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 5397 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5398 | .build()); |
| 5399 | |
| 5400 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 5401 | |
| 5402 | // End the swipe |
| 5403 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 5404 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5405 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 5406 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 5407 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5408 | .build()); |
| 5409 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 5410 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5411 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 5412 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5413 | .build()); |
| 5414 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 5415 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5416 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5417 | .build()); |
| 5418 | |
| 5419 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP)); |
| 5420 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 5421 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 5422 | |
| 5423 | window->assertNoEvents(); |
| 5424 | } |
| 5425 | |
| 5426 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) { |
| 5427 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5428 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5429 | ui::LogicalDisplayId::DEFAULT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 5430 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5431 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 5432 | |
| 5433 | // Start a three-finger touchpad swipe |
| 5434 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 5435 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 5436 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5437 | .build()); |
| 5438 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 5439 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 5440 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 5441 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5442 | .build()); |
| 5443 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 5444 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 5445 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 5446 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 5447 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5448 | .build()); |
| 5449 | |
| 5450 | // Move the swipe a bit |
| 5451 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 5452 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5453 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 5454 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 5455 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5456 | .build()); |
| 5457 | |
| 5458 | // End the swipe |
| 5459 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 5460 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5461 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 5462 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 5463 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5464 | .build()); |
| 5465 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 5466 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5467 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 5468 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5469 | .build()); |
| 5470 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 5471 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5472 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5473 | .build()); |
| 5474 | |
| 5475 | window->assertNoEvents(); |
| 5476 | } |
| 5477 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5478 | /** |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5479 | * Send a two-pointer gesture to a single window. The window's orientation changes in response to |
| 5480 | * the first pointer. |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 5481 | * Ensure that the second pointer and the subsequent gesture is correctly delivered to the window. |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5482 | */ |
| 5483 | TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) { |
| 5484 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5485 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5486 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5487 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 5488 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5489 | |
| 5490 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5491 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5492 | .downTime(baseTime + 10) |
| 5493 | .eventTime(baseTime + 10) |
| 5494 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 5495 | .build()); |
| 5496 | |
| 5497 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 5498 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5499 | // Change the transform so that the orientation is now different from original. |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 5500 | window->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5501 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 5502 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5503 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5504 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5505 | .downTime(baseTime + 10) |
| 5506 | .eventTime(baseTime + 30) |
| 5507 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 5508 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 5509 | .build()); |
| 5510 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 5511 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 5512 | |
| 5513 | // Finish the gesture and start a new one. Ensure all events are sent to the window. |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5514 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 5515 | .downTime(baseTime + 10) |
| 5516 | .eventTime(baseTime + 40) |
| 5517 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 5518 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 5519 | .build()); |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 5520 | |
| 5521 | window->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 5522 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5523 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 5524 | .downTime(baseTime + 10) |
| 5525 | .eventTime(baseTime + 50) |
| 5526 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 5527 | .build()); |
| 5528 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 5529 | window->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 5530 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5531 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5532 | .downTime(baseTime + 60) |
| 5533 | .eventTime(baseTime + 60) |
| 5534 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40)) |
| 5535 | .build()); |
| 5536 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 5537 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5538 | } |
| 5539 | |
| 5540 | /** |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5541 | * When there are multiple screens, such as screen projection to TV or screen recording, if the |
| 5542 | * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and |
| 5543 | * its coordinates should be converted by the transform of the windows of target screen. |
| 5544 | */ |
| 5545 | TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) { |
| 5546 | // This case will create a window and a spy window on the default display and mirror |
| 5547 | // window on the second display. cancel event is sent through spy window pilferPointers |
| 5548 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5549 | |
| 5550 | sp<FakeWindowHandle> spyWindowDefaultDisplay = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5551 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 5552 | ui::LogicalDisplayId::DEFAULT); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5553 | spyWindowDefaultDisplay->setTrustedOverlay(true); |
| 5554 | spyWindowDefaultDisplay->setSpy(true); |
| 5555 | |
| 5556 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 5557 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5558 | ui::LogicalDisplayId::DEFAULT); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5559 | windowDefaultDisplay->setWindowTransform(1, 0, 0, 1); |
| 5560 | |
| 5561 | sp<FakeWindowHandle> windowSecondDisplay = windowDefaultDisplay->clone(SECOND_DISPLAY_ID); |
| 5562 | windowSecondDisplay->setWindowTransform(2, 0, 0, 2); |
| 5563 | |
| 5564 | // Add the windows to the dispatcher |
| 5565 | mDispatcher->onWindowInfosChanged( |
| 5566 | {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(), |
| 5567 | *windowSecondDisplay->getInfo()}, |
| 5568 | {}, |
| 5569 | 0, |
| 5570 | 0}); |
| 5571 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5572 | // Send down to ui::LogicalDisplayId::DEFAULT |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5573 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5574 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 5575 | ui::LogicalDisplayId::DEFAULT, {100, 100})) |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5576 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5577 | |
| 5578 | spyWindowDefaultDisplay->consumeMotionDown(); |
| 5579 | windowDefaultDisplay->consumeMotionDown(); |
| 5580 | |
| 5581 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken())); |
| 5582 | |
| 5583 | // windowDefaultDisplay gets cancel |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5584 | std::unique_ptr<MotionEvent> event = windowDefaultDisplay->consumeMotionEvent(); |
| 5585 | ASSERT_NE(nullptr, event); |
| 5586 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event->getAction()); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5587 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5588 | // The cancel event is sent to windowDefaultDisplay of the ui::LogicalDisplayId::DEFAULT |
| 5589 | // display, so the coordinates of the cancel are converted by windowDefaultDisplay's transform, |
| 5590 | // the x and y coordinates are both 100, otherwise if the cancel event is sent to |
| 5591 | // windowSecondDisplay of SECOND_DISPLAY_ID, the x and y coordinates are 200 |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5592 | EXPECT_EQ(100, event->getX(0)); |
| 5593 | EXPECT_EQ(100, event->getY(0)); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5594 | } |
| 5595 | |
| 5596 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5597 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 5598 | * |
| 5599 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 5600 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 5601 | * space. |
| 5602 | */ |
| 5603 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 5604 | public: |
| 5605 | void SetUp() override { |
| 5606 | InputDispatcherTest::SetUp(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5607 | removeAllWindowsAndDisplays(); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5608 | } |
| 5609 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 5610 | void addDisplayInfo(ui::LogicalDisplayId displayId, const ui::Transform& transform) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5611 | gui::DisplayInfo info; |
| 5612 | info.displayId = displayId; |
| 5613 | info.transform = transform; |
| 5614 | mDisplayInfos.push_back(std::move(info)); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5615 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5616 | } |
| 5617 | |
| 5618 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 5619 | mWindowInfos.push_back(*windowHandle->getInfo()); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5620 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5621 | } |
| 5622 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5623 | void removeAllWindowsAndDisplays() { |
| 5624 | mDisplayInfos.clear(); |
| 5625 | mWindowInfos.clear(); |
| 5626 | } |
| 5627 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5628 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 5629 | // on the display. |
| 5630 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 5631 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 5632 | // respectively. |
| 5633 | ui::Transform displayTransform; |
| 5634 | displayTransform.set(2, 0, 0, 4); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5635 | addDisplayInfo(ui::LogicalDisplayId::DEFAULT, displayTransform); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5636 | |
| 5637 | std::shared_ptr<FakeApplicationHandle> application = |
| 5638 | std::make_shared<FakeApplicationHandle>(); |
| 5639 | |
| 5640 | // Add two windows to the display. Their frames are represented in the display space. |
| 5641 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5642 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5643 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5644 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 5645 | addWindow(firstWindow); |
| 5646 | |
| 5647 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5648 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5649 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5650 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 5651 | addWindow(secondWindow); |
| 5652 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 5653 | } |
| 5654 | |
| 5655 | private: |
| 5656 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 5657 | std::vector<gui::WindowInfo> mWindowInfos; |
| 5658 | }; |
| 5659 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5660 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5661 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5662 | // Send down to the first window. The point is represented in the display space. The point is |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5663 | // selected so that if the hit test was performed with the point and the bounds being in |
| 5664 | // different coordinate spaces, the event would end up in the incorrect window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5665 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5666 | AINPUT_SOURCE_TOUCHSCREEN, |
| 5667 | ui::LogicalDisplayId::DEFAULT, {PointF{75, 55}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5668 | |
| 5669 | firstWindow->consumeMotionDown(); |
| 5670 | secondWindow->assertNoEvents(); |
| 5671 | } |
| 5672 | |
| 5673 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 5674 | // the event should be treated as being in the logical display space. |
| 5675 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 5676 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5677 | // Send down to the first window. The point is represented in the logical display space. The |
| 5678 | // point is selected so that if the hit test was done in logical display space, then it would |
| 5679 | // end up in the incorrect window. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5680 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5681 | PointF{75 * 2, 55 * 4}); |
| 5682 | |
| 5683 | firstWindow->consumeMotionDown(); |
| 5684 | secondWindow->assertNoEvents(); |
| 5685 | } |
| 5686 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5687 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 5688 | // event should be treated as being in the logical display space. |
| 5689 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 5690 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5691 | |
| 5692 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5693 | ui::Transform injectedEventTransform; |
| 5694 | injectedEventTransform.set(matrix); |
| 5695 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 5696 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 5697 | |
| 5698 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5699 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5700 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 5701 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5702 | .x(untransformedPoint.x) |
| 5703 | .y(untransformedPoint.y)) |
| 5704 | .build(); |
| 5705 | event.transform(matrix); |
| 5706 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5707 | injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5708 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 5709 | |
| 5710 | firstWindow->consumeMotionDown(); |
| 5711 | secondWindow->assertNoEvents(); |
| 5712 | } |
| 5713 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5714 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 5715 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5716 | |
| 5717 | // Send down to the second window. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5718 | mDispatcher->notifyMotion( |
| 5719 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5720 | ui::LogicalDisplayId::DEFAULT, {PointF{150, 220}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5721 | |
| 5722 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5723 | std::unique_ptr<MotionEvent> event = secondWindow->consumeMotionEvent(); |
| 5724 | ASSERT_NE(nullptr, event); |
| 5725 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5726 | |
| 5727 | // Ensure that the events from the "getRaw" API are in logical display coordinates. |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5728 | EXPECT_EQ(300, event->getRawX(0)); |
| 5729 | EXPECT_EQ(880, event->getRawY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5730 | |
| 5731 | // Ensure that the x and y values are in the window's coordinate space. |
| 5732 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 5733 | // the logical display space. This will be the origin of the window space. |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5734 | EXPECT_EQ(100, event->getX(0)); |
| 5735 | EXPECT_EQ(80, event->getY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5736 | } |
| 5737 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5738 | TEST_F(InputDispatcherDisplayProjectionTest, CancelMotionWithCorrectCoordinates) { |
| 5739 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5740 | // The monitor will always receive events in the logical display's coordinate space, because |
| 5741 | // it does not have a window. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5742 | FakeMonitorReceiver monitor{*mDispatcher, "Monitor", ui::LogicalDisplayId::DEFAULT}; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5743 | |
| 5744 | // Send down to the first window. |
| 5745 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5746 | ui::LogicalDisplayId::DEFAULT, {PointF{50, 100}})); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5747 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5748 | monitor.consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5749 | |
| 5750 | // Second pointer goes down on second window. |
| 5751 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5752 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5753 | {PointF{50, 100}, PointF{150, 220}})); |
| 5754 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80))); |
| 5755 | const std::map<int32_t, PointF> expectedMonitorPointers{{0, PointF{100, 400}}, |
| 5756 | {1, PointF{300, 880}}}; |
| 5757 | monitor.consumeMotionEvent( |
| 5758 | AllOf(WithMotionAction(POINTER_1_DOWN), WithPointers(expectedMonitorPointers))); |
| 5759 | |
| 5760 | mDispatcher->cancelCurrentTouch(); |
| 5761 | |
| 5762 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 5763 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 80))); |
| 5764 | monitor.consumeMotionEvent( |
| 5765 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedMonitorPointers))); |
| 5766 | } |
| 5767 | |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 5768 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeDownWithCorrectCoordinates) { |
| 5769 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5770 | |
| 5771 | // Send down to the first window. |
| 5772 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5773 | ui::LogicalDisplayId::DEFAULT, {PointF{50, 100}})); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 5774 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5775 | |
| 5776 | // The pointer is transferred to the second window, and the second window receives it in the |
| 5777 | // correct coordinate space. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5778 | mDispatcher->transferTouchGesture(firstWindow->getToken(), secondWindow->getToken()); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 5779 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 5780 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(-100, -400))); |
| 5781 | } |
| 5782 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5783 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverEnterExitWithCorrectCoordinates) { |
| 5784 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5785 | |
| 5786 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 5787 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5788 | ui::LogicalDisplayId::DEFAULT, |
| 5789 | {PointF{150, 220}})); |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5790 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5791 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5792 | |
| 5793 | // Touch down at the same location and ensure a hover exit is synthesized. |
| 5794 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5795 | ui::LogicalDisplayId::DEFAULT, |
| 5796 | {PointF{150, 220}})); |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5797 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5798 | WithRawCoords(300, 880))); |
| 5799 | secondWindow->consumeMotionEvent( |
| 5800 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5801 | secondWindow->assertNoEvents(); |
| 5802 | firstWindow->assertNoEvents(); |
| 5803 | } |
| 5804 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5805 | // Same as above, but while the window is being mirrored. |
| 5806 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 5807 | SynthesizeHoverEnterExitWithCorrectCoordinatesWhenMirrored) { |
| 5808 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5809 | |
| 5810 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5811 | ui::Transform secondDisplayTransform; |
| 5812 | secondDisplayTransform.set(matrix); |
| 5813 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 5814 | |
| 5815 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 5816 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 5817 | addWindow(secondWindowClone); |
| 5818 | |
| 5819 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 5820 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5821 | ui::LogicalDisplayId::DEFAULT, |
| 5822 | {PointF{150, 220}})); |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5823 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5824 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5825 | |
| 5826 | // Touch down at the same location and ensure a hover exit is synthesized for the correct |
| 5827 | // display. |
| 5828 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5829 | ui::LogicalDisplayId::DEFAULT, |
| 5830 | {PointF{150, 220}})); |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5831 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5832 | WithRawCoords(300, 880))); |
| 5833 | secondWindow->consumeMotionEvent( |
| 5834 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5835 | secondWindow->assertNoEvents(); |
| 5836 | firstWindow->assertNoEvents(); |
| 5837 | } |
| 5838 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5839 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverCancelationWithCorrectCoordinates) { |
| 5840 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5841 | |
| 5842 | // Send hover enter to second window |
| 5843 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5844 | ui::LogicalDisplayId::DEFAULT, |
| 5845 | {PointF{150, 220}})); |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5846 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5847 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5848 | |
| 5849 | mDispatcher->cancelCurrentTouch(); |
| 5850 | |
| 5851 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5852 | WithRawCoords(300, 880))); |
| 5853 | secondWindow->assertNoEvents(); |
| 5854 | firstWindow->assertNoEvents(); |
| 5855 | } |
| 5856 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5857 | // Same as above, but while the window is being mirrored. |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5858 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 5859 | SynthesizeHoverCancelationWithCorrectCoordinatesWhenMirrored) { |
| 5860 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5861 | |
| 5862 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5863 | ui::Transform secondDisplayTransform; |
| 5864 | secondDisplayTransform.set(matrix); |
| 5865 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 5866 | |
| 5867 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 5868 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 5869 | addWindow(secondWindowClone); |
| 5870 | |
| 5871 | // Send hover enter to second window |
| 5872 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5873 | ui::LogicalDisplayId::DEFAULT, |
| 5874 | {PointF{150, 220}})); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5875 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5876 | WithCoords(100, 80), WithRawCoords(300, 880), |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5877 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5878 | |
| 5879 | mDispatcher->cancelCurrentTouch(); |
| 5880 | |
| 5881 | // Ensure the cancelation happens with the correct displayId and the correct coordinates. |
| 5882 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5883 | WithRawCoords(300, 880), |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5884 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5885 | secondWindow->assertNoEvents(); |
| 5886 | firstWindow->assertNoEvents(); |
| 5887 | } |
| 5888 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5889 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ |
| 5890 | class InputDispatcherDisplayOrientationFixture |
| 5891 | : public InputDispatcherDisplayProjectionTest, |
| 5892 | public ::testing::WithParamInterface<ui::Rotation> {}; |
| 5893 | |
| 5894 | // This test verifies the touchable region of a window for all rotations of the display by tapping |
| 5895 | // in different locations on the display, specifically points close to the four corners of a |
| 5896 | // window. |
| 5897 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { |
| 5898 | constexpr static int32_t displayWidth = 400; |
| 5899 | constexpr static int32_t displayHeight = 800; |
| 5900 | |
| 5901 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5902 | |
| 5903 | const auto rotation = GetParam(); |
| 5904 | |
| 5905 | // Set up the display with the specified rotation. |
| 5906 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 5907 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 5908 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 5909 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 5910 | logicalDisplayWidth, logicalDisplayHeight); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5911 | addDisplayInfo(ui::LogicalDisplayId::DEFAULT, displayTransform); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5912 | |
| 5913 | // Create a window with its bounds determined in the logical display. |
| 5914 | const Rect frameInLogicalDisplay(100, 100, 200, 300); |
| 5915 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5916 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5917 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5918 | window->setFrame(frameInDisplay, displayTransform); |
| 5919 | addWindow(window); |
| 5920 | |
| 5921 | // The following points in logical display space should be inside the window. |
| 5922 | static const std::array<vec2, 4> insidePoints{ |
| 5923 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 5924 | for (const auto pointInsideWindow : insidePoints) { |
| 5925 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); |
| 5926 | const PointF pointInDisplaySpace{p.x, p.y}; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5927 | mDispatcher->notifyMotion( |
| 5928 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5929 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5930 | window->consumeMotionDown(); |
| 5931 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5932 | mDispatcher->notifyMotion( |
| 5933 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5934 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5935 | window->consumeMotionUp(); |
| 5936 | } |
| 5937 | |
| 5938 | // The following points in logical display space should be outside the window. |
| 5939 | static const std::array<vec2, 5> outsidePoints{ |
| 5940 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 5941 | for (const auto pointOutsideWindow : outsidePoints) { |
| 5942 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); |
| 5943 | const PointF pointInDisplaySpace{p.x, p.y}; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5944 | mDispatcher->notifyMotion( |
| 5945 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5946 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5947 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5948 | mDispatcher->notifyMotion( |
| 5949 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5950 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5951 | } |
| 5952 | window->assertNoEvents(); |
| 5953 | } |
| 5954 | |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 5955 | // This test verifies the occlusion detection for all rotations of the display by tapping |
| 5956 | // in different locations on the display, specifically points close to the four corners of a |
| 5957 | // window. |
| 5958 | TEST_P(InputDispatcherDisplayOrientationFixture, BlockUntrustClickInDifferentOrientations) { |
| 5959 | constexpr static int32_t displayWidth = 400; |
| 5960 | constexpr static int32_t displayHeight = 800; |
| 5961 | |
| 5962 | std::shared_ptr<FakeApplicationHandle> untrustedWindowApplication = |
| 5963 | std::make_shared<FakeApplicationHandle>(); |
| 5964 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5965 | |
| 5966 | const auto rotation = GetParam(); |
| 5967 | |
| 5968 | // Set up the display with the specified rotation. |
| 5969 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 5970 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 5971 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 5972 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 5973 | logicalDisplayWidth, logicalDisplayHeight); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5974 | addDisplayInfo(ui::LogicalDisplayId::DEFAULT, displayTransform); |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 5975 | |
| 5976 | // Create a window that not trusted. |
| 5977 | const Rect untrustedWindowFrameInLogicalDisplay(100, 100, 200, 300); |
| 5978 | |
| 5979 | const Rect untrustedWindowFrameInDisplay = |
| 5980 | displayTransform.inverse().transform(untrustedWindowFrameInLogicalDisplay); |
| 5981 | |
| 5982 | sp<FakeWindowHandle> untrustedWindow = |
| 5983 | sp<FakeWindowHandle>::make(untrustedWindowApplication, mDispatcher, "UntrustedWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5984 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 5985 | untrustedWindow->setFrame(untrustedWindowFrameInDisplay, displayTransform); |
| 5986 | untrustedWindow->setTrustedOverlay(false); |
| 5987 | untrustedWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 5988 | untrustedWindow->setTouchable(false); |
| 5989 | untrustedWindow->setAlpha(1.0f); |
| 5990 | untrustedWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
| 5991 | addWindow(untrustedWindow); |
| 5992 | |
| 5993 | // Create a simple app window below the untrusted window. |
| 5994 | const Rect simpleAppWindowFrameInLogicalDisplay(0, 0, 300, 600); |
| 5995 | const Rect simpleAppWindowFrameInDisplay = |
| 5996 | displayTransform.inverse().transform(simpleAppWindowFrameInLogicalDisplay); |
| 5997 | |
| 5998 | sp<FakeWindowHandle> simpleAppWindow = |
| 5999 | sp<FakeWindowHandle>::make(application, mDispatcher, "SimpleAppWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6000 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6001 | simpleAppWindow->setFrame(simpleAppWindowFrameInDisplay, displayTransform); |
| 6002 | simpleAppWindow->setOwnerInfo(gui::Pid{2}, gui::Uid{202}); |
| 6003 | addWindow(simpleAppWindow); |
| 6004 | |
| 6005 | // The following points in logical display space should be inside the untrusted window, so |
| 6006 | // the simple window could not receive events that coordinate is these point. |
| 6007 | static const std::array<vec2, 4> untrustedPoints{ |
| 6008 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 6009 | |
| 6010 | for (const auto untrustedPoint : untrustedPoints) { |
| 6011 | const vec2 p = displayTransform.inverse().transform(untrustedPoint); |
| 6012 | const PointF pointInDisplaySpace{p.x, p.y}; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6013 | mDispatcher->notifyMotion( |
| 6014 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6015 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
| 6016 | mDispatcher->notifyMotion( |
| 6017 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6018 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6019 | } |
| 6020 | untrustedWindow->assertNoEvents(); |
| 6021 | simpleAppWindow->assertNoEvents(); |
| 6022 | // The following points in logical display space should be outside the untrusted window, so |
| 6023 | // the simple window should receive events that coordinate is these point. |
| 6024 | static const std::array<vec2, 5> trustedPoints{ |
| 6025 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 6026 | for (const auto trustedPoint : trustedPoints) { |
| 6027 | const vec2 p = displayTransform.inverse().transform(trustedPoint); |
| 6028 | const PointF pointInDisplaySpace{p.x, p.y}; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6029 | mDispatcher->notifyMotion( |
| 6030 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6031 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
| 6032 | simpleAppWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6033 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6034 | mDispatcher->notifyMotion( |
| 6035 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6036 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
| 6037 | simpleAppWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6038 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 6039 | } |
| 6040 | untrustedWindow->assertNoEvents(); |
| 6041 | } |
| 6042 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6043 | // Run the precision tests for all rotations. |
| 6044 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, |
| 6045 | InputDispatcherDisplayOrientationFixture, |
| 6046 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, |
| 6047 | ui::ROTATION_270), |
| 6048 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { |
| 6049 | return ftl::enum_string(testParamInfo.param); |
| 6050 | }); |
| 6051 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6052 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 6053 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6054 | |
| 6055 | class TransferTouchFixture : public InputDispatcherTest, |
| 6056 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 6057 | |
| 6058 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6059 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6060 | |
| 6061 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6062 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6063 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6064 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6065 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6066 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6067 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6068 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6069 | sp<FakeWindowHandle> wallpaper = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6070 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 6071 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6072 | wallpaper->setIsWallpaper(true); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6073 | // Add the windows to the dispatcher, and ensure the first window is focused |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6074 | mDispatcher->onWindowInfosChanged( |
| 6075 | {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6076 | setFocusedWindow(firstWindow); |
| 6077 | firstWindow->consumeFocusEvent(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6078 | |
| 6079 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6080 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6081 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6082 | ui::LogicalDisplayId::DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6083 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6084 | // Only the first window should get the down event |
| 6085 | firstWindow->consumeMotionDown(); |
| 6086 | secondWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6087 | wallpaper->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6088 | // Dispatcher reports pointer down outside focus for the wallpaper |
| 6089 | mFakePolicy->assertOnPointerDownEquals(wallpaper->getToken()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6090 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6091 | // Transfer touch to the second window |
| 6092 | TransferFunction f = GetParam(); |
| 6093 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 6094 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6095 | // The first window gets cancel and the second gets down |
| 6096 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6097 | secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 6098 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 6099 | wallpaper->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6100 | // There should not be any changes to the focused window when transferring touch |
| 6101 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertOnPointerDownWasNotCalled()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6102 | |
| 6103 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6104 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6105 | ui::LogicalDisplayId::DEFAULT)); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6106 | // The first window gets no events and the second gets up |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6107 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6108 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6109 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6110 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6111 | } |
| 6112 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6113 | /** |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6114 | * When 'transferTouchGesture' API is invoked, dispatcher needs to find the "best" window to take |
| 6115 | * touch from. When we have spy windows, there are several windows to choose from: either spy, or |
| 6116 | * the 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6117 | * natural to the user. |
| 6118 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 6119 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 6120 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 6121 | * the first window to the second. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6122 | * The main goal here is to test the behaviour of 'transferTouchGesture' API, but it's still valid |
| 6123 | * to test the other API, as well. |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6124 | */ |
| 6125 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 6126 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6127 | |
| 6128 | // Create a couple of windows + a spy window |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6129 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 6130 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6131 | spyWindow->setTrustedOverlay(true); |
| 6132 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6133 | sp<FakeWindowHandle> firstWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "First", |
| 6134 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6135 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6136 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 6137 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6138 | |
| 6139 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6140 | mDispatcher->onWindowInfosChanged( |
| 6141 | {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6142 | |
| 6143 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6144 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6145 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6146 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6147 | // Only the first window and spy should get the down event |
| 6148 | spyWindow->consumeMotionDown(); |
| 6149 | firstWindow->consumeMotionDown(); |
| 6150 | |
| 6151 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6152 | // if f === 'transferTouchGesture'. |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6153 | TransferFunction f = GetParam(); |
| 6154 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 6155 | ASSERT_TRUE(success); |
| 6156 | // The first window gets cancel and the second gets down |
| 6157 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6158 | secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 6159 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6160 | |
| 6161 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6162 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6163 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6164 | // The first window gets no events and the second+spy get up |
| 6165 | firstWindow->assertNoEvents(); |
| 6166 | spyWindow->consumeMotionUp(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6167 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6168 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6169 | } |
| 6170 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6171 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6172 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6173 | |
| 6174 | PointF touchPoint = {10, 10}; |
| 6175 | |
| 6176 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6177 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6178 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6179 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6180 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6181 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6182 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6183 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6184 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6185 | |
| 6186 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6187 | mDispatcher->onWindowInfosChanged( |
| 6188 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6189 | |
| 6190 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6191 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6192 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6193 | ui::LogicalDisplayId::DEFAULT, {touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6194 | // Only the first window should get the down event |
| 6195 | firstWindow->consumeMotionDown(); |
| 6196 | secondWindow->assertNoEvents(); |
| 6197 | |
| 6198 | // Send pointer down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6199 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6200 | ui::LogicalDisplayId::DEFAULT, |
| 6201 | {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6202 | // Only the first window should get the pointer down event |
| 6203 | firstWindow->consumeMotionPointerDown(1); |
| 6204 | secondWindow->assertNoEvents(); |
| 6205 | |
| 6206 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6207 | TransferFunction f = GetParam(); |
| 6208 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 6209 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6210 | // The first window gets cancel and the second gets down and pointer down |
| 6211 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6212 | secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 6213 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 6214 | secondWindow->consumeMotionPointerDown(1, ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6215 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6216 | |
| 6217 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6218 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6219 | ui::LogicalDisplayId::DEFAULT, |
| 6220 | {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6221 | // The first window gets nothing and the second gets pointer up |
| 6222 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6223 | secondWindow->consumeMotionPointerUp(1, ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6224 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6225 | |
| 6226 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6227 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6228 | ui::LogicalDisplayId::DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6229 | // The first window gets nothing and the second gets up |
| 6230 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6231 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6232 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6233 | } |
| 6234 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6235 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 6236 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6237 | |
| 6238 | // Create a couple of windows |
| 6239 | sp<FakeWindowHandle> firstWindow = |
| 6240 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6241 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6242 | firstWindow->setDupTouchToWallpaper(true); |
| 6243 | sp<FakeWindowHandle> secondWindow = |
| 6244 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6245 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6246 | secondWindow->setDupTouchToWallpaper(true); |
| 6247 | |
| 6248 | sp<FakeWindowHandle> wallpaper1 = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6249 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", |
| 6250 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6251 | wallpaper1->setIsWallpaper(true); |
| 6252 | |
| 6253 | sp<FakeWindowHandle> wallpaper2 = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6254 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", |
| 6255 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6256 | wallpaper2->setIsWallpaper(true); |
| 6257 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6258 | mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(), |
| 6259 | *secondWindow->getInfo(), *wallpaper2->getInfo()}, |
| 6260 | {}, |
| 6261 | 0, |
| 6262 | 0}); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6263 | |
| 6264 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6265 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6266 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6267 | ui::LogicalDisplayId::DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6268 | |
| 6269 | // Only the first window should get the down event |
| 6270 | firstWindow->consumeMotionDown(); |
| 6271 | secondWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6272 | wallpaper1->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6273 | wallpaper2->assertNoEvents(); |
| 6274 | |
| 6275 | // Transfer touch focus to the second window |
| 6276 | TransferFunction f = GetParam(); |
| 6277 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 6278 | ASSERT_TRUE(success); |
| 6279 | |
| 6280 | // The first window gets cancel and the second gets down |
| 6281 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6282 | secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 6283 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 6284 | wallpaper1->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
| 6285 | wallpaper2->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 6286 | EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6287 | |
| 6288 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6289 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6290 | ui::LogicalDisplayId::DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6291 | // The first window gets no events and the second gets up |
| 6292 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6293 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6294 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6295 | wallpaper1->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6296 | wallpaper2->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 6297 | EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6298 | } |
| 6299 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6300 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6301 | // 'transferTouchGesture' and 'transferTouchOnDisplay' are equivalent in behaviour. They only differ |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6302 | // for the case where there are multiple pointers split across several windows. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6303 | INSTANTIATE_TEST_SUITE_P( |
| 6304 | InputDispatcherTransferFunctionTests, TransferTouchFixture, |
| 6305 | ::testing::Values( |
| 6306 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, sp<IBinder> /*ignored*/, |
| 6307 | sp<IBinder> destChannelToken) { |
| 6308 | return dispatcher->transferTouchOnDisplay(destChannelToken, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6309 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6310 | }, |
| 6311 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, sp<IBinder> from, |
| 6312 | sp<IBinder> to) { |
| 6313 | return dispatcher->transferTouchGesture(from, to, |
| 6314 | /*isDragAndDrop=*/false); |
| 6315 | })); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6316 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6317 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6318 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6319 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6320 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6321 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6322 | ui::LogicalDisplayId::DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6323 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6324 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6325 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6326 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6327 | ui::LogicalDisplayId::DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6328 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6329 | |
| 6330 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6331 | mDispatcher->onWindowInfosChanged( |
| 6332 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6333 | |
| 6334 | PointF pointInFirst = {300, 200}; |
| 6335 | PointF pointInSecond = {300, 600}; |
| 6336 | |
| 6337 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6338 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6339 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6340 | ui::LogicalDisplayId::DEFAULT, {pointInFirst})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6341 | // Only the first window should get the down event |
| 6342 | firstWindow->consumeMotionDown(); |
| 6343 | secondWindow->assertNoEvents(); |
| 6344 | |
| 6345 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6346 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6347 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6348 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6349 | // The first window gets a move and the second a down |
| 6350 | firstWindow->consumeMotionMove(); |
| 6351 | secondWindow->consumeMotionDown(); |
| 6352 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6353 | // Transfer touch to the second window |
| 6354 | mDispatcher->transferTouchGesture(firstWindow->getToken(), secondWindow->getToken()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6355 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 6356 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6357 | secondWindow->consumeMotionPointerDown(1, ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6358 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6359 | |
| 6360 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6361 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6362 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6363 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6364 | // The first window gets nothing and the second gets pointer up |
| 6365 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6366 | secondWindow->consumeMotionPointerUp(1, ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6367 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6368 | |
| 6369 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6370 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6371 | ui::LogicalDisplayId::DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6372 | // The first window gets nothing and the second gets up |
| 6373 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6374 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6375 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6376 | } |
| 6377 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6378 | // Same as TransferTouch_TwoPointersSplitTouch, but using 'transferTouchOnDisplay' api. |
| 6379 | // Unlike 'transferTouchGesture', calling 'transferTouchOnDisplay' when there are two windows |
| 6380 | // receiving touch is not supported, so the touch should continue on those windows and the |
| 6381 | // transferred-to window should get nothing. |
| 6382 | TEST_F(InputDispatcherTest, TransferTouchOnDisplay_TwoPointersSplitTouch) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6383 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6384 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6385 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6386 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6387 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6388 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6389 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6390 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6391 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6392 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6393 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6394 | |
| 6395 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6396 | mDispatcher->onWindowInfosChanged( |
| 6397 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6398 | |
| 6399 | PointF pointInFirst = {300, 200}; |
| 6400 | PointF pointInSecond = {300, 600}; |
| 6401 | |
| 6402 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6403 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6404 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6405 | ui::LogicalDisplayId::DEFAULT, {pointInFirst})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6406 | // Only the first window should get the down event |
| 6407 | firstWindow->consumeMotionDown(); |
| 6408 | secondWindow->assertNoEvents(); |
| 6409 | |
| 6410 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6411 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6412 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6413 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6414 | // The first window gets a move and the second a down |
| 6415 | firstWindow->consumeMotionMove(); |
| 6416 | secondWindow->consumeMotionDown(); |
| 6417 | |
| 6418 | // Transfer touch focus to the second window |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6419 | const bool transferred = mDispatcher->transferTouchOnDisplay(secondWindow->getToken(), |
| 6420 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6421 | // The 'transferTouchOnDisplay' call should not succeed, because there are 2 touched windows |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6422 | ASSERT_FALSE(transferred); |
| 6423 | firstWindow->assertNoEvents(); |
| 6424 | secondWindow->assertNoEvents(); |
| 6425 | |
| 6426 | // The rest of the dispatch should proceed as normal |
| 6427 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6428 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6429 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6430 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6431 | // The first window gets MOVE and the second gets pointer up |
| 6432 | firstWindow->consumeMotionMove(); |
| 6433 | secondWindow->consumeMotionUp(); |
| 6434 | |
| 6435 | // Send up event to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6436 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6437 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6438 | // The first window gets nothing and the second gets up |
| 6439 | firstWindow->consumeMotionUp(); |
| 6440 | secondWindow->assertNoEvents(); |
| 6441 | } |
| 6442 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6443 | // This case will create two windows and one mirrored window on the default display and mirror |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6444 | // two windows on the second display. It will test if 'transferTouchGesture' works fine if we put |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6445 | // the windows info of second display before default display. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6446 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6447 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6448 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6449 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", |
| 6450 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6451 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6452 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6453 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", |
| 6454 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6455 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6456 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6457 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 6458 | firstWindowInPrimary->clone(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6459 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6460 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6461 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6462 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6463 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6464 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6465 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6466 | |
| 6467 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6468 | mDispatcher->onWindowInfosChanged( |
| 6469 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 6470 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 6471 | *secondWindowInPrimary->getInfo()}, |
| 6472 | {}, |
| 6473 | 0, |
| 6474 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6475 | |
| 6476 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6477 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6478 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6479 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6480 | |
| 6481 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6482 | firstWindowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6483 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6484 | // Transfer touch |
| 6485 | ASSERT_TRUE(mDispatcher->transferTouchGesture(firstWindowInPrimary->getToken(), |
| 6486 | secondWindowInPrimary->getToken())); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6487 | // The first window gets cancel. |
| 6488 | firstWindowInPrimary->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6489 | secondWindowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6490 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6491 | |
| 6492 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6493 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6494 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6495 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6496 | firstWindowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6497 | secondWindowInPrimary->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6498 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6499 | |
| 6500 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6501 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6502 | {150, 50})) |
| 6503 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6504 | firstWindowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6505 | secondWindowInPrimary->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6506 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6507 | } |
| 6508 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6509 | // Same as TransferTouch_CloneSurface, but this touch on the secondary display and use |
| 6510 | // 'transferTouchOnDisplay' api. |
| 6511 | TEST_F(InputDispatcherTest, TransferTouchOnDisplay_CloneSurface) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6512 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6513 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6514 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", |
| 6515 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6516 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6517 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6518 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", |
| 6519 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6520 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6521 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6522 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 6523 | firstWindowInPrimary->clone(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6524 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6525 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6526 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6527 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6528 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6529 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6530 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6531 | |
| 6532 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6533 | mDispatcher->onWindowInfosChanged( |
| 6534 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 6535 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 6536 | *secondWindowInPrimary->getInfo()}, |
| 6537 | {}, |
| 6538 | 0, |
| 6539 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6540 | |
| 6541 | // Touch on second display. |
| 6542 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6543 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 6544 | {50, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6545 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6546 | |
| 6547 | // Window should receive motion event. |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6548 | firstWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6549 | |
| 6550 | // Transfer touch focus |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6551 | ASSERT_TRUE(mDispatcher->transferTouchOnDisplay(secondWindowInSecondary->getToken(), |
| 6552 | SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6553 | |
| 6554 | // The first window gets cancel. |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6555 | firstWindowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6556 | secondWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, |
| 6557 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6558 | |
| 6559 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6560 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6561 | SECOND_DISPLAY_ID, {150, 50})) |
| 6562 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6563 | firstWindowInSecondary->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6564 | secondWindowInSecondary->consumeMotionMove(SECOND_DISPLAY_ID, |
| 6565 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6566 | |
| 6567 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6568 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6569 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6570 | firstWindowInSecondary->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6571 | secondWindowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6572 | } |
| 6573 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6574 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6575 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6576 | sp<FakeWindowHandle> window = |
| 6577 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6578 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6579 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6580 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6581 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6582 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6583 | |
| 6584 | window->consumeFocusEvent(true); |
| 6585 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6586 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6587 | |
| 6588 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6589 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6590 | |
| 6591 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 6592 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6593 | mFakePolicy->assertUserActivityPoked(); |
| 6594 | } |
| 6595 | |
| 6596 | TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) { |
| 6597 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6598 | sp<FakeWindowHandle> window = |
| 6599 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6600 | ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6601 | |
| 6602 | window->setDisableUserActivity(true); |
| 6603 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6604 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6605 | setFocusedWindow(window); |
| 6606 | |
| 6607 | window->consumeFocusEvent(true); |
| 6608 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame^] | 6609 | mDispatcher->notifyKey( |
| 6610 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6611 | |
| 6612 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6613 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6614 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame^] | 6615 | // Should have not poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 6616 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6617 | mFakePolicy->assertUserActivityNotPoked(); |
| 6618 | } |
| 6619 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame^] | 6620 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceivePolicyConsumedKey) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6621 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6622 | sp<FakeWindowHandle> window = |
| 6623 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6624 | ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6625 | |
| 6626 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6627 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6628 | setFocusedWindow(window); |
| 6629 | |
| 6630 | window->consumeFocusEvent(true); |
| 6631 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame^] | 6632 | mFakePolicy->setConsumeKeyBeforeDispatching(true); |
| 6633 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6634 | mDispatcher->notifyKey( |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame^] | 6635 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6636 | mDispatcher->waitForIdle(); |
| 6637 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame^] | 6638 | // Key is not passed down |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6639 | window->assertNoEvents(); |
| 6640 | |
| 6641 | // Should have poked user activity |
| 6642 | mFakePolicy->assertUserActivityPoked(); |
| 6643 | } |
| 6644 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame^] | 6645 | TEST_F(InputDispatcherTest, FocusedWindow_PolicyConsumedKeyIgnoresDisableUserActivity) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6646 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6647 | sp<FakeWindowHandle> window = |
| 6648 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6649 | ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6650 | |
| 6651 | window->setDisableUserActivity(true); |
| 6652 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6653 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6654 | setFocusedWindow(window); |
| 6655 | |
| 6656 | window->consumeFocusEvent(true); |
| 6657 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame^] | 6658 | mFakePolicy->setConsumeKeyBeforeDispatching(true); |
| 6659 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6660 | mDispatcher->notifyKey( |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame^] | 6661 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6662 | mDispatcher->waitForIdle(); |
| 6663 | |
| 6664 | // System key is not passed down |
| 6665 | window->assertNoEvents(); |
| 6666 | |
| 6667 | // Should have poked user activity |
| 6668 | mFakePolicy->assertUserActivityPoked(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6669 | } |
| 6670 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame^] | 6671 | class DisableUserActivityInputDispatcherTest : public InputDispatcherTest, |
| 6672 | public ::testing::WithParamInterface<bool> {}; |
| 6673 | |
| 6674 | TEST_P(DisableUserActivityInputDispatcherTest, NotPassedToUserUserActivity) { |
| 6675 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6676 | sp<FakeWindowHandle> window = |
| 6677 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6678 | ui::LogicalDisplayId::DEFAULT); |
| 6679 | |
| 6680 | window->setDisableUserActivity(GetParam()); |
| 6681 | |
| 6682 | window->setFocusable(true); |
| 6683 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 6684 | setFocusedWindow(window); |
| 6685 | |
| 6686 | window->consumeFocusEvent(true); |
| 6687 | |
| 6688 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 6689 | .keyCode(AKEYCODE_A) |
| 6690 | .policyFlags(0) |
| 6691 | .build()); |
| 6692 | mDispatcher->waitForIdle(); |
| 6693 | |
| 6694 | // Key is not passed down |
| 6695 | window->assertNoEvents(); |
| 6696 | |
| 6697 | // Should not have poked user activity |
| 6698 | mFakePolicy->assertUserActivityNotPoked(); |
| 6699 | } |
| 6700 | |
| 6701 | INSTANTIATE_TEST_CASE_P(DisableUserActivity, DisableUserActivityInputDispatcherTest, |
| 6702 | ::testing::Bool()); |
| 6703 | |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6704 | TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) { |
| 6705 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6706 | sp<FakeWindowHandle> window = |
| 6707 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6708 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6709 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6710 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6711 | |
| 6712 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6713 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6714 | ui::LogicalDisplayId::DEFAULT, {100, 100})) |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6715 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6716 | |
| 6717 | window->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6718 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6719 | |
| 6720 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 6721 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6722 | mFakePolicy->assertUserActivityPoked(); |
| 6723 | } |
| 6724 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6725 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6726 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6727 | sp<FakeWindowHandle> window = |
| 6728 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6729 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6730 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6731 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6732 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6733 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6734 | mDispatcher->waitForIdle(); |
| 6735 | |
| 6736 | window->assertNoEvents(); |
| 6737 | } |
| 6738 | |
| 6739 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 6740 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6741 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6742 | sp<FakeWindowHandle> window = |
| 6743 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6744 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6745 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6746 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6747 | |
| 6748 | // Send key |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6749 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6750 | // Send motion |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6751 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6752 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6753 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6754 | |
| 6755 | // Window should receive only the motion event |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6756 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6757 | window->assertNoEvents(); // Key event or focus event will not be received |
| 6758 | } |
| 6759 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6760 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 6761 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6762 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6763 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6764 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6765 | ui::LogicalDisplayId::DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6766 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6767 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6768 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6769 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6770 | ui::LogicalDisplayId::DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6771 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6772 | |
| 6773 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6774 | mDispatcher->onWindowInfosChanged( |
| 6775 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6776 | |
| 6777 | PointF pointInFirst = {300, 200}; |
| 6778 | PointF pointInSecond = {300, 600}; |
| 6779 | |
| 6780 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6781 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6782 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6783 | ui::LogicalDisplayId::DEFAULT, {pointInFirst})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6784 | // Only the first window should get the down event |
| 6785 | firstWindow->consumeMotionDown(); |
| 6786 | secondWindow->assertNoEvents(); |
| 6787 | |
| 6788 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6789 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6790 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6791 | {pointInFirst, pointInSecond})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6792 | // The first window gets a move and the second a down |
| 6793 | firstWindow->consumeMotionMove(); |
| 6794 | secondWindow->consumeMotionDown(); |
| 6795 | |
| 6796 | // Send pointer cancel to the second window |
| 6797 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6798 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6799 | ui::LogicalDisplayId::DEFAULT, {pointInFirst, pointInSecond}); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6800 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6801 | mDispatcher->notifyMotion(pointerUpMotionArgs); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6802 | // The first window gets move and the second gets cancel. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6803 | firstWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 6804 | secondWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6805 | |
| 6806 | // Send up event. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6807 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6808 | ui::LogicalDisplayId::DEFAULT)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6809 | // The first window gets up and the second gets nothing. |
| 6810 | firstWindow->consumeMotionUp(); |
| 6811 | secondWindow->assertNoEvents(); |
| 6812 | } |
| 6813 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6814 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 6815 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6816 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6817 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 6818 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6819 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6820 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 6821 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 6822 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 6823 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6824 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6825 | window->assertNoEvents(); |
| 6826 | mDispatcher->waitForIdle(); |
| 6827 | } |
| 6828 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6829 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 6830 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6831 | /** |
| 6832 | * Two entities that receive touch: A window, and a global monitor. |
| 6833 | * The touch goes to the window, and then the window disappears. |
| 6834 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 6835 | * for the monitor, as well. |
| 6836 | * 1. foregroundWindow |
| 6837 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 6838 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6839 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6840 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6841 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 6842 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6843 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6844 | FakeMonitorReceiver monitor = |
| 6845 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6846 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6847 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6848 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6849 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6850 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6851 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6852 | |
| 6853 | // Both the foreground window and the global monitor should receive the touch down |
| 6854 | window->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6855 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6856 | |
| 6857 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6858 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6859 | ui::LogicalDisplayId::DEFAULT, {110, 200})) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6860 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6861 | |
| 6862 | window->consumeMotionMove(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6863 | monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6864 | |
| 6865 | // Now the foreground window goes away |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6866 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6867 | window->consumeMotionCancel(); |
| 6868 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 6869 | |
| 6870 | // If more events come in, there will be no more foreground window to send them to. This will |
| 6871 | // cause a cancel for the monitor, as well. |
| 6872 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6873 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6874 | ui::LogicalDisplayId::DEFAULT, {120, 200})) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6875 | << "Injection should fail because the window was removed"; |
| 6876 | window->assertNoEvents(); |
| 6877 | // Global monitor now gets the cancel |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6878 | monitor.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6879 | } |
| 6880 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6881 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6882 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6883 | sp<FakeWindowHandle> window = |
| 6884 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6885 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6886 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6887 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6888 | FakeMonitorReceiver monitor = |
| 6889 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6890 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6891 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6892 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6893 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6894 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6895 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 6896 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6897 | } |
| 6898 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6899 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6900 | FakeMonitorReceiver monitor = |
| 6901 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6902 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6903 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6904 | sp<FakeWindowHandle> window = |
| 6905 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6906 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6907 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6908 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6909 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6910 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6911 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6912 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6913 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 6914 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6915 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6916 | // Pilfer pointers from the monitor. |
| 6917 | // This should not do anything and the window should continue to receive events. |
| 6918 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6919 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6920 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6921 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6922 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6923 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6924 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6925 | monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
| 6926 | window->consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6927 | } |
| 6928 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6929 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6930 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6931 | sp<FakeWindowHandle> window = |
| 6932 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6933 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6934 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6935 | window->setWindowOffset(20, 40); |
| 6936 | window->setWindowTransform(0, 1, -1, 0); |
| 6937 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6938 | FakeMonitorReceiver monitor = |
| 6939 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6940 | |
| 6941 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6942 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6943 | ui::LogicalDisplayId::DEFAULT)) |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6944 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6945 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6946 | std::unique_ptr<MotionEvent> event = monitor.consumeMotion(); |
| 6947 | ASSERT_NE(nullptr, event); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6948 | // Even though window has transform, gesture monitor must not. |
| 6949 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 6950 | } |
| 6951 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6952 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6953 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6954 | FakeMonitorReceiver monitor = |
| 6955 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6956 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6957 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6958 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6959 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6960 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 6961 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6962 | } |
| 6963 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6964 | /** |
| 6965 | * Two displays |
| 6966 | * The first monitor has a foreground window, a monitor |
| 6967 | * The second window has only one monitor. |
| 6968 | * We first inject a Down event into the first display, this injection should succeed and both |
| 6969 | * the foreground window and monitor should receive a down event, then inject a Down event into |
| 6970 | * the second display as well, this injection should fail, at this point, the first display |
| 6971 | * window and monitor should not receive a cancel or any other event. |
| 6972 | * Continue to inject Move and UP events to the first display, the events should be received |
| 6973 | * normally by the foreground window and monitor. |
| 6974 | */ |
| 6975 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCanceledWhenAnotherEmptyDisplayReceiveEvents) { |
| 6976 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6977 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 6978 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6979 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6980 | FakeMonitorReceiver monitor = |
| 6981 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6982 | FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 6983 | |
| 6984 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 6985 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6986 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6987 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6988 | << "The down event injected into the first display should succeed"; |
| 6989 | |
| 6990 | window->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6991 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6992 | |
| 6993 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6994 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 6995 | {100, 200})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6996 | << "The down event injected into the second display should fail since there's no " |
| 6997 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6998 | |
| 6999 | // Continue to inject event to first display. |
| 7000 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7001 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7002 | ui::LogicalDisplayId::DEFAULT, {110, 220})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7003 | << "The move event injected into the first display should succeed"; |
| 7004 | |
| 7005 | window->consumeMotionMove(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7006 | monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7007 | |
| 7008 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7009 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7010 | {110, 220})) |
| 7011 | << "The up event injected into the first display should succeed"; |
| 7012 | |
| 7013 | window->consumeMotionUp(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7014 | monitor.consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7015 | |
| 7016 | window->assertNoEvents(); |
| 7017 | monitor.assertNoEvents(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7018 | secondMonitor.assertNoEvents(); |
| 7019 | } |
| 7020 | |
| 7021 | /** |
| 7022 | * Two displays |
| 7023 | * There is a monitor and foreground window on each display. |
| 7024 | * First, we inject down events into each of the two displays, at this point, the foreground windows |
| 7025 | * and monitors on both displays should receive down events. |
| 7026 | * At this point, the foreground window of the second display goes away, the gone window should |
| 7027 | * receive the cancel event, and the other windows and monitors should not receive any events. |
| 7028 | * Inject a move event into the second display. At this point, the injection should fail because |
| 7029 | * the second display no longer has a foreground window. At this point, the monitor on the second |
| 7030 | * display should receive a cancel event, and any windows or monitors on the first display should |
| 7031 | * not receive any events, and any subsequent injection of events into the second display should |
| 7032 | * also fail. |
| 7033 | * Continue to inject events into the first display, and the events should all be injected |
| 7034 | * successfully and received normally. |
| 7035 | */ |
| 7036 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCancelWhenAnotherDisplayMonitorTouchCanceled) { |
| 7037 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7038 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 7039 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7040 | sp<FakeWindowHandle> secondWindow = |
| 7041 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondForeground", |
| 7042 | SECOND_DISPLAY_ID); |
| 7043 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7044 | FakeMonitorReceiver monitor = |
| 7045 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7046 | FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 7047 | |
| 7048 | // There is a foreground window on both displays. |
| 7049 | mDispatcher->onWindowInfosChanged({{*window->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
| 7050 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7051 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7052 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7053 | << "The down event injected into the first display should succeed"; |
| 7054 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7055 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 7056 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7057 | |
| 7058 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7059 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 7060 | {100, 200})) |
| 7061 | << "The down event injected into the second display should succeed"; |
| 7062 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7063 | secondWindow->consumeMotionDown(SECOND_DISPLAY_ID); |
| 7064 | secondMonitor.consumeMotionDown(SECOND_DISPLAY_ID); |
| 7065 | |
| 7066 | // Now second window is gone away. |
| 7067 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 7068 | |
| 7069 | // The gone window should receive a cancel, and the monitor on the second display should not |
| 7070 | // receive any events. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7071 | secondWindow->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 7072 | secondMonitor.assertNoEvents(); |
| 7073 | |
| 7074 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 7075 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7076 | SECOND_DISPLAY_ID, {110, 220})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7077 | << "The move event injected into the second display should fail because there's no " |
| 7078 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7079 | // Now the monitor on the second display should receive a cancel event. |
| 7080 | secondMonitor.consumeMotionCancel(SECOND_DISPLAY_ID); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7081 | |
| 7082 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7083 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7084 | ui::LogicalDisplayId::DEFAULT, {110, 200})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7085 | << "The move event injected into the first display should succeed"; |
| 7086 | |
| 7087 | window->consumeMotionMove(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7088 | monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7089 | |
| 7090 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7091 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 7092 | {110, 220})) |
| 7093 | << "The up event injected into the second display should fail because there's no " |
| 7094 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7095 | |
| 7096 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7097 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7098 | {110, 220})) |
| 7099 | << "The up event injected into the first display should succeed"; |
| 7100 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7101 | window->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
| 7102 | monitor.consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7103 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7104 | window->assertNoEvents(); |
| 7105 | monitor.assertNoEvents(); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7106 | secondWindow->assertNoEvents(); |
| 7107 | secondMonitor.assertNoEvents(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7108 | } |
| 7109 | |
| 7110 | /** |
| 7111 | * One display with transform |
| 7112 | * There is a foreground window and a monitor on the display |
| 7113 | * Inject down event and move event sequentially, the foreground window and monitor can receive down |
| 7114 | * event and move event, then let the foreground window go away, the foreground window receives |
| 7115 | * cancel event, inject move event again, the monitor receives cancel event, all the events received |
| 7116 | * by the monitor should be with the same transform as the display |
| 7117 | */ |
| 7118 | TEST_F(InputDispatcherMonitorTest, MonitorTouchCancelEventWithDisplayTransform) { |
| 7119 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7120 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 7121 | ui::LogicalDisplayId::DEFAULT); |
| 7122 | FakeMonitorReceiver monitor = |
| 7123 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7124 | |
| 7125 | ui::Transform transform; |
| 7126 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 7127 | |
| 7128 | gui::DisplayInfo displayInfo; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7129 | displayInfo.displayId = ui::LogicalDisplayId::DEFAULT; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7130 | displayInfo.transform = transform; |
| 7131 | |
| 7132 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
| 7133 | |
| 7134 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7135 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7136 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7137 | << "The down event injected should succeed"; |
| 7138 | |
| 7139 | window->consumeMotionDown(); |
| 7140 | std::unique_ptr<MotionEvent> downMotionEvent = monitor.consumeMotion(); |
| 7141 | EXPECT_EQ(transform, downMotionEvent->getTransform()); |
| 7142 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, downMotionEvent->getAction()); |
| 7143 | |
| 7144 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7145 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7146 | ui::LogicalDisplayId::DEFAULT, {110, 220})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7147 | << "The move event injected should succeed"; |
| 7148 | |
| 7149 | window->consumeMotionMove(); |
| 7150 | std::unique_ptr<MotionEvent> moveMotionEvent = monitor.consumeMotion(); |
| 7151 | EXPECT_EQ(transform, moveMotionEvent->getTransform()); |
| 7152 | EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, moveMotionEvent->getAction()); |
| 7153 | |
| 7154 | // Let foreground window gone |
| 7155 | mDispatcher->onWindowInfosChanged({{}, {displayInfo}, 0, 0}); |
| 7156 | |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7157 | // Foreground window should receive a cancel event, but not the monitor. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7158 | window->consumeMotionCancel(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7159 | |
| 7160 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 7161 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7162 | ui::LogicalDisplayId::DEFAULT, {110, 220})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7163 | << "The move event injected should failed"; |
| 7164 | // Now foreground should not receive any events, but monitor should receive a cancel event |
| 7165 | // with transform that same as display's display. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7166 | std::unique_ptr<MotionEvent> cancelMotionEvent = monitor.consumeMotion(); |
| 7167 | EXPECT_EQ(transform, cancelMotionEvent->getTransform()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7168 | EXPECT_EQ(ui::LogicalDisplayId::DEFAULT, cancelMotionEvent->getDisplayId()); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7169 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, cancelMotionEvent->getAction()); |
| 7170 | |
| 7171 | // Other event inject to this display should fail. |
| 7172 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 7173 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7174 | ui::LogicalDisplayId::DEFAULT, {110, 220})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7175 | << "The up event injected should fail because the touched window was removed"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7176 | window->assertNoEvents(); |
| 7177 | monitor.assertNoEvents(); |
| 7178 | } |
| 7179 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7180 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7181 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7182 | sp<FakeWindowHandle> window = |
| 7183 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7184 | ui::LogicalDisplayId::DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7185 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7186 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7187 | |
| 7188 | NotifyMotionArgs motionArgs = |
| 7189 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7190 | ui::LogicalDisplayId::DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7191 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7192 | mDispatcher->notifyMotion(motionArgs); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7193 | // Window should receive motion down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7194 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7195 | |
| 7196 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7197 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7198 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 7199 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 7200 | motionArgs.pointerCoords[0].getX() - 10); |
| 7201 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7202 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7203 | window->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7204 | } |
| 7205 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7206 | /** |
| 7207 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 7208 | * the device default right away. In the test scenario, we check both the default value, |
| 7209 | * and the action of enabling / disabling. |
| 7210 | */ |
| 7211 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7212 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7213 | sp<FakeWindowHandle> window = |
| 7214 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 7215 | ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7216 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7217 | |
| 7218 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7219 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7220 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7221 | |
| 7222 | SCOPED_TRACE("Check default value of touch mode"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7223 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7224 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7225 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7226 | |
| 7227 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7228 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7229 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7230 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7231 | |
| 7232 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7233 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7234 | /*hasPermission=*/true, ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7235 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7236 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7237 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7238 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7239 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7240 | |
| 7241 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7242 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7243 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7244 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7245 | |
| 7246 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7247 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7248 | /*hasPermission=*/true, ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7249 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7250 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7251 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7252 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7253 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7254 | |
| 7255 | window->assertNoEvents(); |
| 7256 | } |
| 7257 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7258 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7259 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7260 | sp<FakeWindowHandle> window = |
| 7261 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 7262 | ui::LogicalDisplayId::DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7263 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7264 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7265 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7266 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7267 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7268 | setFocusedWindow(window); |
| 7269 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7270 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7271 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7272 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 7273 | mDispatcher->notifyKey(keyArgs); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7274 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7275 | std::unique_ptr<KeyEvent> event = window->consumeKey(); |
| 7276 | ASSERT_NE(event, nullptr); |
| 7277 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7278 | ASSERT_NE(verified, nullptr); |
| 7279 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 7280 | |
| 7281 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 7282 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 7283 | ASSERT_EQ(keyArgs.source, verified->source); |
| 7284 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 7285 | |
| 7286 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 7287 | |
| 7288 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7289 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 7290 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7291 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 7292 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 7293 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 7294 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 7295 | } |
| 7296 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7297 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7298 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7299 | sp<FakeWindowHandle> window = |
| 7300 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 7301 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7302 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7303 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7304 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 7305 | ui::Transform transform; |
| 7306 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 7307 | |
| 7308 | gui::DisplayInfo displayInfo; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7309 | displayInfo.displayId = ui::LogicalDisplayId::DEFAULT; |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 7310 | displayInfo.transform = transform; |
| 7311 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 7312 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7313 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7314 | const NotifyMotionArgs motionArgs = |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7315 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7316 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7317 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7318 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7319 | std::unique_ptr<MotionEvent> event = window->consumeMotionEvent(); |
| 7320 | ASSERT_NE(nullptr, event); |
| 7321 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7322 | ASSERT_NE(verified, nullptr); |
| 7323 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 7324 | |
| 7325 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 7326 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 7327 | EXPECT_EQ(motionArgs.source, verified->source); |
| 7328 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 7329 | |
| 7330 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 7331 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 7332 | const vec2 rawXY = |
| 7333 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 7334 | motionArgs.pointerCoords[0].getXYValue()); |
| 7335 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 7336 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7337 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7338 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 7339 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7340 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 7341 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 7342 | } |
| 7343 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 7344 | /** |
| 7345 | * Ensure that separate calls to sign the same data are generating the same key. |
| 7346 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 7347 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 7348 | * tests. |
| 7349 | */ |
| 7350 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 7351 | KeyEvent event = getTestKeyEvent(); |
| 7352 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 7353 | |
| 7354 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 7355 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 7356 | ASSERT_EQ(hmac1, hmac2); |
| 7357 | } |
| 7358 | |
| 7359 | /** |
| 7360 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 7361 | */ |
| 7362 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 7363 | KeyEvent event = getTestKeyEvent(); |
| 7364 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 7365 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 7366 | |
| 7367 | verifiedEvent.deviceId += 1; |
| 7368 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7369 | |
| 7370 | verifiedEvent.source += 1; |
| 7371 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7372 | |
| 7373 | verifiedEvent.eventTimeNanos += 1; |
| 7374 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7375 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 7376 | verifiedEvent.displayId = ui::LogicalDisplayId{verifiedEvent.displayId.val() + 1}; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 7377 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7378 | |
| 7379 | verifiedEvent.action += 1; |
| 7380 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7381 | |
| 7382 | verifiedEvent.downTimeNanos += 1; |
| 7383 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7384 | |
| 7385 | verifiedEvent.flags += 1; |
| 7386 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7387 | |
| 7388 | verifiedEvent.keyCode += 1; |
| 7389 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7390 | |
| 7391 | verifiedEvent.scanCode += 1; |
| 7392 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7393 | |
| 7394 | verifiedEvent.metaState += 1; |
| 7395 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7396 | |
| 7397 | verifiedEvent.repeatCount += 1; |
| 7398 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7399 | } |
| 7400 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7401 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 7402 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7403 | sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 7404 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7405 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7406 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 7407 | ui::LogicalDisplayId::DEFAULT); |
| 7408 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7409 | |
| 7410 | // Top window is also focusable but is not granted focus. |
| 7411 | windowTop->setFocusable(true); |
| 7412 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7413 | mDispatcher->onWindowInfosChanged( |
| 7414 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7415 | setFocusedWindow(windowSecond); |
| 7416 | |
| 7417 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7418 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7419 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7420 | |
| 7421 | // Focused window should receive event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7422 | windowSecond->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7423 | windowTop->assertNoEvents(); |
| 7424 | } |
| 7425 | |
| 7426 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 7427 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7428 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
| 7429 | ui::LogicalDisplayId::DEFAULT); |
| 7430 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7431 | |
| 7432 | window->setFocusable(true); |
| 7433 | // Release channel for window is no longer valid. |
| 7434 | window->releaseChannel(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7435 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7436 | setFocusedWindow(window); |
| 7437 | |
| 7438 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7439 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7440 | |
| 7441 | // window channel is invalid, so it should not receive any input event. |
| 7442 | window->assertNoEvents(); |
| 7443 | } |
| 7444 | |
| 7445 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 7446 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7447 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
| 7448 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7449 | window->setFocusable(false); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7450 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7451 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7452 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7453 | setFocusedWindow(window); |
| 7454 | |
| 7455 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7456 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7457 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7458 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7459 | window->assertNoEvents(); |
| 7460 | } |
| 7461 | |
| 7462 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 7463 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7464 | sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 7465 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7466 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7467 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 7468 | ui::LogicalDisplayId::DEFAULT); |
| 7469 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7470 | |
| 7471 | windowTop->setFocusable(true); |
| 7472 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7473 | mDispatcher->onWindowInfosChanged( |
| 7474 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7475 | setFocusedWindow(windowTop); |
| 7476 | windowTop->consumeFocusEvent(true); |
| 7477 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 7478 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7479 | mDispatcher->onWindowInfosChanged( |
| 7480 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7481 | windowSecond->consumeFocusEvent(true); |
| 7482 | windowTop->consumeFocusEvent(false); |
| 7483 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7484 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7485 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7486 | |
| 7487 | // Focused window should receive event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7488 | windowSecond->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7489 | } |
| 7490 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 7491 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7492 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7493 | sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 7494 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7495 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7496 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 7497 | ui::LogicalDisplayId::DEFAULT); |
| 7498 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7499 | |
| 7500 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 7501 | windowSecond->setFocusable(false); |
| 7502 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7503 | mDispatcher->onWindowInfosChanged( |
| 7504 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 7505 | setFocusedWindow(windowTop); |
| 7506 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7507 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7508 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 7509 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7510 | |
| 7511 | // Event should be dropped. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7512 | windowTop->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7513 | windowSecond->assertNoEvents(); |
| 7514 | } |
| 7515 | |
| 7516 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 7517 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7518 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
| 7519 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7520 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7521 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7522 | ui::LogicalDisplayId::DEFAULT); |
| 7523 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7524 | |
| 7525 | window->setFocusable(true); |
| 7526 | previousFocusedWindow->setFocusable(true); |
| 7527 | window->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7528 | mDispatcher->onWindowInfosChanged( |
| 7529 | {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7530 | setFocusedWindow(previousFocusedWindow); |
| 7531 | previousFocusedWindow->consumeFocusEvent(true); |
| 7532 | |
| 7533 | // Requesting focus on invisible window takes focus from currently focused window. |
| 7534 | setFocusedWindow(window); |
| 7535 | previousFocusedWindow->consumeFocusEvent(false); |
| 7536 | |
| 7537 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7538 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7539 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7540 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7541 | |
| 7542 | // Window does not get focus event or key down. |
| 7543 | window->assertNoEvents(); |
| 7544 | |
| 7545 | // Window becomes visible. |
| 7546 | window->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7547 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7548 | |
| 7549 | // Window receives focus event. |
| 7550 | window->consumeFocusEvent(true); |
| 7551 | // Focused window receives key down. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7552 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7553 | } |
| 7554 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 7555 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 7556 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7557 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "window", |
| 7558 | ui::LogicalDisplayId::DEFAULT); |
| 7559 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 7560 | |
| 7561 | // window is granted focus. |
| 7562 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7563 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 7564 | setFocusedWindow(window); |
| 7565 | window->consumeFocusEvent(true); |
| 7566 | |
| 7567 | // When a display is removed window loses focus. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7568 | mDispatcher->displayRemoved(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 7569 | window->consumeFocusEvent(false); |
| 7570 | } |
| 7571 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7572 | /** |
| 7573 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 7574 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 7575 | * of the 'slipperyEnterWindow'. |
| 7576 | * |
| 7577 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 7578 | * a way so that the touched location is no longer covered by the top window. |
| 7579 | * |
| 7580 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 7581 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 7582 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 7583 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 7584 | * with ACTION_DOWN). |
| 7585 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 7586 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 7587 | * |
| 7588 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 7589 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 7590 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 7591 | * |
| 7592 | * In this test, we ensure that the event received by the bottom window has |
| 7593 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 7594 | */ |
| 7595 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 7596 | constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7597 | constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1}; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7598 | |
| 7599 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7600 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7601 | |
| 7602 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7603 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 7604 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7605 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7606 | // Make sure this one overlaps the bottom window |
| 7607 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 7608 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 7609 | // one. Windows with the same owner are not considered to be occluding each other. |
| 7610 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 7611 | |
| 7612 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7613 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 7614 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7615 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7616 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7617 | mDispatcher->onWindowInfosChanged( |
| 7618 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7619 | |
| 7620 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7621 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7622 | AINPUT_SOURCE_TOUCHSCREEN, |
| 7623 | ui::LogicalDisplayId::DEFAULT, {{50, 50}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7624 | slipperyExitWindow->consumeMotionDown(); |
| 7625 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7626 | mDispatcher->onWindowInfosChanged( |
| 7627 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7628 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7629 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7630 | AINPUT_SOURCE_TOUCHSCREEN, |
| 7631 | ui::LogicalDisplayId::DEFAULT, {{51, 51}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7632 | |
| 7633 | slipperyExitWindow->consumeMotionCancel(); |
| 7634 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7635 | slipperyEnterWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7636 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 7637 | } |
| 7638 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 7639 | /** |
| 7640 | * Two windows, one on the left and another on the right. The left window is slippery. The right |
| 7641 | * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the |
| 7642 | * touch moves from the left window into the right window, the gesture should continue to go to the |
| 7643 | * left window. Touch shouldn't slip because the right window can't receive touches. This test |
| 7644 | * reproduces a crash. |
| 7645 | */ |
| 7646 | TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { |
| 7647 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7648 | |
| 7649 | sp<FakeWindowHandle> leftSlipperyWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7650 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 7651 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 7652 | leftSlipperyWindow->setSlippery(true); |
| 7653 | leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7654 | |
| 7655 | sp<FakeWindowHandle> rightDropTouchesWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7656 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 7657 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 7658 | rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100)); |
| 7659 | rightDropTouchesWindow->setDropInput(true); |
| 7660 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7661 | mDispatcher->onWindowInfosChanged( |
| 7662 | {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 7663 | |
| 7664 | // Start touch in the left window |
| 7665 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7666 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7667 | .build()); |
| 7668 | leftSlipperyWindow->consumeMotionDown(); |
| 7669 | |
| 7670 | // And move it into the right window |
| 7671 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7672 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 7673 | .build()); |
| 7674 | |
| 7675 | // Since the right window isn't eligible to receive input, touch does not slip. |
| 7676 | // The left window continues to receive the gesture. |
| 7677 | leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 7678 | rightDropTouchesWindow->assertNoEvents(); |
| 7679 | } |
| 7680 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 7681 | /** |
| 7682 | * A single window is on screen first. Touch is injected into that window. Next, a second window |
| 7683 | * appears. Since the first window is slippery, touch will move from the first window to the second. |
| 7684 | */ |
| 7685 | TEST_F(InputDispatcherTest, InjectedTouchSlips) { |
| 7686 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7687 | sp<FakeWindowHandle> originalWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7688 | sp<FakeWindowHandle>::make(application, mDispatcher, "Original", |
| 7689 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 7690 | originalWindow->setFrame(Rect(0, 0, 200, 200)); |
| 7691 | originalWindow->setSlippery(true); |
| 7692 | |
| 7693 | sp<FakeWindowHandle> appearingWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7694 | sp<FakeWindowHandle>::make(application, mDispatcher, "Appearing", |
| 7695 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 7696 | appearingWindow->setFrame(Rect(0, 0, 200, 200)); |
| 7697 | |
| 7698 | mDispatcher->onWindowInfosChanged({{*originalWindow->getInfo()}, {}, 0, 0}); |
| 7699 | |
| 7700 | // Touch down on the original window |
| 7701 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7702 | injectMotionEvent(*mDispatcher, |
| 7703 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7704 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 7705 | .build())); |
| 7706 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 7707 | |
| 7708 | // Now, a new window appears. This could be, for example, a notification shade that appears |
| 7709 | // after user starts to drag down on the launcher window. |
| 7710 | mDispatcher->onWindowInfosChanged( |
| 7711 | {{*appearingWindow->getInfo(), *originalWindow->getInfo()}, {}, 0, 0}); |
| 7712 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7713 | injectMotionEvent(*mDispatcher, |
| 7714 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7715 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(110)) |
| 7716 | .build())); |
| 7717 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 7718 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 7719 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7720 | injectMotionEvent(*mDispatcher, |
| 7721 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7722 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 7723 | .build())); |
| 7724 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 7725 | |
| 7726 | originalWindow->assertNoEvents(); |
| 7727 | appearingWindow->assertNoEvents(); |
| 7728 | } |
| 7729 | |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7730 | /** |
| 7731 | * Three windows: |
| 7732 | * - left window, which has FLAG_SLIPPERY, so it supports slippery exit |
| 7733 | * - right window |
| 7734 | * - spy window |
| 7735 | * The three windows do not overlap. |
| 7736 | * |
| 7737 | * We have two devices reporting events: |
| 7738 | * - Device A reports ACTION_DOWN, which lands in the left window |
| 7739 | * - Device B reports ACTION_DOWN, which lands in the spy window. |
| 7740 | * - Now, device B reports ACTION_MOVE events which move to the right window. |
| 7741 | * |
| 7742 | * The right window should not receive any events because the spy window is not a foreground window, |
| 7743 | * and also it does not support slippery touches. |
| 7744 | */ |
| 7745 | TEST_F(InputDispatcherTest, MultiDeviceSpyWindowSlipTest) { |
| 7746 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7747 | sp<FakeWindowHandle> leftWindow = |
| 7748 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7749 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7750 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7751 | leftWindow->setSlippery(true); |
| 7752 | |
| 7753 | sp<FakeWindowHandle> rightWindow = |
| 7754 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7755 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7756 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 7757 | |
| 7758 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7759 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", |
| 7760 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7761 | spyWindow->setFrame(Rect(200, 0, 300, 100)); |
| 7762 | spyWindow->setSpy(true); |
| 7763 | spyWindow->setTrustedOverlay(true); |
| 7764 | |
| 7765 | mDispatcher->onWindowInfosChanged( |
| 7766 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *spyWindow->getInfo()}, {}, 0, 0}); |
| 7767 | |
| 7768 | const DeviceId deviceA = 9; |
| 7769 | const DeviceId deviceB = 3; |
| 7770 | |
| 7771 | // Tap on left window with device A |
| 7772 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7773 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7774 | .deviceId(deviceA) |
| 7775 | .build()); |
| 7776 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 7777 | |
| 7778 | // Tap on spy window with device B |
| 7779 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7780 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(50)) |
| 7781 | .deviceId(deviceB) |
| 7782 | .build()); |
| 7783 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 7784 | |
| 7785 | // Move to right window with device B. Touches should not slip to the right window, because spy |
| 7786 | // window is not a foreground window, and it does not have FLAG_SLIPPERY |
| 7787 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7788 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 7789 | .deviceId(deviceB) |
| 7790 | .build()); |
| 7791 | leftWindow->assertNoEvents(); |
| 7792 | rightWindow->assertNoEvents(); |
| 7793 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB))); |
| 7794 | } |
| 7795 | |
| 7796 | /** |
| 7797 | * Three windows arranged horizontally and without any overlap. |
| 7798 | * The left and right windows have FLAG_SLIPPERY. The middle window does not have any special flags. |
| 7799 | * |
| 7800 | * We have two devices reporting events: |
| 7801 | * - Device A reports ACTION_DOWN which lands in the left window |
| 7802 | * - Device B reports ACTION_DOWN which lands in the right window |
| 7803 | * - Device B reports ACTION_MOVE that shifts to the middle window. |
| 7804 | * This should cause touches for Device B to slip from the right window to the middle window. |
| 7805 | * The right window should receive ACTION_CANCEL for device B and the |
| 7806 | * middle window should receive down event for Device B. |
| 7807 | * If device B reports more ACTION_MOVE events, the middle window should receive remaining events. |
| 7808 | */ |
| 7809 | TEST_F(InputDispatcherTest, MultiDeviceSlipperyWindowTest) { |
Siarhei Vishniakou | dd56df1 | 2024-05-20 14:56:38 -0700 | [diff] [blame] | 7810 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7811 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7812 | sp<FakeWindowHandle> leftWindow = |
| 7813 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7814 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7815 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7816 | leftWindow->setSlippery(true); |
| 7817 | |
| 7818 | sp<FakeWindowHandle> middleWindow = |
| 7819 | sp<FakeWindowHandle>::make(application, mDispatcher, "middle window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7820 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7821 | middleWindow->setFrame(Rect(100, 0, 200, 100)); |
| 7822 | |
| 7823 | sp<FakeWindowHandle> rightWindow = |
| 7824 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7825 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7826 | rightWindow->setFrame(Rect(200, 0, 300, 100)); |
| 7827 | rightWindow->setSlippery(true); |
| 7828 | |
| 7829 | mDispatcher->onWindowInfosChanged( |
| 7830 | {{*leftWindow->getInfo(), *middleWindow->getInfo(), *rightWindow->getInfo()}, |
| 7831 | {}, |
| 7832 | 0, |
| 7833 | 0}); |
| 7834 | |
| 7835 | const DeviceId deviceA = 9; |
| 7836 | const DeviceId deviceB = 3; |
| 7837 | |
| 7838 | // Tap on left window with device A |
| 7839 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7840 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7841 | .deviceId(deviceA) |
| 7842 | .build()); |
| 7843 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 7844 | |
| 7845 | // Tap on right window with device B |
| 7846 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7847 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(50)) |
| 7848 | .deviceId(deviceB) |
| 7849 | .build()); |
| 7850 | rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 7851 | |
| 7852 | // Move to middle window with device B. Touches should slip to middle window, because right |
| 7853 | // window is a foreground window that's associated with device B and has FLAG_SLIPPERY. |
| 7854 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7855 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 7856 | .deviceId(deviceB) |
| 7857 | .build()); |
| 7858 | rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB))); |
| 7859 | middleWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 7860 | |
| 7861 | // Move to middle window with device A. Touches should slip to middle window, because left |
| 7862 | // window is a foreground window that's associated with device A and has FLAG_SLIPPERY. |
| 7863 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7864 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 7865 | .deviceId(deviceA) |
| 7866 | .build()); |
| 7867 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceA))); |
| 7868 | middleWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 7869 | |
| 7870 | // Ensure that middle window can receive the remaining move events. |
| 7871 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7872 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(51)) |
| 7873 | .deviceId(deviceB) |
| 7874 | .build()); |
| 7875 | leftWindow->assertNoEvents(); |
| 7876 | middleWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB))); |
| 7877 | rightWindow->assertNoEvents(); |
| 7878 | } |
| 7879 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7880 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7881 | using Uid = gui::Uid; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7882 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7883 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7884 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 7885 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7886 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 7887 | leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7888 | |
| 7889 | sp<FakeWindowHandle> rightSpy = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7890 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", |
| 7891 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7892 | rightSpy->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 7893 | rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7894 | rightSpy->setSpy(true); |
| 7895 | rightSpy->setTrustedOverlay(true); |
| 7896 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7897 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 7898 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7899 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 7900 | rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7901 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7902 | mDispatcher->onWindowInfosChanged( |
| 7903 | {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7904 | |
| 7905 | // Touch in the left window |
| 7906 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7907 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7908 | .build()); |
| 7909 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown()); |
| 7910 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7911 | ASSERT_NO_FATAL_FAILURE( |
| 7912 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7913 | |
| 7914 | // Touch another finger over the right windows |
| 7915 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7916 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7917 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 7918 | .build()); |
| 7919 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown()); |
| 7920 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown()); |
| 7921 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove()); |
| 7922 | mDispatcher->waitForIdle(); |
| 7923 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7924 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, |
| 7925 | {Uid{101}, Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7926 | |
| 7927 | // Release finger over left window. The UP actions are not treated as device interaction. |
| 7928 | // The windows that did not receive the UP pointer will receive MOVE events, but since this |
| 7929 | // is part of the UP action, we do not treat this as device interaction. |
| 7930 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7931 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7932 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 7933 | .build()); |
| 7934 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp()); |
| 7935 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 7936 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 7937 | mDispatcher->waitForIdle(); |
| 7938 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 7939 | |
| 7940 | // Move remaining finger |
| 7941 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7942 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 7943 | .build()); |
| 7944 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 7945 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 7946 | mDispatcher->waitForIdle(); |
| 7947 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7948 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7949 | |
| 7950 | // Release all fingers |
| 7951 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7952 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 7953 | .build()); |
| 7954 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp()); |
| 7955 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp()); |
| 7956 | mDispatcher->waitForIdle(); |
| 7957 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 7958 | } |
| 7959 | |
| 7960 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) { |
| 7961 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7962 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7963 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 7964 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7965 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 7966 | window->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7967 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7968 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7969 | setFocusedWindow(window); |
| 7970 | ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true)); |
| 7971 | |
| 7972 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7973 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT)); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7974 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7975 | ASSERT_NO_FATAL_FAILURE( |
| 7976 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7977 | |
| 7978 | // The UP actions are not treated as device interaction. |
| 7979 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7980 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT)); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7981 | mDispatcher->waitForIdle(); |
| 7982 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 7983 | } |
| 7984 | |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7985 | TEST_F(InputDispatcherTest, HoverEnterExitSynthesisUsesNewEventId) { |
| 7986 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7987 | |
| 7988 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7989 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7990 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7991 | sp<FakeWindowHandle> right = |
| 7992 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 7993 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7994 | right->setFrame(Rect(100, 0, 200, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7995 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", |
| 7996 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7997 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 7998 | spy->setTrustedOverlay(true); |
| 7999 | spy->setSpy(true); |
| 8000 | |
| 8001 | mDispatcher->onWindowInfosChanged( |
| 8002 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 8003 | |
| 8004 | // Send hover move to the left window, and ensure hover enter is synthesized with a new eventId. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8005 | NotifyMotionArgs notifyArgs = |
| 8006 | generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 8007 | ui::LogicalDisplayId::DEFAULT, {PointF{50, 50}}); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8008 | mDispatcher->notifyMotion(notifyArgs); |
| 8009 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8010 | std::unique_ptr<MotionEvent> leftEnter = left->consumeMotionEvent( |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8011 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), Not(WithEventId(notifyArgs.id)), |
| 8012 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8013 | ASSERT_NE(nullptr, leftEnter); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8014 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 8015 | Not(WithEventId(notifyArgs.id)), |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8016 | Not(WithEventId(leftEnter->getId())), |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8017 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 8018 | |
| 8019 | // Send move to the right window, and ensure hover exit and enter are synthesized with new ids. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8020 | notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 8021 | ui::LogicalDisplayId::DEFAULT, {PointF{150, 50}}); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8022 | mDispatcher->notifyMotion(notifyArgs); |
| 8023 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8024 | std::unique_ptr<MotionEvent> leftExit = left->consumeMotionEvent( |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8025 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), Not(WithEventId(notifyArgs.id)), |
| 8026 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8027 | ASSERT_NE(nullptr, leftExit); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8028 | right->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 8029 | Not(WithEventId(notifyArgs.id)), |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8030 | Not(WithEventId(leftExit->getId())), |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8031 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 8032 | |
| 8033 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithEventId(notifyArgs.id))); |
| 8034 | } |
| 8035 | |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 8036 | /** |
| 8037 | * When a device reports a DOWN event, which lands in a window that supports splits, and then the |
| 8038 | * device then reports a POINTER_DOWN, which lands in the location of a non-existing window, then |
| 8039 | * the previous window should receive this event and not be dropped. |
| 8040 | */ |
| 8041 | TEST_F(InputDispatcherMultiDeviceTest, SingleDevicePointerDownEventRetentionWithoutWindowTarget) { |
| 8042 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8043 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 8044 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 8045 | window->setFrame(Rect(0, 0, 100, 100)); |
| 8046 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 8047 | |
| 8048 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8049 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8050 | .build()); |
| 8051 | |
| 8052 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 8053 | |
| 8054 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8055 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8056 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 8057 | .build()); |
| 8058 | |
| 8059 | window->consumeMotionEvent(AllOf(WithMotionAction(POINTER_1_DOWN))); |
| 8060 | } |
| 8061 | |
| 8062 | /** |
| 8063 | * When deviceA reports a DOWN event, which lands in a window that supports splits, and then deviceB |
| 8064 | * also reports a DOWN event, which lands in the location of a non-existing window, then the |
| 8065 | * previous window should receive deviceB's event and it should be dropped. |
| 8066 | */ |
| 8067 | TEST_F(InputDispatcherMultiDeviceTest, SecondDeviceDownEventDroppedWithoutWindowTarget) { |
| 8068 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8069 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 8070 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 8071 | window->setFrame(Rect(0, 0, 100, 100)); |
| 8072 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 8073 | |
| 8074 | const DeviceId deviceA = 9; |
| 8075 | const DeviceId deviceB = 3; |
| 8076 | |
| 8077 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8078 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8079 | .deviceId(deviceA) |
| 8080 | .build()); |
| 8081 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 8082 | |
| 8083 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8084 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
| 8085 | .deviceId(deviceB) |
| 8086 | .build()); |
| 8087 | window->assertNoEvents(); |
| 8088 | } |
| 8089 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 8090 | class InputDispatcherFallbackKeyTest : public InputDispatcherTest { |
| 8091 | protected: |
| 8092 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 8093 | sp<FakeWindowHandle> mWindow; |
| 8094 | |
| 8095 | virtual void SetUp() override { |
| 8096 | InputDispatcherTest::SetUp(); |
| 8097 | |
| 8098 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 8099 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8100 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Window", |
| 8101 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 8102 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
| 8103 | |
| 8104 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 8105 | setFocusedWindow(mWindow); |
| 8106 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeFocusEvent(/*hasFocus=*/true)); |
| 8107 | } |
| 8108 | |
| 8109 | void setFallback(int32_t keycode) { |
| 8110 | mFakePolicy->setUnhandledKeyHandler([keycode](const KeyEvent& event) { |
| 8111 | return KeyEventBuilder(event).keyCode(keycode).build(); |
| 8112 | }); |
| 8113 | } |
| 8114 | |
| 8115 | void consumeKey(bool handled, const ::testing::Matcher<KeyEvent>& matcher) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8116 | std::unique_ptr<KeyEvent> event = mWindow->consumeKey(handled); |
| 8117 | ASSERT_NE(nullptr, event); |
| 8118 | ASSERT_THAT(*event, matcher); |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 8119 | } |
| 8120 | }; |
| 8121 | |
| 8122 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotNotifiedForHandledKey) { |
| 8123 | mDispatcher->notifyKey( |
| 8124 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8125 | consumeKey(/*handled=*/true, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 8126 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8127 | } |
| 8128 | |
| 8129 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotifiedForUnhandledKey) { |
| 8130 | mDispatcher->notifyKey( |
| 8131 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8132 | consumeKey(/*handled=*/false, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 8133 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8134 | } |
| 8135 | |
| 8136 | TEST_F(InputDispatcherFallbackKeyTest, NoFallbackRequestedByPolicy) { |
| 8137 | mDispatcher->notifyKey( |
| 8138 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8139 | |
| 8140 | // Do not handle this key event. |
| 8141 | consumeKey(/*handled=*/false, |
| 8142 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8143 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8144 | |
| 8145 | // Since the policy did not request any fallback to be generated, ensure there are no events. |
| 8146 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8147 | } |
| 8148 | |
| 8149 | TEST_F(InputDispatcherFallbackKeyTest, FallbackDispatchForUnhandledKey) { |
| 8150 | setFallback(AKEYCODE_B); |
| 8151 | mDispatcher->notifyKey( |
| 8152 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8153 | |
| 8154 | // Do not handle this key event. |
| 8155 | consumeKey(/*handled=*/false, |
| 8156 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8157 | |
| 8158 | // Since the key was not handled, ensure the fallback event was dispatched instead. |
| 8159 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8160 | consumeKey(/*handled=*/true, |
| 8161 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8162 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8163 | |
| 8164 | // Release the original key, and ensure the fallback key is also released. |
| 8165 | mDispatcher->notifyKey( |
| 8166 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8167 | consumeKey(/*handled=*/false, |
| 8168 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8169 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8170 | consumeKey(/*handled=*/true, |
| 8171 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8172 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8173 | |
| 8174 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8175 | mWindow->assertNoEvents(); |
| 8176 | } |
| 8177 | |
| 8178 | TEST_F(InputDispatcherFallbackKeyTest, AppHandlesPreviouslyUnhandledKey) { |
| 8179 | setFallback(AKEYCODE_B); |
| 8180 | mDispatcher->notifyKey( |
| 8181 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8182 | |
| 8183 | // Do not handle this key event, but handle the fallback. |
| 8184 | consumeKey(/*handled=*/false, |
| 8185 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8186 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8187 | consumeKey(/*handled=*/true, |
| 8188 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8189 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8190 | |
| 8191 | // Release the original key, and ensure the fallback key is also released. |
| 8192 | mDispatcher->notifyKey( |
| 8193 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8194 | // But this time, the app handles the original key. |
| 8195 | consumeKey(/*handled=*/true, |
| 8196 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8197 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8198 | // Ensure the fallback key is canceled. |
| 8199 | consumeKey(/*handled=*/true, |
| 8200 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8201 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 8202 | |
| 8203 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8204 | mWindow->assertNoEvents(); |
| 8205 | } |
| 8206 | |
| 8207 | TEST_F(InputDispatcherFallbackKeyTest, AppDoesNotHandleFallback) { |
| 8208 | setFallback(AKEYCODE_B); |
| 8209 | mDispatcher->notifyKey( |
| 8210 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8211 | |
| 8212 | // Do not handle this key event. |
| 8213 | consumeKey(/*handled=*/false, |
| 8214 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8215 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8216 | // App does not handle the fallback either, so ensure another fallback is not generated. |
| 8217 | setFallback(AKEYCODE_C); |
| 8218 | consumeKey(/*handled=*/false, |
| 8219 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8220 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8221 | |
| 8222 | // Release the original key, and ensure the fallback key is also released. |
| 8223 | setFallback(AKEYCODE_B); |
| 8224 | mDispatcher->notifyKey( |
| 8225 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8226 | consumeKey(/*handled=*/false, |
| 8227 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8228 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8229 | consumeKey(/*handled=*/false, |
| 8230 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8231 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8232 | |
| 8233 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8234 | mWindow->assertNoEvents(); |
| 8235 | } |
| 8236 | |
| 8237 | TEST_F(InputDispatcherFallbackKeyTest, InconsistentPolicyCancelsFallback) { |
| 8238 | setFallback(AKEYCODE_B); |
| 8239 | mDispatcher->notifyKey( |
| 8240 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8241 | |
| 8242 | // Do not handle this key event, so fallback is generated. |
| 8243 | consumeKey(/*handled=*/false, |
| 8244 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8245 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8246 | consumeKey(/*handled=*/true, |
| 8247 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8248 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8249 | |
| 8250 | // Release the original key, but assume the policy is misbehaving and it |
| 8251 | // generates an inconsistent fallback to the one from the DOWN event. |
| 8252 | setFallback(AKEYCODE_C); |
| 8253 | mDispatcher->notifyKey( |
| 8254 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8255 | consumeKey(/*handled=*/false, |
| 8256 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8257 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8258 | // Ensure the fallback key reported before as DOWN is canceled due to the inconsistency. |
| 8259 | consumeKey(/*handled=*/true, |
| 8260 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8261 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 8262 | |
| 8263 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8264 | mWindow->assertNoEvents(); |
| 8265 | } |
| 8266 | |
| 8267 | TEST_F(InputDispatcherFallbackKeyTest, CanceledKeyCancelsFallback) { |
| 8268 | setFallback(AKEYCODE_B); |
| 8269 | mDispatcher->notifyKey( |
| 8270 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8271 | |
| 8272 | // Do not handle this key event, so fallback is generated. |
| 8273 | consumeKey(/*handled=*/false, |
| 8274 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8275 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8276 | consumeKey(/*handled=*/true, |
| 8277 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8278 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8279 | |
| 8280 | // The original key is canceled. |
| 8281 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
| 8282 | .keyCode(AKEYCODE_A) |
| 8283 | .addFlag(AKEY_EVENT_FLAG_CANCELED) |
| 8284 | .build()); |
| 8285 | consumeKey(/*handled=*/false, |
| 8286 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), |
| 8287 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 8288 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8289 | // Ensure the fallback key is also canceled due to the original key being canceled. |
| 8290 | consumeKey(/*handled=*/true, |
| 8291 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8292 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 8293 | |
| 8294 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8295 | mWindow->assertNoEvents(); |
| 8296 | } |
| 8297 | |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 8298 | TEST_F(InputDispatcherFallbackKeyTest, InputChannelRemovedDuringPolicyCall) { |
Prabir Pradhan | b13da8f | 2024-01-09 23:10:13 +0000 | [diff] [blame] | 8299 | setFallback(AKEYCODE_B); |
| 8300 | mDispatcher->notifyKey( |
| 8301 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8302 | |
| 8303 | // Do not handle this key event. |
| 8304 | consumeKey(/*handled=*/false, |
| 8305 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8306 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8307 | consumeKey(/*handled=*/true, |
| 8308 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8309 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8310 | |
| 8311 | mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) { |
| 8312 | // When the unhandled key is reported to the policy next, remove the input channel. |
| 8313 | mDispatcher->removeInputChannel(mWindow->getToken()); |
| 8314 | return KeyEventBuilder(event).keyCode(AKEYCODE_B).build(); |
| 8315 | }); |
| 8316 | // Release the original key, and let the app now handle the previously unhandled key. |
| 8317 | // This should result in the previously generated fallback key to be cancelled. |
| 8318 | // Since the policy was notified of the unhandled DOWN event earlier, it will also be notified |
| 8319 | // of the UP event for consistency. The Dispatcher calls into the policy from its own thread |
| 8320 | // without holding the lock, because it need to synchronously fetch the fallback key. While in |
| 8321 | // the policy call, we will now remove the input channel. Once the policy call returns, the |
| 8322 | // Dispatcher will no longer have a channel to send cancellation events to. Ensure this does |
| 8323 | // not cause any crashes. |
| 8324 | mDispatcher->notifyKey( |
| 8325 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8326 | consumeKey(/*handled=*/true, |
| 8327 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8328 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8329 | } |
| 8330 | |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 8331 | TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedDuringPolicyCall) { |
| 8332 | setFallback(AKEYCODE_B); |
| 8333 | mDispatcher->notifyKey( |
| 8334 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8335 | |
| 8336 | // Do not handle this key event. |
| 8337 | consumeKey(/*handled=*/false, |
| 8338 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8339 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8340 | consumeKey(/*handled=*/true, |
| 8341 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8342 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8343 | |
| 8344 | mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) { |
| 8345 | // When the unhandled key is reported to the policy next, remove the window. |
| 8346 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 8347 | return KeyEventBuilder(event).keyCode(AKEYCODE_B).build(); |
| 8348 | }); |
| 8349 | // Release the original key, which the app will not handle. When this unhandled key is reported |
| 8350 | // to the policy, the window will be removed. |
| 8351 | mDispatcher->notifyKey( |
| 8352 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8353 | consumeKey(/*handled=*/false, |
| 8354 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8355 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8356 | |
| 8357 | // Since the window was removed, it loses focus, and the channel state will be reset. |
| 8358 | consumeKey(/*handled=*/true, |
| 8359 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8360 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 8361 | mWindow->consumeFocusEvent(false); |
| 8362 | mWindow->assertNoEvents(); |
| 8363 | } |
| 8364 | |
| 8365 | TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedWhileAwaitingFinishedSignal) { |
| 8366 | setFallback(AKEYCODE_B); |
| 8367 | mDispatcher->notifyKey( |
| 8368 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8369 | |
| 8370 | // Do not handle this key event. |
| 8371 | consumeKey(/*handled=*/false, |
| 8372 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8373 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8374 | const auto [seq, event] = mWindow->receiveEvent(); |
| 8375 | ASSERT_TRUE(seq.has_value() && event != nullptr) << "Failed to receive fallback event"; |
| 8376 | ASSERT_EQ(event->getType(), InputEventType::KEY); |
| 8377 | ASSERT_THAT(static_cast<const KeyEvent&>(*event), |
| 8378 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8379 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8380 | |
| 8381 | // Remove the window now, which should generate a cancellations and make the window lose focus. |
| 8382 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 8383 | consumeKey(/*handled=*/true, |
| 8384 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), |
| 8385 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 8386 | consumeKey(/*handled=*/true, |
| 8387 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8388 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 8389 | mWindow->consumeFocusEvent(false); |
| 8390 | |
| 8391 | // Finish the event by reporting it as handled. |
| 8392 | mWindow->finishEvent(*seq); |
| 8393 | mWindow->assertNoEvents(); |
| 8394 | } |
| 8395 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8396 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 8397 | protected: |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 8398 | static constexpr std::chrono::nanoseconds KEY_REPEAT_TIMEOUT = 40ms; |
| 8399 | static constexpr std::chrono::nanoseconds KEY_REPEAT_DELAY = 40ms; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8400 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8401 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8402 | sp<FakeWindowHandle> mWindow; |
| 8403 | |
| 8404 | virtual void SetUp() override { |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 8405 | InputDispatcherTest::SetUp(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8406 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 8407 | mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8408 | setUpWindow(); |
| 8409 | } |
| 8410 | |
| 8411 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8412 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8413 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", |
| 8414 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8415 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8416 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8417 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8418 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8419 | mWindow->consumeFocusEvent(true); |
| 8420 | } |
| 8421 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8422 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8423 | NotifyKeyArgs keyArgs = |
| 8424 | generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8425 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8426 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8427 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8428 | |
| 8429 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8430 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8431 | } |
| 8432 | |
| 8433 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 8434 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8435 | mWindow->consumeKeyEvent( |
| 8436 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount))); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8437 | } |
| 8438 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8439 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8440 | NotifyKeyArgs keyArgs = |
| 8441 | generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8442 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8443 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8444 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8445 | |
| 8446 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8447 | mWindow->consumeKeyUp(ui::LogicalDisplayId::DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8448 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8449 | } |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 8450 | |
| 8451 | void injectKeyRepeat(int32_t repeatCount) { |
| 8452 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8453 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, repeatCount, |
| 8454 | ui::LogicalDisplayId::DEFAULT)) |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 8455 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8456 | } |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8457 | }; |
| 8458 | |
| 8459 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8460 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8461 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 8462 | expectKeyRepeatOnce(repeatCount); |
| 8463 | } |
| 8464 | } |
| 8465 | |
| 8466 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8467 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8468 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 8469 | expectKeyRepeatOnce(repeatCount); |
| 8470 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8471 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8472 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8473 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 8474 | expectKeyRepeatOnce(repeatCount); |
| 8475 | } |
| 8476 | } |
| 8477 | |
| 8478 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8479 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 8480 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 8481 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8482 | mWindow->assertNoEvents(); |
| 8483 | } |
| 8484 | |
| 8485 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8486 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 8487 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 8488 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 8489 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8490 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8491 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8492 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8493 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 8494 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8495 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8496 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8497 | mWindow->assertNoEvents(); |
| 8498 | } |
| 8499 | |
| 8500 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8501 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 8502 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 8503 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 8504 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8505 | // Device 2 which holds the key repeating goes up, expect the repeating to stop. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8506 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8507 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8508 | mWindow->assertNoEvents(); |
| 8509 | } |
| 8510 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 8511 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 8512 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8513 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8514 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8515 | mWindow->consumeKeyUp(ui::LogicalDisplayId::DEFAULT, |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 8516 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 8517 | mWindow->assertNoEvents(); |
| 8518 | } |
| 8519 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8520 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 8521 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8522 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8523 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8524 | std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey(); |
| 8525 | ASSERT_NE(nullptr, repeatEvent); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8526 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8527 | IdGenerator::getSource(repeatEvent->getId())); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8528 | } |
| 8529 | } |
| 8530 | |
| 8531 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 8532 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8533 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8534 | |
| 8535 | std::unordered_set<int32_t> idSet; |
| 8536 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8537 | std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey(); |
| 8538 | ASSERT_NE(nullptr, repeatEvent); |
| 8539 | int32_t id = repeatEvent->getId(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8540 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 8541 | idSet.insert(id); |
| 8542 | } |
| 8543 | } |
| 8544 | |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 8545 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_CorrectRepeatCountWhenInjectKeyRepeat) { |
| 8546 | injectKeyRepeat(0); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8547 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 8548 | for (int32_t repeatCount = 1; repeatCount <= 2; ++repeatCount) { |
| 8549 | expectKeyRepeatOnce(repeatCount); |
| 8550 | } |
| 8551 | injectKeyRepeat(1); |
| 8552 | // Expect repeatCount to be 3 instead of 1 |
| 8553 | expectKeyRepeatOnce(3); |
| 8554 | } |
| 8555 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8556 | /* Test InputDispatcher for MultiDisplay */ |
| 8557 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 8558 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 8559 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8560 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8561 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8562 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8563 | windowInPrimary = sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", |
| 8564 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8565 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8566 | // Set focus window for primary display, but focused display would be second one. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8567 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8568 | windowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8569 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
| 8570 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8571 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8572 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8573 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8574 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8575 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8576 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8577 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8578 | // Set focus display to second one. |
| 8579 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 8580 | // Set focus window for second display. |
| 8581 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8582 | windowInSecondary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8583 | mDispatcher->onWindowInfosChanged( |
| 8584 | {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8585 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8586 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8587 | } |
| 8588 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 8589 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8590 | InputDispatcherTest::TearDown(); |
| 8591 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8592 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8593 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8594 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8595 | windowInSecondary.clear(); |
| 8596 | } |
| 8597 | |
| 8598 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8599 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8600 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8601 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8602 | sp<FakeWindowHandle> windowInSecondary; |
| 8603 | }; |
| 8604 | |
| 8605 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 8606 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8607 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8608 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 8609 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8610 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8611 | windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8612 | windowInSecondary->assertNoEvents(); |
| 8613 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8614 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8615 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8616 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8617 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8618 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 8619 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8620 | } |
| 8621 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8622 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 8623 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 8624 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8625 | injectKeyDownNoRepeat(*mDispatcher, ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8626 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8627 | windowInPrimary->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 8628 | windowInSecondary->assertNoEvents(); |
| 8629 | |
| 8630 | // Test inject a key down without display id specified. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8631 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8632 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8633 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8634 | windowInSecondary->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8635 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8636 | // Remove all windows in secondary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8637 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8638 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8639 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8640 | windowInSecondary->consumeKeyUp(ui::LogicalDisplayId::INVALID, AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8641 | |
| 8642 | // Test inject a key down, should timeout because of no target window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8643 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8644 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8645 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8646 | windowInSecondary->assertNoEvents(); |
| 8647 | } |
| 8648 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8649 | // Test per-display input monitors for motion event. |
| 8650 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8651 | FakeMonitorReceiver monitorInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8652 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8653 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 8654 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8655 | |
| 8656 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8657 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8658 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 8659 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8660 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8661 | windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 8662 | monitorInPrimary.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8663 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8664 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8665 | |
| 8666 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8667 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8668 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8669 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8670 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8671 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 8672 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8673 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8674 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 8675 | // Lift up the touch from the second display |
| 8676 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8677 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 8678 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8679 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 8680 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 8681 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8682 | // Test inject a non-pointer motion event. |
| 8683 | // If specific a display, it will dispatch to the focused window of particular display, |
| 8684 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8685 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8686 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, |
| 8687 | ui::LogicalDisplayId::INVALID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8688 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8689 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8690 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8691 | windowInSecondary->consumeMotionDown(ui::LogicalDisplayId::INVALID); |
| 8692 | monitorInSecondary.consumeMotionDown(ui::LogicalDisplayId::INVALID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8693 | } |
| 8694 | |
| 8695 | // Test per-display input monitors for key event. |
| 8696 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8697 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8698 | FakeMonitorReceiver monitorInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8699 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8700 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 8701 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8702 | |
| 8703 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8704 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8705 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8706 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8707 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8708 | windowInSecondary->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
| 8709 | monitorInSecondary.consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8710 | } |
| 8711 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8712 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 8713 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8714 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", |
| 8715 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8716 | secondWindowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8717 | mDispatcher->onWindowInfosChanged( |
| 8718 | {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(), |
| 8719 | *windowInSecondary->getInfo()}, |
| 8720 | {}, |
| 8721 | 0, |
| 8722 | 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8723 | setFocusedWindow(secondWindowInPrimary); |
| 8724 | windowInPrimary->consumeFocusEvent(false); |
| 8725 | secondWindowInPrimary->consumeFocusEvent(true); |
| 8726 | |
| 8727 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8728 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8729 | injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8730 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8731 | windowInPrimary->assertNoEvents(); |
| 8732 | windowInSecondary->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8733 | secondWindowInPrimary->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8734 | } |
| 8735 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8736 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 8737 | FakeMonitorReceiver monitorInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8738 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8739 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 8740 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8741 | |
| 8742 | // Test touch down on primary display. |
| 8743 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8744 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 8745 | ui::LogicalDisplayId::DEFAULT)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8746 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8747 | windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 8748 | monitorInPrimary.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8749 | |
| 8750 | // Test touch down on second display. |
| 8751 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8752 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8753 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8754 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 8755 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 8756 | |
| 8757 | // Trigger cancel touch. |
| 8758 | mDispatcher->cancelCurrentTouch(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8759 | windowInPrimary->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT); |
| 8760 | monitorInPrimary.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8761 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 8762 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 8763 | |
| 8764 | // Test inject a move motion event, no window/monitor should receive the event. |
| 8765 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8766 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8767 | ui::LogicalDisplayId::DEFAULT, {110, 200})) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8768 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 8769 | windowInPrimary->assertNoEvents(); |
| 8770 | monitorInPrimary.assertNoEvents(); |
| 8771 | |
| 8772 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8773 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8774 | SECOND_DISPLAY_ID, {110, 200})) |
| 8775 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 8776 | windowInSecondary->assertNoEvents(); |
| 8777 | monitorInSecondary.assertNoEvents(); |
| 8778 | } |
| 8779 | |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8780 | /** |
| 8781 | * Send a key to the primary display and to the secondary display. |
| 8782 | * Then cause the key on the primary display to be canceled by sending in a stale key. |
| 8783 | * Ensure that the key on the primary display is canceled, and that the key on the secondary display |
| 8784 | * does not get canceled. |
| 8785 | */ |
| 8786 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture) { |
| 8787 | // Send a key down on primary display |
| 8788 | mDispatcher->notifyKey( |
| 8789 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8790 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8791 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 8792 | .build()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8793 | windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), |
| 8794 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8795 | windowInSecondary->assertNoEvents(); |
| 8796 | |
| 8797 | // Send a key down on second display |
| 8798 | mDispatcher->notifyKey( |
| 8799 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 8800 | .displayId(SECOND_DISPLAY_ID) |
| 8801 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 8802 | .build()); |
| 8803 | windowInSecondary->consumeKeyEvent( |
| 8804 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID))); |
| 8805 | windowInPrimary->assertNoEvents(); |
| 8806 | |
| 8807 | // Send a valid key up event on primary display that will be dropped because it is stale |
| 8808 | NotifyKeyArgs staleKeyUp = |
| 8809 | KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8810 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8811 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 8812 | .build(); |
| 8813 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms; |
| 8814 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
| 8815 | std::this_thread::sleep_for(STALE_EVENT_TIMEOUT); |
| 8816 | mDispatcher->notifyKey(staleKeyUp); |
| 8817 | |
| 8818 | // Only the key gesture corresponding to the dropped event should receive the cancel event. |
| 8819 | // Therefore, windowInPrimary should get the cancel event and windowInSecondary should not |
| 8820 | // receive any events. |
| 8821 | windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8822 | WithDisplayId(ui::LogicalDisplayId::DEFAULT), |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8823 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 8824 | windowInSecondary->assertNoEvents(); |
| 8825 | } |
| 8826 | |
| 8827 | /** |
| 8828 | * Similar to 'WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture' but for motion events. |
| 8829 | */ |
| 8830 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropMotionEvent_OnlyCancelCorrespondingGesture) { |
| 8831 | // Send touch down on primary display. |
| 8832 | mDispatcher->notifyMotion( |
| 8833 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8834 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8835 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8836 | .build()); |
| 8837 | windowInPrimary->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8838 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8839 | windowInSecondary->assertNoEvents(); |
| 8840 | |
| 8841 | // Send touch down on second display. |
| 8842 | mDispatcher->notifyMotion( |
| 8843 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8844 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 8845 | .displayId(SECOND_DISPLAY_ID) |
| 8846 | .build()); |
| 8847 | windowInPrimary->assertNoEvents(); |
| 8848 | windowInSecondary->consumeMotionEvent( |
| 8849 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID))); |
| 8850 | |
| 8851 | // inject a valid MotionEvent on primary display that will be stale when it arrives. |
| 8852 | NotifyMotionArgs staleMotionUp = |
| 8853 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8854 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8855 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 8856 | .build(); |
| 8857 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms; |
| 8858 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
| 8859 | std::this_thread::sleep_for(STALE_EVENT_TIMEOUT); |
| 8860 | mDispatcher->notifyMotion(staleMotionUp); |
| 8861 | |
| 8862 | // For stale motion events, we let the gesture to complete. This behaviour is different from key |
| 8863 | // events, where we would cancel the current keys instead. |
| 8864 | windowInPrimary->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 8865 | windowInSecondary->assertNoEvents(); |
| 8866 | } |
| 8867 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8868 | class InputFilterTest : public InputDispatcherTest { |
| 8869 | protected: |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 8870 | void testNotifyMotion(ui::LogicalDisplayId displayId, bool expectToBeFiltered, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 8871 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8872 | NotifyMotionArgs motionArgs; |
| 8873 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8874 | motionArgs = |
| 8875 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8876 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8877 | motionArgs = |
| 8878 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8879 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8880 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8881 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 8882 | const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue()); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 8883 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8884 | } else { |
| 8885 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 8886 | } |
| 8887 | } |
| 8888 | |
| 8889 | void testNotifyKey(bool expectToBeFiltered) { |
| 8890 | NotifyKeyArgs keyArgs; |
| 8891 | |
| 8892 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8893 | mDispatcher->notifyKey(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8894 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8895 | mDispatcher->notifyKey(keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8896 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8897 | |
| 8898 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 8899 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8900 | } else { |
| 8901 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 8902 | } |
| 8903 | } |
| 8904 | }; |
| 8905 | |
| 8906 | // Test InputFilter for MotionEvent |
| 8907 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 8908 | // 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] | 8909 | testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/false); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8910 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8911 | |
| 8912 | // Enable InputFilter |
| 8913 | mDispatcher->setInputFilterEnabled(true); |
| 8914 | // 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] | 8915 | testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/true); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8916 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8917 | |
| 8918 | // Disable InputFilter |
| 8919 | mDispatcher->setInputFilterEnabled(false); |
| 8920 | // 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] | 8921 | testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/false); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8922 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8923 | } |
| 8924 | |
| 8925 | // Test InputFilter for KeyEvent |
| 8926 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 8927 | // 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] | 8928 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8929 | |
| 8930 | // Enable InputFilter |
| 8931 | mDispatcher->setInputFilterEnabled(true); |
| 8932 | // Send a key event, and check if it is filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8933 | testNotifyKey(/*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8934 | |
| 8935 | // Disable InputFilter |
| 8936 | mDispatcher->setInputFilterEnabled(false); |
| 8937 | // Send a key event, and check if it isn't filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8938 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8939 | } |
| 8940 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 8941 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 8942 | // logical display coordinate space. |
| 8943 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 8944 | ui::Transform firstDisplayTransform; |
| 8945 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 8946 | ui::Transform secondDisplayTransform; |
| 8947 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 8948 | |
| 8949 | std::vector<gui::DisplayInfo> displayInfos(2); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8950 | displayInfos[0].displayId = ui::LogicalDisplayId::DEFAULT; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 8951 | displayInfos[0].transform = firstDisplayTransform; |
| 8952 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 8953 | displayInfos[1].transform = secondDisplayTransform; |
| 8954 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 8955 | mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0}); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 8956 | |
| 8957 | // Enable InputFilter |
| 8958 | mDispatcher->setInputFilterEnabled(true); |
| 8959 | |
| 8960 | // Ensure the correct transforms are used for the displays. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8961 | testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/true, |
| 8962 | firstDisplayTransform); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8963 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 8964 | } |
| 8965 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8966 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 8967 | protected: |
| 8968 | virtual void SetUp() override { |
| 8969 | InputDispatcherTest::SetUp(); |
| 8970 | |
| 8971 | /** |
| 8972 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 8973 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 8974 | * on. |
| 8975 | */ |
| 8976 | mDispatcher->setInputFilterEnabled(true); |
| 8977 | |
| 8978 | std::shared_ptr<InputApplicationHandle> application = |
| 8979 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8980 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8981 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8982 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8983 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8984 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8985 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8986 | setFocusedWindow(mWindow); |
| 8987 | mWindow->consumeFocusEvent(true); |
| 8988 | } |
| 8989 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 8990 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 8991 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8992 | KeyEvent event; |
| 8993 | |
| 8994 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 8995 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8996 | ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, |
| 8997 | AKEYCODE_A, KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 8998 | const int32_t additionalPolicyFlags = |
| 8999 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 9000 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9001 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 9002 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9003 | policyFlags | additionalPolicyFlags)); |
| 9004 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9005 | mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags))); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9006 | } |
| 9007 | |
| 9008 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 9009 | int32_t flags) { |
| 9010 | MotionEvent event; |
| 9011 | PointerProperties pointerProperties[1]; |
| 9012 | PointerCoords pointerCoords[1]; |
| 9013 | pointerProperties[0].clear(); |
| 9014 | pointerProperties[0].id = 0; |
| 9015 | pointerCoords[0].clear(); |
| 9016 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 9017 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 9018 | |
| 9019 | ui::Transform identityTransform; |
| 9020 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 9021 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 9022 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 9023 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 9024 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 9025 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 9026 | eventTime, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 9027 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9028 | |
| 9029 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 9030 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9031 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 9032 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9033 | policyFlags | additionalPolicyFlags)); |
| 9034 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9035 | mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId))); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9036 | } |
| 9037 | |
| 9038 | private: |
| 9039 | sp<FakeWindowHandle> mWindow; |
| 9040 | }; |
| 9041 | |
| 9042 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9043 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 9044 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 9045 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9046 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 9047 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9048 | } |
| 9049 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9050 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9051 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9052 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9053 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 9054 | } |
| 9055 | |
| 9056 | TEST_F(InputFilterInjectionPolicyTest, |
| 9057 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 9058 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9059 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9060 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9061 | } |
| 9062 | |
| 9063 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9064 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 9065 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9066 | } |
| 9067 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9068 | class InputDispatcherUserActivityPokeTests : public InputDispatcherTest { |
| 9069 | protected: |
| 9070 | virtual void SetUp() override { |
| 9071 | InputDispatcherTest::SetUp(); |
| 9072 | |
| 9073 | std::shared_ptr<FakeApplicationHandle> application = |
| 9074 | std::make_shared<FakeApplicationHandle>(); |
| 9075 | application->setDispatchingTimeout(100ms); |
| 9076 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9077 | ui::LogicalDisplayId::DEFAULT); |
Yeabkal Wubshit | 222d83d | 2024-01-24 18:00:09 +0000 | [diff] [blame] | 9078 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9079 | mWindow->setDispatchingTimeout(100ms); |
| 9080 | mWindow->setFocusable(true); |
| 9081 | |
| 9082 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9083 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9084 | |
| 9085 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 9086 | setFocusedWindow(mWindow); |
| 9087 | mWindow->consumeFocusEvent(true); |
| 9088 | } |
| 9089 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 9090 | void notifyAndConsumeMotion(int32_t action, uint32_t source, ui::LogicalDisplayId displayId, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9091 | nsecs_t eventTime) { |
| 9092 | mDispatcher->notifyMotion(MotionArgsBuilder(action, source) |
| 9093 | .displayId(displayId) |
| 9094 | .eventTime(eventTime) |
| 9095 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 9096 | .build()); |
| 9097 | mWindow->consumeMotionEvent(WithMotionAction(action)); |
| 9098 | } |
| 9099 | |
| 9100 | private: |
| 9101 | sp<FakeWindowHandle> mWindow; |
| 9102 | }; |
| 9103 | |
| 9104 | TEST_F_WITH_FLAGS( |
| 9105 | InputDispatcherUserActivityPokeTests, MinPokeTimeObserved, |
| 9106 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 9107 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 9108 | mDispatcher->setMinTimeBetweenUserActivityPokes(50ms); |
| 9109 | |
| 9110 | // First event of type TOUCH. Should poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9111 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9112 | milliseconds_to_nanoseconds(50)); |
| 9113 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9114 | {{milliseconds_to_nanoseconds(50), USER_ACTIVITY_EVENT_TOUCH, |
| 9115 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9116 | |
| 9117 | // 80ns > 50ns has passed since previous TOUCH event. Should poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9118 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9119 | milliseconds_to_nanoseconds(130)); |
| 9120 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9121 | {{milliseconds_to_nanoseconds(130), USER_ACTIVITY_EVENT_TOUCH, |
| 9122 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9123 | |
| 9124 | // 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] | 9125 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, |
| 9126 | ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(135)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9127 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9128 | {{milliseconds_to_nanoseconds(135), USER_ACTIVITY_EVENT_OTHER, |
| 9129 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9130 | |
| 9131 | // Within 50ns of previous TOUCH event. Should NOT poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9132 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9133 | milliseconds_to_nanoseconds(140)); |
| 9134 | mFakePolicy->assertUserActivityNotPoked(); |
| 9135 | |
| 9136 | // Within 50ns of previous OTHER event. Should NOT poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9137 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, |
| 9138 | ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(150)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9139 | mFakePolicy->assertUserActivityNotPoked(); |
| 9140 | |
| 9141 | // Within 50ns of previous TOUCH event (which was at time 130). Should NOT poke. |
| 9142 | // 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] | 9143 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_STYLUS, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9144 | milliseconds_to_nanoseconds(160)); |
| 9145 | mFakePolicy->assertUserActivityNotPoked(); |
| 9146 | |
| 9147 | // 65ns > 50ns has passed since previous OTHER event. Should poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9148 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, |
| 9149 | ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(200)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9150 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9151 | {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_OTHER, |
| 9152 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9153 | |
| 9154 | // 170ns > 50ns has passed since previous TOUCH event. Should poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9155 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_STYLUS, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9156 | milliseconds_to_nanoseconds(300)); |
| 9157 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9158 | {{milliseconds_to_nanoseconds(300), USER_ACTIVITY_EVENT_TOUCH, |
| 9159 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9160 | |
| 9161 | // Assert that there's no more user activity poke event. |
| 9162 | mFakePolicy->assertUserActivityNotPoked(); |
| 9163 | } |
| 9164 | |
| 9165 | TEST_F_WITH_FLAGS( |
| 9166 | InputDispatcherUserActivityPokeTests, DefaultMinPokeTimeOf100MsUsed, |
| 9167 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 9168 | rate_limit_user_activity_poke_in_dispatcher))) { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9169 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9170 | milliseconds_to_nanoseconds(200)); |
| 9171 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9172 | {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_TOUCH, |
| 9173 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9174 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9175 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9176 | milliseconds_to_nanoseconds(280)); |
| 9177 | mFakePolicy->assertUserActivityNotPoked(); |
| 9178 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9179 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9180 | milliseconds_to_nanoseconds(340)); |
| 9181 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9182 | {{milliseconds_to_nanoseconds(340), USER_ACTIVITY_EVENT_TOUCH, |
| 9183 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9184 | } |
| 9185 | |
| 9186 | TEST_F_WITH_FLAGS( |
| 9187 | InputDispatcherUserActivityPokeTests, ZeroMinPokeTimeDisablesRateLimiting, |
| 9188 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 9189 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 9190 | mDispatcher->setMinTimeBetweenUserActivityPokes(0ms); |
| 9191 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9192 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
| 9193 | 20); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9194 | mFakePolicy->assertUserActivityPoked(); |
| 9195 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9196 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
| 9197 | 30); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9198 | mFakePolicy->assertUserActivityPoked(); |
| 9199 | } |
| 9200 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9201 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 9202 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9203 | InputDispatcherTest::SetUp(); |
| 9204 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9205 | std::shared_ptr<FakeApplicationHandle> application = |
| 9206 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9207 | mUnfocusedWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 9208 | ui::LogicalDisplayId::DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9209 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9210 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9211 | mFocusedWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 9212 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 9213 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9214 | |
| 9215 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9216 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9217 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9218 | |
| 9219 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9220 | mDispatcher->onWindowInfosChanged( |
| 9221 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9222 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 9223 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9224 | } |
| 9225 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 9226 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9227 | InputDispatcherTest::TearDown(); |
| 9228 | |
| 9229 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 9230 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9231 | } |
| 9232 | |
| 9233 | protected: |
| 9234 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 9235 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 9236 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9237 | }; |
| 9238 | |
| 9239 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 9240 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 9241 | // the onPointerDownOutsideFocus callback. |
| 9242 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9243 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9244 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9245 | ui::LogicalDisplayId::DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9246 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 9247 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9248 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 9249 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9250 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 9251 | } |
| 9252 | |
| 9253 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 9254 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 9255 | // onPointerDownOutsideFocus callback. |
| 9256 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9257 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9258 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9259 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9260 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 9261 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9262 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 9263 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9264 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9265 | } |
| 9266 | |
| 9267 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 9268 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 9269 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 9270 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9271 | injectKeyDownNoRepeat(*mDispatcher, ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9272 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9273 | mFocusedWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9274 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 9275 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9276 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9277 | } |
| 9278 | |
| 9279 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 9280 | // DOWN on the window that already has focus. Ensure no window received the |
| 9281 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 9282 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9283 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9284 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9285 | ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9286 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 9287 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9288 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 9289 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9290 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9291 | } |
| 9292 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 9293 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 9294 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 9295 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 9296 | const MotionEvent event = |
| 9297 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 9298 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9299 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 9300 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 9301 | .build(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9302 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 9303 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9304 | mUnfocusedWindow->consumeAnyMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 9305 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 9306 | |
| 9307 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9308 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 9309 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 9310 | mUnfocusedWindow->assertNoEvents(); |
| 9311 | } |
| 9312 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9313 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 9314 | // windows that point to the same client token. |
| 9315 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 9316 | virtual void SetUp() override { |
| 9317 | InputDispatcherTest::SetUp(); |
| 9318 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9319 | std::shared_ptr<FakeApplicationHandle> application = |
| 9320 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9321 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9322 | ui::LogicalDisplayId::DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9323 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 9324 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9325 | mWindow2 = mWindow1->clone(ui::LogicalDisplayId::DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9326 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 9327 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9328 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9329 | } |
| 9330 | |
| 9331 | protected: |
| 9332 | sp<FakeWindowHandle> mWindow1; |
| 9333 | sp<FakeWindowHandle> mWindow2; |
| 9334 | |
| 9335 | // 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] | 9336 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 9337 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 9338 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9339 | } |
| 9340 | |
| 9341 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 9342 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 9343 | const std::string name = window->getName(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9344 | std::unique_ptr<MotionEvent> motionEvent = |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 9345 | window->consumeMotionEvent(WithMotionAction(expectedAction)); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9346 | ASSERT_NE(nullptr, motionEvent); |
| 9347 | ASSERT_EQ(points.size(), motionEvent->getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9348 | |
| 9349 | for (size_t i = 0; i < points.size(); i++) { |
| 9350 | float expectedX = points[i].x; |
| 9351 | float expectedY = points[i].y; |
| 9352 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9353 | EXPECT_EQ(expectedX, motionEvent->getX(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9354 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9355 | << ", got " << motionEvent->getX(i); |
| 9356 | EXPECT_EQ(expectedY, motionEvent->getY(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9357 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9358 | << ", got " << motionEvent->getY(i); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9359 | } |
| 9360 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9361 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9362 | void touchAndAssertPositions(sp<FakeWindowHandle> touchedWindow, int32_t action, |
| 9363 | const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9364 | std::vector<PointF> expectedPoints) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9365 | mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9366 | ui::LogicalDisplayId::DEFAULT, touchedPoints)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9367 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9368 | consumeMotionEvent(touchedWindow, action, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9369 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9370 | }; |
| 9371 | |
| 9372 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 9373 | // Touch Window 1 |
| 9374 | PointF touchedPoint = {10, 10}; |
| 9375 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9376 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9377 | |
| 9378 | // Release touch on Window 1 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9379 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9380 | |
| 9381 | // Touch Window 2 |
| 9382 | touchedPoint = {150, 150}; |
| 9383 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9384 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9385 | } |
| 9386 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9387 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 9388 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9389 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9390 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9391 | |
| 9392 | // Touch Window 1 |
| 9393 | PointF touchedPoint = {10, 10}; |
| 9394 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9395 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9396 | // Release touch on Window 1 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9397 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9398 | |
| 9399 | // Touch Window 2 |
| 9400 | touchedPoint = {150, 150}; |
| 9401 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9402 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 9403 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9404 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9405 | // Update the transform so rotation is set |
| 9406 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9407 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9408 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9409 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9410 | } |
| 9411 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9412 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9413 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9414 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9415 | |
| 9416 | // Touch Window 1 |
| 9417 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 9418 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9419 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9420 | |
| 9421 | // Touch Window 2 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9422 | // Since this is part of the same touch gesture that has already been dispatched to Window 1, |
| 9423 | // the touch stream from Window 2 will be merged with the stream in Window 1. The merged stream |
| 9424 | // will continue to be dispatched through Window 1. |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9425 | touchedPoints.push_back(PointF{150, 150}); |
| 9426 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9427 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9428 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9429 | // Release Window 2 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9430 | touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9431 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9432 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9433 | // Update the transform so rotation is set for Window 2 |
| 9434 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9435 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9436 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9437 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9438 | } |
| 9439 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9440 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9441 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9442 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9443 | |
| 9444 | // Touch Window 1 |
| 9445 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 9446 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9447 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9448 | |
| 9449 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9450 | touchedPoints.push_back(PointF{150, 150}); |
| 9451 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9452 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9453 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9454 | |
| 9455 | // Move both windows |
| 9456 | touchedPoints = {{20, 20}, {175, 175}}; |
| 9457 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 9458 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 9459 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9460 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9461 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9462 | // Release Window 2 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9463 | touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9464 | expectedPoints.pop_back(); |
| 9465 | |
| 9466 | // Touch Window 2 |
| 9467 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9468 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9469 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9470 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9471 | |
| 9472 | // Move both windows |
| 9473 | touchedPoints = {{20, 20}, {175, 175}}; |
| 9474 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 9475 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 9476 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9477 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9478 | } |
| 9479 | |
| 9480 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 9481 | mWindow1->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9482 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9483 | |
| 9484 | // Touch Window 1 |
| 9485 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 9486 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9487 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9488 | |
| 9489 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9490 | touchedPoints.push_back(PointF{150, 150}); |
| 9491 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9492 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9493 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9494 | |
| 9495 | // Move both windows |
| 9496 | touchedPoints = {{20, 20}, {175, 175}}; |
| 9497 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 9498 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 9499 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9500 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9501 | } |
| 9502 | |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 9503 | /** |
| 9504 | * When one of the windows is slippery, the touch should not slip into the other window with the |
| 9505 | * same input channel. |
| 9506 | */ |
| 9507 | TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) { |
| 9508 | mWindow1->setSlippery(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9509 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 9510 | |
| 9511 | // Touch down in window 1 |
| 9512 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9513 | ui::LogicalDisplayId::DEFAULT, {{50, 50}})); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 9514 | consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}}); |
| 9515 | |
| 9516 | // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip. |
| 9517 | // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN |
| 9518 | // getting generated. |
| 9519 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9520 | ui::LogicalDisplayId::DEFAULT, {{150, 150}})); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 9521 | |
| 9522 | consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}}); |
| 9523 | } |
| 9524 | |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 9525 | /** |
| 9526 | * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and |
| 9527 | * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window |
| 9528 | * that the pointer is hovering over may have a different transform. |
| 9529 | */ |
| 9530 | TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) { |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9531 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 9532 | |
| 9533 | // Start hover in window 1 |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 9534 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN) |
| 9535 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 9536 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 9537 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 9538 | {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 9539 | // Move hover to window 2. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 9540 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 9541 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 9542 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 9543 | consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}}); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9544 | consumeMotionEvent(mWindow2, ACTION_HOVER_ENTER, |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 9545 | {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})}); |
| 9546 | } |
| 9547 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9548 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 9549 | virtual void SetUp() override { |
| 9550 | InputDispatcherTest::SetUp(); |
| 9551 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9552 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9553 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9554 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9555 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9556 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9557 | mWindow->setDispatchingTimeout(100ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9558 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9559 | |
| 9560 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9561 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9562 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9563 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9564 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9565 | mWindow->consumeFocusEvent(true); |
| 9566 | } |
| 9567 | |
| 9568 | virtual void TearDown() override { |
| 9569 | InputDispatcherTest::TearDown(); |
| 9570 | mWindow.clear(); |
| 9571 | } |
| 9572 | |
| 9573 | protected: |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9574 | static constexpr std::chrono::duration SPY_TIMEOUT = 200ms; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9575 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9576 | sp<FakeWindowHandle> mWindow; |
| 9577 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 9578 | |
| 9579 | void tapOnWindow() { |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 9580 | const auto touchingPointer = PointerBuilder(/*id=*/0, ToolType::FINGER) |
| 9581 | .x(WINDOW_LOCATION.x) |
| 9582 | .y(WINDOW_LOCATION.y); |
| 9583 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9584 | .pointer(touchingPointer) |
| 9585 | .build()); |
| 9586 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 9587 | .pointer(touchingPointer) |
| 9588 | .build()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9589 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9590 | |
| 9591 | sp<FakeWindowHandle> addSpyWindow() { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9592 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", |
| 9593 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9594 | spy->setTrustedOverlay(true); |
| 9595 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9596 | spy->setSpy(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9597 | spy->setDispatchingTimeout(SPY_TIMEOUT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9598 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9599 | return spy; |
| 9600 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9601 | }; |
| 9602 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9603 | // Send a tap and respond, which should not cause an ANR. |
| 9604 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 9605 | tapOnWindow(); |
| 9606 | mWindow->consumeMotionDown(); |
| 9607 | mWindow->consumeMotionUp(); |
| 9608 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9609 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9610 | } |
| 9611 | |
| 9612 | // Send a regular key and respond, which should not cause an ANR. |
| 9613 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9614 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9615 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9616 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9617 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9618 | } |
| 9619 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 9620 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 9621 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9622 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 9623 | mWindow->consumeFocusEvent(false); |
| 9624 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9625 | InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9626 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 9627 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
| 9628 | CONSUME_TIMEOUT_EVENT_EXPECTED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9629 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9630 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 9631 | // Key will not go to window because we have no focused window. |
| 9632 | // The 'no focused window' ANR timer should start instead. |
| 9633 | |
| 9634 | // Now, the focused application goes away. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9635 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, nullptr); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 9636 | // The key should get dropped and there should be no ANR. |
| 9637 | |
| 9638 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9639 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9640 | } |
| 9641 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9642 | // 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] | 9643 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 9644 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9645 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9646 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9647 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9648 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9649 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9650 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9651 | ASSERT_TRUE(sequenceNum); |
| 9652 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9653 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9654 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9655 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 9656 | mWindow->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9657 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9658 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9659 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9660 | } |
| 9661 | |
| 9662 | // Send a key to the app and have the app not respond right away. |
| 9663 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 9664 | // Inject a key, and don't respond - expect that ANR is called. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9665 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9666 | const auto [sequenceNum, _] = mWindow->receiveEvent(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9667 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9668 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9669 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9670 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9671 | } |
| 9672 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9673 | // We have a focused application, but no focused window |
| 9674 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9675 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9676 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9677 | mWindow->consumeFocusEvent(false); |
| 9678 | |
| 9679 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9680 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9681 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9682 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9683 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 9684 | mDispatcher->waitForIdle(); |
| 9685 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9686 | |
| 9687 | // Once a focused event arrives, we get an ANR for this application |
| 9688 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 9689 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9690 | const InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9691 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 9692 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 9693 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9694 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9695 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 9696 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9697 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9698 | } |
| 9699 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 9700 | /** |
| 9701 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 9702 | * there will not be an ANR. |
| 9703 | */ |
| 9704 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 9705 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9706 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 9707 | mWindow->consumeFocusEvent(false); |
| 9708 | |
| 9709 | KeyEvent event; |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 9710 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 9711 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 9712 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 9713 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 9714 | |
| 9715 | // Define a valid key down event that is stale (too old). |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9716 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 9717 | ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, |
| 9718 | /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, |
| 9719 | eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 9720 | |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 9721 | const int32_t policyFlags = |
| 9722 | 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] | 9723 | |
| 9724 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9725 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 9726 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 9727 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 9728 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 9729 | << "Injection should fail because the event is stale"; |
| 9730 | |
| 9731 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9732 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9733 | mWindow->assertNoEvents(); |
| 9734 | } |
| 9735 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9736 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9737 | // Make sure that we don't notify policy twice about the same ANR. |
| 9738 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 9739 | const std::chrono::duration appTimeout = 400ms; |
| 9740 | mApplication->setDispatchingTimeout(appTimeout); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9741 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication); |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 9742 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9743 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9744 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9745 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9746 | |
| 9747 | // Once a focused event arrives, we get an ANR for this application |
| 9748 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 9749 | // injection times out (instead of failing). |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 9750 | const std::chrono::duration eventInjectionTimeout = 100ms; |
| 9751 | ASSERT_LT(eventInjectionTimeout, appTimeout); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9752 | const InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9753 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 9754 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 9755 | eventInjectionTimeout, |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 9756 | /*allowKeyRepeat=*/false); |
| 9757 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result) |
| 9758 | << "result=" << ftl::enum_string(result); |
| 9759 | // We already waited for 'eventInjectionTimeout`, because the countdown started when the event |
| 9760 | // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait. |
| 9761 | std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout; |
| 9762 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9763 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 9764 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9765 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 9766 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9767 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9768 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 9769 | // '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] | 9770 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9771 | } |
| 9772 | |
| 9773 | // We have a focused application, but no focused window |
| 9774 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9775 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9776 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9777 | mWindow->consumeFocusEvent(false); |
| 9778 | |
| 9779 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9780 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9781 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 9782 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 9783 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9784 | |
| 9785 | // Future focused events get dropped right away |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9786 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9787 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9788 | mWindow->assertNoEvents(); |
| 9789 | } |
| 9790 | |
| 9791 | /** |
| 9792 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 9793 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 9794 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 9795 | * the ANR mechanism should still work. |
| 9796 | * |
| 9797 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 9798 | * DOWN event, while not responding on the second one. |
| 9799 | */ |
| 9800 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 9801 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9802 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9803 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9804 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 9805 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9806 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9807 | |
| 9808 | // Now send ACTION_UP, with identical timestamp |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9809 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9810 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9811 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 9812 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9813 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9814 | |
| 9815 | // 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] | 9816 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9817 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9818 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9819 | } |
| 9820 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9821 | // A spy window can receive an ANR |
| 9822 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 9823 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 9824 | |
| 9825 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9826 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9827 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9828 | mWindow->consumeMotionDown(); |
| 9829 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9830 | const auto [sequenceNum, _] = spy->receiveEvent(); // ACTION_DOWN |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9831 | ASSERT_TRUE(sequenceNum); |
| 9832 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9833 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9834 | |
| 9835 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 9836 | spy->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9837 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9838 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9839 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9840 | } |
| 9841 | |
| 9842 | // 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] | 9843 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9844 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 9845 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9846 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9847 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9848 | injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT)); |
| 9849 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
| 9850 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9851 | injectKeyUp(*mDispatcher, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9852 | |
| 9853 | // Stuck on the ACTION_UP |
| 9854 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9855 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9856 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9857 | // 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] | 9858 | tapOnWindow(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9859 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 9860 | spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9861 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9862 | mWindow->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); // still the previous motion |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9863 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9864 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9865 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9866 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9867 | } |
| 9868 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9869 | // 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] | 9870 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9871 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 9872 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9873 | |
| 9874 | tapOnWindow(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9875 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 9876 | spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9877 | |
| 9878 | mWindow->consumeMotionDown(); |
| 9879 | // Stuck on the ACTION_UP |
| 9880 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9881 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9882 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9883 | // 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] | 9884 | tapOnWindow(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9885 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 9886 | spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9887 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9888 | mWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); // still the previous motion |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9889 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9890 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9891 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9892 | spy->assertNoEvents(); |
| 9893 | } |
| 9894 | |
| 9895 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9896 | mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9897 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9898 | FakeMonitorReceiver monitor = |
| 9899 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9900 | |
| 9901 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9902 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9903 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9904 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9905 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9906 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 9907 | ASSERT_TRUE(consumeSeq); |
| 9908 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9909 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(), |
| 9910 | MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9911 | |
| 9912 | monitor.finishEvent(*consumeSeq); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9913 | monitor.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9914 | |
| 9915 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9916 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9917 | } |
| 9918 | |
| 9919 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 9920 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 9921 | // get an ANR again. |
| 9922 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 9923 | // 2. consume all pending events (= queue becomes healthy again) |
| 9924 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 9925 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 9926 | tapOnWindow(); |
| 9927 | |
| 9928 | mWindow->consumeMotionDown(); |
| 9929 | // Block on ACTION_UP |
| 9930 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9931 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9932 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 9933 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9934 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9935 | mWindow->assertNoEvents(); |
| 9936 | |
| 9937 | tapOnWindow(); |
| 9938 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9939 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9940 | mWindow->consumeMotionUp(); |
| 9941 | |
| 9942 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9943 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9944 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9945 | mWindow->assertNoEvents(); |
| 9946 | } |
| 9947 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9948 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 9949 | // it. |
| 9950 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9951 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9952 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9953 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9954 | |
| 9955 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9956 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9957 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9958 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 9959 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9960 | // 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] | 9961 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 9962 | mWindow->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9963 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9964 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9965 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9966 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9967 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9968 | } |
| 9969 | |
| 9970 | /** |
| 9971 | * 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] | 9972 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9973 | */ |
| 9974 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 9975 | // The timeouts in this test are established by relying on the fact that the "key waiting for |
| 9976 | // events timeout" is equal to 500ms. |
| 9977 | ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9978 | 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] | 9979 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9980 | |
| 9981 | tapOnWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9982 | const auto& [downSequenceNum, downEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9983 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9984 | const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9985 | ASSERT_TRUE(upSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9986 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 9987 | // Don't finish the events yet, and send a key |
| 9988 | mDispatcher->notifyKey( |
| 9989 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 9990 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 9991 | .build()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9992 | // Key will not be sent to the window, yet, because the window is still processing events |
| 9993 | // 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] | 9994 | // 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] | 9995 | mWindow->assertNoEvents(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9996 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 9997 | std::this_thread::sleep_for(400ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9998 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 9999 | // 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] | 10000 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10001 | mWindow->finishEvent(*downSequenceNum); |
| 10002 | mWindow->finishEvent(*upSequenceNum); |
| 10003 | } |
| 10004 | |
| 10005 | /** |
| 10006 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 10007 | * not go to the focused window until the motion is processed. |
| 10008 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 10009 | * focused window right away. |
| 10010 | */ |
| 10011 | TEST_F(InputDispatcherSingleWindowAnr, |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 10012 | PendingKey_IsDeliveredWhileMotionIsProcessingAndNewTouchComesIn) { |
| 10013 | // The timeouts in this test are established by relying on the fact that the "key waiting for |
| 10014 | // events timeout" is equal to 500ms. |
| 10015 | ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10016 | 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] | 10017 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10018 | |
| 10019 | tapOnWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10020 | const auto& [downSequenceNum, _] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10021 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10022 | const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10023 | ASSERT_TRUE(upSequenceNum); |
| 10024 | // Don't finish the events yet, and send a key |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 10025 | mDispatcher->notifyKey( |
| 10026 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 10027 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 10028 | .build()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10029 | // 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] | 10030 | mWindow->assertNoEvents(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10031 | |
| 10032 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 10033 | tapOnWindow(); |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 10034 | // Now that we tapped, we should receive the key immediately. |
| 10035 | // Since there's still room for slowness, we use 200ms, which is much less than |
| 10036 | // the "key waiting for events' timeout of 500ms minus the already waited 100ms duration. |
| 10037 | std::unique_ptr<InputEvent> keyEvent = mWindow->consume(200ms); |
| 10038 | ASSERT_NE(nullptr, keyEvent); |
| 10039 | ASSERT_EQ(InputEventType::KEY, keyEvent->getType()); |
| 10040 | ASSERT_THAT(static_cast<KeyEvent&>(*keyEvent), WithKeyAction(AKEY_EVENT_ACTION_DOWN)); |
| 10041 | // it doesn't matter that we haven't ack'd the other events yet. We can finish events in any |
| 10042 | // order. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10043 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 10044 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 10045 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 10046 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10047 | mWindow->assertNoEvents(); |
| 10048 | } |
| 10049 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 10050 | /** |
| 10051 | * Send an event to the app and have the app not respond right away. |
| 10052 | * When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 10053 | * So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 10054 | * At some point, the window becomes responsive again. |
| 10055 | * Ensure that subsequent events get dropped, and the next gesture is delivered. |
| 10056 | */ |
| 10057 | TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) { |
| 10058 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10059 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 10060 | .build()); |
| 10061 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10062 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 10063 | ASSERT_TRUE(sequenceNum); |
| 10064 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 10065 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 10066 | |
| 10067 | mWindow->finishEvent(*sequenceNum); |
| 10068 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 10069 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10070 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
| 10071 | |
| 10072 | // Now that the window is responsive, let's continue the gesture. |
| 10073 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 10074 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 10075 | .build()); |
| 10076 | |
| 10077 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10078 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 10079 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 10080 | .build()); |
| 10081 | |
| 10082 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 10083 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 10084 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 10085 | .build()); |
| 10086 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 10087 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 10088 | .build()); |
| 10089 | // We already canceled this pointer, so the window shouldn't get any new events. |
| 10090 | mWindow->assertNoEvents(); |
| 10091 | |
| 10092 | // Start another one. |
| 10093 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10094 | .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15)) |
| 10095 | .build()); |
| 10096 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 10097 | } |
| 10098 | |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10099 | // Send an event to the app and have the app not respond right away. Then remove the app window. |
| 10100 | // When the window is removed, the dispatcher will cancel the events for that window. |
| 10101 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 10102 | TEST_F(InputDispatcherSingleWindowAnr, AnrAfterWindowRemoval) { |
| 10103 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10104 | AINPUT_SOURCE_TOUCHSCREEN, |
| 10105 | ui::LogicalDisplayId::DEFAULT, {WINDOW_LOCATION})); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10106 | |
| 10107 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
| 10108 | ASSERT_TRUE(sequenceNum); |
| 10109 | |
| 10110 | // Remove the window, but the input channel should remain alive. |
| 10111 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 10112 | |
| 10113 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 10114 | // Since the window was removed, Dispatcher does not know the PID associated with the window |
| 10115 | // anymore, so the policy is notified without the PID. |
| 10116 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken(), |
| 10117 | /*pid=*/std::nullopt); |
| 10118 | |
| 10119 | mWindow->finishEvent(*sequenceNum); |
| 10120 | // The cancellation was generated when the window was removed, along with the focus event. |
| 10121 | mWindow->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10122 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10123 | mWindow->consumeFocusEvent(false); |
| 10124 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10125 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt); |
| 10126 | } |
| 10127 | |
| 10128 | // Send an event to the app and have the app not respond right away. Wait for the policy to be |
| 10129 | // notified of the unresponsive window, then remove the app window. |
| 10130 | TEST_F(InputDispatcherSingleWindowAnr, AnrFollowedByWindowRemoval) { |
| 10131 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10132 | AINPUT_SOURCE_TOUCHSCREEN, |
| 10133 | ui::LogicalDisplayId::DEFAULT, {WINDOW_LOCATION})); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10134 | |
| 10135 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
| 10136 | ASSERT_TRUE(sequenceNum); |
| 10137 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 10138 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 10139 | |
| 10140 | // Remove the window, but the input channel should remain alive. |
| 10141 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 10142 | |
| 10143 | mWindow->finishEvent(*sequenceNum); |
| 10144 | // The cancellation was generated during the ANR, and the window lost focus when it was removed. |
| 10145 | mWindow->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10146 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10147 | mWindow->consumeFocusEvent(false); |
| 10148 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10149 | // Since the window was removed, Dispatcher does not know the PID associated with the window |
| 10150 | // becoming responsive, so the policy is notified without the PID. |
| 10151 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt); |
| 10152 | } |
| 10153 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10154 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 10155 | virtual void SetUp() override { |
| 10156 | InputDispatcherTest::SetUp(); |
| 10157 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 10158 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 10159 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10160 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10161 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10162 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10163 | // 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] | 10164 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10165 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10166 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10167 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 10168 | mFocusedWindow->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10169 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10170 | |
| 10171 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10172 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 10173 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10174 | |
| 10175 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10176 | mDispatcher->onWindowInfosChanged( |
| 10177 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10178 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10179 | mFocusedWindow->consumeFocusEvent(true); |
| 10180 | } |
| 10181 | |
| 10182 | virtual void TearDown() override { |
| 10183 | InputDispatcherTest::TearDown(); |
| 10184 | |
| 10185 | mUnfocusedWindow.clear(); |
| 10186 | mFocusedWindow.clear(); |
| 10187 | } |
| 10188 | |
| 10189 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 10190 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10191 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 10192 | sp<FakeWindowHandle> mFocusedWindow; |
| 10193 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 10194 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 10195 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 10196 | |
| 10197 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 10198 | |
| 10199 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 10200 | |
| 10201 | private: |
| 10202 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10203 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10204 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10205 | ui::LogicalDisplayId::DEFAULT, location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10206 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10207 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10208 | ui::LogicalDisplayId::DEFAULT, location)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10209 | } |
| 10210 | }; |
| 10211 | |
| 10212 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 10213 | // should be ANR'd first. |
| 10214 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10215 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 10216 | injectMotionEvent(*mDispatcher, |
| 10217 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10218 | AINPUT_SOURCE_TOUCHSCREEN) |
| 10219 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 10220 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 10221 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 10222 | .build())); |
| 10223 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10224 | injectMotionEvent(*mDispatcher, |
| 10225 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 10226 | AINPUT_SOURCE_TOUCHSCREEN) |
| 10227 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 10228 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 10229 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 10230 | .build())); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10231 | mFocusedWindow->consumeMotionDown(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 10232 | mFocusedWindow->consumeMotionUp(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10233 | mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10234 | // We consumed all events, so no ANR |
| 10235 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10236 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 10237 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10238 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 10239 | injectMotionEvent(*mDispatcher, |
| 10240 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10241 | AINPUT_SOURCE_TOUCHSCREEN) |
| 10242 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 10243 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 10244 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 10245 | .build())); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10246 | const auto [unfocusedSequenceNum, _] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10247 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10248 | |
| 10249 | const std::chrono::duration timeout = |
| 10250 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10251 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 10252 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10253 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10254 | mFocusedWindow->consumeMotionDown(); |
| 10255 | // This cancel is generated because the connection was unresponsive |
| 10256 | mFocusedWindow->consumeMotionCancel(); |
| 10257 | mFocusedWindow->assertNoEvents(); |
| 10258 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10259 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10260 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 10261 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10262 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10263 | } |
| 10264 | |
| 10265 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 10266 | // it doesn't matter which order they should have ANR. |
| 10267 | // But we should receive ANR for both. |
| 10268 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 10269 | // Set the timeout for unfocused window to match the focused window |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 10270 | mUnfocusedWindow->setDispatchingTimeout( |
| 10271 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10272 | mDispatcher->onWindowInfosChanged( |
| 10273 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10274 | |
| 10275 | tapOnFocusedWindow(); |
| 10276 | // 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] | 10277 | // 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] | 10278 | std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken( |
| 10279 | mFocusedWindow->getDispatchingTimeout( |
| 10280 | DISPATCHING_TIMEOUT)), |
| 10281 | mFakePolicy->getUnresponsiveWindowToken(0ms)}; |
| 10282 | |
| 10283 | ASSERT_THAT(anrConnectionTokens, |
| 10284 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 10285 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10286 | |
| 10287 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10288 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10289 | |
| 10290 | mFocusedWindow->consumeMotionDown(); |
| 10291 | mFocusedWindow->consumeMotionUp(); |
| 10292 | mUnfocusedWindow->consumeMotionOutside(); |
| 10293 | |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 10294 | std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(), |
| 10295 | mFakePolicy->getResponsiveWindowToken()}; |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10296 | |
| 10297 | // Both applications should be marked as responsive, in any order |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 10298 | ASSERT_THAT(responsiveTokens, |
| 10299 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 10300 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10301 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10302 | } |
| 10303 | |
| 10304 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 10305 | // We should also log an error to account for the dropped event (not tested here). |
| 10306 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 10307 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 10308 | tapOnFocusedWindow(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10309 | mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10310 | // Receive the events, but don't respond |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10311 | const auto [downEventSequenceNum, downEvent] = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10312 | ASSERT_TRUE(downEventSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10313 | const auto [upEventSequenceNum, upEvent] = mFocusedWindow->receiveEvent(); // ACTION_UP |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10314 | ASSERT_TRUE(upEventSequenceNum); |
| 10315 | const std::chrono::duration timeout = |
| 10316 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10317 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10318 | |
| 10319 | // Tap once again |
| 10320 | // 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] | 10321 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10322 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10323 | ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10324 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10325 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10326 | FOCUSED_WINDOW_LOCATION)); |
| 10327 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 10328 | // valid touch target |
| 10329 | mUnfocusedWindow->assertNoEvents(); |
| 10330 | |
| 10331 | // Consume the first tap |
| 10332 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 10333 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 10334 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10335 | // The second tap did not go to the focused window |
| 10336 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10337 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10338 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 10339 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10340 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 10341 | } |
| 10342 | |
| 10343 | // If you tap outside of all windows, there will not be ANR |
| 10344 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10345 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10346 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10347 | ui::LogicalDisplayId::DEFAULT, LOCATION_OUTSIDE_ALL_WINDOWS)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10348 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10349 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 10350 | } |
| 10351 | |
| 10352 | // Since the focused window is paused, tapping on it should not produce any events |
| 10353 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 10354 | mFocusedWindow->setPaused(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10355 | mDispatcher->onWindowInfosChanged( |
| 10356 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10357 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10358 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10359 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10360 | ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10361 | |
| 10362 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 10363 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10364 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 10365 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 10366 | |
| 10367 | mFocusedWindow->assertNoEvents(); |
| 10368 | mUnfocusedWindow->assertNoEvents(); |
| 10369 | } |
| 10370 | |
| 10371 | /** |
| 10372 | * 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] | 10373 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10374 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 10375 | * |
| 10376 | * Warning!!! |
| 10377 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 10378 | * and the injection timeout that we specify when injecting the key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10379 | * We must have the injection timeout (100ms) be smaller than |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10380 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 10381 | * |
| 10382 | * If that value changes, this test should also change. |
| 10383 | */ |
| 10384 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 10385 | // Set a long ANR timeout to prevent it from triggering |
| 10386 | mFocusedWindow->setDispatchingTimeout(2s); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10387 | mDispatcher->onWindowInfosChanged( |
| 10388 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10389 | |
| 10390 | tapOnUnfocusedWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10391 | const auto [downSequenceNum, downEvent] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10392 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10393 | const auto [upSequenceNum, upEvent] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10394 | ASSERT_TRUE(upSequenceNum); |
| 10395 | // Don't finish the events yet, and send a key |
| 10396 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 10397 | // window even if motions are still being processed. |
| 10398 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10399 | InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10400 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 10401 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 10402 | /*injectionTimeout=*/100ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10403 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10404 | // 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] | 10405 | // and the key remains pending, waiting for the touch events to be processed. |
| 10406 | // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED |
| 10407 | // under the hood. |
| 10408 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 10409 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10410 | |
| 10411 | // 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] | 10412 | mFocusedWindow->setFocusable(false); |
| 10413 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10414 | mDispatcher->onWindowInfosChanged( |
| 10415 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10416 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10417 | |
| 10418 | // Focus events should precede the key events |
| 10419 | mUnfocusedWindow->consumeFocusEvent(true); |
| 10420 | mFocusedWindow->consumeFocusEvent(false); |
| 10421 | |
| 10422 | // Finish the tap events, which should unblock dispatcher |
| 10423 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 10424 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 10425 | |
| 10426 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 10427 | // can finally go to the newly focused "mUnfocusedWindow". |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10428 | mUnfocusedWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10429 | mFocusedWindow->assertNoEvents(); |
| 10430 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10431 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10432 | } |
| 10433 | |
| 10434 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 10435 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 10436 | // The other window should not be affected by that. |
| 10437 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 10438 | // Touch Window 1 |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10439 | mDispatcher->notifyMotion( |
| 10440 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 10441 | ui::LogicalDisplayId::DEFAULT, {FOCUSED_WINDOW_LOCATION})); |
| 10442 | mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10443 | |
| 10444 | // Touch Window 2 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10445 | mDispatcher->notifyMotion( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10446 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 10447 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10448 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10449 | |
| 10450 | const std::chrono::duration timeout = |
| 10451 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10452 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10453 | |
| 10454 | mUnfocusedWindow->consumeMotionDown(); |
| 10455 | mFocusedWindow->consumeMotionDown(); |
| 10456 | // Focused window may or may not receive ACTION_MOVE |
| 10457 | // But it should definitely receive ACTION_CANCEL due to the ANR |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10458 | const auto [moveOrCancelSequenceNum, event] = mFocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10459 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 10460 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 10461 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 10462 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10463 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 10464 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 10465 | mFocusedWindow->consumeMotionCancel(); |
| 10466 | } else { |
| 10467 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 10468 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10469 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10470 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 10471 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10472 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10473 | mUnfocusedWindow->assertNoEvents(); |
| 10474 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10475 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10476 | } |
| 10477 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10478 | /** |
| 10479 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 10480 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 10481 | * |
| 10482 | * If the user touches another application during this time, the key should be dropped. |
| 10483 | * Next, if a new focused window comes in, without toggling the focused application, |
| 10484 | * then no ANR should occur. |
| 10485 | * |
| 10486 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 10487 | * but in some cases the policy may not update the focused application. |
| 10488 | */ |
| 10489 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 10490 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 10491 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | c033dfb | 2023-10-03 10:45:16 -0700 | [diff] [blame] | 10492 | focusedApplication->setDispatchingTimeout(300ms); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10493 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, focusedApplication); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10494 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 10495 | mFocusedWindow->setFocusable(false); |
| 10496 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10497 | mDispatcher->onWindowInfosChanged( |
| 10498 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10499 | mFocusedWindow->consumeFocusEvent(false); |
| 10500 | |
| 10501 | // Send a key. The ANR timer should start because there is no focused window. |
| 10502 | // 'focusedApplication' will get blamed if this timer completes. |
| 10503 | // 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] | 10504 | InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10505 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 10506 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 10507 | /*injectionTimeout=*/100ms, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10508 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10509 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10510 | |
| 10511 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 10512 | // then the injected touches won't cause the focused event to get dropped. |
| 10513 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 10514 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 10515 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 10516 | // For this test, it means that the key would get delivered to the window once it becomes |
| 10517 | // focused. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10518 | std::this_thread::sleep_for(100ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10519 | |
| 10520 | // Touch unfocused window. This should force the pending key to get dropped. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10521 | mDispatcher->notifyMotion( |
| 10522 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 10523 | ui::LogicalDisplayId::DEFAULT, {UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10524 | |
| 10525 | // We do not consume the motion right away, because that would require dispatcher to first |
| 10526 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 10527 | // Set the focused window first. |
| 10528 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10529 | mDispatcher->onWindowInfosChanged( |
| 10530 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10531 | setFocusedWindow(mFocusedWindow); |
| 10532 | mFocusedWindow->consumeFocusEvent(true); |
| 10533 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 10534 | // to another application. This could be a bug / behaviour in the policy. |
| 10535 | |
| 10536 | mUnfocusedWindow->consumeMotionDown(); |
| 10537 | |
| 10538 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10539 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 10540 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 10541 | } |
| 10542 | |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 10543 | /** |
| 10544 | * If we are pruning input queue, we should never drop pointer events. Otherwise, we risk having |
| 10545 | * an inconsistent event stream inside the dispatcher. In this test, we make sure that the |
| 10546 | * dispatcher doesn't prune pointer events incorrectly. |
| 10547 | * |
| 10548 | * This test reproduces a crash in InputDispatcher. |
| 10549 | * To reproduce the crash, we need to simulate the conditions for "pruning input queue" to occur. |
| 10550 | * |
| 10551 | * Keep the currently focused application (mApplication), and have no focused window. |
| 10552 | * We set up two additional windows: |
| 10553 | * 1) The navigation bar window. This simulates the system "NavigationBar", which is used in the |
| 10554 | * 3-button navigation mode. This window injects a BACK button when it's touched. 2) The application |
| 10555 | * window. This window is not focusable, but is touchable. |
| 10556 | * |
| 10557 | * We first touch the navigation bar, which causes it to inject a key. Since there's no focused |
| 10558 | * window, the dispatcher doesn't process this key, and all other events inside dispatcher are now |
| 10559 | * blocked. The dispatcher is waiting for 'mApplication' to add a focused window. |
| 10560 | * |
| 10561 | * Now, we touch "Another window". This window is owned by a different application than |
| 10562 | * 'mApplication'. This causes the dispatcher to stop waiting for 'mApplication' to add a focused |
| 10563 | * window. Now, the "pruning input queue" behaviour should kick in, and the dispatcher should start |
| 10564 | * dropping the events from its queue. Ensure that no crash occurs. |
| 10565 | * |
| 10566 | * In this test, we are setting long timeouts to prevent ANRs and events dropped due to being stale. |
| 10567 | * This does not affect the test running time. |
| 10568 | */ |
| 10569 | TEST_F(InputDispatcherMultiWindowAnr, PruningInputQueueShouldNotDropPointerEvents) { |
| 10570 | std::shared_ptr<FakeApplicationHandle> systemUiApplication = |
| 10571 | std::make_shared<FakeApplicationHandle>(); |
| 10572 | systemUiApplication->setDispatchingTimeout(3000ms); |
| 10573 | mFakePolicy->setStaleEventTimeout(3000ms); |
| 10574 | sp<FakeWindowHandle> navigationBar = |
| 10575 | sp<FakeWindowHandle>::make(systemUiApplication, mDispatcher, "NavigationBar", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10576 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 10577 | navigationBar->setFocusable(false); |
| 10578 | navigationBar->setWatchOutsideTouch(true); |
| 10579 | navigationBar->setFrame(Rect(0, 0, 100, 100)); |
| 10580 | |
| 10581 | mApplication->setDispatchingTimeout(3000ms); |
| 10582 | // '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] | 10583 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication); |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 10584 | |
| 10585 | std::shared_ptr<FakeApplicationHandle> anotherApplication = |
| 10586 | std::make_shared<FakeApplicationHandle>(); |
| 10587 | sp<FakeWindowHandle> appWindow = |
| 10588 | sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Another window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10589 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 10590 | appWindow->setFocusable(false); |
| 10591 | appWindow->setFrame(Rect(100, 100, 200, 200)); |
| 10592 | |
| 10593 | mDispatcher->onWindowInfosChanged( |
| 10594 | {{*navigationBar->getInfo(), *appWindow->getInfo()}, {}, 0, 0}); |
| 10595 | // 'mFocusedWindow' is no longer in the dispatcher window list, and therefore loses focus |
| 10596 | mFocusedWindow->consumeFocusEvent(false); |
| 10597 | |
| 10598 | // Touch down the navigation bar. It consumes the touch and injects a key into the dispatcher |
| 10599 | // in response. |
| 10600 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10601 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 10602 | .build()); |
| 10603 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 10604 | |
| 10605 | // Key will not be sent anywhere because we have no focused window. It will remain pending. |
| 10606 | // Pretend we are injecting KEYCODE_BACK, but it doesn't actually matter what key it is. |
| 10607 | InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10608 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 10609 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 10610 | /*injectionTimeout=*/100ms, |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 10611 | /*allowKeyRepeat=*/false); |
| 10612 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
| 10613 | |
| 10614 | // Finish the gesture - lift up finger and inject ACTION_UP key event |
| 10615 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 10616 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 10617 | .build()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10618 | result = injectKey(*mDispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, |
| 10619 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 10620 | /*injectionTimeout=*/100ms, |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 10621 | /*allowKeyRepeat=*/false); |
| 10622 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
| 10623 | // The key that was injected is blocking the dispatcher, so the navigation bar shouldn't be |
| 10624 | // getting any events yet. |
| 10625 | navigationBar->assertNoEvents(); |
| 10626 | |
| 10627 | // Now touch "Another window". This touch is going to a different application than the one we |
| 10628 | // are waiting for (which is 'mApplication'). |
| 10629 | // This should cause the dispatcher to drop the pending focus-dispatched events (like the key |
| 10630 | // trying to be injected) and to continue processing the rest of the events in the original |
| 10631 | // order. |
| 10632 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10633 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 10634 | .build()); |
| 10635 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 10636 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_OUTSIDE)); |
| 10637 | appWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 10638 | |
| 10639 | appWindow->assertNoEvents(); |
| 10640 | navigationBar->assertNoEvents(); |
| 10641 | } |
| 10642 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10643 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 10644 | // that has feature NO_INPUT_CHANNEL. |
| 10645 | // Layout: |
| 10646 | // Top (closest to user) |
| 10647 | // mNoInputWindow (above all windows) |
| 10648 | // mBottomWindow |
| 10649 | // Bottom (furthest from user) |
| 10650 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 10651 | virtual void SetUp() override { |
| 10652 | InputDispatcherTest::SetUp(); |
| 10653 | |
| 10654 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10655 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 10656 | "Window without input channel", |
| 10657 | ui::LogicalDisplayId::DEFAULT, |
| 10658 | /*createInputChannel=*/false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10659 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10660 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 10661 | // It's perfectly valid for this window to not have an associated input channel |
| 10662 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10663 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10664 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10665 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 10666 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10667 | mDispatcher->onWindowInfosChanged( |
| 10668 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10669 | } |
| 10670 | |
| 10671 | protected: |
| 10672 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 10673 | sp<FakeWindowHandle> mNoInputWindow; |
| 10674 | sp<FakeWindowHandle> mBottomWindow; |
| 10675 | }; |
| 10676 | |
| 10677 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 10678 | PointF touchedPoint = {10, 10}; |
| 10679 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10680 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10681 | AINPUT_SOURCE_TOUCHSCREEN, |
| 10682 | ui::LogicalDisplayId::DEFAULT, {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10683 | |
| 10684 | mNoInputWindow->assertNoEvents(); |
| 10685 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 10686 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 10687 | // and therefore should prevent mBottomWindow from receiving touches |
| 10688 | mBottomWindow->assertNoEvents(); |
| 10689 | } |
| 10690 | |
| 10691 | /** |
| 10692 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 10693 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 10694 | */ |
| 10695 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 10696 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10697 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 10698 | "Window with input channel and NO_INPUT_CHANNEL", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10699 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10700 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10701 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10702 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10703 | mDispatcher->onWindowInfosChanged( |
| 10704 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10705 | |
| 10706 | PointF touchedPoint = {10, 10}; |
| 10707 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10708 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10709 | AINPUT_SOURCE_TOUCHSCREEN, |
| 10710 | ui::LogicalDisplayId::DEFAULT, {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10711 | |
| 10712 | mNoInputWindow->assertNoEvents(); |
| 10713 | mBottomWindow->assertNoEvents(); |
| 10714 | } |
| 10715 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10716 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 10717 | protected: |
| 10718 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 10719 | sp<FakeWindowHandle> mWindow; |
| 10720 | sp<FakeWindowHandle> mMirror; |
| 10721 | |
| 10722 | virtual void SetUp() override { |
| 10723 | InputDispatcherTest::SetUp(); |
| 10724 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10725 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", |
| 10726 | ui::LogicalDisplayId::DEFAULT); |
| 10727 | mMirror = mWindow->clone(ui::LogicalDisplayId::DEFAULT); |
| 10728 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10729 | mWindow->setFocusable(true); |
| 10730 | mMirror->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10731 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10732 | } |
| 10733 | }; |
| 10734 | |
| 10735 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 10736 | // Request focus on a mirrored window |
| 10737 | setFocusedWindow(mMirror); |
| 10738 | |
| 10739 | // window gets focused |
| 10740 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10741 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10742 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10743 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10744 | } |
| 10745 | |
| 10746 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 10747 | // focusable. |
| 10748 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 10749 | setFocusedWindow(mMirror); |
| 10750 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10751 | // window gets focused because it is above the mirror |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10752 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10753 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10754 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10755 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10756 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10757 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10758 | mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10759 | |
| 10760 | mMirror->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10761 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10762 | |
| 10763 | // window loses focus since one of the windows associated with the token in not focusable |
| 10764 | mWindow->consumeFocusEvent(false); |
| 10765 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10766 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10767 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10768 | mWindow->assertNoEvents(); |
| 10769 | } |
| 10770 | |
| 10771 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 10772 | // invisible. |
| 10773 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 10774 | setFocusedWindow(mMirror); |
| 10775 | |
| 10776 | // window gets focused |
| 10777 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10778 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10779 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10780 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10781 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10782 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10783 | mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10784 | |
| 10785 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10786 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10787 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10788 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10789 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10790 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10791 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10792 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10793 | mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10794 | |
| 10795 | mWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10796 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10797 | |
| 10798 | // window loses focus only after all windows associated with the token become invisible. |
| 10799 | mWindow->consumeFocusEvent(false); |
| 10800 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10801 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10802 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10803 | mWindow->assertNoEvents(); |
| 10804 | } |
| 10805 | |
| 10806 | // A focused & mirrored window remains focused until both windows are removed. |
| 10807 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 10808 | setFocusedWindow(mMirror); |
| 10809 | |
| 10810 | // window gets focused |
| 10811 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10812 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10813 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10814 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10815 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10816 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10817 | mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10818 | |
| 10819 | // single window is removed but the window token remains focused |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10820 | mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10821 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10822 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10823 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10824 | mMirror->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10825 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10826 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10827 | mMirror->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10828 | |
| 10829 | // Both windows are removed |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10830 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10831 | mWindow->consumeFocusEvent(false); |
| 10832 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10833 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10834 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10835 | mWindow->assertNoEvents(); |
| 10836 | } |
| 10837 | |
| 10838 | // Focus request can be pending until one window becomes visible. |
| 10839 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 10840 | // Request focus on an invisible mirror. |
| 10841 | mWindow->setVisible(false); |
| 10842 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10843 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10844 | setFocusedWindow(mMirror); |
| 10845 | |
| 10846 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10847 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10848 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10849 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10850 | |
| 10851 | mMirror->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10852 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10853 | |
| 10854 | // window gets focused |
| 10855 | mWindow->consumeFocusEvent(true); |
| 10856 | // window gets the pending key event |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10857 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10858 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10859 | |
| 10860 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 10861 | protected: |
| 10862 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 10863 | sp<FakeWindowHandle> mWindow; |
| 10864 | sp<FakeWindowHandle> mSecondWindow; |
| 10865 | |
| 10866 | void SetUp() override { |
| 10867 | InputDispatcherTest::SetUp(); |
| 10868 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10869 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", |
| 10870 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10871 | mWindow->setFocusable(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10872 | mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", |
| 10873 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10874 | mSecondWindow->setFocusable(true); |
| 10875 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10876 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10877 | mDispatcher->onWindowInfosChanged( |
| 10878 | {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10879 | |
| 10880 | setFocusedWindow(mWindow); |
| 10881 | mWindow->consumeFocusEvent(true); |
| 10882 | } |
| 10883 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10884 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10885 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10886 | } |
| 10887 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10888 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 10889 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10890 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 10891 | auto request = mFakePolicy->assertSetPointerCaptureCalled(window, enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10892 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10893 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10894 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10895 | } |
| 10896 | }; |
| 10897 | |
| 10898 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 10899 | // Ensure that capture cannot be obtained for unfocused windows. |
| 10900 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 10901 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 10902 | mSecondWindow->assertNoEvents(); |
| 10903 | |
| 10904 | // Ensure that capture can be enabled from the focus window. |
| 10905 | requestAndVerifyPointerCapture(mWindow, true); |
| 10906 | |
| 10907 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 10908 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 10909 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 10910 | |
| 10911 | // Ensure that capture can be disabled from the window with capture. |
| 10912 | requestAndVerifyPointerCapture(mWindow, false); |
| 10913 | } |
| 10914 | |
| 10915 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10916 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10917 | |
| 10918 | setFocusedWindow(mSecondWindow); |
| 10919 | |
| 10920 | // Ensure that the capture disabled event was sent first. |
| 10921 | mWindow->consumeCaptureEvent(false); |
| 10922 | mWindow->consumeFocusEvent(false); |
| 10923 | mSecondWindow->consumeFocusEvent(true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 10924 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10925 | |
| 10926 | // 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] | 10927 | notifyPointerCaptureChanged({}); |
| 10928 | notifyPointerCaptureChanged(request); |
| 10929 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10930 | mWindow->assertNoEvents(); |
| 10931 | mSecondWindow->assertNoEvents(); |
| 10932 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 10933 | } |
| 10934 | |
| 10935 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10936 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10937 | |
| 10938 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10939 | notifyPointerCaptureChanged({}); |
| 10940 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10941 | |
| 10942 | // Ensure that Pointer Capture is disabled. |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 10943 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10944 | mWindow->consumeCaptureEvent(false); |
| 10945 | mWindow->assertNoEvents(); |
| 10946 | } |
| 10947 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 10948 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 10949 | requestAndVerifyPointerCapture(mWindow, true); |
| 10950 | |
| 10951 | // The first window loses focus. |
| 10952 | setFocusedWindow(mSecondWindow); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 10953 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 10954 | mWindow->consumeCaptureEvent(false); |
| 10955 | |
| 10956 | // Request Pointer Capture from the second window before the notification from InputReader |
| 10957 | // arrives. |
| 10958 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 10959 | auto request = mFakePolicy->assertSetPointerCaptureCalled(mSecondWindow, true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 10960 | |
| 10961 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10962 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 10963 | |
| 10964 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10965 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 10966 | |
| 10967 | mSecondWindow->consumeFocusEvent(true); |
| 10968 | mSecondWindow->consumeCaptureEvent(true); |
| 10969 | } |
| 10970 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10971 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 10972 | // App repeatedly enables and disables capture. |
| 10973 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 10974 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10975 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 10976 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10977 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 10978 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 10979 | |
| 10980 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 10981 | // first request is now stale, this should do nothing. |
| 10982 | notifyPointerCaptureChanged(firstRequest); |
| 10983 | mWindow->assertNoEvents(); |
| 10984 | |
| 10985 | // InputReader notifies that the second request was enabled. |
| 10986 | notifyPointerCaptureChanged(secondRequest); |
| 10987 | mWindow->consumeCaptureEvent(true); |
| 10988 | } |
| 10989 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 10990 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 10991 | requestAndVerifyPointerCapture(mWindow, true); |
| 10992 | |
| 10993 | // App toggles pointer capture off and on. |
| 10994 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 10995 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 10996 | |
| 10997 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 10998 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 10999 | |
| 11000 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 11001 | // preceding "disable" request. |
| 11002 | notifyPointerCaptureChanged(enableRequest); |
| 11003 | |
| 11004 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 11005 | // any notifications. |
| 11006 | mWindow->assertNoEvents(); |
| 11007 | } |
| 11008 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11009 | /** |
| 11010 | * One window. Hover mouse in the window, and then start capture. Make sure that the relative |
| 11011 | * mouse movements don't affect the previous mouse hovering state. |
| 11012 | * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no |
| 11013 | * HOVER_MOVE events). |
| 11014 | */ |
| 11015 | TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) { |
| 11016 | // Mouse hover on the window |
| 11017 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 11018 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 11019 | .build()); |
| 11020 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 11021 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 11022 | .build()); |
| 11023 | |
| 11024 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER))); |
| 11025 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE))); |
| 11026 | |
| 11027 | // Start pointer capture |
| 11028 | requestAndVerifyPointerCapture(mWindow, true); |
| 11029 | |
| 11030 | // Send some relative mouse movements and receive them in the window. |
| 11031 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE) |
| 11032 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11)) |
| 11033 | .build()); |
| 11034 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11), |
| 11035 | WithSource(AINPUT_SOURCE_MOUSE_RELATIVE))); |
| 11036 | |
| 11037 | // Stop pointer capture |
| 11038 | requestAndVerifyPointerCapture(mWindow, false); |
| 11039 | |
| 11040 | // Continue hovering on the window |
| 11041 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 11042 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115)) |
| 11043 | .build()); |
| 11044 | mWindow->consumeMotionEvent( |
| 11045 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 11046 | |
| 11047 | mWindow->assertNoEvents(); |
| 11048 | } |
| 11049 | |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11050 | using InputDispatcherPointerCaptureDeathTest = InputDispatcherPointerCaptureTests; |
| 11051 | |
| 11052 | TEST_F(InputDispatcherPointerCaptureDeathTest, |
| 11053 | NotifyPointerCaptureChangedWithWrongTokenAbortsDispatcher) { |
| 11054 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
| 11055 | ScopedSilentDeath _silentDeath; |
| 11056 | |
| 11057 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 11058 | auto request = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
| 11059 | |
| 11060 | // Dispatch a pointer changed event with a wrong token. |
| 11061 | request.window = mSecondWindow->getToken(); |
| 11062 | ASSERT_DEATH( |
| 11063 | { |
| 11064 | notifyPointerCaptureChanged(request); |
| 11065 | mSecondWindow->consumeCaptureEvent(true); |
| 11066 | }, |
| 11067 | "Unexpected requested window for Pointer Capture."); |
| 11068 | } |
| 11069 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11070 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 11071 | protected: |
| 11072 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11073 | |
| 11074 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 11075 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 11076 | |
| 11077 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 11078 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 11079 | |
| 11080 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 11081 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 11082 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 11083 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 11084 | MAXIMUM_OBSCURING_OPACITY); |
| 11085 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11086 | static constexpr gui::Uid TOUCHED_APP_UID{10001}; |
| 11087 | static constexpr gui::Uid APP_B_UID{10002}; |
| 11088 | static constexpr gui::Uid APP_C_UID{10003}; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11089 | |
| 11090 | sp<FakeWindowHandle> mTouchWindow; |
| 11091 | |
| 11092 | virtual void SetUp() override { |
| 11093 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11094 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11095 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 11096 | } |
| 11097 | |
| 11098 | virtual void TearDown() override { |
| 11099 | InputDispatcherTest::TearDown(); |
| 11100 | mTouchWindow.clear(); |
| 11101 | } |
| 11102 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11103 | sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 11104 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11105 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11106 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11107 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11108 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11109 | return window; |
| 11110 | } |
| 11111 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11112 | sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11113 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 11114 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11115 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ui::LogicalDisplayId::DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11116 | // Generate an arbitrary PID based on the UID |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11117 | 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] | 11118 | return window; |
| 11119 | } |
| 11120 | |
| 11121 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11122 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11123 | AINPUT_SOURCE_TOUCHSCREEN, |
| 11124 | ui::LogicalDisplayId::DEFAULT, points)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11125 | } |
| 11126 | }; |
| 11127 | |
| 11128 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11129 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11130 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11131 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11132 | |
| 11133 | touch(); |
| 11134 | |
| 11135 | mTouchWindow->assertNoEvents(); |
| 11136 | } |
| 11137 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11138 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11139 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 11140 | const sp<FakeWindowHandle>& w = |
| 11141 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11142 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11143 | |
| 11144 | touch(); |
| 11145 | |
| 11146 | mTouchWindow->assertNoEvents(); |
| 11147 | } |
| 11148 | |
| 11149 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11150 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 11151 | const sp<FakeWindowHandle>& w = |
| 11152 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11153 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11154 | |
| 11155 | touch(); |
| 11156 | |
| 11157 | w->assertNoEvents(); |
| 11158 | } |
| 11159 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11160 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11161 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11162 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11163 | |
| 11164 | touch(); |
| 11165 | |
| 11166 | mTouchWindow->consumeAnyMotionDown(); |
| 11167 | } |
| 11168 | |
| 11169 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11170 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11171 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11172 | w->setFrame(Rect(0, 0, 50, 50)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11173 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11174 | |
| 11175 | touch({PointF{100, 100}}); |
| 11176 | |
| 11177 | mTouchWindow->consumeAnyMotionDown(); |
| 11178 | } |
| 11179 | |
| 11180 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11181 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11182 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11183 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11184 | |
| 11185 | touch(); |
| 11186 | |
| 11187 | mTouchWindow->consumeAnyMotionDown(); |
| 11188 | } |
| 11189 | |
| 11190 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 11191 | const sp<FakeWindowHandle>& w = |
| 11192 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11193 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11194 | |
| 11195 | touch(); |
| 11196 | |
| 11197 | mTouchWindow->consumeAnyMotionDown(); |
| 11198 | } |
| 11199 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11200 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 11201 | const sp<FakeWindowHandle>& w = |
| 11202 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11203 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11204 | |
| 11205 | touch(); |
| 11206 | |
| 11207 | w->assertNoEvents(); |
| 11208 | } |
| 11209 | |
| 11210 | /** |
| 11211 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 11212 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 11213 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 11214 | */ |
| 11215 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11216 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 11217 | const sp<FakeWindowHandle>& w = |
| 11218 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11219 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11220 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11221 | |
| 11222 | touch(); |
| 11223 | |
| 11224 | w->consumeMotionOutside(); |
| 11225 | } |
| 11226 | |
| 11227 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 11228 | const sp<FakeWindowHandle>& w = |
| 11229 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11230 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11231 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11232 | |
| 11233 | touch(); |
| 11234 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 11235 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11236 | } |
| 11237 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11238 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11239 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11240 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11241 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11242 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11243 | |
| 11244 | touch(); |
| 11245 | |
| 11246 | mTouchWindow->consumeAnyMotionDown(); |
| 11247 | } |
| 11248 | |
| 11249 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 11250 | const sp<FakeWindowHandle>& w = |
| 11251 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11252 | MAXIMUM_OBSCURING_OPACITY); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11253 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11254 | |
| 11255 | touch(); |
| 11256 | |
| 11257 | mTouchWindow->consumeAnyMotionDown(); |
| 11258 | } |
| 11259 | |
| 11260 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11261 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11262 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11263 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11264 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11265 | |
| 11266 | touch(); |
| 11267 | |
| 11268 | mTouchWindow->assertNoEvents(); |
| 11269 | } |
| 11270 | |
| 11271 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 11272 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 11273 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11274 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 11275 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11276 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11277 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 11278 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11279 | mDispatcher->onWindowInfosChanged( |
| 11280 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11281 | |
| 11282 | touch(); |
| 11283 | |
| 11284 | mTouchWindow->assertNoEvents(); |
| 11285 | } |
| 11286 | |
| 11287 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 11288 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 11289 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11290 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 11291 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11292 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11293 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 11294 | OPACITY_FAR_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11295 | mDispatcher->onWindowInfosChanged( |
| 11296 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11297 | |
| 11298 | touch(); |
| 11299 | |
| 11300 | mTouchWindow->consumeAnyMotionDown(); |
| 11301 | } |
| 11302 | |
| 11303 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11304 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 11305 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11306 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11307 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11308 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11309 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 11310 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11311 | mDispatcher->onWindowInfosChanged( |
| 11312 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11313 | |
| 11314 | touch(); |
| 11315 | |
| 11316 | mTouchWindow->consumeAnyMotionDown(); |
| 11317 | } |
| 11318 | |
| 11319 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 11320 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11321 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11322 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11323 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11324 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 11325 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11326 | mDispatcher->onWindowInfosChanged( |
| 11327 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11328 | |
| 11329 | touch(); |
| 11330 | |
| 11331 | mTouchWindow->assertNoEvents(); |
| 11332 | } |
| 11333 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11334 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11335 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 11336 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11337 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 11338 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11339 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11340 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11341 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11342 | mDispatcher->onWindowInfosChanged( |
| 11343 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11344 | |
| 11345 | touch(); |
| 11346 | |
| 11347 | mTouchWindow->assertNoEvents(); |
| 11348 | } |
| 11349 | |
| 11350 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11351 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 11352 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11353 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 11354 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11355 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11356 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11357 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11358 | mDispatcher->onWindowInfosChanged( |
| 11359 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11360 | |
| 11361 | touch(); |
| 11362 | |
| 11363 | mTouchWindow->consumeAnyMotionDown(); |
| 11364 | } |
| 11365 | |
| 11366 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 11367 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11368 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 11369 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11370 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11371 | |
| 11372 | touch(); |
| 11373 | |
| 11374 | mTouchWindow->consumeAnyMotionDown(); |
| 11375 | } |
| 11376 | |
| 11377 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 11378 | const sp<FakeWindowHandle>& w = |
| 11379 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11380 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11381 | |
| 11382 | touch(); |
| 11383 | |
| 11384 | mTouchWindow->consumeAnyMotionDown(); |
| 11385 | } |
| 11386 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 11387 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11388 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 11389 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 11390 | const sp<FakeWindowHandle>& w = |
| 11391 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11392 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 11393 | |
| 11394 | touch(); |
| 11395 | |
| 11396 | mTouchWindow->assertNoEvents(); |
| 11397 | } |
| 11398 | |
| 11399 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 11400 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 11401 | const sp<FakeWindowHandle>& w = |
| 11402 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11403 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 11404 | |
| 11405 | touch(); |
| 11406 | |
| 11407 | mTouchWindow->consumeAnyMotionDown(); |
| 11408 | } |
| 11409 | |
| 11410 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11411 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 11412 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 11413 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11414 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11415 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11416 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11417 | |
| 11418 | touch(); |
| 11419 | |
| 11420 | mTouchWindow->consumeAnyMotionDown(); |
| 11421 | } |
| 11422 | |
| 11423 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11424 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 11425 | const sp<FakeWindowHandle>& w1 = |
| 11426 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 11427 | OPACITY_BELOW_THRESHOLD); |
| 11428 | const sp<FakeWindowHandle>& w2 = |
| 11429 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11430 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11431 | mDispatcher->onWindowInfosChanged( |
| 11432 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11433 | |
| 11434 | touch(); |
| 11435 | |
| 11436 | mTouchWindow->assertNoEvents(); |
| 11437 | } |
| 11438 | |
| 11439 | /** |
| 11440 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 11441 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 11442 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 11443 | */ |
| 11444 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11445 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 11446 | const sp<FakeWindowHandle>& wB = |
| 11447 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 11448 | OPACITY_BELOW_THRESHOLD); |
| 11449 | const sp<FakeWindowHandle>& wC = |
| 11450 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 11451 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11452 | mDispatcher->onWindowInfosChanged( |
| 11453 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11454 | |
| 11455 | touch(); |
| 11456 | |
| 11457 | mTouchWindow->assertNoEvents(); |
| 11458 | } |
| 11459 | |
| 11460 | /** |
| 11461 | * This test is testing that a window from a different UID but with same application token doesn't |
| 11462 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 11463 | */ |
| 11464 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11465 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 11466 | const sp<FakeWindowHandle>& w = |
| 11467 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 11468 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11469 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 11470 | |
| 11471 | touch(); |
| 11472 | |
| 11473 | mTouchWindow->consumeAnyMotionDown(); |
| 11474 | } |
| 11475 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11476 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 11477 | protected: |
| 11478 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 11479 | sp<FakeWindowHandle> mWindow; |
| 11480 | sp<FakeWindowHandle> mSecondWindow; |
| 11481 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11482 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11483 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 11484 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11485 | |
| 11486 | void SetUp() override { |
| 11487 | InputDispatcherTest::SetUp(); |
| 11488 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11489 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", |
| 11490 | ui::LogicalDisplayId::DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11491 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11492 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11493 | mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", |
| 11494 | ui::LogicalDisplayId::DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11495 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11496 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11497 | mSpyWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", |
| 11498 | ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11499 | mSpyWindow->setSpy(true); |
| 11500 | mSpyWindow->setTrustedOverlay(true); |
| 11501 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 11502 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11503 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11504 | mDispatcher->onWindowInfosChanged( |
| 11505 | {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 11506 | {}, |
| 11507 | 0, |
| 11508 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11509 | } |
| 11510 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11511 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 11512 | switch (fromSource) { |
| 11513 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 11514 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11515 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11516 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11517 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11518 | break; |
| 11519 | case AINPUT_SOURCE_STYLUS: |
| 11520 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11521 | injectMotionEvent(*mDispatcher, |
| 11522 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 11523 | AINPUT_SOURCE_STYLUS) |
| 11524 | .buttonState( |
| 11525 | AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 11526 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
| 11527 | .x(50) |
| 11528 | .y(50)) |
| 11529 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11530 | break; |
| 11531 | case AINPUT_SOURCE_MOUSE: |
| 11532 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11533 | injectMotionEvent(*mDispatcher, |
| 11534 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 11535 | AINPUT_SOURCE_MOUSE) |
| 11536 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 11537 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 11538 | ToolType::MOUSE) |
| 11539 | .x(50) |
| 11540 | .y(50)) |
| 11541 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11542 | break; |
| 11543 | default: |
| 11544 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 11545 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11546 | |
| 11547 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11548 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11549 | // Spy window should also receive motion event |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11550 | mSpyWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11551 | } |
| 11552 | |
| 11553 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 11554 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 11555 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11556 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11557 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11558 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11559 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11560 | |
| 11561 | // The drag window covers the entire display |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11562 | mDragWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", |
| 11563 | ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11564 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11565 | mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), |
| 11566 | *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 11567 | {}, |
| 11568 | 0, |
| 11569 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11570 | |
| 11571 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11572 | bool transferred = |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 11573 | mDispatcher->transferTouchGesture(mWindow->getToken(), mDragWindow->getToken(), |
| 11574 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11575 | if (transferred) { |
| 11576 | mWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11577 | mDragWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 11578 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11579 | } |
| 11580 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11581 | } |
| 11582 | }; |
| 11583 | |
| 11584 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11585 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11586 | |
| 11587 | // Move on window. |
| 11588 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11589 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11590 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11591 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11592 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11593 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11594 | mWindow->consumeDragEvent(false, 50, 50); |
| 11595 | mSecondWindow->assertNoEvents(); |
| 11596 | |
| 11597 | // Move to another window. |
| 11598 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11599 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11600 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11601 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11602 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11603 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11604 | mWindow->consumeDragEvent(true, 150, 50); |
| 11605 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 11606 | |
| 11607 | // Move back to original window. |
| 11608 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11609 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11610 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11611 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11612 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11613 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11614 | mWindow->consumeDragEvent(false, 50, 50); |
| 11615 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 11616 | |
| 11617 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11618 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11619 | {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11620 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11621 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11622 | mWindow->assertNoEvents(); |
| 11623 | mSecondWindow->assertNoEvents(); |
| 11624 | } |
| 11625 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11626 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11627 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11628 | |
| 11629 | // No cancel event after drag start |
| 11630 | mSpyWindow->assertNoEvents(); |
| 11631 | |
| 11632 | const MotionEvent secondFingerDownEvent = |
| 11633 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11634 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11635 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11636 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11637 | .build(); |
| 11638 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11639 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11640 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11641 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11642 | |
| 11643 | // Receives cancel for first pointer after next pointer down |
| 11644 | mSpyWindow->consumeMotionCancel(); |
| 11645 | mSpyWindow->consumeMotionDown(); |
| 11646 | |
| 11647 | mSpyWindow->assertNoEvents(); |
| 11648 | } |
| 11649 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11650 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11651 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11652 | |
| 11653 | // Move on window. |
| 11654 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11655 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11656 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11657 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11658 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11659 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11660 | mWindow->consumeDragEvent(false, 50, 50); |
| 11661 | mSecondWindow->assertNoEvents(); |
| 11662 | |
| 11663 | // Move to another window. |
| 11664 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11665 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11666 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11667 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11668 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11669 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11670 | mWindow->consumeDragEvent(true, 150, 50); |
| 11671 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 11672 | |
| 11673 | // drop to another window. |
| 11674 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11675 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11676 | {150, 50})) |
| 11677 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11678 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 11679 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11680 | mWindow->assertNoEvents(); |
| 11681 | mSecondWindow->assertNoEvents(); |
| 11682 | } |
| 11683 | |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 11684 | TEST_F(InputDispatcherDragTests, DragAndDropNotCancelledIfSomeOtherPointerIsPilfered) { |
| 11685 | startDrag(); |
| 11686 | |
| 11687 | // No cancel event after drag start |
| 11688 | mSpyWindow->assertNoEvents(); |
| 11689 | |
| 11690 | const MotionEvent secondFingerDownEvent = |
| 11691 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11692 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 11693 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11694 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 11695 | .build(); |
| 11696 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11697 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 11698 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11699 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11700 | |
| 11701 | // Receives cancel for first pointer after next pointer down |
| 11702 | mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 11703 | mSpyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithPointerIds({1}))); |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 11704 | mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 11705 | |
| 11706 | mSpyWindow->assertNoEvents(); |
| 11707 | |
| 11708 | // Spy window calls pilfer pointers |
| 11709 | EXPECT_EQ(OK, mDispatcher->pilferPointers(mSpyWindow->getToken())); |
| 11710 | mDragWindow->assertNoEvents(); |
| 11711 | |
| 11712 | const MotionEvent firstFingerMoveEvent = |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 11713 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 11714 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 11715 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(60).y(60)) |
| 11716 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 11717 | .build(); |
| 11718 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 11719 | injectMotionEvent(*mDispatcher, firstFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 11720 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11721 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11722 | |
| 11723 | // Drag window should still receive the new event |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 11724 | mDragWindow->consumeMotionEvent( |
| 11725 | AllOf(WithMotionAction(ACTION_MOVE), WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 11726 | mDragWindow->assertNoEvents(); |
| 11727 | } |
| 11728 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11729 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11730 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11731 | |
| 11732 | // Move on window and keep button pressed. |
| 11733 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11734 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11735 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 11736 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11737 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11738 | .build())) |
| 11739 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11740 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11741 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11742 | mWindow->consumeDragEvent(false, 50, 50); |
| 11743 | mSecondWindow->assertNoEvents(); |
| 11744 | |
| 11745 | // Move to another window and release button, expect to drop item. |
| 11746 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11747 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11748 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 11749 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11750 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11751 | .build())) |
| 11752 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11753 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11754 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11755 | mWindow->assertNoEvents(); |
| 11756 | mSecondWindow->assertNoEvents(); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 11757 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11758 | |
| 11759 | // nothing to the window. |
| 11760 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11761 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11762 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 11763 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11764 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11765 | .build())) |
| 11766 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11767 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11768 | mWindow->assertNoEvents(); |
| 11769 | mSecondWindow->assertNoEvents(); |
| 11770 | } |
| 11771 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11772 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11773 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11774 | |
| 11775 | // Set second window invisible. |
| 11776 | mSecondWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11777 | mDispatcher->onWindowInfosChanged( |
| 11778 | {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11779 | |
| 11780 | // Move on window. |
| 11781 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11782 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11783 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11784 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11785 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11786 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11787 | mWindow->consumeDragEvent(false, 50, 50); |
| 11788 | mSecondWindow->assertNoEvents(); |
| 11789 | |
| 11790 | // Move to another window. |
| 11791 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11792 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11793 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11794 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11795 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11796 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11797 | mWindow->consumeDragEvent(true, 150, 50); |
| 11798 | mSecondWindow->assertNoEvents(); |
| 11799 | |
| 11800 | // drop to another window. |
| 11801 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11802 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11803 | {150, 50})) |
| 11804 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11805 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 11806 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11807 | mWindow->assertNoEvents(); |
| 11808 | mSecondWindow->assertNoEvents(); |
| 11809 | } |
| 11810 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11811 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11812 | // Ensure window could track pointerIds if it didn't support split touch. |
| 11813 | mWindow->setPreventSplitting(true); |
| 11814 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11815 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11816 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 11817 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11818 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11819 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11820 | |
| 11821 | const MotionEvent secondFingerDownEvent = |
| 11822 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11823 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11824 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11825 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11826 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11827 | .build(); |
| 11828 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11829 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11830 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11831 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11832 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11833 | |
| 11834 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11835 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11836 | } |
| 11837 | |
| 11838 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 11839 | // First down on second window. |
| 11840 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11841 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 11842 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11843 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11844 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11845 | mSecondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11846 | |
| 11847 | // Second down on first window. |
| 11848 | const MotionEvent secondFingerDownEvent = |
| 11849 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11850 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11851 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11852 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 11853 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11854 | .build(); |
| 11855 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11856 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11857 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11858 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11859 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 11860 | mSecondWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11861 | |
| 11862 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11863 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11864 | |
| 11865 | // Move on window. |
| 11866 | const MotionEvent secondFingerMoveEvent = |
| 11867 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 11868 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11869 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 11870 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11871 | .build(); |
| 11872 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11873 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11874 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11875 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11876 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11877 | mWindow->consumeDragEvent(false, 50, 50); |
| 11878 | mSecondWindow->consumeMotionMove(); |
| 11879 | |
| 11880 | // Release the drag pointer should perform drop. |
| 11881 | const MotionEvent secondFingerUpEvent = |
| 11882 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 11883 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11884 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 11885 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11886 | .build(); |
| 11887 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11888 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11889 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11890 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 11891 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken()); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11892 | mWindow->assertNoEvents(); |
| 11893 | mSecondWindow->consumeMotionMove(); |
| 11894 | } |
| 11895 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11896 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11897 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11898 | |
| 11899 | // Update window of second display. |
| 11900 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11901 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11902 | mDispatcher->onWindowInfosChanged( |
| 11903 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 11904 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 11905 | {}, |
| 11906 | 0, |
| 11907 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11908 | |
| 11909 | // Let second display has a touch state. |
| 11910 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11911 | injectMotionEvent(*mDispatcher, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11912 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 11913 | AINPUT_SOURCE_TOUCHSCREEN) |
| 11914 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11915 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11916 | .build())); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 11917 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11918 | // Update window again. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11919 | mDispatcher->onWindowInfosChanged( |
| 11920 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 11921 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 11922 | {}, |
| 11923 | 0, |
| 11924 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11925 | |
| 11926 | // Move on window. |
| 11927 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11928 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11929 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11930 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11931 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11932 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11933 | mWindow->consumeDragEvent(false, 50, 50); |
| 11934 | mSecondWindow->assertNoEvents(); |
| 11935 | |
| 11936 | // Move to another window. |
| 11937 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11938 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11939 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11940 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11941 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11942 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11943 | mWindow->consumeDragEvent(true, 150, 50); |
| 11944 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 11945 | |
| 11946 | // drop to another window. |
| 11947 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11948 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11949 | {150, 50})) |
| 11950 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11951 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 11952 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 11953 | mWindow->assertNoEvents(); |
| 11954 | mSecondWindow->assertNoEvents(); |
| 11955 | } |
| 11956 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11957 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 11958 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 11959 | // Move on window. |
| 11960 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11961 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11962 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 11963 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11964 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11965 | .x(50) |
| 11966 | .y(50)) |
| 11967 | .build())) |
| 11968 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11969 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11970 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11971 | mWindow->consumeDragEvent(false, 50, 50); |
| 11972 | mSecondWindow->assertNoEvents(); |
| 11973 | |
| 11974 | // Move to another window. |
| 11975 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11976 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11977 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 11978 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11979 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11980 | .x(150) |
| 11981 | .y(50)) |
| 11982 | .build())) |
| 11983 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11984 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11985 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11986 | mWindow->consumeDragEvent(true, 150, 50); |
| 11987 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 11988 | |
| 11989 | // drop to another window. |
| 11990 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11991 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11992 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 11993 | .buttonState(0) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11994 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11995 | .x(150) |
| 11996 | .y(50)) |
| 11997 | .build())) |
| 11998 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11999 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 12000 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12001 | mWindow->assertNoEvents(); |
| 12002 | mSecondWindow->assertNoEvents(); |
| 12003 | } |
| 12004 | |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12005 | /** |
| 12006 | * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag |
| 12007 | * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash. |
| 12008 | */ |
| 12009 | TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) { |
| 12010 | // Down on second window |
| 12011 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12012 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12013 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12014 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12015 | |
| 12016 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown()); |
| 12017 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown()); |
| 12018 | |
| 12019 | // Down on first window |
| 12020 | const MotionEvent secondFingerDownEvent = |
| 12021 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12022 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12023 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 12024 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 12025 | .build(); |
| 12026 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12027 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 12028 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12029 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12030 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 12031 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove()); |
| 12032 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1)); |
| 12033 | |
| 12034 | // Start drag on first window |
| 12035 | ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN)); |
| 12036 | |
| 12037 | // Trigger cancel |
| 12038 | mDispatcher->cancelCurrentTouch(); |
| 12039 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12040 | ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 12041 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)); |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12042 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel()); |
| 12043 | |
| 12044 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 12045 | // The D&D finished with nullptr |
| 12046 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
| 12047 | |
| 12048 | // Remove drag window |
| 12049 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
| 12050 | |
| 12051 | // Inject a simple gesture, ensure dispatcher not crashed |
| 12052 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12053 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12054 | ui::LogicalDisplayId::DEFAULT, PointF{50, 50})) |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12055 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12056 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 12057 | |
| 12058 | const MotionEvent moveEvent = |
| 12059 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12060 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12061 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12062 | .build(); |
| 12063 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12064 | injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT, |
| 12065 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12066 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12067 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove()); |
| 12068 | |
| 12069 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12070 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12071 | {50, 50})) |
| 12072 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12073 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp()); |
| 12074 | } |
| 12075 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 12076 | TEST_F(InputDispatcherDragTests, NoDragAndDropWithHoveringPointer) { |
| 12077 | // Start hovering over the window. |
| 12078 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12079 | injectMotionEvent(*mDispatcher, ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12080 | ui::LogicalDisplayId::DEFAULT, {50, 50})); |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 12081 | |
| 12082 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 12083 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 12084 | |
| 12085 | ASSERT_FALSE(startDrag(/*sendDown=*/false)) |
| 12086 | << "Drag and drop should not work with a hovering pointer"; |
| 12087 | } |
| 12088 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12089 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 12090 | |
| 12091 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 12092 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12093 | sp<FakeWindowHandle> window = |
| 12094 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 12095 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12096 | window->setDropInput(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12097 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12098 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12099 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12100 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12101 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12102 | |
| 12103 | // With the flag set, window should not get any input |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12104 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12105 | window->assertNoEvents(); |
| 12106 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12107 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12108 | AINPUT_SOURCE_TOUCHSCREEN, |
| 12109 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 12110 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12111 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 12112 | mDispatcher->waitForIdle(); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12113 | window->assertNoEvents(); |
| 12114 | |
| 12115 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12116 | window->setDropInput(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12117 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12118 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12119 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT)); |
| 12120 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12121 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12122 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12123 | AINPUT_SOURCE_TOUCHSCREEN, |
| 12124 | ui::LogicalDisplayId::DEFAULT)); |
| 12125 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12126 | window->assertNoEvents(); |
| 12127 | } |
| 12128 | |
| 12129 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 12130 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 12131 | std::make_shared<FakeApplicationHandle>(); |
| 12132 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 12133 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12134 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12135 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12136 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12137 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12138 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12139 | sp<FakeWindowHandle> window = |
| 12140 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 12141 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12142 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12143 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12144 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12145 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12146 | mDispatcher->onWindowInfosChanged( |
| 12147 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12148 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12149 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12150 | |
| 12151 | // With the flag set, window should not get any input |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12152 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12153 | window->assertNoEvents(); |
| 12154 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12155 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12156 | AINPUT_SOURCE_TOUCHSCREEN, |
| 12157 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 12158 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12159 | ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12160 | window->assertNoEvents(); |
| 12161 | |
| 12162 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12163 | window->setDropInputIfObscured(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12164 | mDispatcher->onWindowInfosChanged( |
| 12165 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12166 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12167 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT)); |
| 12168 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12169 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12170 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12171 | AINPUT_SOURCE_TOUCHSCREEN, |
| 12172 | ui::LogicalDisplayId::DEFAULT)); |
| 12173 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 12174 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12175 | window->assertNoEvents(); |
| 12176 | } |
| 12177 | |
| 12178 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 12179 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 12180 | std::make_shared<FakeApplicationHandle>(); |
| 12181 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 12182 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12183 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12184 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12185 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12186 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12187 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12188 | sp<FakeWindowHandle> window = |
| 12189 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 12190 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12191 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12192 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12193 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12194 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12195 | mDispatcher->onWindowInfosChanged( |
| 12196 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12197 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12198 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12199 | |
| 12200 | // With the flag set, window should not get any input |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12201 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12202 | window->assertNoEvents(); |
| 12203 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12204 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12205 | AINPUT_SOURCE_TOUCHSCREEN, |
| 12206 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 12207 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12208 | ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12209 | window->assertNoEvents(); |
| 12210 | |
| 12211 | // 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] | 12212 | mDispatcher->onWindowInfosChanged( |
| 12213 | {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12214 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12215 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT)); |
| 12216 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12217 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12218 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12219 | AINPUT_SOURCE_TOUCHSCREEN, |
| 12220 | ui::LogicalDisplayId::DEFAULT)); |
| 12221 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12222 | window->assertNoEvents(); |
| 12223 | } |
| 12224 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12225 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 12226 | protected: |
| 12227 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12228 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12229 | sp<FakeWindowHandle> mWindow; |
| 12230 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12231 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12232 | |
| 12233 | void SetUp() override { |
| 12234 | InputDispatcherTest::SetUp(); |
| 12235 | |
| 12236 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12237 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12238 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", |
| 12239 | ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12240 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12241 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12242 | mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", |
| 12243 | ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12244 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12245 | mThirdWindow = |
| 12246 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 12247 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 12248 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12249 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12250 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12251 | mDispatcher->onWindowInfosChanged( |
| 12252 | {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()}, |
| 12253 | {}, |
| 12254 | 0, |
| 12255 | 0}); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12256 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12257 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12258 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12259 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12260 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12261 | WINDOW_UID, /*hasPermission=*/true, |
| 12262 | ui::LogicalDisplayId::DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12263 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 12264 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12265 | mThirdWindow->assertNoEvents(); |
| 12266 | } |
| 12267 | |
| 12268 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 12269 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12270 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12271 | SECOND_DISPLAY_ID)) { |
| 12272 | mWindow->assertNoEvents(); |
| 12273 | mSecondWindow->assertNoEvents(); |
| 12274 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12275 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12276 | } |
| 12277 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12278 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12279 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 12280 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12281 | ui::LogicalDisplayId::DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12282 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 12283 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12284 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12285 | } |
| 12286 | }; |
| 12287 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12288 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 12289 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12290 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 12291 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12292 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12293 | } |
| 12294 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12295 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 12296 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12297 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 12298 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12299 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12300 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12301 | ownerUid, /*hasPermission=*/false, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12302 | ui::LogicalDisplayId::DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12303 | mWindow->assertNoEvents(); |
| 12304 | mSecondWindow->assertNoEvents(); |
| 12305 | } |
| 12306 | |
| 12307 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 12308 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12309 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 12310 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12311 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12312 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12313 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12314 | } |
| 12315 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12316 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 12317 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12318 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 12319 | windowInfo.ownerPid, windowInfo.ownerUid, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12320 | /*hasPermission=*/true, |
| 12321 | ui::LogicalDisplayId::DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12322 | mWindow->assertNoEvents(); |
| 12323 | mSecondWindow->assertNoEvents(); |
| 12324 | } |
| 12325 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12326 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 12327 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 12328 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 12329 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12330 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12331 | mWindow->assertNoEvents(); |
| 12332 | mSecondWindow->assertNoEvents(); |
| 12333 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 12334 | } |
| 12335 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12336 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 12337 | // Interact with the window first. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12338 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12339 | injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT)) |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12340 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12341 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12342 | |
| 12343 | // Then remove focus. |
| 12344 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12345 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12346 | |
| 12347 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 12348 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 12349 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 12350 | windowInfo.ownerPid, windowInfo.ownerUid, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12351 | /*hasPermission=*/false, |
| 12352 | ui::LogicalDisplayId::DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12353 | } |
| 12354 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12355 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 12356 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12357 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12358 | std::shared_ptr<FakeApplicationHandle> application = |
| 12359 | std::make_shared<FakeApplicationHandle>(); |
| 12360 | std::string name = "Fake Spy "; |
| 12361 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12362 | sp<FakeWindowHandle> spy = |
| 12363 | sp<FakeWindowHandle>::make(application, mDispatcher, name.c_str(), |
| 12364 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12365 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 12366 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12367 | return spy; |
| 12368 | } |
| 12369 | |
| 12370 | sp<FakeWindowHandle> createForeground() { |
| 12371 | std::shared_ptr<FakeApplicationHandle> application = |
| 12372 | std::make_shared<FakeApplicationHandle>(); |
| 12373 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 12374 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12375 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12376 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12377 | return window; |
| 12378 | } |
| 12379 | |
| 12380 | private: |
| 12381 | int mSpyCount{0}; |
| 12382 | }; |
| 12383 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 12384 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12385 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 12386 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 12387 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 12388 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
Siarhei Vishniakou | 21f77bd | 2023-07-18 17:51:35 -0700 | [diff] [blame] | 12389 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 12390 | ScopedSilentDeath _silentDeath; |
| 12391 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12392 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 12393 | spy->setTrustedOverlay(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12394 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 12395 | ".* not a trusted overlay"); |
| 12396 | } |
| 12397 | |
| 12398 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12399 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 12400 | */ |
| 12401 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12402 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12403 | mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12404 | |
| 12405 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12406 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12407 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12408 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12409 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12410 | } |
| 12411 | |
| 12412 | /** |
| 12413 | * Verify the order in which different input windows receive events. The touched foreground window |
| 12414 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 12415 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 12416 | * receive events before ones belows it. |
| 12417 | * |
| 12418 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 12419 | * spy1, spy2, window, spy3. |
| 12420 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 12421 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 12422 | * window. |
| 12423 | */ |
| 12424 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 12425 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12426 | auto spy1 = createSpy(); |
| 12427 | auto spy2 = createSpy(); |
| 12428 | auto spy3 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12429 | mDispatcher->onWindowInfosChanged( |
| 12430 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12431 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 12432 | const size_t numChannels = channels.size(); |
| 12433 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 12434 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12435 | if (!epollFd.ok()) { |
| 12436 | FAIL() << "Failed to create epoll fd"; |
| 12437 | } |
| 12438 | |
| 12439 | for (size_t i = 0; i < numChannels; i++) { |
| 12440 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 12441 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 12442 | FAIL() << "Failed to add fd to epoll"; |
| 12443 | } |
| 12444 | } |
| 12445 | |
| 12446 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12447 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12448 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12449 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12450 | |
| 12451 | std::vector<size_t> eventOrder; |
| 12452 | std::vector<struct epoll_event> events(numChannels); |
| 12453 | for (;;) { |
| 12454 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 12455 | (100ms).count()); |
| 12456 | if (nFds < 0) { |
| 12457 | FAIL() << "Failed to call epoll_wait"; |
| 12458 | } |
| 12459 | if (nFds == 0) { |
| 12460 | break; // epoll_wait timed out |
| 12461 | } |
| 12462 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 12463 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 12464 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12465 | channels[i]->consumeMotionDown(); |
| 12466 | } |
| 12467 | } |
| 12468 | |
| 12469 | // Verify the order in which the events were received. |
| 12470 | EXPECT_EQ(3u, eventOrder.size()); |
| 12471 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 12472 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 12473 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 12474 | } |
| 12475 | |
| 12476 | /** |
| 12477 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 12478 | */ |
| 12479 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 12480 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12481 | auto spy = createSpy(); |
| 12482 | spy->setTouchable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12483 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12484 | |
| 12485 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12486 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12487 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12488 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12489 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12490 | spy->assertNoEvents(); |
| 12491 | } |
| 12492 | |
| 12493 | /** |
| 12494 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 12495 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 12496 | * to the window. |
| 12497 | */ |
| 12498 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 12499 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12500 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12501 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12502 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12503 | |
| 12504 | // Inject an event outside the spy window's touchable region. |
| 12505 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12506 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12507 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12508 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12509 | window->consumeMotionDown(); |
| 12510 | spy->assertNoEvents(); |
| 12511 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12512 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12513 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12514 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12515 | window->consumeMotionUp(); |
| 12516 | spy->assertNoEvents(); |
| 12517 | |
| 12518 | // Inject an event inside the spy window's touchable region. |
| 12519 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12520 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12521 | ui::LogicalDisplayId::DEFAULT, {5, 10})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12522 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12523 | window->consumeMotionDown(); |
| 12524 | spy->consumeMotionDown(); |
| 12525 | } |
| 12526 | |
| 12527 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12528 | * 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] | 12529 | * 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] | 12530 | */ |
| 12531 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 12532 | auto window = createForeground(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12533 | window->setOwnerInfo(gui::Pid{12}, gui::Uid{34}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12534 | auto spy = createSpy(); |
| 12535 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12536 | spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12537 | spy->setFrame(Rect{0, 0, 20, 20}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12538 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12539 | |
| 12540 | // Inject an event outside the spy window's frame and touchable region. |
| 12541 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12542 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12543 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12544 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12545 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 12546 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12547 | } |
| 12548 | |
| 12549 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12550 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 12551 | * pointers that are down within its bounds. |
| 12552 | */ |
| 12553 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 12554 | auto windowLeft = createForeground(); |
| 12555 | windowLeft->setFrame({0, 0, 100, 200}); |
| 12556 | auto windowRight = createForeground(); |
| 12557 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12558 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12559 | spy->setFrame({0, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12560 | mDispatcher->onWindowInfosChanged( |
| 12561 | {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12562 | |
| 12563 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12564 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12565 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12566 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12567 | windowLeft->consumeMotionDown(); |
| 12568 | spy->consumeMotionDown(); |
| 12569 | |
| 12570 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 12571 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12572 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12573 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12574 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12575 | .build(); |
| 12576 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12577 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12578 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12579 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12580 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12581 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12582 | } |
| 12583 | |
| 12584 | /** |
| 12585 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 12586 | * the spy should receive the second pointer with ACTION_DOWN. |
| 12587 | */ |
| 12588 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 12589 | auto window = createForeground(); |
| 12590 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12591 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12592 | spyRight->setFrame({100, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12593 | mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12594 | |
| 12595 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12596 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12597 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12598 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12599 | window->consumeMotionDown(); |
| 12600 | spyRight->assertNoEvents(); |
| 12601 | |
| 12602 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 12603 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12604 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12605 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12606 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12607 | .build(); |
| 12608 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12609 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12610 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12611 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12612 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12613 | spyRight->consumeMotionDown(); |
| 12614 | } |
| 12615 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12616 | /** |
| 12617 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 12618 | * windows should be allowed to control split touch. |
| 12619 | */ |
| 12620 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 12621 | // 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] | 12622 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12623 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 12624 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12625 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12626 | auto window = createForeground(); |
| 12627 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12628 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12629 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12630 | |
| 12631 | // First finger down, no window touched. |
| 12632 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12633 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12634 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12635 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12636 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12637 | window->assertNoEvents(); |
| 12638 | |
| 12639 | // Second finger down on window, the window should receive touch down. |
| 12640 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 12641 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12642 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12643 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12644 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 12645 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12646 | .build(); |
| 12647 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12648 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12649 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12650 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12651 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12652 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12653 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12654 | } |
| 12655 | |
| 12656 | /** |
| 12657 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 12658 | * do not receive key events. |
| 12659 | */ |
| 12660 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12661 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12662 | spy->setFocusable(false); |
| 12663 | |
| 12664 | auto window = createForeground(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12665 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12666 | setFocusedWindow(window); |
| 12667 | window->consumeFocusEvent(true); |
| 12668 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12669 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12670 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12671 | window->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12672 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12673 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12674 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12675 | window->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12676 | |
| 12677 | spy->assertNoEvents(); |
| 12678 | } |
| 12679 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12680 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 12681 | |
| 12682 | /** |
| 12683 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 12684 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 12685 | */ |
| 12686 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 12687 | auto window = createForeground(); |
| 12688 | auto spy1 = createSpy(); |
| 12689 | auto spy2 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12690 | mDispatcher->onWindowInfosChanged( |
| 12691 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12692 | |
| 12693 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12694 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12695 | ui::LogicalDisplayId::DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12696 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12697 | window->consumeMotionDown(); |
| 12698 | spy1->consumeMotionDown(); |
| 12699 | spy2->consumeMotionDown(); |
| 12700 | |
| 12701 | // Pilfer pointers from the second spy window. |
| 12702 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 12703 | spy2->assertNoEvents(); |
| 12704 | spy1->consumeMotionCancel(); |
| 12705 | window->consumeMotionCancel(); |
| 12706 | |
| 12707 | // The rest of the gesture should only be sent to the second spy window. |
| 12708 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12709 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12710 | ui::LogicalDisplayId::DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12711 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12712 | spy2->consumeMotionMove(); |
| 12713 | spy1->assertNoEvents(); |
| 12714 | window->assertNoEvents(); |
| 12715 | } |
| 12716 | |
| 12717 | /** |
| 12718 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 12719 | * in the middle of the gesture. |
| 12720 | */ |
| 12721 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 12722 | auto window = createForeground(); |
| 12723 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12724 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12725 | |
| 12726 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12727 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12728 | ui::LogicalDisplayId::DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12729 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12730 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 12731 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12732 | |
| 12733 | window->releaseChannel(); |
| 12734 | |
| 12735 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 12736 | |
| 12737 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12738 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12739 | ui::LogicalDisplayId::DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12740 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12741 | spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12742 | } |
| 12743 | |
| 12744 | /** |
| 12745 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 12746 | * the spy, but not to any other windows. |
| 12747 | */ |
| 12748 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 12749 | auto spy = createSpy(); |
| 12750 | auto window = createForeground(); |
| 12751 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12752 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12753 | |
| 12754 | // First finger down on the window and the spy. |
| 12755 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12756 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12757 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12758 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12759 | spy->consumeMotionDown(); |
| 12760 | window->consumeMotionDown(); |
| 12761 | |
| 12762 | // Spy window pilfers the pointers. |
| 12763 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 12764 | window->consumeMotionCancel(); |
| 12765 | |
| 12766 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 12767 | const MotionEvent secondFingerDownEvent = |
| 12768 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12769 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12770 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12771 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 12772 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12773 | .build(); |
| 12774 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12775 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12776 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12777 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12778 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12779 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12780 | |
| 12781 | // Third finger goes down outside all windows, so injection should fail. |
| 12782 | const MotionEvent thirdFingerDownEvent = |
| 12783 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12784 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12785 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12786 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 12787 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 12788 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12789 | .build(); |
| 12790 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12791 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12792 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 12793 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12794 | |
| 12795 | spy->assertNoEvents(); |
| 12796 | window->assertNoEvents(); |
| 12797 | } |
| 12798 | |
| 12799 | /** |
| 12800 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 12801 | */ |
| 12802 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 12803 | auto spy = createSpy(); |
| 12804 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 12805 | auto window = createForeground(); |
| 12806 | window->setFrame(Rect(0, 0, 200, 200)); |
| 12807 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12808 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12809 | |
| 12810 | // First finger down on the window only |
| 12811 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12812 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12813 | ui::LogicalDisplayId::DEFAULT, {150, 150})) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12814 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12815 | window->consumeMotionDown(); |
| 12816 | |
| 12817 | // Second finger down on the spy and window |
| 12818 | const MotionEvent secondFingerDownEvent = |
| 12819 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12820 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12821 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12822 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 12823 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12824 | .build(); |
| 12825 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12826 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12827 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12828 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12829 | spy->consumeMotionDown(); |
| 12830 | window->consumeMotionPointerDown(1); |
| 12831 | |
| 12832 | // Third finger down on the spy and window |
| 12833 | const MotionEvent thirdFingerDownEvent = |
| 12834 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12835 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12836 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12837 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 12838 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 12839 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12840 | .build(); |
| 12841 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12842 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12843 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12844 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12845 | spy->consumeMotionPointerDown(1); |
| 12846 | window->consumeMotionPointerDown(2); |
| 12847 | |
| 12848 | // Spy window pilfers the pointers. |
| 12849 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12850 | window->consumeMotionPointerUp(/*idx=*/2, ui::LogicalDisplayId::DEFAULT, |
| 12851 | AMOTION_EVENT_FLAG_CANCELED); |
| 12852 | window->consumeMotionPointerUp(/*idx=*/1, ui::LogicalDisplayId::DEFAULT, |
| 12853 | AMOTION_EVENT_FLAG_CANCELED); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12854 | |
| 12855 | spy->assertNoEvents(); |
| 12856 | window->assertNoEvents(); |
| 12857 | } |
| 12858 | |
| 12859 | /** |
| 12860 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 12861 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 12862 | * window, then that window should receive ACTION_CANCEL. |
| 12863 | */ |
| 12864 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 12865 | auto spy = createSpy(); |
| 12866 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 12867 | auto window = createForeground(); |
| 12868 | window->setFrame(Rect(0, 0, 200, 200)); |
| 12869 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12870 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12871 | |
| 12872 | // First finger down on both spy and window |
| 12873 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12874 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12875 | ui::LogicalDisplayId::DEFAULT, {10, 10})) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12876 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12877 | window->consumeMotionDown(); |
| 12878 | spy->consumeMotionDown(); |
| 12879 | |
| 12880 | // Second finger down on the spy and window |
| 12881 | const MotionEvent secondFingerDownEvent = |
| 12882 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12883 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12884 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12885 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 12886 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12887 | .build(); |
| 12888 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12889 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12890 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12891 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12892 | spy->consumeMotionPointerDown(1); |
| 12893 | window->consumeMotionPointerDown(1); |
| 12894 | |
| 12895 | // Spy window pilfers the pointers. |
| 12896 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 12897 | window->consumeMotionCancel(); |
| 12898 | |
| 12899 | spy->assertNoEvents(); |
| 12900 | window->assertNoEvents(); |
| 12901 | } |
| 12902 | |
| 12903 | /** |
| 12904 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 12905 | * be sent to other windows |
| 12906 | */ |
| 12907 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 12908 | auto spy = createSpy(); |
| 12909 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 12910 | auto window = createForeground(); |
| 12911 | window->setFrame(Rect(0, 0, 200, 200)); |
| 12912 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12913 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12914 | |
| 12915 | // First finger down on both window and spy |
| 12916 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12917 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12918 | ui::LogicalDisplayId::DEFAULT, {10, 10})) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12919 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12920 | window->consumeMotionDown(); |
| 12921 | spy->consumeMotionDown(); |
| 12922 | |
| 12923 | // Spy window pilfers the pointers. |
| 12924 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 12925 | window->consumeMotionCancel(); |
| 12926 | |
| 12927 | // Second finger down on the window only |
| 12928 | const MotionEvent secondFingerDownEvent = |
| 12929 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12930 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12931 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12932 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 12933 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12934 | .build(); |
| 12935 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12936 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12937 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12938 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12939 | window->consumeMotionDown(); |
| 12940 | window->assertNoEvents(); |
| 12941 | |
| 12942 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 12943 | spy->consumeMotionMove(); |
| 12944 | spy->assertNoEvents(); |
| 12945 | } |
| 12946 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 12947 | /** |
| 12948 | * A window on the left and a window on the right. Also, a spy window that's above all of the |
| 12949 | * windows, and spanning both left and right windows. |
| 12950 | * Send simultaneous motion streams from two different devices, one to the left window, and another |
| 12951 | * to the right window. |
| 12952 | * Pilfer from spy window. |
| 12953 | * Check that the pilfering only affects the pointers that are actually being received by the spy. |
| 12954 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 12955 | TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer_legacy) { |
| 12956 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 12957 | sp<FakeWindowHandle> spy = createSpy(); |
| 12958 | spy->setFrame(Rect(0, 0, 200, 200)); |
| 12959 | sp<FakeWindowHandle> leftWindow = createForeground(); |
| 12960 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 12961 | |
| 12962 | sp<FakeWindowHandle> rightWindow = createForeground(); |
| 12963 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 12964 | |
| 12965 | constexpr int32_t stylusDeviceId = 1; |
| 12966 | constexpr int32_t touchDeviceId = 2; |
| 12967 | |
| 12968 | mDispatcher->onWindowInfosChanged( |
| 12969 | {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 12970 | |
| 12971 | // Stylus down on left window and spy |
| 12972 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 12973 | .deviceId(stylusDeviceId) |
| 12974 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 12975 | .build()); |
| 12976 | leftWindow->consumeMotionEvent( |
| 12977 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 12978 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 12979 | |
| 12980 | // Finger down on right window and spy - but spy already has stylus |
| 12981 | mDispatcher->notifyMotion( |
| 12982 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 12983 | .deviceId(touchDeviceId) |
| 12984 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 12985 | .build()); |
| 12986 | rightWindow->consumeMotionEvent( |
| 12987 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 12988 | spy->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 12989 | |
| 12990 | // Act: pilfer from spy. Spy is currently receiving touch events. |
| 12991 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 12992 | leftWindow->consumeMotionEvent( |
| 12993 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 12994 | rightWindow->consumeMotionEvent( |
| 12995 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 12996 | |
| 12997 | // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus |
| 12998 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 12999 | .deviceId(stylusDeviceId) |
| 13000 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52)) |
| 13001 | .build()); |
| 13002 | mDispatcher->notifyMotion( |
| 13003 | MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 13004 | .deviceId(touchDeviceId) |
| 13005 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52)) |
| 13006 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 13007 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 13008 | |
| 13009 | spy->assertNoEvents(); |
| 13010 | leftWindow->assertNoEvents(); |
| 13011 | rightWindow->assertNoEvents(); |
| 13012 | } |
| 13013 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13014 | /** |
| 13015 | * A window on the left and a window on the right. Also, a spy window that's above all of the |
| 13016 | * windows, and spanning both left and right windows. |
| 13017 | * Send simultaneous motion streams from two different devices, one to the left window, and another |
| 13018 | * to the right window. |
| 13019 | * Pilfer from spy window. |
| 13020 | * Check that the pilfering affects all of the pointers that are actually being received by the spy. |
| 13021 | * The spy should receive both the touch and the stylus events after pilfer. |
| 13022 | */ |
| 13023 | TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) { |
| 13024 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 13025 | sp<FakeWindowHandle> spy = createSpy(); |
| 13026 | spy->setFrame(Rect(0, 0, 200, 200)); |
| 13027 | sp<FakeWindowHandle> leftWindow = createForeground(); |
| 13028 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 13029 | |
| 13030 | sp<FakeWindowHandle> rightWindow = createForeground(); |
| 13031 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 13032 | |
| 13033 | constexpr int32_t stylusDeviceId = 1; |
| 13034 | constexpr int32_t touchDeviceId = 2; |
| 13035 | |
| 13036 | mDispatcher->onWindowInfosChanged( |
| 13037 | {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 13038 | |
| 13039 | // Stylus down on left window and spy |
| 13040 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 13041 | .deviceId(stylusDeviceId) |
| 13042 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 13043 | .build()); |
| 13044 | leftWindow->consumeMotionEvent( |
| 13045 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 13046 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 13047 | |
| 13048 | // Finger down on right window and spy |
| 13049 | mDispatcher->notifyMotion( |
| 13050 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 13051 | .deviceId(touchDeviceId) |
| 13052 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 13053 | .build()); |
| 13054 | rightWindow->consumeMotionEvent( |
| 13055 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 13056 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 13057 | |
| 13058 | // Act: pilfer from spy. Spy is currently receiving touch events. |
| 13059 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 13060 | leftWindow->consumeMotionEvent( |
| 13061 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId))); |
| 13062 | rightWindow->consumeMotionEvent( |
| 13063 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 13064 | |
| 13065 | // 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] | 13066 | // Instead of sending the two MOVE events for each input device together, and then receiving |
| 13067 | // them both, process them one at at time. InputConsumer is always in the batching mode, which |
| 13068 | // means that the two MOVE events will be initially put into a batch. Once the events are |
| 13069 | // batched, the 'consume' call may result in any of the MOVE events to be sent first (depending |
| 13070 | // on the implementation of InputConsumer), which would mean that the order of the received |
| 13071 | // events could be different depending on whether there are 1 or 2 events pending in the |
| 13072 | // InputChannel at the time the test calls 'consume'. To make assertions simpler here, and to |
| 13073 | // avoid this confusing behaviour, send and receive each MOVE event separately. |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13074 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 13075 | .deviceId(stylusDeviceId) |
| 13076 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52)) |
| 13077 | .build()); |
Siarhei Vishniakou | 92bca1c | 2024-04-01 14:06:59 -0700 | [diff] [blame] | 13078 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13079 | mDispatcher->notifyMotion( |
| 13080 | MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 13081 | .deviceId(touchDeviceId) |
| 13082 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52)) |
| 13083 | .build()); |
Siarhei Vishniakou | 92bca1c | 2024-04-01 14:06:59 -0700 | [diff] [blame] | 13084 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13085 | |
| 13086 | spy->assertNoEvents(); |
| 13087 | leftWindow->assertNoEvents(); |
| 13088 | rightWindow->assertNoEvents(); |
| 13089 | } |
| 13090 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 13091 | TEST_F(InputDispatcherPilferPointersTest, NoPilferingWithHoveringPointers) { |
| 13092 | auto window = createForeground(); |
| 13093 | auto spy = createSpy(); |
| 13094 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 13095 | |
| 13096 | mDispatcher->notifyMotion( |
| 13097 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 13098 | .deviceId(1) |
| 13099 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(100).y(200)) |
| 13100 | .build()); |
| 13101 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13102 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13103 | |
| 13104 | // Pilfer pointers from the spy window should fail. |
| 13105 | EXPECT_NE(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 13106 | spy->assertNoEvents(); |
| 13107 | window->assertNoEvents(); |
| 13108 | } |
| 13109 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13110 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 13111 | public: |
| 13112 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 13113 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 13114 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13115 | sp<FakeWindowHandle> overlay = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 13116 | "Stylus interceptor window", |
| 13117 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13118 | overlay->setFocusable(false); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13119 | overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 13120 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 13121 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13122 | overlay->setTrustedOverlay(true); |
| 13123 | |
| 13124 | std::shared_ptr<FakeApplicationHandle> application = |
| 13125 | std::make_shared<FakeApplicationHandle>(); |
| 13126 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 13127 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13128 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13129 | window->setFocusable(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13130 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13131 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13132 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13133 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13134 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 13135 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13136 | return {std::move(overlay), std::move(window)}; |
| 13137 | } |
| 13138 | |
| 13139 | void sendFingerEvent(int32_t action) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 13140 | mDispatcher->notifyMotion( |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13141 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13142 | ui::LogicalDisplayId::DEFAULT, {PointF{20, 20}})); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13143 | } |
| 13144 | |
| 13145 | void sendStylusEvent(int32_t action) { |
| 13146 | NotifyMotionArgs motionArgs = |
| 13147 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13148 | ui::LogicalDisplayId::DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 13149 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 13150 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13151 | } |
| 13152 | }; |
| 13153 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 13154 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 13155 | |
| 13156 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
Siarhei Vishniakou | ad3b682 | 2023-06-22 14:17:35 -0700 | [diff] [blame] | 13157 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 13158 | ScopedSilentDeath _silentDeath; |
| 13159 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13160 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 13161 | overlay->setTrustedOverlay(false); |
| 13162 | // 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] | 13163 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 13164 | {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13165 | ".* not a trusted overlay"); |
| 13166 | } |
| 13167 | |
| 13168 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 13169 | auto [overlay, window] = setupStylusOverlayScenario(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13170 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13171 | |
| 13172 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 13173 | overlay->consumeMotionDown(); |
| 13174 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 13175 | overlay->consumeMotionUp(); |
| 13176 | |
| 13177 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 13178 | window->consumeMotionDown(); |
| 13179 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 13180 | window->consumeMotionUp(); |
| 13181 | |
| 13182 | overlay->assertNoEvents(); |
| 13183 | window->assertNoEvents(); |
| 13184 | } |
| 13185 | |
| 13186 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 13187 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 13188 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13189 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13190 | |
| 13191 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 13192 | overlay->consumeMotionDown(); |
| 13193 | window->consumeMotionDown(); |
| 13194 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 13195 | overlay->consumeMotionUp(); |
| 13196 | window->consumeMotionUp(); |
| 13197 | |
| 13198 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 13199 | window->consumeMotionDown(); |
| 13200 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 13201 | window->consumeMotionUp(); |
| 13202 | |
| 13203 | overlay->assertNoEvents(); |
| 13204 | window->assertNoEvents(); |
| 13205 | } |
| 13206 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 13207 | /** |
| 13208 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 13209 | * The scenario is as follows: |
| 13210 | * - The stylus interceptor overlay is configured as a spy window. |
| 13211 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 13212 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 13213 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 13214 | */ |
| 13215 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 13216 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 13217 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13218 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 13219 | |
| 13220 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 13221 | overlay->consumeMotionDown(); |
| 13222 | window->consumeMotionDown(); |
| 13223 | |
| 13224 | // The interceptor pilfers the pointers. |
| 13225 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 13226 | window->consumeMotionCancel(); |
| 13227 | |
| 13228 | // The interceptor configures itself so that it is no longer a spy. |
| 13229 | overlay->setSpy(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13230 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 13231 | |
| 13232 | // It continues to receive the rest of the stylus gesture. |
| 13233 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 13234 | overlay->consumeMotionMove(); |
| 13235 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 13236 | overlay->consumeMotionUp(); |
| 13237 | |
| 13238 | window->assertNoEvents(); |
| 13239 | } |
| 13240 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13241 | struct User { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13242 | gui::Pid mPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 13243 | gui::Uid mUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13244 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 13245 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 13246 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13247 | User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid) |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13248 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 13249 | |
| 13250 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13251 | return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13252 | ui::LogicalDisplayId::DEFAULT, {100, 200}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13253 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 13254 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 13255 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 13256 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 13257 | } |
| 13258 | |
| 13259 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 13260 | return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13261 | ui::LogicalDisplayId::INVALID, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13262 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 13263 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13264 | mPolicyFlags); |
| 13265 | } |
| 13266 | |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13267 | sp<FakeWindowHandle> createWindow(const char* name) const { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13268 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 13269 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13270 | sp<FakeWindowHandle> window = |
| 13271 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, name, |
| 13272 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13273 | window->setOwnerInfo(mPid, mUid); |
| 13274 | return window; |
| 13275 | } |
| 13276 | }; |
| 13277 | |
| 13278 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 13279 | |
| 13280 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13281 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13282 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13283 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13284 | |
| 13285 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 13286 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 13287 | window->consumeMotionDown(); |
| 13288 | |
| 13289 | setFocusedWindow(window); |
| 13290 | window->consumeFocusEvent(true); |
| 13291 | |
| 13292 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 13293 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13294 | window->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13295 | } |
| 13296 | |
| 13297 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13298 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13299 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13300 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13301 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13302 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13303 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 13304 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 13305 | |
| 13306 | setFocusedWindow(window); |
| 13307 | window->consumeFocusEvent(true); |
| 13308 | |
| 13309 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 13310 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 13311 | window->assertNoEvents(); |
| 13312 | } |
| 13313 | |
| 13314 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13315 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13316 | auto window = owner.createWindow("Owned window"); |
| 13317 | auto spy = owner.createWindow("Owned spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13318 | spy->setSpy(true); |
| 13319 | spy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13320 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13321 | |
| 13322 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 13323 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 13324 | spy->consumeMotionDown(); |
| 13325 | window->consumeMotionDown(); |
| 13326 | } |
| 13327 | |
| 13328 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13329 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13330 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13331 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13332 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13333 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13334 | randosSpy->setSpy(true); |
| 13335 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13336 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13337 | |
| 13338 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 13339 | // not receive the event. |
| 13340 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 13341 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 13342 | randosSpy->assertNoEvents(); |
| 13343 | window->consumeMotionDown(); |
| 13344 | } |
| 13345 | |
| 13346 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13347 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13348 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13349 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13350 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13351 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13352 | randosSpy->setSpy(true); |
| 13353 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13354 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13355 | |
| 13356 | // A user that has injection permission can inject into any window. |
| 13357 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13358 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13359 | ui::LogicalDisplayId::DEFAULT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13360 | randosSpy->consumeMotionDown(); |
| 13361 | window->consumeMotionDown(); |
| 13362 | |
| 13363 | setFocusedWindow(randosSpy); |
| 13364 | randosSpy->consumeFocusEvent(true); |
| 13365 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13366 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13367 | randosSpy->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13368 | window->assertNoEvents(); |
| 13369 | } |
| 13370 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 13371 | TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13372 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13373 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13374 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13375 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13376 | auto randosWindow = rando.createWindow("Rando's window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13377 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 13378 | randosWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13379 | mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13380 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 13381 | // 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] | 13382 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 13383 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 13384 | window->consumeMotionDown(); |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 13385 | randosWindow->assertNoEvents(); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13386 | } |
| 13387 | |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13388 | using InputDispatcherPointerInWindowTest = InputDispatcherTest; |
| 13389 | |
| 13390 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWhenHovering) { |
| 13391 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 13392 | |
| 13393 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13394 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13395 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13396 | sp<FakeWindowHandle> right = |
| 13397 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 13398 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13399 | right->setFrame(Rect(100, 0, 200, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13400 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", |
| 13401 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13402 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 13403 | spy->setTrustedOverlay(true); |
| 13404 | spy->setSpy(true); |
| 13405 | |
| 13406 | mDispatcher->onWindowInfosChanged( |
| 13407 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 13408 | |
| 13409 | // Hover into the left window. |
| 13410 | mDispatcher->notifyMotion( |
| 13411 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 13412 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(50).y(50)) |
| 13413 | .build()); |
| 13414 | |
| 13415 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13416 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13417 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13418 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13419 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13420 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13421 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13422 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13423 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13424 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13425 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13426 | /*pointerId=*/0)); |
| 13427 | |
| 13428 | // Hover move to the right window. |
| 13429 | mDispatcher->notifyMotion( |
| 13430 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 13431 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 13432 | .build()); |
| 13433 | |
| 13434 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 13435 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13436 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 13437 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13438 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13439 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13440 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13441 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13442 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13443 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13444 | ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13445 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13446 | /*pointerId=*/0)); |
| 13447 | |
| 13448 | // Stop hovering. |
| 13449 | mDispatcher->notifyMotion( |
| 13450 | MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 13451 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 13452 | .build()); |
| 13453 | |
| 13454 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 13455 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 13456 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13457 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13458 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13459 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13460 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13461 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13462 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13463 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13464 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13465 | /*pointerId=*/0)); |
| 13466 | } |
| 13467 | |
| 13468 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWithSplitTouch) { |
| 13469 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 13470 | |
| 13471 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13472 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13473 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13474 | sp<FakeWindowHandle> right = |
| 13475 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 13476 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13477 | right->setFrame(Rect(100, 0, 200, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13478 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", |
| 13479 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13480 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 13481 | spy->setTrustedOverlay(true); |
| 13482 | spy->setSpy(true); |
| 13483 | |
| 13484 | mDispatcher->onWindowInfosChanged( |
| 13485 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 13486 | |
| 13487 | // First pointer down on left window. |
| 13488 | mDispatcher->notifyMotion( |
| 13489 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 13490 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 13491 | .build()); |
| 13492 | |
| 13493 | left->consumeMotionDown(); |
| 13494 | spy->consumeMotionDown(); |
| 13495 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13496 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13497 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13498 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13499 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13500 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13501 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13502 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13503 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13504 | /*pointerId=*/0)); |
| 13505 | |
| 13506 | // Second pointer down on right window. |
| 13507 | mDispatcher->notifyMotion( |
| 13508 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 13509 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 13510 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 13511 | .build()); |
| 13512 | |
| 13513 | left->consumeMotionMove(); |
| 13514 | right->consumeMotionDown(); |
| 13515 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 13516 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13517 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13518 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13519 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13520 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13521 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13522 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13523 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13524 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13525 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13526 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13527 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13528 | /*pointerId=*/1)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13529 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13530 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13531 | /*pointerId=*/1)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13532 | ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13533 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13534 | /*pointerId=*/1)); |
| 13535 | |
| 13536 | // Second pointer up. |
| 13537 | mDispatcher->notifyMotion( |
| 13538 | MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 13539 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 13540 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 13541 | .build()); |
| 13542 | |
| 13543 | left->consumeMotionMove(); |
| 13544 | right->consumeMotionUp(); |
| 13545 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 13546 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13547 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13548 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13549 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13550 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13551 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13552 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13553 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13554 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13555 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13556 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13557 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13558 | /*pointerId=*/1)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13559 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13560 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13561 | /*pointerId=*/1)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13562 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13563 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13564 | /*pointerId=*/1)); |
| 13565 | |
| 13566 | // First pointer up. |
| 13567 | mDispatcher->notifyMotion( |
| 13568 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 13569 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 13570 | .build()); |
| 13571 | |
| 13572 | left->consumeMotionUp(); |
| 13573 | spy->consumeMotionUp(); |
| 13574 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13575 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13576 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13577 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13578 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13579 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13580 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13581 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13582 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13583 | /*pointerId=*/0)); |
| 13584 | } |
| 13585 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13586 | TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse_legacy) { |
| 13587 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13588 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 13589 | |
| 13590 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13591 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13592 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13593 | sp<FakeWindowHandle> right = |
| 13594 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 13595 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13596 | right->setFrame(Rect(100, 0, 200, 100)); |
| 13597 | |
| 13598 | mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 13599 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13600 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13601 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13602 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13603 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13604 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13605 | /*pointerId=*/0)); |
| 13606 | |
| 13607 | // Hover move into the window. |
| 13608 | mDispatcher->notifyMotion( |
| 13609 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 13610 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50)) |
| 13611 | .rawXCursorPosition(50) |
| 13612 | .rawYCursorPosition(50) |
| 13613 | .deviceId(DEVICE_ID) |
| 13614 | .build()); |
| 13615 | |
| 13616 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13617 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13618 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13619 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13620 | /*pointerId=*/0)); |
| 13621 | |
| 13622 | // Move the mouse with another device. This cancels the hovering pointer from the first device. |
| 13623 | mDispatcher->notifyMotion( |
| 13624 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 13625 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50)) |
| 13626 | .rawXCursorPosition(51) |
| 13627 | .rawYCursorPosition(50) |
| 13628 | .deviceId(SECOND_DEVICE_ID) |
| 13629 | .build()); |
| 13630 | |
| 13631 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 13632 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13633 | |
| 13634 | // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets |
| 13635 | // a HOVER_EXIT from the first device. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13636 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13637 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13638 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13639 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13640 | SECOND_DEVICE_ID, |
| 13641 | /*pointerId=*/0)); |
| 13642 | |
| 13643 | // Move the mouse outside the window. Document the current behavior, where the window does not |
| 13644 | // receive HOVER_EXIT even though the mouse left the window. |
| 13645 | mDispatcher->notifyMotion( |
| 13646 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 13647 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50)) |
| 13648 | .rawXCursorPosition(150) |
| 13649 | .rawYCursorPosition(50) |
| 13650 | .deviceId(SECOND_DEVICE_ID) |
| 13651 | .build()); |
| 13652 | |
| 13653 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 13654 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13655 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13656 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13657 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13658 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13659 | SECOND_DEVICE_ID, |
| 13660 | /*pointerId=*/0)); |
| 13661 | } |
| 13662 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13663 | /** |
| 13664 | * TODO(b/313689709) - correctly support multiple mouse devices, because they should be controlling |
| 13665 | * the same cursor, and therefore have a shared motion event stream. |
| 13666 | */ |
| 13667 | TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse) { |
| 13668 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 13669 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 13670 | |
| 13671 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13672 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13673 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13674 | sp<FakeWindowHandle> right = |
| 13675 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 13676 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13677 | right->setFrame(Rect(100, 0, 200, 100)); |
| 13678 | |
| 13679 | mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 13680 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13681 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13682 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13683 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13684 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13685 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13686 | /*pointerId=*/0)); |
| 13687 | |
| 13688 | // Hover move into the window. |
| 13689 | mDispatcher->notifyMotion( |
| 13690 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 13691 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50)) |
| 13692 | .rawXCursorPosition(50) |
| 13693 | .rawYCursorPosition(50) |
| 13694 | .deviceId(DEVICE_ID) |
| 13695 | .build()); |
| 13696 | |
| 13697 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13698 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13699 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13700 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13701 | /*pointerId=*/0)); |
| 13702 | |
| 13703 | // Move the mouse with another device |
| 13704 | mDispatcher->notifyMotion( |
| 13705 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 13706 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50)) |
| 13707 | .rawXCursorPosition(51) |
| 13708 | .rawYCursorPosition(50) |
| 13709 | .deviceId(SECOND_DEVICE_ID) |
| 13710 | .build()); |
| 13711 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13712 | |
| 13713 | // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets |
| 13714 | // a HOVER_EXIT from the first device. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13715 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13716 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13717 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13718 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13719 | SECOND_DEVICE_ID, |
| 13720 | /*pointerId=*/0)); |
| 13721 | |
| 13722 | // Move the mouse outside the window. Document the current behavior, where the window does not |
| 13723 | // receive HOVER_EXIT even though the mouse left the window. |
| 13724 | mDispatcher->notifyMotion( |
| 13725 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 13726 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50)) |
| 13727 | .rawXCursorPosition(150) |
| 13728 | .rawYCursorPosition(50) |
| 13729 | .deviceId(SECOND_DEVICE_ID) |
| 13730 | .build()); |
| 13731 | |
| 13732 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13733 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13734 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13735 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13736 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13737 | SECOND_DEVICE_ID, |
| 13738 | /*pointerId=*/0)); |
| 13739 | } |
| 13740 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 13741 | } // namespace android::inputdispatcher |