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"; |
Prabir Pradhan | 33cfc6d | 2024-06-11 20:17:44 +0000 | [diff] [blame] | 1285 | foregroundWindow->consumeMotionPointerUp(/*pointerIdx=*/0, |
| 1286 | WithDisplayId(ui::LogicalDisplayId::DEFAULT)); |
| 1287 | wallpaperWindow->consumeMotionPointerUp(/*pointerIdx=*/0, |
| 1288 | AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT), |
| 1289 | WithFlags(EXPECTED_WALLPAPER_FLAGS))); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1290 | |
| 1291 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1292 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1293 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1294 | AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1295 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1296 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1297 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1298 | .x(100) |
| 1299 | .y(100)) |
| 1300 | .build(), |
| 1301 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1302 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1303 | foregroundWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
| 1304 | wallpaperWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | /** |
| 1308 | * Two windows: a window on the left and window on the right. |
| 1309 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1310 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1311 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1312 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1313 | * ACTION_POINTER_DOWN(1). |
| 1314 | */ |
| 1315 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1316 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1317 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 1318 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1319 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1320 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1321 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1322 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 1323 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1324 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1325 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1326 | |
| 1327 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1328 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 1329 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1330 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1331 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1332 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1333 | mDispatcher->onWindowInfosChanged( |
| 1334 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 1335 | {}, |
| 1336 | 0, |
| 1337 | 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1338 | |
| 1339 | // Touch down on left window |
| 1340 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1341 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 1342 | ui::LogicalDisplayId::DEFAULT, {100, 100})) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1343 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1344 | |
| 1345 | // Both foreground window and its wallpaper should receive the touch down |
| 1346 | leftWindow->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1347 | wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1348 | |
| 1349 | // Second finger down on the right window |
| 1350 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1351 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1352 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1353 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1354 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1355 | .build(); |
| 1356 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1357 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1358 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1359 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1360 | |
| 1361 | leftWindow->consumeMotionMove(); |
| 1362 | // Since the touch is split, right window gets ACTION_DOWN |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1363 | rightWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 1364 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 1365 | EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1366 | |
| 1367 | // Now, leftWindow, which received the first finger, disappears. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1368 | mDispatcher->onWindowInfosChanged( |
| 1369 | {{*rightWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1370 | leftWindow->consumeMotionCancel(); |
| 1371 | // 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] | 1372 | wallpaperWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1373 | |
| 1374 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 1375 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 1376 | const MotionEvent secondFingerMoveEvent = |
| 1377 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1378 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1379 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1380 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1381 | .build(); |
| 1382 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1383 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1384 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 1385 | rightWindow->consumeMotionMove(); |
| 1386 | |
| 1387 | leftWindow->assertNoEvents(); |
| 1388 | rightWindow->assertNoEvents(); |
| 1389 | wallpaperWindow->assertNoEvents(); |
| 1390 | } |
| 1391 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1392 | /** |
| 1393 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 1394 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 1395 | * The right window should receive ACTION_DOWN. |
| 1396 | */ |
| 1397 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1398 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1399 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 1400 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1401 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1402 | leftWindow->setDupTouchToWallpaper(true); |
| 1403 | leftWindow->setSlippery(true); |
| 1404 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1405 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 1406 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1407 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1408 | |
| 1409 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1410 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 1411 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1412 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1413 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1414 | mDispatcher->onWindowInfosChanged( |
| 1415 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 1416 | {}, |
| 1417 | 0, |
| 1418 | 0}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1419 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1420 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1421 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1422 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 1423 | ui::LogicalDisplayId::DEFAULT, {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1424 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1425 | |
| 1426 | // Both foreground window and its wallpaper should receive the touch down |
| 1427 | leftWindow->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1428 | wallpaperWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1429 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1430 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1431 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1432 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1433 | ui::LogicalDisplayId::DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1434 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1435 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1436 | leftWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1437 | rightWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 1438 | wallpaperWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1441 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1442 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 1443 | * interactive, it might stop sending this flag. |
| 1444 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 1445 | * to have a consistent input stream. |
| 1446 | * |
| 1447 | * Test procedure: |
| 1448 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 1449 | * DOWN (new gesture). |
| 1450 | * |
| 1451 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 1452 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 1453 | * |
| 1454 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 1455 | * regular window below) to emulate the actual situation where it happens on the device. |
| 1456 | */ |
| 1457 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 1458 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1459 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 1460 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1461 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1462 | spyWindow->setTrustedOverlay(true); |
| 1463 | spyWindow->setSpy(true); |
| 1464 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1465 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1466 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1467 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1468 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1469 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1470 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1471 | |
| 1472 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1473 | mDispatcher->notifyMotion( |
| 1474 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1475 | .deviceId(touchDeviceId) |
| 1476 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1477 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1478 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1479 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1480 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1481 | .deviceId(touchDeviceId) |
| 1482 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1483 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1484 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 1485 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1486 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1487 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1488 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1489 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1490 | |
| 1491 | // Cancel the current gesture. Send the cancel without the default policy flags. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1492 | mDispatcher->notifyMotion( |
| 1493 | MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 1494 | .deviceId(touchDeviceId) |
| 1495 | .policyFlags(0) |
| 1496 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1497 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 1498 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1499 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 1500 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 1501 | |
| 1502 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 1503 | // 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] | 1504 | mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1505 | |
| 1506 | // Start new gesture |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1507 | mDispatcher->notifyMotion( |
| 1508 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1509 | .deviceId(touchDeviceId) |
| 1510 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1511 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1512 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1513 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1514 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1515 | |
| 1516 | // No more events |
| 1517 | spyWindow->assertNoEvents(); |
| 1518 | window->assertNoEvents(); |
| 1519 | } |
| 1520 | |
| 1521 | /** |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 1522 | * Same as the above 'TwoPointerCancelInconsistentPolicy' test, but for hovers. |
| 1523 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 1524 | * interactive, it might stop sending this flag. |
| 1525 | * We've already ensured the consistency of the touch event in this case, and we should also ensure |
| 1526 | * the consistency of the hover event in this case. |
| 1527 | * |
| 1528 | * Test procedure: |
| 1529 | * HOVER_ENTER -> HOVER_MOVE -> (stop sending POLICY_FLAG_PASS_TO_USER) -> HOVER_EXIT |
| 1530 | * HOVER_ENTER -> HOVER_MOVE -> HOVER_EXIT |
| 1531 | * |
| 1532 | * We expect to receive two full streams of hover events. |
| 1533 | */ |
| 1534 | TEST_F(InputDispatcherTest, HoverEventInconsistentPolicy) { |
| 1535 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1536 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1537 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1538 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 1539 | window->setFrame(Rect(0, 0, 300, 300)); |
| 1540 | |
| 1541 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1542 | |
| 1543 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1544 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1545 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 1546 | .build()); |
| 1547 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1548 | |
| 1549 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1550 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1551 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 1552 | .build()); |
| 1553 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 1554 | |
| 1555 | // Send hover exit without the default policy flags. |
| 1556 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 1557 | .policyFlags(0) |
| 1558 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 1559 | .build()); |
| 1560 | |
| 1561 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1562 | |
| 1563 | // Send a simple hover event stream, ensure dispatcher not crashed and window can receive |
| 1564 | // right event. |
| 1565 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1566 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1567 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(200).y(201)) |
| 1568 | .build()); |
| 1569 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1570 | |
| 1571 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1572 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1573 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 1574 | .build()); |
| 1575 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 1576 | |
| 1577 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 1578 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1579 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 1580 | .build()); |
| 1581 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1582 | } |
| 1583 | |
| 1584 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1585 | * Two windows: a window on the left and a window on the right. |
| 1586 | * Mouse is hovered from the right window into the left window. |
| 1587 | * Next, we tap on the left window, where the cursor was last seen. |
| 1588 | * The second tap is done onto the right window. |
| 1589 | * The mouse and tap are from two different devices. |
| 1590 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 1591 | * explicitly helps during debugging. |
| 1592 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 1593 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 1594 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1595 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap_legacy) { |
| 1596 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
| 1597 | |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1598 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1599 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 1600 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1601 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1602 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1603 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 1604 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1605 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 1606 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1607 | mDispatcher->onWindowInfosChanged( |
| 1608 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1609 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 1610 | // stale. |
| 1611 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1612 | const int32_t mouseDeviceId = 6; |
| 1613 | const int32_t touchDeviceId = 4; |
| 1614 | // Move the cursor from right |
| 1615 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1616 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1617 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1618 | AINPUT_SOURCE_MOUSE) |
| 1619 | .deviceId(mouseDeviceId) |
| 1620 | .downTime(baseTime + 10) |
| 1621 | .eventTime(baseTime + 20) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1622 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1623 | .build())); |
| 1624 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 1625 | |
| 1626 | // .. to the left window |
| 1627 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1628 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1629 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1630 | AINPUT_SOURCE_MOUSE) |
| 1631 | .deviceId(mouseDeviceId) |
| 1632 | .downTime(baseTime + 10) |
| 1633 | .eventTime(baseTime + 30) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1634 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1635 | .build())); |
| 1636 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 1637 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 1638 | // Now tap the left window |
| 1639 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1640 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1641 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 1642 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1643 | .deviceId(touchDeviceId) |
| 1644 | .downTime(baseTime + 40) |
| 1645 | .eventTime(baseTime + 40) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1646 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1647 | .build())); |
| 1648 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 1649 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1650 | |
| 1651 | // release tap |
| 1652 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1653 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1654 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1655 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1656 | .deviceId(touchDeviceId) |
| 1657 | .downTime(baseTime + 40) |
| 1658 | .eventTime(baseTime + 50) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1659 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1660 | .build())); |
| 1661 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 1662 | |
| 1663 | // Tap the window on the right |
| 1664 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1665 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1666 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 1667 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1668 | .deviceId(touchDeviceId) |
| 1669 | .downTime(baseTime + 60) |
| 1670 | .eventTime(baseTime + 60) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1671 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1672 | .build())); |
| 1673 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1674 | |
| 1675 | // release tap |
| 1676 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1677 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1678 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1679 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1680 | .deviceId(touchDeviceId) |
| 1681 | .downTime(baseTime + 60) |
| 1682 | .eventTime(baseTime + 70) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1683 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1684 | .build())); |
| 1685 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 1686 | |
| 1687 | // No more events |
| 1688 | leftWindow->assertNoEvents(); |
| 1689 | rightWindow->assertNoEvents(); |
| 1690 | } |
| 1691 | |
| 1692 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1693 | * Two windows: a window on the left and a window on the right. |
| 1694 | * Mouse is hovered from the right window into the left window. |
| 1695 | * Next, we tap on the left window, where the cursor was last seen. |
| 1696 | * The second tap is done onto the right window. |
| 1697 | * The mouse and tap are from two different devices. |
| 1698 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 1699 | * explicitly helps during debugging. |
| 1700 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 1701 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 1702 | */ |
| 1703 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 1704 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 1705 | |
| 1706 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1707 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 1708 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1709 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1710 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1711 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 1712 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1713 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 1714 | |
| 1715 | mDispatcher->onWindowInfosChanged( |
| 1716 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 1717 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 1718 | // stale. |
| 1719 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1720 | const int32_t mouseDeviceId = 6; |
| 1721 | const int32_t touchDeviceId = 4; |
| 1722 | // Move the cursor from right |
| 1723 | mDispatcher->notifyMotion( |
| 1724 | MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 1725 | .deviceId(mouseDeviceId) |
| 1726 | .downTime(baseTime + 10) |
| 1727 | .eventTime(baseTime + 20) |
| 1728 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100)) |
| 1729 | .build()); |
| 1730 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 1731 | |
| 1732 | // .. to the left window |
| 1733 | mDispatcher->notifyMotion( |
| 1734 | MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 1735 | .deviceId(mouseDeviceId) |
| 1736 | .downTime(baseTime + 10) |
| 1737 | .eventTime(baseTime + 30) |
| 1738 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100)) |
| 1739 | .build()); |
| 1740 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 1741 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 1742 | // Now tap the left window |
| 1743 | mDispatcher->notifyMotion( |
| 1744 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1745 | .deviceId(touchDeviceId) |
| 1746 | .downTime(baseTime + 40) |
| 1747 | .eventTime(baseTime + 40) |
| 1748 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1749 | .build()); |
| 1750 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1751 | |
| 1752 | // release tap |
| 1753 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1754 | .deviceId(touchDeviceId) |
| 1755 | .downTime(baseTime + 40) |
| 1756 | .eventTime(baseTime + 50) |
| 1757 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1758 | .build()); |
| 1759 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 1760 | |
| 1761 | // Tap the window on the right |
| 1762 | mDispatcher->notifyMotion( |
| 1763 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1764 | .deviceId(touchDeviceId) |
| 1765 | .downTime(baseTime + 60) |
| 1766 | .eventTime(baseTime + 60) |
| 1767 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 1768 | .build()); |
| 1769 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1770 | |
| 1771 | // release tap |
| 1772 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1773 | .deviceId(touchDeviceId) |
| 1774 | .downTime(baseTime + 60) |
| 1775 | .eventTime(baseTime + 70) |
| 1776 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 1777 | .build()); |
| 1778 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 1779 | |
| 1780 | // No more events |
| 1781 | leftWindow->assertNoEvents(); |
| 1782 | rightWindow->assertNoEvents(); |
| 1783 | } |
| 1784 | |
| 1785 | /** |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1786 | * Start hovering in a window. While this hover is still active, make another window appear on top. |
| 1787 | * The top, obstructing window has no input channel, so it's not supposed to receive input. |
| 1788 | * While the top window is present, the hovering is stopped. |
| 1789 | * Later, hovering gets resumed again. |
| 1790 | * Ensure that new hover gesture is handled correctly. |
| 1791 | * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly |
| 1792 | * to the window that's currently being hovered over. |
| 1793 | */ |
| 1794 | TEST_F(InputDispatcherTest, HoverWhileWindowAppears) { |
| 1795 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1796 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1797 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1798 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1799 | |
| 1800 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1801 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1802 | |
| 1803 | // Start hovering in the window |
| 1804 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1805 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1806 | .build()); |
| 1807 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1808 | |
| 1809 | // Now, an obscuring window appears! |
| 1810 | sp<FakeWindowHandle> obscuringWindow = |
| 1811 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1812 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1813 | /*createInputChannel=*/false); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1814 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1815 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 1816 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 1817 | obscuringWindow->setNoInputChannel(true); |
| 1818 | obscuringWindow->setFocusable(false); |
| 1819 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1820 | mDispatcher->onWindowInfosChanged( |
| 1821 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1822 | |
| 1823 | // While this new obscuring window is present, the hovering is stopped |
| 1824 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 1825 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1826 | .build()); |
| 1827 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1828 | |
| 1829 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1830 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1831 | |
| 1832 | // And a new hover gesture starts. |
| 1833 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1834 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1835 | .build()); |
| 1836 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1837 | } |
| 1838 | |
| 1839 | /** |
| 1840 | * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to |
| 1841 | * the obscuring window. |
| 1842 | */ |
| 1843 | TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) { |
| 1844 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1845 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1846 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1847 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1848 | |
| 1849 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1850 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1851 | |
| 1852 | // Start hovering in the window |
| 1853 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1854 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1855 | .build()); |
| 1856 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1857 | |
| 1858 | // Now, an obscuring window appears! |
| 1859 | sp<FakeWindowHandle> obscuringWindow = |
| 1860 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1861 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1862 | /*createInputChannel=*/false); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1863 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1864 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 1865 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 1866 | obscuringWindow->setNoInputChannel(true); |
| 1867 | obscuringWindow->setFocusable(false); |
| 1868 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1869 | mDispatcher->onWindowInfosChanged( |
| 1870 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1871 | |
| 1872 | // While this new obscuring window is present, the hovering continues. The event can't go to the |
| 1873 | // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window. |
| 1874 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1875 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1876 | .build()); |
| 1877 | obscuringWindow->assertNoEvents(); |
| 1878 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1879 | |
| 1880 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1881 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1882 | |
| 1883 | // Hovering continues in the same position. The hovering pointer re-enters the bottom window, |
| 1884 | // so it should generate a HOVER_ENTER |
| 1885 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1886 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1887 | .build()); |
| 1888 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1889 | |
| 1890 | // Now the MOVE should be getting dispatched normally |
| 1891 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1892 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 1893 | .build()); |
| 1894 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 1895 | } |
| 1896 | |
| 1897 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1898 | * Hover mouse over a window, and then send ACTION_SCROLL. Ensure both the hover and the scroll |
| 1899 | * events are delivered to the window. |
| 1900 | */ |
| 1901 | TEST_F(InputDispatcherTest, HoverMoveAndScroll) { |
| 1902 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1903 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1904 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1905 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1906 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1907 | |
| 1908 | // Start hovering in the window |
| 1909 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 1910 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 1911 | .build()); |
| 1912 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1913 | |
| 1914 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 1915 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 1916 | .build()); |
| 1917 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 1918 | |
| 1919 | // Scroll with the mouse |
| 1920 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_SCROLL, AINPUT_SOURCE_MOUSE) |
| 1921 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 1922 | .build()); |
| 1923 | window->consumeMotionEvent(WithMotionAction(ACTION_SCROLL)); |
| 1924 | } |
| 1925 | |
| 1926 | using InputDispatcherMultiDeviceTest = InputDispatcherTest; |
| 1927 | |
| 1928 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1929 | * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that |
| 1930 | * touch is dropped, because stylus should be preferred over touch. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1931 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1932 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownBlocksTouchDown) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1933 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1934 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1935 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1936 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1937 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1938 | |
| 1939 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1940 | |
| 1941 | constexpr int32_t touchDeviceId = 4; |
| 1942 | constexpr int32_t stylusDeviceId = 2; |
| 1943 | |
| 1944 | // Stylus down |
| 1945 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 1946 | .deviceId(stylusDeviceId) |
| 1947 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 1948 | .build()); |
| 1949 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 1950 | |
| 1951 | // Touch down |
| 1952 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1953 | .deviceId(touchDeviceId) |
| 1954 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 1955 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1956 | |
| 1957 | // Touch move |
| 1958 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1959 | .deviceId(touchDeviceId) |
| 1960 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 1961 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1962 | // Touch is ignored because stylus is already down |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1963 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1964 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1965 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 1966 | .deviceId(stylusDeviceId) |
| 1967 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 1968 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 1969 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 1970 | WithCoords(101, 111))); |
| 1971 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 1972 | window->assertNoEvents(); |
| 1973 | } |
| 1974 | |
| 1975 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1976 | * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that |
| 1977 | * touch is not dropped, because multiple devices are allowed to be active in the same window. |
| 1978 | */ |
| 1979 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownDoesNotBlockTouchDown) { |
| 1980 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 1981 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 1982 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 1983 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 1984 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1985 | |
| 1986 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1987 | |
| 1988 | constexpr int32_t touchDeviceId = 4; |
| 1989 | constexpr int32_t stylusDeviceId = 2; |
| 1990 | |
| 1991 | // Stylus down |
| 1992 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 1993 | .deviceId(stylusDeviceId) |
| 1994 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 1995 | .build()); |
| 1996 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 1997 | |
| 1998 | // Touch down |
| 1999 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2000 | .deviceId(touchDeviceId) |
| 2001 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2002 | .build()); |
| 2003 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2004 | |
| 2005 | // Touch move |
| 2006 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2007 | .deviceId(touchDeviceId) |
| 2008 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2009 | .build()); |
| 2010 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2011 | |
| 2012 | // Stylus move |
| 2013 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2014 | .deviceId(stylusDeviceId) |
| 2015 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2016 | .build()); |
| 2017 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2018 | WithCoords(101, 111))); |
| 2019 | |
| 2020 | window->assertNoEvents(); |
| 2021 | } |
| 2022 | |
| 2023 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2024 | * 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] | 2025 | * 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] | 2026 | * Similar test as above, but with added SPY window. |
| 2027 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2028 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyBlocksTouchDown) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2029 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2030 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2031 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2032 | ui::LogicalDisplayId::DEFAULT); |
| 2033 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 2034 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2035 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2036 | spyWindow->setTrustedOverlay(true); |
| 2037 | spyWindow->setSpy(true); |
| 2038 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2039 | |
| 2040 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 2041 | |
| 2042 | constexpr int32_t touchDeviceId = 4; |
| 2043 | constexpr int32_t stylusDeviceId = 2; |
| 2044 | |
| 2045 | // Stylus down |
| 2046 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2047 | .deviceId(stylusDeviceId) |
| 2048 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2049 | .build()); |
| 2050 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2051 | spyWindow->consumeMotionEvent( |
| 2052 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2053 | |
| 2054 | // Touch down |
| 2055 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2056 | .deviceId(touchDeviceId) |
| 2057 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2058 | .build()); |
| 2059 | |
| 2060 | // Touch move |
| 2061 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2062 | .deviceId(touchDeviceId) |
| 2063 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2064 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2065 | |
| 2066 | // Touch is ignored because stylus is already down |
| 2067 | |
| 2068 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2069 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2070 | .deviceId(stylusDeviceId) |
| 2071 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2072 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2073 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2074 | WithCoords(101, 111))); |
| 2075 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2076 | WithCoords(101, 111))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2077 | |
| 2078 | window->assertNoEvents(); |
| 2079 | spyWindow->assertNoEvents(); |
| 2080 | } |
| 2081 | |
| 2082 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2083 | * One window and one spy window. Stylus down on the window. Next, touch from another device goes |
| 2084 | * down. Ensure that touch is not dropped, because multiple devices can be active at the same time. |
| 2085 | * Similar test as above, but with added SPY window. |
| 2086 | */ |
| 2087 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyDoesNotBlockTouchDown) { |
| 2088 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2089 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2090 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2091 | ui::LogicalDisplayId::DEFAULT); |
| 2092 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 2093 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2094 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2095 | spyWindow->setTrustedOverlay(true); |
| 2096 | spyWindow->setSpy(true); |
| 2097 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2098 | |
| 2099 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 2100 | |
| 2101 | constexpr int32_t touchDeviceId = 4; |
| 2102 | constexpr int32_t stylusDeviceId = 2; |
| 2103 | |
| 2104 | // Stylus down |
| 2105 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2106 | .deviceId(stylusDeviceId) |
| 2107 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2108 | .build()); |
| 2109 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2110 | spyWindow->consumeMotionEvent( |
| 2111 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2112 | |
| 2113 | // Touch down |
| 2114 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2115 | .deviceId(touchDeviceId) |
| 2116 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2117 | .build()); |
| 2118 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2119 | spyWindow->consumeMotionEvent( |
| 2120 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2121 | |
| 2122 | // Touch move |
| 2123 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2124 | .deviceId(touchDeviceId) |
| 2125 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2126 | .build()); |
| 2127 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2128 | spyWindow->consumeMotionEvent( |
| 2129 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2130 | |
| 2131 | // Subsequent stylus movements are delivered correctly |
| 2132 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2133 | .deviceId(stylusDeviceId) |
| 2134 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2135 | .build()); |
| 2136 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2137 | WithCoords(101, 111))); |
| 2138 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2139 | WithCoords(101, 111))); |
| 2140 | |
| 2141 | window->assertNoEvents(); |
| 2142 | spyWindow->assertNoEvents(); |
| 2143 | } |
| 2144 | |
| 2145 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2146 | * 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] | 2147 | * touch is dropped, because stylus hover takes precedence. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2148 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2149 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverBlocksTouchDown) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2150 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2151 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2152 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2153 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2154 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2155 | |
| 2156 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2157 | |
| 2158 | constexpr int32_t touchDeviceId = 4; |
| 2159 | constexpr int32_t stylusDeviceId = 2; |
| 2160 | |
| 2161 | // Stylus down on the window |
| 2162 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2163 | .deviceId(stylusDeviceId) |
| 2164 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2165 | .build()); |
| 2166 | window->consumeMotionEvent( |
| 2167 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2168 | |
| 2169 | // Touch down on window |
| 2170 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2171 | .deviceId(touchDeviceId) |
| 2172 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2173 | .build()); |
| 2174 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2175 | .deviceId(touchDeviceId) |
| 2176 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2177 | .build()); |
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 | // Touch is ignored because stylus is hovering |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2180 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2181 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2182 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2183 | .deviceId(stylusDeviceId) |
| 2184 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2185 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2186 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2187 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2188 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2189 | // and subsequent touches continue to be ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2190 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2191 | .deviceId(touchDeviceId) |
| 2192 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2193 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2194 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2198 | * One window. Stylus hover on the window. Next, touch from another device goes down. Ensure that |
| 2199 | * touch is not dropped, because stylus hover and touch can be both active at the same time. |
| 2200 | */ |
| 2201 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverDoesNotBlockTouchDown) { |
| 2202 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2203 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2204 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2205 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2206 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2207 | |
| 2208 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2209 | |
| 2210 | constexpr int32_t touchDeviceId = 4; |
| 2211 | constexpr int32_t stylusDeviceId = 2; |
| 2212 | |
| 2213 | // Stylus down on the window |
| 2214 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2215 | .deviceId(stylusDeviceId) |
| 2216 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2217 | .build()); |
| 2218 | window->consumeMotionEvent( |
| 2219 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2220 | |
| 2221 | // Touch down on window |
| 2222 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2223 | .deviceId(touchDeviceId) |
| 2224 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2225 | .build()); |
| 2226 | // Touch move on window |
| 2227 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2228 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2229 | .deviceId(touchDeviceId) |
| 2230 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2231 | .build()); |
| 2232 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2233 | |
| 2234 | // Subsequent stylus movements are delivered correctly |
| 2235 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2236 | .deviceId(stylusDeviceId) |
| 2237 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2238 | .build()); |
| 2239 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2240 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
| 2241 | |
| 2242 | // and subsequent touches continue to work |
| 2243 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2244 | .deviceId(touchDeviceId) |
| 2245 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2246 | .build()); |
| 2247 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2248 | window->assertNoEvents(); |
| 2249 | } |
| 2250 | |
| 2251 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2252 | * 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] | 2253 | * Ensure that touch is canceled, because stylus hover should take precedence. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2254 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2255 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusHover) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2256 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2257 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2258 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2259 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2260 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2261 | |
| 2262 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2263 | |
| 2264 | constexpr int32_t touchDeviceId = 4; |
| 2265 | constexpr int32_t stylusDeviceId = 2; |
| 2266 | |
| 2267 | // Touch down on window |
| 2268 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2269 | .deviceId(touchDeviceId) |
| 2270 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2271 | .build()); |
| 2272 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2273 | .deviceId(touchDeviceId) |
| 2274 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2275 | .build()); |
| 2276 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2277 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2278 | |
| 2279 | // Stylus hover on the window |
| 2280 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2281 | .deviceId(stylusDeviceId) |
| 2282 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2283 | .build()); |
| 2284 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2285 | .deviceId(stylusDeviceId) |
| 2286 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2287 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2288 | // Stylus hover movement causes touch to be canceled |
| 2289 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 2290 | WithCoords(141, 146))); |
| 2291 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 2292 | WithDeviceId(stylusDeviceId), WithCoords(100, 110))); |
| 2293 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2294 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2295 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2296 | // Subsequent touch movements are ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2297 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2298 | .deviceId(touchDeviceId) |
| 2299 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2300 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2301 | |
| 2302 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2306 | * One window. Touch down on the window. Then, stylus hover on the window from another device. |
| 2307 | * Ensure that touch is not canceled, because stylus hover can be active at the same time as touch. |
| 2308 | */ |
| 2309 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsNotCanceledByStylusHover) { |
| 2310 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2311 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2312 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2313 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2314 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2315 | |
| 2316 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2317 | |
| 2318 | constexpr int32_t touchDeviceId = 4; |
| 2319 | constexpr int32_t stylusDeviceId = 2; |
| 2320 | |
| 2321 | // Touch down on window |
| 2322 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2323 | .deviceId(touchDeviceId) |
| 2324 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2325 | .build()); |
| 2326 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2327 | .deviceId(touchDeviceId) |
| 2328 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2329 | .build()); |
| 2330 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2331 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2332 | |
| 2333 | // Stylus hover on the window |
| 2334 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2335 | .deviceId(stylusDeviceId) |
| 2336 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2337 | .build()); |
| 2338 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2339 | .deviceId(stylusDeviceId) |
| 2340 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2341 | .build()); |
| 2342 | // Stylus hover movement is received normally |
| 2343 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 2344 | WithDeviceId(stylusDeviceId), WithCoords(100, 110))); |
| 2345 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2346 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
| 2347 | |
| 2348 | // Subsequent touch movements also work |
| 2349 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2350 | .deviceId(touchDeviceId) |
| 2351 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2352 | .build()); |
| 2353 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId), |
| 2354 | WithCoords(142, 147))); |
| 2355 | |
| 2356 | window->assertNoEvents(); |
| 2357 | } |
| 2358 | |
| 2359 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2360 | * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that |
| 2361 | * the latest stylus takes over. That is, old stylus should be canceled and the new stylus should |
| 2362 | * become active. |
| 2363 | */ |
| 2364 | TEST_F(InputDispatcherMultiDeviceTest, LatestStylusWins) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2365 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2366 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2367 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2368 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2369 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2370 | |
| 2371 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2372 | |
| 2373 | constexpr int32_t stylusDeviceId1 = 3; |
| 2374 | constexpr int32_t stylusDeviceId2 = 5; |
| 2375 | |
| 2376 | // Touch down on window |
| 2377 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2378 | .deviceId(stylusDeviceId1) |
| 2379 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100)) |
| 2380 | .build()); |
| 2381 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2382 | .deviceId(stylusDeviceId1) |
| 2383 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2384 | .build()); |
| 2385 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1))); |
| 2386 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2387 | |
| 2388 | // Second stylus down |
| 2389 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2390 | .deviceId(stylusDeviceId2) |
| 2391 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10)) |
| 2392 | .build()); |
| 2393 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2394 | .deviceId(stylusDeviceId2) |
| 2395 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11)) |
| 2396 | .build()); |
| 2397 | |
| 2398 | // First stylus is canceled, second one takes over. |
| 2399 | window->consumeMotionEvent( |
| 2400 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId1))); |
| 2401 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2))); |
| 2402 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2))); |
| 2403 | |
| 2404 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2405 | .deviceId(stylusDeviceId1) |
| 2406 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2407 | .build()); |
| 2408 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2409 | window->assertNoEvents(); |
| 2410 | } |
| 2411 | |
| 2412 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2413 | * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that |
| 2414 | * both stylus devices can function simultaneously. |
| 2415 | */ |
| 2416 | TEST_F(InputDispatcherMultiDeviceTest, TwoStylusDevicesActiveAtTheSameTime) { |
| 2417 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2418 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2419 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2420 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2421 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2422 | |
| 2423 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2424 | |
| 2425 | constexpr int32_t stylusDeviceId1 = 3; |
| 2426 | constexpr int32_t stylusDeviceId2 = 5; |
| 2427 | |
| 2428 | // Touch down on window |
| 2429 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2430 | .deviceId(stylusDeviceId1) |
| 2431 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100)) |
| 2432 | .build()); |
| 2433 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2434 | .deviceId(stylusDeviceId1) |
| 2435 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2436 | .build()); |
| 2437 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1))); |
| 2438 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2439 | |
| 2440 | // Second stylus down |
| 2441 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2442 | .deviceId(stylusDeviceId2) |
| 2443 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10)) |
| 2444 | .build()); |
| 2445 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2446 | .deviceId(stylusDeviceId2) |
| 2447 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11)) |
| 2448 | .build()); |
| 2449 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2))); |
| 2450 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2))); |
| 2451 | |
| 2452 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2453 | .deviceId(stylusDeviceId1) |
| 2454 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2455 | .build()); |
| 2456 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2457 | window->assertNoEvents(); |
| 2458 | } |
| 2459 | |
| 2460 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2461 | * One window. Touch down on the window. Then, stylus down on the window from another device. |
| 2462 | * Ensure that is canceled, because stylus down should be preferred over touch. |
| 2463 | */ |
| 2464 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusDown) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2465 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2466 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2467 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2468 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2469 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2470 | |
| 2471 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2472 | |
| 2473 | constexpr int32_t touchDeviceId = 4; |
| 2474 | constexpr int32_t stylusDeviceId = 2; |
| 2475 | |
| 2476 | // Touch down on window |
| 2477 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2478 | .deviceId(touchDeviceId) |
| 2479 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2480 | .build()); |
| 2481 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2482 | .deviceId(touchDeviceId) |
| 2483 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2484 | .build()); |
| 2485 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2486 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2487 | |
| 2488 | // Stylus down on the window |
| 2489 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2490 | .deviceId(stylusDeviceId) |
| 2491 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2492 | .build()); |
| 2493 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 2494 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2495 | |
| 2496 | // Subsequent stylus movements are delivered correctly |
| 2497 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2498 | .deviceId(stylusDeviceId) |
| 2499 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2500 | .build()); |
| 2501 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2502 | WithCoords(101, 111))); |
| 2503 | } |
| 2504 | |
| 2505 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2506 | * One window. Touch down on the window. Then, stylus down on the window from another device. |
| 2507 | * Ensure that both touch and stylus are functioning independently. |
| 2508 | */ |
| 2509 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsNotCanceledByStylusDown) { |
| 2510 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2511 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2512 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 2513 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2514 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2515 | |
| 2516 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2517 | |
| 2518 | constexpr int32_t touchDeviceId = 4; |
| 2519 | constexpr int32_t stylusDeviceId = 2; |
| 2520 | |
| 2521 | // Touch down on window |
| 2522 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2523 | .deviceId(touchDeviceId) |
| 2524 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2525 | .build()); |
| 2526 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2527 | .deviceId(touchDeviceId) |
| 2528 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2529 | .build()); |
| 2530 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2531 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2532 | |
| 2533 | // Stylus down on the window |
| 2534 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2535 | .deviceId(stylusDeviceId) |
| 2536 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2537 | .build()); |
| 2538 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2539 | |
| 2540 | // Subsequent stylus movements are delivered correctly |
| 2541 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2542 | .deviceId(stylusDeviceId) |
| 2543 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2544 | .build()); |
| 2545 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2546 | WithCoords(101, 111))); |
| 2547 | |
| 2548 | // Touch continues to work too |
| 2549 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2550 | .deviceId(touchDeviceId) |
| 2551 | .pointer(PointerBuilder(0, ToolType::FINGER).x(148).y(149)) |
| 2552 | .build()); |
| 2553 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2554 | } |
| 2555 | |
| 2556 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2557 | * Two windows: a window on the left and a window on the right. |
| 2558 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2559 | * down. Then, on the left window, also place second touch pointer down. |
| 2560 | * This test tries to reproduce a crash. |
| 2561 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2562 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2563 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch_legacy) { |
| 2564 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2565 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2566 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2567 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2568 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2569 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2570 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2571 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2572 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2573 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2574 | mDispatcher->onWindowInfosChanged( |
| 2575 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2576 | |
| 2577 | const int32_t touchDeviceId = 4; |
| 2578 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2579 | |
| 2580 | // Start hovering over the left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2581 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2582 | .deviceId(mouseDeviceId) |
| 2583 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2584 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2585 | leftWindow->consumeMotionEvent( |
| 2586 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2587 | |
| 2588 | // Mouse down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2589 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2590 | .deviceId(mouseDeviceId) |
| 2591 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2592 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2593 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2594 | |
| 2595 | leftWindow->consumeMotionEvent( |
| 2596 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2597 | leftWindow->consumeMotionEvent( |
| 2598 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2599 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2600 | mDispatcher->notifyMotion( |
| 2601 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2602 | .deviceId(mouseDeviceId) |
| 2603 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2604 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2605 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2606 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2607 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2608 | |
| 2609 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2610 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2611 | .deviceId(touchDeviceId) |
| 2612 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2613 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2614 | leftWindow->assertNoEvents(); |
| 2615 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2616 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2617 | |
| 2618 | // Second touch pointer down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2619 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2620 | .deviceId(touchDeviceId) |
| 2621 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2622 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2623 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2624 | // Since this is now a new splittable pointer going down on the left window, and it's coming |
| 2625 | // from a different device, the current gesture in the left window (pointer down) should first |
| 2626 | // be canceled. |
| 2627 | leftWindow->consumeMotionEvent( |
| 2628 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2629 | leftWindow->consumeMotionEvent( |
| 2630 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2631 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2632 | // current implementation. |
| 2633 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2634 | rightWindow->consumeMotionEvent( |
| 2635 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2636 | |
| 2637 | leftWindow->assertNoEvents(); |
| 2638 | rightWindow->assertNoEvents(); |
| 2639 | } |
| 2640 | |
| 2641 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2642 | * 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] | 2643 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2644 | * down. Then, on the left window, also place second touch pointer down. |
| 2645 | * This test tries to reproduce a crash. |
| 2646 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2647 | */ |
| 2648 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch) { |
| 2649 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2650 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2651 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2652 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2653 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2654 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2655 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2656 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2657 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2658 | |
| 2659 | mDispatcher->onWindowInfosChanged( |
| 2660 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2661 | |
| 2662 | const int32_t touchDeviceId = 4; |
| 2663 | const int32_t mouseDeviceId = 6; |
| 2664 | |
| 2665 | // Start hovering over the left window |
| 2666 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2667 | .deviceId(mouseDeviceId) |
| 2668 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2669 | .build()); |
| 2670 | leftWindow->consumeMotionEvent( |
| 2671 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2672 | |
| 2673 | // Mouse down on left window |
| 2674 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2675 | .deviceId(mouseDeviceId) |
| 2676 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2677 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2678 | .build()); |
| 2679 | |
| 2680 | leftWindow->consumeMotionEvent( |
| 2681 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2682 | leftWindow->consumeMotionEvent( |
| 2683 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2684 | |
| 2685 | mDispatcher->notifyMotion( |
| 2686 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2687 | .deviceId(mouseDeviceId) |
| 2688 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2689 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2690 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2691 | .build()); |
| 2692 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2693 | |
| 2694 | // First touch pointer down on right window |
| 2695 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2696 | .deviceId(touchDeviceId) |
| 2697 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2698 | .build()); |
| 2699 | leftWindow->assertNoEvents(); |
| 2700 | |
| 2701 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2702 | |
| 2703 | // Second touch pointer down on left window |
| 2704 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2705 | .deviceId(touchDeviceId) |
| 2706 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2707 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2708 | .build()); |
| 2709 | // Since this is now a new splittable pointer going down on the left window, and it's coming |
| 2710 | // from a different device, it will be split and delivered to left window separately. |
| 2711 | leftWindow->consumeMotionEvent( |
| 2712 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2713 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2714 | // current implementation. |
| 2715 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2716 | rightWindow->consumeMotionEvent( |
| 2717 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2718 | |
| 2719 | leftWindow->assertNoEvents(); |
| 2720 | rightWindow->assertNoEvents(); |
| 2721 | } |
| 2722 | |
| 2723 | /** |
| 2724 | * 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] | 2725 | * Mouse is hovered on the left window and stylus is hovered on the right window. |
| 2726 | */ |
| 2727 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHover) { |
| 2728 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2729 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2730 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2731 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2732 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2733 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2734 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2735 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2736 | |
| 2737 | mDispatcher->onWindowInfosChanged( |
| 2738 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2739 | |
| 2740 | const int32_t stylusDeviceId = 3; |
| 2741 | const int32_t mouseDeviceId = 6; |
| 2742 | |
| 2743 | // Start hovering over the left window |
| 2744 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2745 | .deviceId(mouseDeviceId) |
| 2746 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 2747 | .build()); |
| 2748 | leftWindow->consumeMotionEvent( |
| 2749 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2750 | |
| 2751 | // Stylus hovered on right window |
| 2752 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2753 | .deviceId(stylusDeviceId) |
| 2754 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(300).y(100)) |
| 2755 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2756 | rightWindow->consumeMotionEvent( |
| 2757 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2758 | |
| 2759 | // Subsequent HOVER_MOVE events are dispatched correctly. |
| 2760 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 2761 | .deviceId(mouseDeviceId) |
| 2762 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2763 | .build()); |
| 2764 | leftWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2765 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2766 | |
| 2767 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2768 | .deviceId(stylusDeviceId) |
| 2769 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(310).y(110)) |
| 2770 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2771 | rightWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2772 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2773 | |
| 2774 | leftWindow->assertNoEvents(); |
| 2775 | rightWindow->assertNoEvents(); |
| 2776 | } |
| 2777 | |
| 2778 | /** |
| 2779 | * Three windows: a window on the left and a window on the right. |
| 2780 | * And a spy window that's positioned above all of them. |
| 2781 | * Stylus down on the left window and remains down. Touch goes down on the right and remains down. |
| 2782 | * Check the stream that's received by the spy. |
| 2783 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2784 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy_legacy) { |
| 2785 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2786 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2787 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2788 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 2789 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2790 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 2791 | spyWindow->setTrustedOverlay(true); |
| 2792 | spyWindow->setSpy(true); |
| 2793 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2794 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2795 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2796 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2797 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2798 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2799 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2800 | |
| 2801 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2802 | |
| 2803 | mDispatcher->onWindowInfosChanged( |
| 2804 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2805 | |
| 2806 | const int32_t stylusDeviceId = 1; |
| 2807 | const int32_t touchDeviceId = 2; |
| 2808 | |
| 2809 | // Stylus down on the left window |
| 2810 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2811 | .deviceId(stylusDeviceId) |
| 2812 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2813 | .build()); |
| 2814 | leftWindow->consumeMotionEvent( |
| 2815 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2816 | spyWindow->consumeMotionEvent( |
| 2817 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2818 | |
| 2819 | // Touch down on the right window |
| 2820 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2821 | .deviceId(touchDeviceId) |
| 2822 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2823 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2824 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2825 | rightWindow->consumeMotionEvent( |
| 2826 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2827 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2828 | // Spy window does not receive touch events, because stylus events take precedence, and it |
| 2829 | // already has an active stylus gesture. |
| 2830 | |
| 2831 | // 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] | 2832 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2833 | .deviceId(stylusDeviceId) |
| 2834 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2835 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2836 | leftWindow->consumeMotionEvent( |
| 2837 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 2838 | spyWindow->consumeMotionEvent( |
| 2839 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2840 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2841 | // Further MOVE events keep going to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2842 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2843 | .deviceId(touchDeviceId) |
| 2844 | .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110)) |
| 2845 | .build()); |
| 2846 | rightWindow->consumeMotionEvent( |
| 2847 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2848 | |
| 2849 | spyWindow->assertNoEvents(); |
| 2850 | leftWindow->assertNoEvents(); |
| 2851 | rightWindow->assertNoEvents(); |
| 2852 | } |
| 2853 | |
| 2854 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2855 | * Three windows: a window on the left and a window on the right. |
| 2856 | * And a spy window that's positioned above all of them. |
| 2857 | * Stylus down on the left window and remains down. Touch goes down on the right and remains down. |
| 2858 | * Check the stream that's received by the spy. |
| 2859 | */ |
| 2860 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy) { |
| 2861 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 2862 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2863 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2864 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 2865 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2866 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 2867 | spyWindow->setTrustedOverlay(true); |
| 2868 | spyWindow->setSpy(true); |
| 2869 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2870 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2871 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2872 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2873 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2874 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2875 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2876 | |
| 2877 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2878 | |
| 2879 | mDispatcher->onWindowInfosChanged( |
| 2880 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2881 | |
| 2882 | const int32_t stylusDeviceId = 1; |
| 2883 | const int32_t touchDeviceId = 2; |
| 2884 | |
| 2885 | // Stylus down on the left window |
| 2886 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2887 | .deviceId(stylusDeviceId) |
| 2888 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2889 | .build()); |
| 2890 | leftWindow->consumeMotionEvent( |
| 2891 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2892 | spyWindow->consumeMotionEvent( |
| 2893 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2894 | |
| 2895 | // Touch down on the right window |
| 2896 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2897 | .deviceId(touchDeviceId) |
| 2898 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2899 | .build()); |
| 2900 | leftWindow->assertNoEvents(); |
| 2901 | rightWindow->consumeMotionEvent( |
| 2902 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2903 | spyWindow->consumeMotionEvent( |
| 2904 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2905 | |
| 2906 | // Stylus movements continue. They should be delivered to the left window and to the spy window |
| 2907 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2908 | .deviceId(stylusDeviceId) |
| 2909 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2910 | .build()); |
| 2911 | leftWindow->consumeMotionEvent( |
| 2912 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 2913 | spyWindow->consumeMotionEvent( |
| 2914 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 2915 | |
| 2916 | // Further touch MOVE events keep going to the right window and to the spy |
| 2917 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2918 | .deviceId(touchDeviceId) |
| 2919 | .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110)) |
| 2920 | .build()); |
| 2921 | rightWindow->consumeMotionEvent( |
| 2922 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2923 | spyWindow->consumeMotionEvent( |
| 2924 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2925 | |
| 2926 | spyWindow->assertNoEvents(); |
| 2927 | leftWindow->assertNoEvents(); |
| 2928 | rightWindow->assertNoEvents(); |
| 2929 | } |
| 2930 | |
| 2931 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2932 | * Three windows: a window on the left, a window on the right, and a spy window positioned above |
| 2933 | * both. |
| 2934 | * Check hover in left window and touch down in the right window. |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2935 | * 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] | 2936 | * At the same time, left and right should be getting independent streams of hovering and touch, |
| 2937 | * respectively. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2938 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2939 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverBlocksTouchWithSpy) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 2940 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2941 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2942 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2943 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 2944 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2945 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 2946 | spyWindow->setTrustedOverlay(true); |
| 2947 | spyWindow->setSpy(true); |
| 2948 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2949 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 2950 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2951 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2952 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 2953 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 2954 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2955 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2956 | |
| 2957 | mDispatcher->onWindowInfosChanged( |
| 2958 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2959 | |
| 2960 | const int32_t stylusDeviceId = 1; |
| 2961 | const int32_t touchDeviceId = 2; |
| 2962 | |
| 2963 | // Stylus hover on the left window |
| 2964 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2965 | .deviceId(stylusDeviceId) |
| 2966 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2967 | .build()); |
| 2968 | leftWindow->consumeMotionEvent( |
| 2969 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2970 | spyWindow->consumeMotionEvent( |
| 2971 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2972 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2973 | // Touch down on the right window. Spy doesn't receive this touch because it already has |
| 2974 | // stylus hovering there. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2975 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2976 | .deviceId(touchDeviceId) |
| 2977 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2978 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2979 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2980 | spyWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2981 | rightWindow->consumeMotionEvent( |
| 2982 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2983 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2984 | // 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] | 2985 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2986 | .deviceId(stylusDeviceId) |
| 2987 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2988 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2989 | leftWindow->consumeMotionEvent( |
| 2990 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2991 | spyWindow->consumeMotionEvent( |
| 2992 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2993 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2994 | // Touch movements continue. They should be delivered to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2995 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2996 | .deviceId(touchDeviceId) |
| 2997 | .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101)) |
| 2998 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2999 | rightWindow->consumeMotionEvent( |
| 3000 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3001 | |
| 3002 | spyWindow->assertNoEvents(); |
| 3003 | leftWindow->assertNoEvents(); |
| 3004 | rightWindow->assertNoEvents(); |
| 3005 | } |
| 3006 | |
| 3007 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3008 | * Three windows: a window on the left, a window on the right, and a spy window positioned above |
| 3009 | * both. |
| 3010 | * Check hover in left window and touch down in the right window. |
| 3011 | * At first, spy should receive hover. Next, spy should receive touch. |
| 3012 | * At the same time, left and right should be getting independent streams of hovering and touch, |
| 3013 | * respectively. |
| 3014 | */ |
| 3015 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverDoesNotBlockTouchWithSpy) { |
| 3016 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3017 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3018 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3019 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 3020 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3021 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 3022 | spyWindow->setTrustedOverlay(true); |
| 3023 | spyWindow->setSpy(true); |
| 3024 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3025 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 3026 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3027 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3028 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3029 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 3030 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3031 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3032 | |
| 3033 | mDispatcher->onWindowInfosChanged( |
| 3034 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3035 | |
| 3036 | const int32_t stylusDeviceId = 1; |
| 3037 | const int32_t touchDeviceId = 2; |
| 3038 | |
| 3039 | // Stylus hover on the left window |
| 3040 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3041 | .deviceId(stylusDeviceId) |
| 3042 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 3043 | .build()); |
| 3044 | leftWindow->consumeMotionEvent( |
| 3045 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3046 | spyWindow->consumeMotionEvent( |
| 3047 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3048 | |
| 3049 | // Touch down on the right window. |
| 3050 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3051 | .deviceId(touchDeviceId) |
| 3052 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3053 | .build()); |
| 3054 | leftWindow->assertNoEvents(); |
| 3055 | spyWindow->consumeMotionEvent( |
| 3056 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3057 | rightWindow->consumeMotionEvent( |
| 3058 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3059 | |
| 3060 | // Stylus movements continue. They should be delivered to the left window and the spy. |
| 3061 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3062 | .deviceId(stylusDeviceId) |
| 3063 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 3064 | .build()); |
| 3065 | leftWindow->consumeMotionEvent( |
| 3066 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3067 | spyWindow->consumeMotionEvent( |
| 3068 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3069 | |
| 3070 | // Touch movements continue. They should be delivered to the right window and the spy |
| 3071 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3072 | .deviceId(touchDeviceId) |
| 3073 | .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101)) |
| 3074 | .build()); |
| 3075 | rightWindow->consumeMotionEvent( |
| 3076 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3077 | spyWindow->consumeMotionEvent( |
| 3078 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3079 | |
| 3080 | spyWindow->assertNoEvents(); |
| 3081 | leftWindow->assertNoEvents(); |
| 3082 | rightWindow->assertNoEvents(); |
| 3083 | } |
| 3084 | |
| 3085 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3086 | * On a single window, use two different devices: mouse and touch. |
| 3087 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 3088 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 3089 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 3090 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 3091 | * represent a new gesture. |
| 3092 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3093 | TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown_legacy) { |
| 3094 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3095 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3096 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3097 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3098 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3099 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3100 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3101 | |
| 3102 | const int32_t touchDeviceId = 4; |
| 3103 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3104 | |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3105 | // First touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3106 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3107 | .deviceId(touchDeviceId) |
| 3108 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3109 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3110 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3111 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3112 | .deviceId(touchDeviceId) |
| 3113 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3114 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3115 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3116 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3117 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3118 | .deviceId(touchDeviceId) |
| 3119 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3120 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3121 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3122 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3123 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3124 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 3125 | |
| 3126 | // Mouse down. The touch should be canceled |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3127 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3128 | .deviceId(mouseDeviceId) |
| 3129 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3130 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3131 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3132 | |
| 3133 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 3134 | WithPointerCount(1u))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3135 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3136 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3137 | mDispatcher->notifyMotion( |
| 3138 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3139 | .deviceId(mouseDeviceId) |
| 3140 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3141 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3142 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3143 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3144 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3145 | |
| 3146 | // Second touch pointer down. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3147 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3148 | .deviceId(touchDeviceId) |
| 3149 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3150 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3151 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3152 | // Since we already canceled this touch gesture, it will be ignored until a completely new |
| 3153 | // gesture is started. This is easier to implement than trying to keep track of the new pointer |
| 3154 | // and generating an ACTION_DOWN instead of ACTION_POINTER_DOWN. |
| 3155 | // However, mouse movements should continue to work. |
| 3156 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3157 | .deviceId(mouseDeviceId) |
| 3158 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3159 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110)) |
| 3160 | .build()); |
| 3161 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 3162 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3163 | window->assertNoEvents(); |
| 3164 | } |
| 3165 | |
| 3166 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3167 | * On a single window, use two different devices: mouse and touch. |
| 3168 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 3169 | * Mouse is clicked next, which should not interfere with the touch stream. |
| 3170 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is also |
| 3171 | * delivered correctly. |
| 3172 | */ |
| 3173 | TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown) { |
| 3174 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3175 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3176 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3177 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3178 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3179 | |
| 3180 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3181 | |
| 3182 | const int32_t touchDeviceId = 4; |
| 3183 | const int32_t mouseDeviceId = 6; |
| 3184 | |
| 3185 | // First touch pointer down |
| 3186 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3187 | .deviceId(touchDeviceId) |
| 3188 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3189 | .build()); |
| 3190 | // Second touch pointer down |
| 3191 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3192 | .deviceId(touchDeviceId) |
| 3193 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3194 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3195 | .build()); |
| 3196 | // First touch pointer lifts. The second one remains down |
| 3197 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3198 | .deviceId(touchDeviceId) |
| 3199 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3200 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3201 | .build()); |
| 3202 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3203 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3204 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 3205 | |
| 3206 | // Mouse down |
| 3207 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3208 | .deviceId(mouseDeviceId) |
| 3209 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3210 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3211 | .build()); |
| 3212 | |
| 3213 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3214 | |
| 3215 | mDispatcher->notifyMotion( |
| 3216 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3217 | .deviceId(mouseDeviceId) |
| 3218 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3219 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3220 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3221 | .build()); |
| 3222 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3223 | |
| 3224 | // Second touch pointer down. |
| 3225 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3226 | .deviceId(touchDeviceId) |
| 3227 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3228 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3229 | .build()); |
| 3230 | window->consumeMotionEvent(AllOf(WithMotionAction(POINTER_0_DOWN), WithDeviceId(touchDeviceId), |
| 3231 | WithPointerCount(2u))); |
| 3232 | |
| 3233 | // Mouse movements should continue to work |
| 3234 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3235 | .deviceId(mouseDeviceId) |
| 3236 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3237 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110)) |
| 3238 | .build()); |
| 3239 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 3240 | |
| 3241 | window->assertNoEvents(); |
| 3242 | } |
| 3243 | |
| 3244 | /** |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3245 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels |
| 3246 | * the injected event. |
| 3247 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3248 | TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent_legacy) { |
| 3249 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3250 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3251 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3252 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3253 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3254 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3255 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3256 | |
| 3257 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3258 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 3259 | // completion. |
| 3260 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3261 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3262 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3263 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3264 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3265 | .build())); |
| 3266 | window->consumeMotionEvent( |
| 3267 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3268 | |
| 3269 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer |
| 3270 | // should be canceled and the new gesture should take over. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3271 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3272 | .deviceId(touchDeviceId) |
| 3273 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3274 | .build()); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3275 | |
| 3276 | window->consumeMotionEvent( |
| 3277 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3278 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3279 | } |
| 3280 | |
| 3281 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3282 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event runs |
| 3283 | * parallel to the injected event. |
| 3284 | */ |
| 3285 | TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent) { |
| 3286 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3287 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3288 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3289 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3290 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3291 | |
| 3292 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3293 | |
| 3294 | const int32_t touchDeviceId = 4; |
| 3295 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 3296 | // completion. |
| 3297 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3298 | injectMotionEvent(*mDispatcher, |
| 3299 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3300 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
| 3301 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 3302 | .build())); |
| 3303 | window->consumeMotionEvent( |
| 3304 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3305 | |
| 3306 | // Now a real touch comes. The injected pointer will remain, and the new gesture will also be |
| 3307 | // allowed through. |
| 3308 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3309 | .deviceId(touchDeviceId) |
| 3310 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3311 | .build()); |
| 3312 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3313 | } |
| 3314 | |
| 3315 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3316 | * This test is similar to the test above, but the sequence of injected events is different. |
| 3317 | * |
| 3318 | * Two windows: a window on the left and a window on the right. |
| 3319 | * Mouse is hovered over the left window. |
| 3320 | * Next, we tap on the left window, where the cursor was last seen. |
| 3321 | * |
| 3322 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 3323 | * the left window. |
| 3324 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 3325 | * window (first), and then another on the left window (second). |
| 3326 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 3327 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 3328 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3329 | TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch_legacy) { |
| 3330 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3331 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3332 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 3333 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3334 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3335 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3336 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 3337 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3338 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3339 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3340 | mDispatcher->onWindowInfosChanged( |
| 3341 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3342 | |
| 3343 | const int32_t mouseDeviceId = 6; |
| 3344 | const int32_t touchDeviceId = 4; |
| 3345 | // Hover over the left window. Keep the cursor there. |
| 3346 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3347 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3348 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3349 | AINPUT_SOURCE_MOUSE) |
| 3350 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3351 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3352 | .build())); |
| 3353 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3354 | |
| 3355 | // Tap on left window |
| 3356 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3357 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3358 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3359 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3360 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3361 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3362 | .build())); |
| 3363 | |
| 3364 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3365 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3366 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3367 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3368 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3369 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3370 | .build())); |
| 3371 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3372 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3373 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 3374 | |
| 3375 | // First finger down on right window |
| 3376 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3377 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3378 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3379 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3380 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3381 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3382 | .build())); |
| 3383 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3384 | |
| 3385 | // Second finger down on the left window |
| 3386 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3387 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3388 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3389 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3390 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3391 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3392 | .build())); |
| 3393 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3394 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 3395 | |
| 3396 | // No more events |
| 3397 | leftWindow->assertNoEvents(); |
| 3398 | rightWindow->assertNoEvents(); |
| 3399 | } |
| 3400 | |
| 3401 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3402 | * This test is similar to the test above, but the sequence of injected events is different. |
| 3403 | * |
| 3404 | * Two windows: a window on the left and a window on the right. |
| 3405 | * Mouse is hovered over the left window. |
| 3406 | * Next, we tap on the left window, where the cursor was last seen. |
| 3407 | * |
| 3408 | * After that, we send one finger down onto the right window, and then a second finger down onto |
| 3409 | * the left window. |
| 3410 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 3411 | * window (first), and then another on the left window (second). |
| 3412 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 3413 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 3414 | */ |
| 3415 | TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch) { |
| 3416 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3417 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3418 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 3419 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3420 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3421 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3422 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 3423 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3424 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3425 | |
| 3426 | mDispatcher->onWindowInfosChanged( |
| 3427 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3428 | |
| 3429 | const int32_t mouseDeviceId = 6; |
| 3430 | const int32_t touchDeviceId = 4; |
| 3431 | // Hover over the left window. Keep the cursor there. |
| 3432 | mDispatcher->notifyMotion( |
| 3433 | MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3434 | .deviceId(mouseDeviceId) |
| 3435 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 3436 | .build()); |
| 3437 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3438 | |
| 3439 | // Tap on left window |
| 3440 | mDispatcher->notifyMotion( |
| 3441 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3442 | .deviceId(touchDeviceId) |
| 3443 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3444 | .build()); |
| 3445 | |
| 3446 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3447 | .deviceId(touchDeviceId) |
| 3448 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3449 | .build()); |
| 3450 | leftWindow->consumeMotionEvent( |
| 3451 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3452 | leftWindow->consumeMotionEvent( |
| 3453 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithDeviceId(touchDeviceId))); |
| 3454 | |
| 3455 | // First finger down on right window |
| 3456 | mDispatcher->notifyMotion( |
| 3457 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3458 | .deviceId(touchDeviceId) |
| 3459 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3460 | .build()); |
| 3461 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3462 | |
| 3463 | // Second finger down on the left window |
| 3464 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3465 | .deviceId(touchDeviceId) |
| 3466 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3467 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 3468 | .build()); |
| 3469 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3470 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 3471 | |
| 3472 | // No more events |
| 3473 | leftWindow->assertNoEvents(); |
| 3474 | rightWindow->assertNoEvents(); |
| 3475 | } |
| 3476 | |
| 3477 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3478 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 3479 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 3480 | * touch is gone, stylus hovering should start working again. |
| 3481 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3482 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverIgnoresTouchTap) { |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3483 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3484 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3485 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3486 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3487 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3488 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3489 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3490 | |
| 3491 | const int32_t stylusDeviceId = 5; |
| 3492 | const int32_t touchDeviceId = 4; |
| 3493 | // Start hovering with stylus |
| 3494 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3495 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3496 | MotionEventBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3497 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3498 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3499 | .build())); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3500 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3501 | |
| 3502 | // Finger down on the window |
| 3503 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3504 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3505 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3506 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3507 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3508 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3509 | // The touch device should be ignored! |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3510 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3511 | // Continue hovering with stylus. |
| 3512 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3513 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3514 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3515 | AINPUT_SOURCE_STYLUS) |
| 3516 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3517 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3518 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3519 | // Hovers continue to work |
| 3520 | window->consumeMotionEvent( |
| 3521 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3522 | |
| 3523 | // Lift up the finger |
| 3524 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3525 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3526 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3527 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3528 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3529 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3530 | .build())); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3531 | |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3532 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3533 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3534 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3535 | AINPUT_SOURCE_STYLUS) |
| 3536 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3537 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3538 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3539 | window->consumeMotionEvent( |
| 3540 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3541 | window->assertNoEvents(); |
| 3542 | } |
| 3543 | |
| 3544 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3545 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 3546 | * While the touch is down, hovering from the stylus is not affected. After the touch is gone, |
| 3547 | * check that the stylus hovering continues to work. |
| 3548 | */ |
| 3549 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverWithTouchTap) { |
| 3550 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3551 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3552 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3553 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3554 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3555 | |
| 3556 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3557 | |
| 3558 | const int32_t stylusDeviceId = 5; |
| 3559 | const int32_t touchDeviceId = 4; |
| 3560 | // Start hovering with stylus |
| 3561 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3562 | .deviceId(stylusDeviceId) |
| 3563 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3564 | .build()); |
| 3565 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3566 | |
| 3567 | // Finger down on the window |
| 3568 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3569 | .deviceId(touchDeviceId) |
| 3570 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3571 | .build()); |
| 3572 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3573 | |
| 3574 | // Continue hovering with stylus. |
| 3575 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3576 | .deviceId(stylusDeviceId) |
| 3577 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60)) |
| 3578 | .build()); |
| 3579 | // Hovers continue to work |
| 3580 | window->consumeMotionEvent( |
| 3581 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3582 | |
| 3583 | // Lift up the finger |
| 3584 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3585 | .deviceId(touchDeviceId) |
| 3586 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3587 | .build()); |
| 3588 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDeviceId(touchDeviceId))); |
| 3589 | |
| 3590 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3591 | .deviceId(stylusDeviceId) |
| 3592 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70)) |
| 3593 | .build()); |
| 3594 | window->consumeMotionEvent( |
| 3595 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3596 | window->assertNoEvents(); |
| 3597 | } |
| 3598 | |
| 3599 | /** |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3600 | * If stylus is down anywhere on the screen, then touches should not be delivered to windows that |
| 3601 | * have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 3602 | * |
| 3603 | * Two windows: one on the left and one on the right. |
| 3604 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 3605 | * Stylus down on the left window, and then touch down on the right window. |
| 3606 | * Check that the right window doesn't get touches while the stylus is down on the left window. |
| 3607 | */ |
| 3608 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusDownBlocksTouch) { |
| 3609 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3610 | sp<FakeWindowHandle> leftWindow = |
| 3611 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3612 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3613 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3614 | |
| 3615 | sp<FakeWindowHandle> sbtRightWindow = |
| 3616 | sp<FakeWindowHandle>::make(application, mDispatcher, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3617 | "Stylus blocks touch (right) window", |
| 3618 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3619 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3620 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 3621 | |
| 3622 | mDispatcher->onWindowInfosChanged( |
| 3623 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 3624 | |
| 3625 | const int32_t stylusDeviceId = 5; |
| 3626 | const int32_t touchDeviceId = 4; |
| 3627 | |
| 3628 | // Stylus down in the left window |
| 3629 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3630 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 3631 | .deviceId(stylusDeviceId) |
| 3632 | .build()); |
| 3633 | leftWindow->consumeMotionEvent( |
| 3634 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 3635 | |
| 3636 | // Finger tap on the right window |
| 3637 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3638 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3639 | .deviceId(touchDeviceId) |
| 3640 | .build()); |
| 3641 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3642 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3643 | .deviceId(touchDeviceId) |
| 3644 | .build()); |
| 3645 | |
| 3646 | // The touch should be blocked, because stylus is down somewhere else on screen! |
| 3647 | sbtRightWindow->assertNoEvents(); |
| 3648 | |
| 3649 | // Continue stylus motion, and ensure it's not impacted. |
| 3650 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 3651 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3652 | .deviceId(stylusDeviceId) |
| 3653 | .build()); |
| 3654 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3655 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3656 | .deviceId(stylusDeviceId) |
| 3657 | .build()); |
| 3658 | leftWindow->consumeMotionEvent( |
| 3659 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 3660 | leftWindow->consumeMotionEvent( |
| 3661 | AllOf(WithMotionAction(ACTION_UP), WithDeviceId(stylusDeviceId))); |
| 3662 | |
| 3663 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3664 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3665 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3666 | .deviceId(touchDeviceId) |
| 3667 | .build()); |
| 3668 | sbtRightWindow->consumeMotionEvent( |
| 3669 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3670 | } |
| 3671 | |
| 3672 | /** |
| 3673 | * If stylus is hovering anywhere on the screen, then touches should not be delivered to windows |
| 3674 | * that have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 3675 | * |
| 3676 | * Two windows: one on the left and one on the right. |
| 3677 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 3678 | * Stylus hover on the left window, and then touch down on the right window. |
| 3679 | * Check that the right window doesn't get touches while the stylus is hovering on the left window. |
| 3680 | */ |
| 3681 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusHoverBlocksTouch) { |
| 3682 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3683 | sp<FakeWindowHandle> leftWindow = |
| 3684 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3685 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3686 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3687 | |
| 3688 | sp<FakeWindowHandle> sbtRightWindow = |
| 3689 | sp<FakeWindowHandle>::make(application, mDispatcher, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3690 | "Stylus blocks touch (right) window", |
| 3691 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3692 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3693 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 3694 | |
| 3695 | mDispatcher->onWindowInfosChanged( |
| 3696 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 3697 | |
| 3698 | const int32_t stylusDeviceId = 5; |
| 3699 | const int32_t touchDeviceId = 4; |
| 3700 | |
| 3701 | // Stylus hover in the left window |
| 3702 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3703 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 3704 | .deviceId(stylusDeviceId) |
| 3705 | .build()); |
| 3706 | leftWindow->consumeMotionEvent( |
| 3707 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3708 | |
| 3709 | // Finger tap on the right window |
| 3710 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3711 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3712 | .deviceId(touchDeviceId) |
| 3713 | .build()); |
| 3714 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3715 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3716 | .deviceId(touchDeviceId) |
| 3717 | .build()); |
| 3718 | |
| 3719 | // The touch should be blocked, because stylus is hovering somewhere else on screen! |
| 3720 | sbtRightWindow->assertNoEvents(); |
| 3721 | |
| 3722 | // Continue stylus motion, and ensure it's not impacted. |
| 3723 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3724 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3725 | .deviceId(stylusDeviceId) |
| 3726 | .build()); |
| 3727 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3728 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3729 | .deviceId(stylusDeviceId) |
| 3730 | .build()); |
| 3731 | leftWindow->consumeMotionEvent( |
| 3732 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3733 | leftWindow->consumeMotionEvent( |
| 3734 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId))); |
| 3735 | |
| 3736 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3737 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3738 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3739 | .deviceId(touchDeviceId) |
| 3740 | .build()); |
| 3741 | sbtRightWindow->consumeMotionEvent( |
| 3742 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3743 | } |
| 3744 | |
| 3745 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3746 | * A spy window above a window with no input channel. |
| 3747 | * Start hovering with a stylus device, and then tap with it. |
| 3748 | * Ensure spy window receives the entire sequence. |
| 3749 | */ |
| 3750 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 3751 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3752 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 3753 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3754 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3755 | spyWindow->setTrustedOverlay(true); |
| 3756 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3757 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3758 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3759 | window->setNoInputChannel(true); |
| 3760 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3761 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3762 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3763 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3764 | // Start hovering with stylus |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3765 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3766 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3767 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3768 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3769 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3770 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3771 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3772 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3773 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3774 | |
| 3775 | // Stylus touches down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3776 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3777 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3778 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3779 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3780 | |
| 3781 | // Stylus goes up |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3782 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3783 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3784 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3785 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 3786 | |
| 3787 | // Again hover |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3788 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3789 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3790 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3791 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3792 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3793 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3794 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3795 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3796 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3797 | |
| 3798 | // No more events |
| 3799 | spyWindow->assertNoEvents(); |
| 3800 | window->assertNoEvents(); |
| 3801 | } |
| 3802 | |
| 3803 | /** |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 3804 | * A stale stylus HOVER_EXIT event is injected. Since it's a stale event, it should generally be |
| 3805 | * rejected. But since we already have an ongoing gesture, this event should be processed. |
| 3806 | * This prevents inconsistent events being handled inside the dispatcher. |
| 3807 | */ |
| 3808 | TEST_F(InputDispatcherTest, StaleStylusHoverGestureIsComplete) { |
| 3809 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3810 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3811 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3812 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 3813 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3814 | |
| 3815 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3816 | |
| 3817 | // Start hovering with stylus |
| 3818 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3819 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3820 | .build()); |
| 3821 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3822 | |
| 3823 | NotifyMotionArgs hoverExit = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3824 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3825 | .build(); |
| 3826 | // Make this 'hoverExit' event stale |
| 3827 | mFakePolicy->setStaleEventTimeout(100ms); |
| 3828 | std::this_thread::sleep_for(100ms); |
| 3829 | |
| 3830 | // It shouldn't be dropped by the dispatcher, even though it's stale. |
| 3831 | mDispatcher->notifyMotion(hoverExit); |
| 3832 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3833 | |
| 3834 | // Stylus starts hovering again! There should be no crash. |
| 3835 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3836 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(51)) |
| 3837 | .build()); |
| 3838 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3839 | } |
| 3840 | |
| 3841 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3842 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 3843 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 3844 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 3845 | * While the mouse is down, new move events from the touch device should be ignored. |
| 3846 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3847 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove_legacy) { |
| 3848 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3849 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3850 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 3851 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3852 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3853 | spyWindow->setTrustedOverlay(true); |
| 3854 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3855 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3856 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3857 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3858 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3859 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3860 | |
| 3861 | const int32_t mouseDeviceId = 7; |
| 3862 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3863 | |
| 3864 | // Hover a bit with mouse first |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3865 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3866 | .deviceId(mouseDeviceId) |
| 3867 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3868 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3869 | spyWindow->consumeMotionEvent( |
| 3870 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3871 | window->consumeMotionEvent( |
| 3872 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3873 | |
| 3874 | // Start touching |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3875 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3876 | .deviceId(touchDeviceId) |
| 3877 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3878 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3879 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3880 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3881 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3882 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3883 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3884 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3885 | .deviceId(touchDeviceId) |
| 3886 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 3887 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3888 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3889 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3890 | |
| 3891 | // Pilfer the stream |
| 3892 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3893 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 3894 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3895 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3896 | .deviceId(touchDeviceId) |
| 3897 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 3898 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3899 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3900 | |
| 3901 | // Mouse down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3902 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3903 | .deviceId(mouseDeviceId) |
| 3904 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3905 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3906 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3907 | |
| 3908 | spyWindow->consumeMotionEvent( |
| 3909 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 3910 | spyWindow->consumeMotionEvent( |
| 3911 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3912 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3913 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3914 | mDispatcher->notifyMotion( |
| 3915 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3916 | .deviceId(mouseDeviceId) |
| 3917 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3918 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3919 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3920 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3921 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3922 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3923 | |
| 3924 | // Mouse move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3925 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3926 | .deviceId(mouseDeviceId) |
| 3927 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3928 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 3929 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3930 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3931 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3932 | |
| 3933 | // Touch move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3934 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3935 | .deviceId(touchDeviceId) |
| 3936 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 3937 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3938 | |
| 3939 | // No more events |
| 3940 | spyWindow->assertNoEvents(); |
| 3941 | window->assertNoEvents(); |
| 3942 | } |
| 3943 | |
| 3944 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3945 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 3946 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 3947 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 3948 | * While the mouse is down, new move events from the touch device should continue to work. |
| 3949 | */ |
| 3950 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 3951 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 3952 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3953 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 3954 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3955 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3956 | spyWindow->setTrustedOverlay(true); |
| 3957 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 3958 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 3959 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 3960 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3961 | |
| 3962 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 3963 | |
| 3964 | const int32_t mouseDeviceId = 7; |
| 3965 | const int32_t touchDeviceId = 4; |
| 3966 | |
| 3967 | // Hover a bit with mouse first |
| 3968 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3969 | .deviceId(mouseDeviceId) |
| 3970 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3971 | .build()); |
| 3972 | spyWindow->consumeMotionEvent( |
| 3973 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3974 | window->consumeMotionEvent( |
| 3975 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3976 | |
| 3977 | // Start touching |
| 3978 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3979 | .deviceId(touchDeviceId) |
| 3980 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3981 | .build()); |
| 3982 | |
| 3983 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3984 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3985 | |
| 3986 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3987 | .deviceId(touchDeviceId) |
| 3988 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 3989 | .build()); |
| 3990 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3991 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3992 | |
| 3993 | // Pilfer the stream |
| 3994 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3995 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 3996 | // Hover is not pilfered! Only touch. |
| 3997 | |
| 3998 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3999 | .deviceId(touchDeviceId) |
| 4000 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 4001 | .build()); |
| 4002 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 4003 | |
| 4004 | // Mouse down |
| 4005 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4006 | .deviceId(mouseDeviceId) |
| 4007 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4008 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 4009 | .build()); |
| 4010 | |
| 4011 | spyWindow->consumeMotionEvent( |
| 4012 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 4013 | spyWindow->consumeMotionEvent( |
| 4014 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 4015 | window->consumeMotionEvent( |
| 4016 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 4017 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 4018 | |
| 4019 | mDispatcher->notifyMotion( |
| 4020 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 4021 | .deviceId(mouseDeviceId) |
| 4022 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4023 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4024 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 4025 | .build()); |
| 4026 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4027 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4028 | |
| 4029 | // Mouse move! |
| 4030 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4031 | .deviceId(mouseDeviceId) |
| 4032 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4033 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 4034 | .build()); |
| 4035 | spyWindow->consumeMotionEvent( |
| 4036 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 4037 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 4038 | |
| 4039 | // Touch move! |
| 4040 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 4041 | .deviceId(touchDeviceId) |
| 4042 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 4043 | .build()); |
| 4044 | spyWindow->consumeMotionEvent( |
| 4045 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 4046 | |
| 4047 | // No more events |
| 4048 | spyWindow->assertNoEvents(); |
| 4049 | window->assertNoEvents(); |
| 4050 | } |
| 4051 | |
| 4052 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4053 | * On the display, have a single window, and also an area where there's no window. |
| 4054 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 4055 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 4056 | */ |
| 4057 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 4058 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4059 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4060 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4061 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4062 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4063 | |
| 4064 | // Touch down on the empty space |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4065 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4066 | |
| 4067 | mDispatcher->waitForIdle(); |
| 4068 | window->assertNoEvents(); |
| 4069 | |
| 4070 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4071 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4072 | mDispatcher->waitForIdle(); |
| 4073 | window->consumeMotionDown(); |
| 4074 | } |
| 4075 | |
| 4076 | /** |
| 4077 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 4078 | * non-touchable window. |
| 4079 | */ |
| 4080 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 4081 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4082 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4083 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4084 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 4085 | window1->setTouchable(false); |
| 4086 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4087 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4088 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 4089 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4090 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4091 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4092 | // Touch down on the non-touchable window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4093 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4094 | |
| 4095 | mDispatcher->waitForIdle(); |
| 4096 | window1->assertNoEvents(); |
| 4097 | window2->assertNoEvents(); |
| 4098 | |
| 4099 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4100 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 4101 | mDispatcher->waitForIdle(); |
| 4102 | window2->consumeMotionDown(); |
| 4103 | } |
| 4104 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4105 | /** |
| 4106 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 4107 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 4108 | */ |
| 4109 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 4110 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4111 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4112 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4113 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 4114 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4115 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4116 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 4117 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4118 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4119 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4120 | // Touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4121 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4122 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4123 | |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4124 | const std::unique_ptr<MotionEvent> firstDown = |
| 4125 | window1->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 4126 | ASSERT_EQ(firstDown->getDownTime(), firstDown->getEventTime()); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4127 | window2->assertNoEvents(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4128 | |
| 4129 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4130 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4131 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4132 | |
| 4133 | const std::unique_ptr<MotionEvent> secondDown = |
| 4134 | window2->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 4135 | ASSERT_EQ(secondDown->getDownTime(), secondDown->getEventTime()); |
| 4136 | ASSERT_NE(firstDown->getDownTime(), secondDown->getDownTime()); |
| 4137 | // We currently send MOVE events to all windows receiving a split touch when there is any change |
| 4138 | // in the touch state, even when none of the pointers in the split window actually moved. |
| 4139 | // Document this behavior in the test. |
| 4140 | window1->consumeMotionMove(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4141 | |
| 4142 | // Now move the pointer on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4143 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4144 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4145 | |
| 4146 | window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime())); |
| 4147 | window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime())); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4148 | |
| 4149 | // Now add new touch down on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4150 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4151 | mDispatcher->waitForIdle(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4152 | |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4153 | window2->consumeMotionEvent( |
| 4154 | AllOf(WithMotionAction(POINTER_1_DOWN), WithDownTime(secondDown->getDownTime()))); |
| 4155 | window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime())); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4156 | |
| 4157 | // Now move the pointer on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4158 | mDispatcher->notifyMotion( |
| 4159 | generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4160 | mDispatcher->waitForIdle(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4161 | |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4162 | window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime())); |
| 4163 | window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime())); |
| 4164 | |
| 4165 | // Now add new touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4166 | mDispatcher->notifyMotion( |
| 4167 | generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4168 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 4169 | |
| 4170 | window1->consumeMotionEvent( |
| 4171 | AllOf(WithMotionAction(POINTER_1_DOWN), WithDownTime(firstDown->getDownTime()))); |
| 4172 | window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime())); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 4173 | } |
| 4174 | |
Siarhei Vishniakou | ac42d24 | 2024-06-28 10:43:53 -0700 | [diff] [blame^] | 4175 | /** |
| 4176 | * When events are not split, the downTime should be adjusted such that the downTime corresponds |
| 4177 | * to the event time of the first ACTION_DOWN. If a new window appears, it should not affect |
| 4178 | * the event routing because the first window prevents splitting. |
| 4179 | */ |
| 4180 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTimeForNewWindow) { |
| 4181 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4182 | sp<FakeWindowHandle> window1 = |
| 4183 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
| 4184 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 4185 | window1->setPreventSplitting(true); |
| 4186 | |
| 4187 | sp<FakeWindowHandle> window2 = |
| 4188 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
| 4189 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 4190 | |
| 4191 | mDispatcher->onWindowInfosChanged({{*window1->getInfo()}, {}, 0, 0}); |
| 4192 | |
| 4193 | // Touch down on the first window |
| 4194 | NotifyMotionArgs downArgs = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4195 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4196 | .build(); |
| 4197 | mDispatcher->notifyMotion(downArgs); |
| 4198 | |
| 4199 | window1->consumeMotionEvent( |
| 4200 | AllOf(WithMotionAction(ACTION_DOWN), WithDownTime(downArgs.downTime))); |
| 4201 | |
| 4202 | // Second window is added |
| 4203 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
| 4204 | |
| 4205 | // Now touch down on the window with another pointer |
| 4206 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4207 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4208 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 4209 | .downTime(downArgs.downTime) |
| 4210 | .build()); |
| 4211 | window1->consumeMotionPointerDown(1, AllOf(WithDownTime(downArgs.downTime))); |
| 4212 | |
| 4213 | // Finish the gesture |
| 4214 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4215 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4216 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 4217 | .downTime(downArgs.downTime) |
| 4218 | .build()); |
| 4219 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4220 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4221 | .downTime(downArgs.downTime) |
| 4222 | .build()); |
| 4223 | window1->consumeMotionPointerUp(1, AllOf(WithDownTime(downArgs.downTime))); |
| 4224 | window1->consumeMotionEvent( |
| 4225 | AllOf(WithMotionAction(ACTION_UP), WithDownTime(downArgs.downTime))); |
| 4226 | window2->assertNoEvents(); |
| 4227 | } |
| 4228 | |
| 4229 | /** |
| 4230 | * When splitting touch events, the downTime should be adjusted such that the downTime corresponds |
| 4231 | * to the event time of the first ACTION_DOWN sent to the new window. |
| 4232 | * If a new window that does not support split appears on the screen and gets touched with the |
| 4233 | * second finger, it should not get any events because it doesn't want split touches. At the same |
| 4234 | * time, the first window should not get the pointer_down event because it supports split touches |
| 4235 | * (and the touch occurred outside of the bounds of window1). |
| 4236 | */ |
| 4237 | TEST_F(InputDispatcherTest, SplitTouchesDropsEventForNonSplittableSecondWindow) { |
| 4238 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4239 | sp<FakeWindowHandle> window1 = |
| 4240 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
| 4241 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 4242 | |
| 4243 | sp<FakeWindowHandle> window2 = |
| 4244 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
| 4245 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 4246 | |
| 4247 | mDispatcher->onWindowInfosChanged({{*window1->getInfo()}, {}, 0, 0}); |
| 4248 | |
| 4249 | // Touch down on the first window |
| 4250 | NotifyMotionArgs downArgs = MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4251 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4252 | .build(); |
| 4253 | mDispatcher->notifyMotion(downArgs); |
| 4254 | |
| 4255 | window1->consumeMotionEvent( |
| 4256 | AllOf(WithMotionAction(ACTION_DOWN), WithDownTime(downArgs.downTime))); |
| 4257 | |
| 4258 | // Second window is added |
| 4259 | window2->setPreventSplitting(true); |
| 4260 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
| 4261 | |
| 4262 | // Now touch down on the window with another pointer |
| 4263 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4264 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4265 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 4266 | .downTime(downArgs.downTime) |
| 4267 | .build()); |
| 4268 | // Event is dropped because window2 doesn't support split touch, and window1 does. |
| 4269 | |
| 4270 | // Complete the gesture |
| 4271 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4272 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4273 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 4274 | .downTime(downArgs.downTime) |
| 4275 | .build()); |
| 4276 | // A redundant MOVE event is generated that doesn't carry any new information |
| 4277 | window1->consumeMotionEvent( |
| 4278 | AllOf(WithMotionAction(ACTION_MOVE), WithDownTime(downArgs.downTime))); |
| 4279 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4280 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4281 | .downTime(downArgs.downTime) |
| 4282 | .build()); |
| 4283 | |
| 4284 | window1->consumeMotionEvent( |
| 4285 | AllOf(WithMotionAction(ACTION_UP), WithDownTime(downArgs.downTime))); |
| 4286 | window1->assertNoEvents(); |
| 4287 | window2->assertNoEvents(); |
| 4288 | } |
| 4289 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4290 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4291 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4292 | sp<FakeWindowHandle> windowLeft = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 4293 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4294 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4295 | sp<FakeWindowHandle> windowRight = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 4296 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4297 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4298 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4299 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4300 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4301 | mDispatcher->onWindowInfosChanged( |
| 4302 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4303 | |
| 4304 | // 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] | 4305 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4306 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4307 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4308 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4309 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4310 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4311 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4312 | |
| 4313 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4314 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4315 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4316 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4317 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4318 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4319 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4320 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 4321 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4322 | |
| 4323 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4324 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4325 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4326 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4327 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4328 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4329 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4330 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4331 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4332 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4333 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4334 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4335 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 4336 | AINPUT_SOURCE_MOUSE) |
| 4337 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4338 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4339 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4340 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4341 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4342 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4343 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4344 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4345 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 4346 | AINPUT_SOURCE_MOUSE) |
| 4347 | .buttonState(0) |
| 4348 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4349 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4350 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4351 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4352 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4353 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4354 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4355 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4356 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4357 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4358 | .build())); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4359 | windowLeft->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4360 | |
| 4361 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4362 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4363 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4364 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4365 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4366 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4367 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4368 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4369 | |
| 4370 | // No more events |
| 4371 | windowLeft->assertNoEvents(); |
| 4372 | windowRight->assertNoEvents(); |
| 4373 | } |
| 4374 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4375 | /** |
| 4376 | * Put two fingers down (and don't release them) and click the mouse button. |
| 4377 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 4378 | * currently active gesture should be canceled, and the new one should proceed. |
| 4379 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4380 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick_legacy) { |
| 4381 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4382 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4383 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4384 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4385 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4386 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4387 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4388 | |
| 4389 | const int32_t touchDeviceId = 4; |
| 4390 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4391 | |
| 4392 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4393 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4394 | .deviceId(touchDeviceId) |
| 4395 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4396 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4397 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4398 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4399 | .deviceId(touchDeviceId) |
| 4400 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4401 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 4402 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4403 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4404 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4405 | |
| 4406 | // Inject a series of mouse events for a mouse click |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4407 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4408 | .deviceId(mouseDeviceId) |
| 4409 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4410 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4411 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4412 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 4413 | WithPointerCount(2u))); |
| 4414 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 4415 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4416 | mDispatcher->notifyMotion( |
| 4417 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 4418 | .deviceId(mouseDeviceId) |
| 4419 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4420 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4421 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4422 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4423 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4424 | |
| 4425 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 4426 | // already canceled gesture, it should be ignored. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4427 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 4428 | .deviceId(touchDeviceId) |
| 4429 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 4430 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 4431 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4432 | window->assertNoEvents(); |
| 4433 | } |
| 4434 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4435 | /** |
| 4436 | * Put two fingers down (and don't release them) and click the mouse button. |
| 4437 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 4438 | * currently active gesture should not be canceled, and the new one should proceed in parallel. |
| 4439 | */ |
| 4440 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 4441 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 4442 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4443 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4444 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4445 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4446 | |
| 4447 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 4448 | |
| 4449 | const int32_t touchDeviceId = 4; |
| 4450 | const int32_t mouseDeviceId = 6; |
| 4451 | |
| 4452 | // Two pointers down |
| 4453 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4454 | .deviceId(touchDeviceId) |
| 4455 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4456 | .build()); |
| 4457 | |
| 4458 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4459 | .deviceId(touchDeviceId) |
| 4460 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4461 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 4462 | .build()); |
| 4463 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4464 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4465 | |
| 4466 | // Send a series of mouse events for a mouse click |
| 4467 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4468 | .deviceId(mouseDeviceId) |
| 4469 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4470 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4471 | .build()); |
| 4472 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 4473 | |
| 4474 | mDispatcher->notifyMotion( |
| 4475 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 4476 | .deviceId(mouseDeviceId) |
| 4477 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4478 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4479 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4480 | .build()); |
| 4481 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4482 | |
| 4483 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 4484 | // already active gesture, it should be sent normally. |
| 4485 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 4486 | .deviceId(touchDeviceId) |
| 4487 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 4488 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 4489 | .build()); |
| 4490 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 4491 | window->assertNoEvents(); |
| 4492 | } |
| 4493 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4494 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 4495 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4496 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4497 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 4498 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4499 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4500 | spyWindow->setTrustedOverlay(true); |
| 4501 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4502 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4503 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4504 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4505 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4506 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4507 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4508 | |
| 4509 | // Send mouse cursor to the window |
| 4510 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4511 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4512 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4513 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4514 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4515 | .build())); |
| 4516 | |
| 4517 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4518 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4519 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4520 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4521 | |
| 4522 | window->assertNoEvents(); |
| 4523 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4524 | } |
| 4525 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4526 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows_legacy) { |
| 4527 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4528 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4529 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4530 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 4531 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4532 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4533 | spyWindow->setTrustedOverlay(true); |
| 4534 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4535 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4536 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4537 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4538 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4539 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4540 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4541 | |
| 4542 | // Send mouse cursor to the window |
| 4543 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4544 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4545 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4546 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4547 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4548 | .build())); |
| 4549 | |
| 4550 | // Move mouse cursor |
| 4551 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4552 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4553 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4554 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4555 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4556 | .build())); |
| 4557 | |
| 4558 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4559 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4560 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4561 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4562 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4563 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4564 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4565 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4566 | // Touch down on the window |
| 4567 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4568 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4569 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 4570 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4571 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4572 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4573 | .build())); |
| 4574 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4575 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4576 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4577 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4578 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4579 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4580 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4581 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4582 | |
| 4583 | // pilfer the motion, retaining the gesture on the spy window. |
| 4584 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 4585 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 4586 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4587 | |
| 4588 | // Touch UP on the window |
| 4589 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4590 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4591 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 4592 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4593 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4594 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4595 | .build())); |
| 4596 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4597 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4598 | |
| 4599 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 4600 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 4601 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 4602 | |
| 4603 | // One more tap - DOWN |
| 4604 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4605 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4606 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 4607 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4608 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4609 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4610 | .build())); |
| 4611 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4612 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4613 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4614 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4615 | |
| 4616 | // Touch UP on the window |
| 4617 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4618 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4619 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 4620 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4621 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4622 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4623 | .build())); |
| 4624 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4625 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4626 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4627 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4628 | |
| 4629 | window->assertNoEvents(); |
| 4630 | spyWindow->assertNoEvents(); |
| 4631 | } |
| 4632 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4633 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 4634 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 4635 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4636 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4637 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 4638 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4639 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4640 | spyWindow->setTrustedOverlay(true); |
| 4641 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4642 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4643 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4644 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4645 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4646 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4647 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 4648 | |
| 4649 | // Send mouse cursor to the window |
| 4650 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4651 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 4652 | .build()); |
| 4653 | |
| 4654 | // Move mouse cursor |
| 4655 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4656 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 4657 | .build()); |
| 4658 | |
| 4659 | window->consumeMotionEvent( |
| 4660 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4661 | spyWindow->consumeMotionEvent( |
| 4662 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4663 | window->consumeMotionEvent( |
| 4664 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4665 | spyWindow->consumeMotionEvent( |
| 4666 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4667 | // Touch down on the window |
| 4668 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4669 | .deviceId(SECOND_DEVICE_ID) |
| 4670 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
| 4671 | .build()); |
| 4672 | window->consumeMotionEvent( |
| 4673 | AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4674 | spyWindow->consumeMotionEvent( |
| 4675 | AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4676 | |
| 4677 | // pilfer the motion, retaining the gesture on the spy window. |
| 4678 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 4679 | window->consumeMotionEvent( |
| 4680 | AllOf(WithMotionAction(ACTION_CANCEL), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4681 | // Mouse hover is not pilfered |
| 4682 | |
| 4683 | // Touch UP on the window |
| 4684 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4685 | .deviceId(SECOND_DEVICE_ID) |
| 4686 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
| 4687 | .build()); |
| 4688 | spyWindow->consumeMotionEvent( |
| 4689 | AllOf(WithMotionAction(ACTION_UP), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4690 | |
| 4691 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 4692 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 4693 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 4694 | |
| 4695 | // One more tap - DOWN |
| 4696 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4697 | .deviceId(SECOND_DEVICE_ID) |
| 4698 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
| 4699 | .build()); |
| 4700 | window->consumeMotionEvent( |
| 4701 | AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4702 | spyWindow->consumeMotionEvent( |
| 4703 | AllOf(WithMotionAction(ACTION_DOWN), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4704 | |
| 4705 | // Touch UP on the window |
| 4706 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4707 | .deviceId(SECOND_DEVICE_ID) |
| 4708 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
| 4709 | .build()); |
| 4710 | window->consumeMotionEvent( |
| 4711 | AllOf(WithMotionAction(ACTION_UP), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4712 | spyWindow->consumeMotionEvent( |
| 4713 | AllOf(WithMotionAction(ACTION_UP), WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4714 | |
| 4715 | // Mouse movement continues normally as well |
| 4716 | // Move mouse cursor |
| 4717 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4718 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(120).y(130)) |
| 4719 | .build()); |
| 4720 | window->consumeMotionEvent( |
| 4721 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4722 | spyWindow->consumeMotionEvent( |
| 4723 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 4724 | |
| 4725 | window->assertNoEvents(); |
| 4726 | spyWindow->assertNoEvents(); |
| 4727 | } |
| 4728 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4729 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 4730 | // directly in this test. |
| 4731 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4732 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4733 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4734 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4735 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4736 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4737 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4738 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4739 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4740 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4741 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4742 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4743 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4744 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4745 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4746 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4747 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4748 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4749 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4750 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4751 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4752 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4753 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4754 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4755 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4756 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4757 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4758 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4759 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4760 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 4761 | AINPUT_SOURCE_MOUSE) |
| 4762 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4763 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4764 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4765 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4766 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4767 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4768 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4769 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4770 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 4771 | AINPUT_SOURCE_MOUSE) |
| 4772 | .buttonState(0) |
| 4773 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4774 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4775 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4776 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4777 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4778 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4779 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4780 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4781 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4782 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4783 | .build())); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4784 | window->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4785 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 4786 | // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the |
| 4787 | // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail. |
| 4788 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4789 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4790 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 4791 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4792 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4793 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4794 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4795 | } |
| 4796 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4797 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4798 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 4799 | * is generated. |
| 4800 | */ |
| 4801 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 4802 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4803 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4804 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4805 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 4806 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4807 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4808 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4809 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4810 | |
| 4811 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4812 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4813 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4814 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4815 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4816 | .build())); |
| 4817 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4818 | |
| 4819 | // Remove the window, but keep the channel. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4820 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4821 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 4822 | } |
| 4823 | |
| 4824 | /** |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4825 | * Test that invalid HOVER events sent by accessibility do not cause a fatal crash. |
| 4826 | */ |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 4827 | TEST_F_WITH_FLAGS(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash, |
| 4828 | REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(com::android::input::flags, |
| 4829 | a11y_crash_on_inconsistent_event_stream))) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4830 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4831 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4832 | ui::LogicalDisplayId::DEFAULT); |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4833 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 4834 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4835 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4836 | |
| 4837 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 4838 | |
| 4839 | MotionEventBuilder hoverEnterBuilder = |
| 4840 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4841 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4842 | .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 4843 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4844 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 4845 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4846 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 4847 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4848 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4849 | } |
| 4850 | |
| 4851 | /** |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4852 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. |
| 4853 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4854 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover_legacy) { |
| 4855 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4856 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4857 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4858 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4859 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4860 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4861 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4862 | |
| 4863 | const int32_t mouseDeviceId = 7; |
| 4864 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4865 | |
| 4866 | // Start hovering with the mouse |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4867 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4868 | .deviceId(mouseDeviceId) |
| 4869 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 4870 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4871 | window->consumeMotionEvent( |
| 4872 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 4873 | |
| 4874 | // Touch goes down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4875 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4876 | .deviceId(touchDeviceId) |
| 4877 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4878 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4879 | |
| 4880 | window->consumeMotionEvent( |
| 4881 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 4882 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 4883 | } |
| 4884 | |
| 4885 | /** |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4886 | * If mouse is hovering when the touch goes down, the hovering should not be stopped. |
| 4887 | */ |
| 4888 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { |
| 4889 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 4890 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4891 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4892 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4893 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4894 | |
| 4895 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 4896 | |
| 4897 | const int32_t mouseDeviceId = 7; |
| 4898 | const int32_t touchDeviceId = 4; |
| 4899 | |
| 4900 | // Start hovering with the mouse |
| 4901 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4902 | .deviceId(mouseDeviceId) |
| 4903 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 4904 | .build()); |
| 4905 | window->consumeMotionEvent( |
| 4906 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 4907 | |
| 4908 | // Touch goes down |
| 4909 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4910 | .deviceId(touchDeviceId) |
| 4911 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4912 | .build()); |
| 4913 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 4914 | } |
| 4915 | |
| 4916 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4917 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4918 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 4919 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4920 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4921 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap_legacy) { |
| 4922 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4923 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4924 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4925 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4926 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4927 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4928 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4929 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4930 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 4931 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4932 | ASSERT_NO_FATAL_FAILURE( |
| 4933 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4934 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4935 | |
| 4936 | // Tap on the window |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4937 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4938 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4939 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4940 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4941 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4942 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 4943 | |
| 4944 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4945 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4946 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4947 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4948 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4949 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4950 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4951 | ASSERT_NO_FATAL_FAILURE( |
| 4952 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4953 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4954 | } |
| 4955 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4956 | /** |
| 4957 | * Send a mouse hover event followed by a tap from touchscreen. |
| 4958 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 4959 | * stream's source has been switched. |
| 4960 | */ |
| 4961 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 4962 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 4963 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4964 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 4965 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 4966 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4967 | |
| 4968 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 4969 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4970 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 4971 | .build()); |
| 4972 | |
| 4973 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4974 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4975 | |
| 4976 | // Tap on the window |
| 4977 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4978 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4979 | .build()); |
| 4980 | |
| 4981 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4982 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4983 | |
| 4984 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4985 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4986 | |
| 4987 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4988 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4989 | .build()); |
| 4990 | |
| 4991 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4992 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4993 | } |
| 4994 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4995 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 4996 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4997 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 4998 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 4999 | ui::LogicalDisplayId::DEFAULT); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5000 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 5001 | sp<FakeWindowHandle> windowSecondDisplay = |
| 5002 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 5003 | SECOND_DISPLAY_ID); |
| 5004 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 5005 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5006 | mDispatcher->onWindowInfosChanged( |
| 5007 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5008 | |
| 5009 | // Set cursor position in window in default display and check that hover enter and move |
| 5010 | // events are generated. |
| 5011 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5012 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5013 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 5014 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5015 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5016 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5017 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 5018 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5019 | |
| 5020 | // Remove all windows in secondary display and check that no event happens on window in |
| 5021 | // primary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5022 | mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0}); |
| 5023 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5024 | windowDefaultDisplay->assertNoEvents(); |
| 5025 | |
| 5026 | // Move cursor position in window in default display and check that only hover move |
| 5027 | // event is generated and not hover enter event. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5028 | mDispatcher->onWindowInfosChanged( |
| 5029 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5030 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5031 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5032 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 5033 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5034 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5035 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5036 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 5037 | windowDefaultDisplay->consumeMotionEvent( |
| 5038 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 5039 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 5040 | windowDefaultDisplay->assertNoEvents(); |
| 5041 | } |
| 5042 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 5043 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5044 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 5045 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5046 | sp<FakeWindowHandle> windowLeft = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 5047 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 5048 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5049 | sp<FakeWindowHandle> windowRight = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 5050 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 5051 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 5052 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5053 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 5054 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5055 | mDispatcher->onWindowInfosChanged( |
| 5056 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 5057 | |
| 5058 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 5059 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5060 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5061 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5062 | ui::LogicalDisplayId::DEFAULT, {610, 400}, {599, 400})); |
| 5063 | windowLeft->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 5064 | windowRight->assertNoEvents(); |
| 5065 | } |
| 5066 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5067 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5068 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5069 | sp<FakeWindowHandle> window = |
| 5070 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5071 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5072 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5073 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5074 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5075 | setFocusedWindow(window); |
| 5076 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5077 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5078 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5079 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5080 | |
| 5081 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5082 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5083 | |
| 5084 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 5085 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5086 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5087 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT, AKEY_EVENT_FLAG_CANCELED); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5088 | } |
| 5089 | |
| 5090 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5091 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5092 | sp<FakeWindowHandle> window = |
| 5093 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5094 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5095 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5096 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5097 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5098 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5099 | AINPUT_SOURCE_TOUCHSCREEN, |
| 5100 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5101 | |
| 5102 | // Window should receive motion down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5103 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5104 | |
| 5105 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 5106 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5107 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 5108 | window->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5109 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 5110 | } |
| 5111 | |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 5112 | TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) { |
| 5113 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5114 | sp<FakeWindowHandle> window = |
| 5115 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5116 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 5117 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5118 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 5119 | |
| 5120 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 5121 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10)) |
| 5122 | .build()); |
| 5123 | |
| 5124 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 5125 | |
| 5126 | // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT |
| 5127 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
| 5128 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 5129 | |
| 5130 | // After the device has been reset, a new hovering stream can be sent to the window |
| 5131 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 5132 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15)) |
| 5133 | .build()); |
| 5134 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 5135 | } |
| 5136 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5137 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 5138 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5139 | sp<FakeWindowHandle> window = |
| 5140 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5141 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5142 | window->setFocusable(true); |
| 5143 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5144 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5145 | setFocusedWindow(window); |
| 5146 | |
| 5147 | window->consumeFocusEvent(true); |
| 5148 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5149 | const NotifyKeyArgs keyArgs = |
| 5150 | generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5151 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 5152 | const nsecs_t injectTime = keyArgs.eventTime; |
| 5153 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5154 | mDispatcher->notifyKey(keyArgs); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5155 | // 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] | 5156 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5157 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 5158 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 5159 | } |
| 5160 | |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 5161 | /** |
| 5162 | * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set. |
| 5163 | */ |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5164 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 5165 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5166 | sp<FakeWindowHandle> window = |
| 5167 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5168 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5169 | window->setFocusable(true); |
| 5170 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5171 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5172 | setFocusedWindow(window); |
| 5173 | |
| 5174 | window->consumeFocusEvent(true); |
| 5175 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5176 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
| 5177 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 5178 | |
| 5179 | // Set a value that's significantly larger than the default consumption timeout. If the |
| 5180 | // implementation is correct, the actual value doesn't matter; it won't slow down the test. |
| 5181 | mFakePolicy->setInterceptKeyTimeout(600ms); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5182 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 5183 | // Window should receive key event immediately when same key up. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5184 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 5185 | } |
| 5186 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5187 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5188 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 5189 | * WATCH_OUTSIDE_TOUCH. |
| 5190 | * Both windows are owned by the same UID. |
| 5191 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 5192 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 5193 | */ |
| 5194 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 5195 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5196 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5197 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5198 | window->setFrame(Rect{0, 0, 100, 100}); |
| 5199 | |
| 5200 | sp<FakeWindowHandle> outsideWindow = |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 5201 | sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5202 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5203 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 5204 | outsideWindow->setWatchOutsideTouch(true); |
| 5205 | // 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] | 5206 | mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5207 | |
| 5208 | // Tap on first window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5209 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5210 | AINPUT_SOURCE_TOUCHSCREEN, |
| 5211 | ui::LogicalDisplayId::DEFAULT, {PointF{50, 50}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5212 | window->consumeMotionDown(); |
| 5213 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 5214 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 5215 | outsideWindow->consumeMotionEvent( |
| 5216 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 5217 | |
| 5218 | // Ensure outsideWindow doesn't get any more events for the gesture. |
| 5219 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5220 | ui::LogicalDisplayId::DEFAULT, {PointF{51, 51}})); |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 5221 | window->consumeMotionMove(); |
| 5222 | outsideWindow->assertNoEvents(); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5223 | } |
| 5224 | |
| 5225 | /** |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 5226 | * Three windows: |
| 5227 | * - Left window |
| 5228 | * - Right window |
| 5229 | * - Outside window(watch for ACTION_OUTSIDE events) |
| 5230 | * The windows "left" and "outside" share the same owner, the window "right" has a different owner, |
| 5231 | * In order to allow the outside window can receive the ACTION_OUTSIDE events, the outside window is |
| 5232 | * positioned above the "left" and "right" windows, and it doesn't overlap with them. |
| 5233 | * |
| 5234 | * First, device A report a down event landed in the right window, the outside window can receive |
| 5235 | * an ACTION_OUTSIDE event that with zeroed coordinates, the device B report a down event landed |
| 5236 | * in the left window, the outside window can receive an ACTION_OUTSIDE event the with valid |
| 5237 | * coordinates, after these, device A and device B continue report MOVE event, the right and left |
| 5238 | * window can receive it, but outside window event can't receive it. |
| 5239 | */ |
| 5240 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinatesWhenMultiDevice) { |
| 5241 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5242 | sp<FakeWindowHandle> leftWindow = |
| 5243 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5244 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 5245 | leftWindow->setFrame(Rect{0, 0, 100, 100}); |
| 5246 | leftWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
| 5247 | |
| 5248 | sp<FakeWindowHandle> outsideWindow = |
| 5249 | sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5250 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 5251 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 5252 | outsideWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
| 5253 | outsideWindow->setWatchOutsideTouch(true); |
| 5254 | |
| 5255 | std::shared_ptr<FakeApplicationHandle> anotherApplication = |
| 5256 | std::make_shared<FakeApplicationHandle>(); |
| 5257 | sp<FakeWindowHandle> rightWindow = |
| 5258 | sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Right Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5259 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 5260 | rightWindow->setFrame(Rect{100, 0, 200, 100}); |
| 5261 | rightWindow->setOwnerInfo(gui::Pid{2}, gui::Uid{202}); |
| 5262 | |
| 5263 | // OutsideWindow must be above left window and right window to receive ACTION_OUTSIDE events |
| 5264 | // when left window or right window is tapped |
| 5265 | mDispatcher->onWindowInfosChanged( |
| 5266 | {{*outsideWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, |
| 5267 | {}, |
| 5268 | 0, |
| 5269 | 0}); |
| 5270 | |
| 5271 | const DeviceId deviceA = 9; |
| 5272 | const DeviceId deviceB = 3; |
| 5273 | |
| 5274 | // Tap on right window use device A |
| 5275 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5276 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 5277 | .deviceId(deviceA) |
| 5278 | .build()); |
| 5279 | leftWindow->assertNoEvents(); |
| 5280 | rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 5281 | // Right window is belonged to another owner, so outsideWindow should receive ACTION_OUTSIDE |
| 5282 | // with zeroed coords. |
| 5283 | outsideWindow->consumeMotionEvent( |
| 5284 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithDeviceId(deviceA), WithCoords(0, 0))); |
| 5285 | |
| 5286 | // Tap on left window use device B |
| 5287 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5288 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5289 | .deviceId(deviceB) |
| 5290 | .build()); |
| 5291 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 5292 | rightWindow->assertNoEvents(); |
| 5293 | // Because new gesture down on the left window that has the same owner with outside Window, the |
| 5294 | // outside Window should receive the ACTION_OUTSIDE with coords. |
| 5295 | outsideWindow->consumeMotionEvent( |
| 5296 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithDeviceId(deviceB), WithCoords(-50, -50))); |
| 5297 | |
| 5298 | // Ensure that windows that can only accept outside do not receive remaining gestures |
| 5299 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5300 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(51)) |
| 5301 | .deviceId(deviceA) |
| 5302 | .build()); |
| 5303 | leftWindow->assertNoEvents(); |
| 5304 | rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceA))); |
| 5305 | |
| 5306 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 5307 | .pointer(PointerBuilder(0, ToolType::FINGER).x(51).y(51)) |
| 5308 | .deviceId(deviceB) |
| 5309 | .build()); |
| 5310 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB))); |
| 5311 | rightWindow->assertNoEvents(); |
| 5312 | outsideWindow->assertNoEvents(); |
| 5313 | } |
| 5314 | |
| 5315 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5316 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 5317 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 5318 | * ACTION_OUTSIDE event is sent per gesture. |
| 5319 | */ |
| 5320 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 5321 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 5322 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5323 | sp<FakeWindowHandle> window = |
| 5324 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5325 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5326 | window->setWatchOutsideTouch(true); |
| 5327 | window->setFrame(Rect{0, 0, 100, 100}); |
| 5328 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5329 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5330 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5331 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 5332 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5333 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5334 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5335 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5336 | mDispatcher->onWindowInfosChanged( |
| 5337 | {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5338 | |
| 5339 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5340 | mDispatcher->notifyMotion( |
| 5341 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5342 | ui::LogicalDisplayId::DEFAULT, {PointF{-10, -10}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5343 | window->assertNoEvents(); |
| 5344 | secondWindow->assertNoEvents(); |
| 5345 | |
| 5346 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 5347 | // Now, `window` should get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5348 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5349 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5350 | {PointF{-10, -10}, PointF{105, 105}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 5351 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 5352 | window->consumeMotionEvent( |
| 5353 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5354 | secondWindow->consumeMotionDown(); |
| 5355 | thirdWindow->assertNoEvents(); |
| 5356 | |
| 5357 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 5358 | // 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] | 5359 | mDispatcher->notifyMotion( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5360 | generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5361 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5362 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5363 | window->assertNoEvents(); |
| 5364 | secondWindow->consumeMotionMove(); |
| 5365 | thirdWindow->consumeMotionDown(); |
| 5366 | } |
| 5367 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5368 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 5369 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5370 | sp<FakeWindowHandle> window = |
| 5371 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5372 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5373 | window->setFocusable(true); |
| 5374 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5375 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5376 | setFocusedWindow(window); |
| 5377 | |
| 5378 | window->consumeFocusEvent(true); |
| 5379 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5380 | const NotifyKeyArgs keyDown = |
| 5381 | generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT); |
| 5382 | const NotifyKeyArgs keyUp = |
| 5383 | generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5384 | mDispatcher->notifyKey(keyDown); |
| 5385 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5386 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5387 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
| 5388 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5389 | |
| 5390 | // 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] | 5391 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5392 | |
| 5393 | window->consumeFocusEvent(false); |
| 5394 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5395 | mDispatcher->notifyKey(keyDown); |
| 5396 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 5397 | window->assertNoEvents(); |
| 5398 | } |
| 5399 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5400 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 5401 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5402 | sp<FakeWindowHandle> window = |
| 5403 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 5404 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5405 | // Ensure window is non-split and have some transform. |
| 5406 | window->setPreventSplitting(true); |
| 5407 | window->setWindowOffset(20, 40); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5408 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5409 | |
| 5410 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5411 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 5412 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5413 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5414 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5415 | |
| 5416 | const MotionEvent secondFingerDownEvent = |
| 5417 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5418 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5419 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 5420 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 5421 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5422 | .build(); |
| 5423 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5424 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5425 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 5426 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5427 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5428 | std::unique_ptr<MotionEvent> event = window->consumeMotionEvent(); |
| 5429 | ASSERT_NE(nullptr, event); |
| 5430 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 5431 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 5432 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 5433 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 5434 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 5435 | } |
| 5436 | |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 5437 | /** |
| 5438 | * Two windows: a splittable and a non-splittable. |
| 5439 | * The non-splittable window shouldn't receive any "incomplete" gestures. |
| 5440 | * Send the first pointer to the splittable window, and then touch the non-splittable window. |
| 5441 | * The second pointer should be dropped because the initial window is splittable, so it won't get |
| 5442 | * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any |
| 5443 | * "incomplete" gestures. |
| 5444 | */ |
| 5445 | TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) { |
| 5446 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5447 | sp<FakeWindowHandle> leftWindow = |
| 5448 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5449 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 5450 | leftWindow->setPreventSplitting(false); |
| 5451 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 5452 | sp<FakeWindowHandle> rightWindow = |
| 5453 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5454 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 5455 | rightWindow->setPreventSplitting(true); |
| 5456 | rightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 5457 | mDispatcher->onWindowInfosChanged( |
| 5458 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 5459 | |
| 5460 | // Touch down on left, splittable window |
| 5461 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5462 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 5463 | .build()); |
| 5464 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 5465 | |
| 5466 | mDispatcher->notifyMotion( |
| 5467 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5468 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 5469 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
| 5470 | .build()); |
| 5471 | leftWindow->assertNoEvents(); |
| 5472 | rightWindow->assertNoEvents(); |
| 5473 | } |
| 5474 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 5475 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) { |
| 5476 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5477 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5478 | ui::LogicalDisplayId::DEFAULT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 5479 | window->setFrame(Rect(0, 0, 400, 400)); |
| 5480 | sp<FakeWindowHandle> trustedOverlay = |
| 5481 | sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5482 | ui::LogicalDisplayId::DEFAULT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 5483 | trustedOverlay->setSpy(true); |
| 5484 | trustedOverlay->setTrustedOverlay(true); |
| 5485 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5486 | mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 5487 | |
| 5488 | // Start a three-finger touchpad swipe |
| 5489 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 5490 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 5491 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5492 | .build()); |
| 5493 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 5494 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 5495 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 5496 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5497 | .build()); |
| 5498 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 5499 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 5500 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 5501 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 5502 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5503 | .build()); |
| 5504 | |
| 5505 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 5506 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 5507 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN)); |
| 5508 | |
| 5509 | // Move the swipe a bit |
| 5510 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 5511 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5512 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 5513 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 5514 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5515 | .build()); |
| 5516 | |
| 5517 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 5518 | |
| 5519 | // End the swipe |
| 5520 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 5521 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5522 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 5523 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 5524 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5525 | .build()); |
| 5526 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 5527 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5528 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 5529 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5530 | .build()); |
| 5531 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 5532 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5533 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5534 | .build()); |
| 5535 | |
| 5536 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP)); |
| 5537 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 5538 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 5539 | |
| 5540 | window->assertNoEvents(); |
| 5541 | } |
| 5542 | |
| 5543 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) { |
| 5544 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5545 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5546 | ui::LogicalDisplayId::DEFAULT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 5547 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5548 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 5549 | |
| 5550 | // Start a three-finger touchpad swipe |
| 5551 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 5552 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 5553 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5554 | .build()); |
| 5555 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 5556 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 5557 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 5558 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5559 | .build()); |
| 5560 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 5561 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 5562 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 5563 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 5564 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5565 | .build()); |
| 5566 | |
| 5567 | // Move the swipe a bit |
| 5568 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 5569 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5570 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 5571 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 5572 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5573 | .build()); |
| 5574 | |
| 5575 | // End the swipe |
| 5576 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 5577 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5578 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 5579 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 5580 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5581 | .build()); |
| 5582 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 5583 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5584 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 5585 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5586 | .build()); |
| 5587 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 5588 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 5589 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 5590 | .build()); |
| 5591 | |
| 5592 | window->assertNoEvents(); |
| 5593 | } |
| 5594 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 5595 | /** |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5596 | * Send a two-pointer gesture to a single window. The window's orientation changes in response to |
| 5597 | * the first pointer. |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 5598 | * 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] | 5599 | */ |
| 5600 | TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) { |
| 5601 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5602 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 5603 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5604 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 5605 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5606 | |
| 5607 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 5608 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5609 | .downTime(baseTime + 10) |
| 5610 | .eventTime(baseTime + 10) |
| 5611 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 5612 | .build()); |
| 5613 | |
| 5614 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 5615 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5616 | // Change the transform so that the orientation is now different from original. |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 5617 | window->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5618 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 5619 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5620 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5621 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5622 | .downTime(baseTime + 10) |
| 5623 | .eventTime(baseTime + 30) |
| 5624 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 5625 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 5626 | .build()); |
| 5627 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 5628 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 5629 | |
| 5630 | // 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] | 5631 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 5632 | .downTime(baseTime + 10) |
| 5633 | .eventTime(baseTime + 40) |
| 5634 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 5635 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 5636 | .build()); |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 5637 | |
| 5638 | window->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 5639 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5640 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 5641 | .downTime(baseTime + 10) |
| 5642 | .eventTime(baseTime + 50) |
| 5643 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 5644 | .build()); |
| 5645 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 5646 | window->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 5647 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5648 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5649 | .downTime(baseTime + 60) |
| 5650 | .eventTime(baseTime + 60) |
| 5651 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40)) |
| 5652 | .build()); |
| 5653 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 5654 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5655 | } |
| 5656 | |
| 5657 | /** |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5658 | * When there are multiple screens, such as screen projection to TV or screen recording, if the |
| 5659 | * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and |
| 5660 | * its coordinates should be converted by the transform of the windows of target screen. |
| 5661 | */ |
| 5662 | TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) { |
| 5663 | // This case will create a window and a spy window on the default display and mirror |
| 5664 | // window on the second display. cancel event is sent through spy window pilferPointers |
| 5665 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5666 | |
| 5667 | sp<FakeWindowHandle> spyWindowDefaultDisplay = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5668 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 5669 | ui::LogicalDisplayId::DEFAULT); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5670 | spyWindowDefaultDisplay->setTrustedOverlay(true); |
| 5671 | spyWindowDefaultDisplay->setSpy(true); |
| 5672 | |
| 5673 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 5674 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5675 | ui::LogicalDisplayId::DEFAULT); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5676 | windowDefaultDisplay->setWindowTransform(1, 0, 0, 1); |
| 5677 | |
| 5678 | sp<FakeWindowHandle> windowSecondDisplay = windowDefaultDisplay->clone(SECOND_DISPLAY_ID); |
| 5679 | windowSecondDisplay->setWindowTransform(2, 0, 0, 2); |
| 5680 | |
| 5681 | // Add the windows to the dispatcher |
| 5682 | mDispatcher->onWindowInfosChanged( |
| 5683 | {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(), |
| 5684 | *windowSecondDisplay->getInfo()}, |
| 5685 | {}, |
| 5686 | 0, |
| 5687 | 0}); |
| 5688 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5689 | // Send down to ui::LogicalDisplayId::DEFAULT |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5690 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5691 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 5692 | ui::LogicalDisplayId::DEFAULT, {100, 100})) |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5693 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5694 | |
| 5695 | spyWindowDefaultDisplay->consumeMotionDown(); |
| 5696 | windowDefaultDisplay->consumeMotionDown(); |
| 5697 | |
| 5698 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken())); |
| 5699 | |
| 5700 | // windowDefaultDisplay gets cancel |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5701 | std::unique_ptr<MotionEvent> event = windowDefaultDisplay->consumeMotionEvent(); |
| 5702 | ASSERT_NE(nullptr, event); |
| 5703 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event->getAction()); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5704 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5705 | // The cancel event is sent to windowDefaultDisplay of the ui::LogicalDisplayId::DEFAULT |
| 5706 | // display, so the coordinates of the cancel are converted by windowDefaultDisplay's transform, |
| 5707 | // the x and y coordinates are both 100, otherwise if the cancel event is sent to |
| 5708 | // windowSecondDisplay of SECOND_DISPLAY_ID, the x and y coordinates are 200 |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5709 | EXPECT_EQ(100, event->getX(0)); |
| 5710 | EXPECT_EQ(100, event->getY(0)); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 5711 | } |
| 5712 | |
| 5713 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5714 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 5715 | * |
| 5716 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 5717 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 5718 | * space. |
| 5719 | */ |
| 5720 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 5721 | public: |
| 5722 | void SetUp() override { |
| 5723 | InputDispatcherTest::SetUp(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5724 | removeAllWindowsAndDisplays(); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5725 | } |
| 5726 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 5727 | void addDisplayInfo(ui::LogicalDisplayId displayId, const ui::Transform& transform) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5728 | gui::DisplayInfo info; |
| 5729 | info.displayId = displayId; |
| 5730 | info.transform = transform; |
| 5731 | mDisplayInfos.push_back(std::move(info)); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5732 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5733 | } |
| 5734 | |
| 5735 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 5736 | mWindowInfos.push_back(*windowHandle->getInfo()); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5737 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5738 | } |
| 5739 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5740 | void removeAllWindowsAndDisplays() { |
| 5741 | mDisplayInfos.clear(); |
| 5742 | mWindowInfos.clear(); |
| 5743 | } |
| 5744 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5745 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 5746 | // on the display. |
| 5747 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 5748 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 5749 | // respectively. |
| 5750 | ui::Transform displayTransform; |
| 5751 | displayTransform.set(2, 0, 0, 4); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5752 | addDisplayInfo(ui::LogicalDisplayId::DEFAULT, displayTransform); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5753 | |
| 5754 | std::shared_ptr<FakeApplicationHandle> application = |
| 5755 | std::make_shared<FakeApplicationHandle>(); |
| 5756 | |
| 5757 | // Add two windows to the display. Their frames are represented in the display space. |
| 5758 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5759 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5760 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5761 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 5762 | addWindow(firstWindow); |
| 5763 | |
| 5764 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5765 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5766 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5767 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 5768 | addWindow(secondWindow); |
| 5769 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 5770 | } |
| 5771 | |
| 5772 | private: |
| 5773 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 5774 | std::vector<gui::WindowInfo> mWindowInfos; |
| 5775 | }; |
| 5776 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5777 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5778 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5779 | // 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] | 5780 | // selected so that if the hit test was performed with the point and the bounds being in |
| 5781 | // different coordinate spaces, the event would end up in the incorrect window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5782 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5783 | AINPUT_SOURCE_TOUCHSCREEN, |
| 5784 | ui::LogicalDisplayId::DEFAULT, {PointF{75, 55}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5785 | |
| 5786 | firstWindow->consumeMotionDown(); |
| 5787 | secondWindow->assertNoEvents(); |
| 5788 | } |
| 5789 | |
| 5790 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 5791 | // the event should be treated as being in the logical display space. |
| 5792 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 5793 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5794 | // Send down to the first window. The point is represented in the logical display space. The |
| 5795 | // point is selected so that if the hit test was done in logical display space, then it would |
| 5796 | // end up in the incorrect window. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5797 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5798 | PointF{75 * 2, 55 * 4}); |
| 5799 | |
| 5800 | firstWindow->consumeMotionDown(); |
| 5801 | secondWindow->assertNoEvents(); |
| 5802 | } |
| 5803 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5804 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 5805 | // event should be treated as being in the logical display space. |
| 5806 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 5807 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5808 | |
| 5809 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5810 | ui::Transform injectedEventTransform; |
| 5811 | injectedEventTransform.set(matrix); |
| 5812 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 5813 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 5814 | |
| 5815 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5816 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5817 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 5818 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5819 | .x(untransformedPoint.x) |
| 5820 | .y(untransformedPoint.y)) |
| 5821 | .build(); |
| 5822 | event.transform(matrix); |
| 5823 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5824 | injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5825 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 5826 | |
| 5827 | firstWindow->consumeMotionDown(); |
| 5828 | secondWindow->assertNoEvents(); |
| 5829 | } |
| 5830 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5831 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 5832 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5833 | |
| 5834 | // Send down to the second window. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5835 | mDispatcher->notifyMotion( |
| 5836 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5837 | ui::LogicalDisplayId::DEFAULT, {PointF{150, 220}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5838 | |
| 5839 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5840 | std::unique_ptr<MotionEvent> event = secondWindow->consumeMotionEvent(); |
| 5841 | ASSERT_NE(nullptr, event); |
| 5842 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5843 | |
| 5844 | // 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] | 5845 | EXPECT_EQ(300, event->getRawX(0)); |
| 5846 | EXPECT_EQ(880, event->getRawY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5847 | |
| 5848 | // Ensure that the x and y values are in the window's coordinate space. |
| 5849 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 5850 | // 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] | 5851 | EXPECT_EQ(100, event->getX(0)); |
| 5852 | EXPECT_EQ(80, event->getY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5853 | } |
| 5854 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5855 | TEST_F(InputDispatcherDisplayProjectionTest, CancelMotionWithCorrectCoordinates) { |
| 5856 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5857 | // The monitor will always receive events in the logical display's coordinate space, because |
| 5858 | // it does not have a window. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5859 | FakeMonitorReceiver monitor{*mDispatcher, "Monitor", ui::LogicalDisplayId::DEFAULT}; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5860 | |
| 5861 | // Send down to the first window. |
| 5862 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5863 | ui::LogicalDisplayId::DEFAULT, {PointF{50, 100}})); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5864 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5865 | monitor.consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5866 | |
| 5867 | // Second pointer goes down on second window. |
| 5868 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5869 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5870 | {PointF{50, 100}, PointF{150, 220}})); |
| 5871 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80))); |
| 5872 | const std::map<int32_t, PointF> expectedMonitorPointers{{0, PointF{100, 400}}, |
| 5873 | {1, PointF{300, 880}}}; |
| 5874 | monitor.consumeMotionEvent( |
| 5875 | AllOf(WithMotionAction(POINTER_1_DOWN), WithPointers(expectedMonitorPointers))); |
| 5876 | |
| 5877 | mDispatcher->cancelCurrentTouch(); |
| 5878 | |
| 5879 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 5880 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 80))); |
| 5881 | monitor.consumeMotionEvent( |
| 5882 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedMonitorPointers))); |
| 5883 | } |
| 5884 | |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 5885 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeDownWithCorrectCoordinates) { |
| 5886 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5887 | |
| 5888 | // Send down to the first window. |
| 5889 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5890 | ui::LogicalDisplayId::DEFAULT, {PointF{50, 100}})); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 5891 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5892 | |
| 5893 | // The pointer is transferred to the second window, and the second window receives it in the |
| 5894 | // correct coordinate space. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5895 | mDispatcher->transferTouchGesture(firstWindow->getToken(), secondWindow->getToken()); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 5896 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 5897 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(-100, -400))); |
| 5898 | } |
| 5899 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5900 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverEnterExitWithCorrectCoordinates) { |
| 5901 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5902 | |
| 5903 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 5904 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5905 | ui::LogicalDisplayId::DEFAULT, |
| 5906 | {PointF{150, 220}})); |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5907 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5908 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5909 | |
| 5910 | // Touch down at the same location and ensure a hover exit is synthesized. |
| 5911 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5912 | ui::LogicalDisplayId::DEFAULT, |
| 5913 | {PointF{150, 220}})); |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5914 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5915 | WithRawCoords(300, 880))); |
| 5916 | secondWindow->consumeMotionEvent( |
| 5917 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5918 | secondWindow->assertNoEvents(); |
| 5919 | firstWindow->assertNoEvents(); |
| 5920 | } |
| 5921 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5922 | // Same as above, but while the window is being mirrored. |
| 5923 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 5924 | SynthesizeHoverEnterExitWithCorrectCoordinatesWhenMirrored) { |
| 5925 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5926 | |
| 5927 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5928 | ui::Transform secondDisplayTransform; |
| 5929 | secondDisplayTransform.set(matrix); |
| 5930 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 5931 | |
| 5932 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 5933 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 5934 | addWindow(secondWindowClone); |
| 5935 | |
| 5936 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 5937 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5938 | ui::LogicalDisplayId::DEFAULT, |
| 5939 | {PointF{150, 220}})); |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5940 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5941 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5942 | |
| 5943 | // Touch down at the same location and ensure a hover exit is synthesized for the correct |
| 5944 | // display. |
| 5945 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5946 | ui::LogicalDisplayId::DEFAULT, |
| 5947 | {PointF{150, 220}})); |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5948 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5949 | WithRawCoords(300, 880))); |
| 5950 | secondWindow->consumeMotionEvent( |
| 5951 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5952 | secondWindow->assertNoEvents(); |
| 5953 | firstWindow->assertNoEvents(); |
| 5954 | } |
| 5955 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5956 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverCancelationWithCorrectCoordinates) { |
| 5957 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5958 | |
| 5959 | // Send hover enter to second window |
| 5960 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5961 | ui::LogicalDisplayId::DEFAULT, |
| 5962 | {PointF{150, 220}})); |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5963 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5964 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5965 | |
| 5966 | mDispatcher->cancelCurrentTouch(); |
| 5967 | |
| 5968 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5969 | WithRawCoords(300, 880))); |
| 5970 | secondWindow->assertNoEvents(); |
| 5971 | firstWindow->assertNoEvents(); |
| 5972 | } |
| 5973 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5974 | // Same as above, but while the window is being mirrored. |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5975 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 5976 | SynthesizeHoverCancelationWithCorrectCoordinatesWhenMirrored) { |
| 5977 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5978 | |
| 5979 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5980 | ui::Transform secondDisplayTransform; |
| 5981 | secondDisplayTransform.set(matrix); |
| 5982 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 5983 | |
| 5984 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 5985 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 5986 | addWindow(secondWindowClone); |
| 5987 | |
| 5988 | // Send hover enter to second window |
| 5989 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5990 | ui::LogicalDisplayId::DEFAULT, |
| 5991 | {PointF{150, 220}})); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5992 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5993 | WithCoords(100, 80), WithRawCoords(300, 880), |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 5994 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5995 | |
| 5996 | mDispatcher->cancelCurrentTouch(); |
| 5997 | |
| 5998 | // Ensure the cancelation happens with the correct displayId and the correct coordinates. |
| 5999 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 6000 | WithRawCoords(300, 880), |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6001 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 6002 | secondWindow->assertNoEvents(); |
| 6003 | firstWindow->assertNoEvents(); |
| 6004 | } |
| 6005 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6006 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ |
| 6007 | class InputDispatcherDisplayOrientationFixture |
| 6008 | : public InputDispatcherDisplayProjectionTest, |
| 6009 | public ::testing::WithParamInterface<ui::Rotation> {}; |
| 6010 | |
| 6011 | // This test verifies the touchable region of a window for all rotations of the display by tapping |
| 6012 | // in different locations on the display, specifically points close to the four corners of a |
| 6013 | // window. |
| 6014 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { |
| 6015 | constexpr static int32_t displayWidth = 400; |
| 6016 | constexpr static int32_t displayHeight = 800; |
| 6017 | |
| 6018 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6019 | |
| 6020 | const auto rotation = GetParam(); |
| 6021 | |
| 6022 | // Set up the display with the specified rotation. |
| 6023 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 6024 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 6025 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 6026 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 6027 | logicalDisplayWidth, logicalDisplayHeight); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6028 | addDisplayInfo(ui::LogicalDisplayId::DEFAULT, displayTransform); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6029 | |
| 6030 | // Create a window with its bounds determined in the logical display. |
| 6031 | const Rect frameInLogicalDisplay(100, 100, 200, 300); |
| 6032 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6033 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 6034 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6035 | window->setFrame(frameInDisplay, displayTransform); |
| 6036 | addWindow(window); |
| 6037 | |
| 6038 | // The following points in logical display space should be inside the window. |
| 6039 | static const std::array<vec2, 4> insidePoints{ |
| 6040 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 6041 | for (const auto pointInsideWindow : insidePoints) { |
| 6042 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); |
| 6043 | const PointF pointInDisplaySpace{p.x, p.y}; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6044 | mDispatcher->notifyMotion( |
| 6045 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6046 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6047 | window->consumeMotionDown(); |
| 6048 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6049 | mDispatcher->notifyMotion( |
| 6050 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6051 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6052 | window->consumeMotionUp(); |
| 6053 | } |
| 6054 | |
| 6055 | // The following points in logical display space should be outside the window. |
| 6056 | static const std::array<vec2, 5> outsidePoints{ |
| 6057 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 6058 | for (const auto pointOutsideWindow : outsidePoints) { |
| 6059 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); |
| 6060 | const PointF pointInDisplaySpace{p.x, p.y}; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6061 | mDispatcher->notifyMotion( |
| 6062 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6063 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6064 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6065 | mDispatcher->notifyMotion( |
| 6066 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6067 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6068 | } |
| 6069 | window->assertNoEvents(); |
| 6070 | } |
| 6071 | |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6072 | // This test verifies the occlusion detection for all rotations of the display by tapping |
| 6073 | // in different locations on the display, specifically points close to the four corners of a |
| 6074 | // window. |
| 6075 | TEST_P(InputDispatcherDisplayOrientationFixture, BlockUntrustClickInDifferentOrientations) { |
| 6076 | constexpr static int32_t displayWidth = 400; |
| 6077 | constexpr static int32_t displayHeight = 800; |
| 6078 | |
| 6079 | std::shared_ptr<FakeApplicationHandle> untrustedWindowApplication = |
| 6080 | std::make_shared<FakeApplicationHandle>(); |
| 6081 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6082 | |
| 6083 | const auto rotation = GetParam(); |
| 6084 | |
| 6085 | // Set up the display with the specified rotation. |
| 6086 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 6087 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 6088 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 6089 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 6090 | logicalDisplayWidth, logicalDisplayHeight); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6091 | addDisplayInfo(ui::LogicalDisplayId::DEFAULT, displayTransform); |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6092 | |
| 6093 | // Create a window that not trusted. |
| 6094 | const Rect untrustedWindowFrameInLogicalDisplay(100, 100, 200, 300); |
| 6095 | |
| 6096 | const Rect untrustedWindowFrameInDisplay = |
| 6097 | displayTransform.inverse().transform(untrustedWindowFrameInLogicalDisplay); |
| 6098 | |
| 6099 | sp<FakeWindowHandle> untrustedWindow = |
| 6100 | sp<FakeWindowHandle>::make(untrustedWindowApplication, mDispatcher, "UntrustedWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6101 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6102 | untrustedWindow->setFrame(untrustedWindowFrameInDisplay, displayTransform); |
| 6103 | untrustedWindow->setTrustedOverlay(false); |
| 6104 | untrustedWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 6105 | untrustedWindow->setTouchable(false); |
| 6106 | untrustedWindow->setAlpha(1.0f); |
| 6107 | untrustedWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
| 6108 | addWindow(untrustedWindow); |
| 6109 | |
| 6110 | // Create a simple app window below the untrusted window. |
| 6111 | const Rect simpleAppWindowFrameInLogicalDisplay(0, 0, 300, 600); |
| 6112 | const Rect simpleAppWindowFrameInDisplay = |
| 6113 | displayTransform.inverse().transform(simpleAppWindowFrameInLogicalDisplay); |
| 6114 | |
| 6115 | sp<FakeWindowHandle> simpleAppWindow = |
| 6116 | sp<FakeWindowHandle>::make(application, mDispatcher, "SimpleAppWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6117 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6118 | simpleAppWindow->setFrame(simpleAppWindowFrameInDisplay, displayTransform); |
| 6119 | simpleAppWindow->setOwnerInfo(gui::Pid{2}, gui::Uid{202}); |
| 6120 | addWindow(simpleAppWindow); |
| 6121 | |
| 6122 | // The following points in logical display space should be inside the untrusted window, so |
| 6123 | // the simple window could not receive events that coordinate is these point. |
| 6124 | static const std::array<vec2, 4> untrustedPoints{ |
| 6125 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 6126 | |
| 6127 | for (const auto untrustedPoint : untrustedPoints) { |
| 6128 | const vec2 p = displayTransform.inverse().transform(untrustedPoint); |
| 6129 | const PointF pointInDisplaySpace{p.x, p.y}; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6130 | mDispatcher->notifyMotion( |
| 6131 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6132 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
| 6133 | mDispatcher->notifyMotion( |
| 6134 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6135 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6136 | } |
| 6137 | untrustedWindow->assertNoEvents(); |
| 6138 | simpleAppWindow->assertNoEvents(); |
| 6139 | // The following points in logical display space should be outside the untrusted window, so |
| 6140 | // the simple window should receive events that coordinate is these point. |
| 6141 | static const std::array<vec2, 5> trustedPoints{ |
| 6142 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 6143 | for (const auto trustedPoint : trustedPoints) { |
| 6144 | const vec2 p = displayTransform.inverse().transform(trustedPoint); |
| 6145 | const PointF pointInDisplaySpace{p.x, p.y}; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6146 | mDispatcher->notifyMotion( |
| 6147 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6148 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
| 6149 | simpleAppWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6150 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6151 | mDispatcher->notifyMotion( |
| 6152 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6153 | ui::LogicalDisplayId::DEFAULT, {pointInDisplaySpace})); |
| 6154 | simpleAppWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 6155 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 6156 | } |
| 6157 | untrustedWindow->assertNoEvents(); |
| 6158 | } |
| 6159 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 6160 | // Run the precision tests for all rotations. |
| 6161 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, |
| 6162 | InputDispatcherDisplayOrientationFixture, |
| 6163 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, |
| 6164 | ui::ROTATION_270), |
| 6165 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { |
| 6166 | return ftl::enum_string(testParamInfo.param); |
| 6167 | }); |
| 6168 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 6169 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 6170 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6171 | |
| 6172 | class TransferTouchFixture : public InputDispatcherTest, |
| 6173 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 6174 | |
| 6175 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6176 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6177 | |
| 6178 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6179 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6180 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6181 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6182 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6183 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6184 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6185 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6186 | sp<FakeWindowHandle> wallpaper = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6187 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", |
| 6188 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6189 | wallpaper->setIsWallpaper(true); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6190 | // 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] | 6191 | mDispatcher->onWindowInfosChanged( |
| 6192 | {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6193 | setFocusedWindow(firstWindow); |
| 6194 | firstWindow->consumeFocusEvent(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6195 | |
| 6196 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6197 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6198 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6199 | ui::LogicalDisplayId::DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6200 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6201 | // Only the first window should get the down event |
| 6202 | firstWindow->consumeMotionDown(); |
| 6203 | secondWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6204 | wallpaper->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6205 | // Dispatcher reports pointer down outside focus for the wallpaper |
| 6206 | mFakePolicy->assertOnPointerDownEquals(wallpaper->getToken()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6207 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6208 | // Transfer touch to the second window |
| 6209 | TransferFunction f = GetParam(); |
| 6210 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 6211 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6212 | // The first window gets cancel and the second gets down |
| 6213 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6214 | secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 6215 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 6216 | wallpaper->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6217 | // There should not be any changes to the focused window when transferring touch |
| 6218 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertOnPointerDownWasNotCalled()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6219 | |
| 6220 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6221 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6222 | ui::LogicalDisplayId::DEFAULT)); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6223 | // The first window gets no events and the second gets up |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6224 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6225 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6226 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6227 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6228 | } |
| 6229 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6230 | /** |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6231 | * When 'transferTouchGesture' API is invoked, dispatcher needs to find the "best" window to take |
| 6232 | * touch from. When we have spy windows, there are several windows to choose from: either spy, or |
| 6233 | * 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] | 6234 | * natural to the user. |
| 6235 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 6236 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 6237 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 6238 | * the first window to the second. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6239 | * The main goal here is to test the behaviour of 'transferTouchGesture' API, but it's still valid |
| 6240 | * to test the other API, as well. |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6241 | */ |
| 6242 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 6243 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6244 | |
| 6245 | // Create a couple of windows + a spy window |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6246 | sp<FakeWindowHandle> spyWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", |
| 6247 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6248 | spyWindow->setTrustedOverlay(true); |
| 6249 | spyWindow->setSpy(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6250 | sp<FakeWindowHandle> firstWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "First", |
| 6251 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6252 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6253 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 6254 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6255 | |
| 6256 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6257 | mDispatcher->onWindowInfosChanged( |
| 6258 | {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6259 | |
| 6260 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6261 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6262 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6263 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6264 | // Only the first window and spy should get the down event |
| 6265 | spyWindow->consumeMotionDown(); |
| 6266 | firstWindow->consumeMotionDown(); |
| 6267 | |
| 6268 | // 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] | 6269 | // if f === 'transferTouchGesture'. |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6270 | TransferFunction f = GetParam(); |
| 6271 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 6272 | ASSERT_TRUE(success); |
| 6273 | // The first window gets cancel and the second gets down |
| 6274 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6275 | secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 6276 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6277 | |
| 6278 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6279 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6280 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6281 | // The first window gets no events and the second+spy get up |
| 6282 | firstWindow->assertNoEvents(); |
| 6283 | spyWindow->consumeMotionUp(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6284 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6285 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 6286 | } |
| 6287 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6288 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6289 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6290 | |
| 6291 | PointF touchPoint = {10, 10}; |
| 6292 | |
| 6293 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6294 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6295 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6296 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6297 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6298 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6299 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6300 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6301 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6302 | |
| 6303 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6304 | mDispatcher->onWindowInfosChanged( |
| 6305 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6306 | |
| 6307 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6308 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6309 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6310 | ui::LogicalDisplayId::DEFAULT, {touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6311 | // Only the first window should get the down event |
| 6312 | firstWindow->consumeMotionDown(); |
| 6313 | secondWindow->assertNoEvents(); |
| 6314 | |
| 6315 | // Send pointer down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6316 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6317 | ui::LogicalDisplayId::DEFAULT, |
| 6318 | {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6319 | // Only the first window should get the pointer down event |
| 6320 | firstWindow->consumeMotionPointerDown(1); |
| 6321 | secondWindow->assertNoEvents(); |
| 6322 | |
| 6323 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6324 | TransferFunction f = GetParam(); |
| 6325 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 6326 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6327 | // The first window gets cancel and the second gets down and pointer down |
| 6328 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6329 | secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 6330 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 6331 | secondWindow->consumeMotionPointerDown(1, ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6332 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6333 | |
| 6334 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6335 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6336 | ui::LogicalDisplayId::DEFAULT, |
| 6337 | {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6338 | // The first window gets nothing and the second gets pointer up |
| 6339 | firstWindow->assertNoEvents(); |
Prabir Pradhan | 33cfc6d | 2024-06-11 20:17:44 +0000 | [diff] [blame] | 6340 | secondWindow->consumeMotionPointerUp(/*pointerIdx=*/1, |
| 6341 | AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT), |
| 6342 | WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE), |
| 6343 | WithPointerCount(2))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6344 | |
| 6345 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6346 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6347 | ui::LogicalDisplayId::DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6348 | // The first window gets nothing and the second gets up |
| 6349 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6350 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6351 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6352 | } |
| 6353 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6354 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 6355 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6356 | |
| 6357 | // Create a couple of windows |
| 6358 | sp<FakeWindowHandle> firstWindow = |
| 6359 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6360 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6361 | firstWindow->setDupTouchToWallpaper(true); |
| 6362 | sp<FakeWindowHandle> secondWindow = |
| 6363 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6364 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6365 | secondWindow->setDupTouchToWallpaper(true); |
| 6366 | |
| 6367 | sp<FakeWindowHandle> wallpaper1 = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6368 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", |
| 6369 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6370 | wallpaper1->setIsWallpaper(true); |
| 6371 | |
| 6372 | sp<FakeWindowHandle> wallpaper2 = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6373 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", |
| 6374 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6375 | wallpaper2->setIsWallpaper(true); |
| 6376 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6377 | mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(), |
| 6378 | *secondWindow->getInfo(), *wallpaper2->getInfo()}, |
| 6379 | {}, |
| 6380 | 0, |
| 6381 | 0}); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6382 | |
| 6383 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6384 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6385 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6386 | ui::LogicalDisplayId::DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6387 | |
| 6388 | // Only the first window should get the down event |
| 6389 | firstWindow->consumeMotionDown(); |
| 6390 | secondWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6391 | wallpaper1->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6392 | wallpaper2->assertNoEvents(); |
| 6393 | |
| 6394 | // Transfer touch focus to the second window |
| 6395 | TransferFunction f = GetParam(); |
| 6396 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 6397 | ASSERT_TRUE(success); |
| 6398 | |
| 6399 | // The first window gets cancel and the second gets down |
| 6400 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6401 | secondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 6402 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 6403 | wallpaper1->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, EXPECTED_WALLPAPER_FLAGS); |
| 6404 | wallpaper2->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 6405 | EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6406 | |
| 6407 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6408 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6409 | ui::LogicalDisplayId::DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6410 | // The first window gets no events and the second gets up |
| 6411 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6412 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6413 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6414 | wallpaper1->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6415 | wallpaper2->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 7235222 | 2024-04-12 18:55:57 +0800 | [diff] [blame] | 6416 | EXPECTED_WALLPAPER_FLAGS | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 6417 | } |
| 6418 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6419 | // 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] | 6420 | // 'transferTouchGesture' and 'transferTouchOnDisplay' are equivalent in behaviour. They only differ |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6421 | // for the case where there are multiple pointers split across several windows. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6422 | INSTANTIATE_TEST_SUITE_P( |
| 6423 | InputDispatcherTransferFunctionTests, TransferTouchFixture, |
| 6424 | ::testing::Values( |
| 6425 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, sp<IBinder> /*ignored*/, |
| 6426 | sp<IBinder> destChannelToken) { |
| 6427 | return dispatcher->transferTouchOnDisplay(destChannelToken, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6428 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6429 | }, |
| 6430 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, sp<IBinder> from, |
| 6431 | sp<IBinder> to) { |
| 6432 | return dispatcher->transferTouchGesture(from, to, |
| 6433 | /*isDragAndDrop=*/false); |
| 6434 | })); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6435 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6436 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6437 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6438 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6439 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6440 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6441 | ui::LogicalDisplayId::DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6442 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6443 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6444 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6445 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6446 | ui::LogicalDisplayId::DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6447 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6448 | |
| 6449 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6450 | mDispatcher->onWindowInfosChanged( |
| 6451 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6452 | |
| 6453 | PointF pointInFirst = {300, 200}; |
| 6454 | PointF pointInSecond = {300, 600}; |
| 6455 | |
| 6456 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6457 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6458 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6459 | ui::LogicalDisplayId::DEFAULT, {pointInFirst})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6460 | // Only the first window should get the down event |
| 6461 | firstWindow->consumeMotionDown(); |
| 6462 | secondWindow->assertNoEvents(); |
| 6463 | |
| 6464 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6465 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6466 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6467 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6468 | // The first window gets a move and the second a down |
| 6469 | firstWindow->consumeMotionMove(); |
| 6470 | secondWindow->consumeMotionDown(); |
| 6471 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6472 | // Transfer touch to the second window |
| 6473 | mDispatcher->transferTouchGesture(firstWindow->getToken(), secondWindow->getToken()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6474 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 6475 | firstWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6476 | secondWindow->consumeMotionPointerDown(1, ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6477 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6478 | |
| 6479 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6480 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6481 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6482 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6483 | // The first window gets nothing and the second gets pointer up |
| 6484 | firstWindow->assertNoEvents(); |
Prabir Pradhan | 33cfc6d | 2024-06-11 20:17:44 +0000 | [diff] [blame] | 6485 | secondWindow->consumeMotionPointerUp(/*pointerIdx=*/1, |
| 6486 | AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT), |
| 6487 | WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE), |
| 6488 | WithPointerCount(2))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6489 | |
| 6490 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6491 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6492 | ui::LogicalDisplayId::DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6493 | // The first window gets nothing and the second gets up |
| 6494 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6495 | secondWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6496 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 6497 | } |
| 6498 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6499 | // Same as TransferTouch_TwoPointersSplitTouch, but using 'transferTouchOnDisplay' api. |
| 6500 | // Unlike 'transferTouchGesture', calling 'transferTouchOnDisplay' when there are two windows |
| 6501 | // receiving touch is not supported, so the touch should continue on those windows and the |
| 6502 | // transferred-to window should get nothing. |
| 6503 | TEST_F(InputDispatcherTest, TransferTouchOnDisplay_TwoPointersSplitTouch) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6504 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6505 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6506 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6507 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6508 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6509 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6510 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6511 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6512 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6513 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6514 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6515 | |
| 6516 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6517 | mDispatcher->onWindowInfosChanged( |
| 6518 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6519 | |
| 6520 | PointF pointInFirst = {300, 200}; |
| 6521 | PointF pointInSecond = {300, 600}; |
| 6522 | |
| 6523 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6524 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6525 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6526 | ui::LogicalDisplayId::DEFAULT, {pointInFirst})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6527 | // Only the first window should get the down event |
| 6528 | firstWindow->consumeMotionDown(); |
| 6529 | secondWindow->assertNoEvents(); |
| 6530 | |
| 6531 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6532 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6533 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6534 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6535 | // The first window gets a move and the second a down |
| 6536 | firstWindow->consumeMotionMove(); |
| 6537 | secondWindow->consumeMotionDown(); |
| 6538 | |
| 6539 | // Transfer touch focus to the second window |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6540 | const bool transferred = mDispatcher->transferTouchOnDisplay(secondWindow->getToken(), |
| 6541 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6542 | // The 'transferTouchOnDisplay' call should not succeed, because there are 2 touched windows |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6543 | ASSERT_FALSE(transferred); |
| 6544 | firstWindow->assertNoEvents(); |
| 6545 | secondWindow->assertNoEvents(); |
| 6546 | |
| 6547 | // The rest of the dispatch should proceed as normal |
| 6548 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6549 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6550 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6551 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6552 | // The first window gets MOVE and the second gets pointer up |
| 6553 | firstWindow->consumeMotionMove(); |
| 6554 | secondWindow->consumeMotionUp(); |
| 6555 | |
| 6556 | // Send up event to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6557 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6558 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 6559 | // The first window gets nothing and the second gets up |
| 6560 | firstWindow->consumeMotionUp(); |
| 6561 | secondWindow->assertNoEvents(); |
| 6562 | } |
| 6563 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6564 | // 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] | 6565 | // 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] | 6566 | // the windows info of second display before default display. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6567 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6568 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6569 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6570 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", |
| 6571 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6572 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6573 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6574 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", |
| 6575 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6576 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6577 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6578 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 6579 | firstWindowInPrimary->clone(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6580 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6581 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6582 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6583 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6584 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6585 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6586 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6587 | |
| 6588 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6589 | mDispatcher->onWindowInfosChanged( |
| 6590 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 6591 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 6592 | *secondWindowInPrimary->getInfo()}, |
| 6593 | {}, |
| 6594 | 0, |
| 6595 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6596 | |
| 6597 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6598 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6599 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6600 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6601 | |
| 6602 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6603 | firstWindowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6604 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6605 | // Transfer touch |
| 6606 | ASSERT_TRUE(mDispatcher->transferTouchGesture(firstWindowInPrimary->getToken(), |
| 6607 | secondWindowInPrimary->getToken())); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6608 | // The first window gets cancel. |
| 6609 | firstWindowInPrimary->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6610 | secondWindowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6611 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6612 | |
| 6613 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6614 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6615 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6616 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6617 | firstWindowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6618 | secondWindowInPrimary->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6619 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6620 | |
| 6621 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6622 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6623 | {150, 50})) |
| 6624 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6625 | firstWindowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6626 | secondWindowInPrimary->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, |
| 6627 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6628 | } |
| 6629 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6630 | // Same as TransferTouch_CloneSurface, but this touch on the secondary display and use |
| 6631 | // 'transferTouchOnDisplay' api. |
| 6632 | TEST_F(InputDispatcherTest, TransferTouchOnDisplay_CloneSurface) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6633 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6634 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6635 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", |
| 6636 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6637 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6638 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6639 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", |
| 6640 | ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6641 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6642 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6643 | sp<FakeWindowHandle> mirrorWindowInPrimary = |
| 6644 | firstWindowInPrimary->clone(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6645 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6646 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6647 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6648 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6649 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 6650 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6651 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6652 | |
| 6653 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6654 | mDispatcher->onWindowInfosChanged( |
| 6655 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 6656 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 6657 | *secondWindowInPrimary->getInfo()}, |
| 6658 | {}, |
| 6659 | 0, |
| 6660 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6661 | |
| 6662 | // Touch on second display. |
| 6663 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6664 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 6665 | {50, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6666 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6667 | |
| 6668 | // Window should receive motion event. |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6669 | firstWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6670 | |
| 6671 | // Transfer touch focus |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 6672 | ASSERT_TRUE(mDispatcher->transferTouchOnDisplay(secondWindowInSecondary->getToken(), |
| 6673 | SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6674 | |
| 6675 | // The first window gets cancel. |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6676 | firstWindowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6677 | secondWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, |
| 6678 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6679 | |
| 6680 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6681 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6682 | SECOND_DISPLAY_ID, {150, 50})) |
| 6683 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6684 | firstWindowInSecondary->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6685 | secondWindowInSecondary->consumeMotionMove(SECOND_DISPLAY_ID, |
| 6686 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6687 | |
| 6688 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6689 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6690 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 6691 | firstWindowInSecondary->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 6692 | secondWindowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 6693 | } |
| 6694 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6695 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6696 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6697 | sp<FakeWindowHandle> window = |
| 6698 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6699 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6700 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6701 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6702 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6703 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6704 | |
| 6705 | window->consumeFocusEvent(true); |
| 6706 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6707 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6708 | |
| 6709 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6710 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6711 | |
| 6712 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 6713 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6714 | mFakePolicy->assertUserActivityPoked(); |
| 6715 | } |
| 6716 | |
| 6717 | TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) { |
| 6718 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6719 | sp<FakeWindowHandle> window = |
| 6720 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6721 | ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6722 | |
| 6723 | window->setDisableUserActivity(true); |
| 6724 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6725 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6726 | setFocusedWindow(window); |
| 6727 | |
| 6728 | window->consumeFocusEvent(true); |
| 6729 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6730 | mDispatcher->notifyKey( |
| 6731 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6732 | |
| 6733 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6734 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6735 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6736 | // Should have not poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 6737 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6738 | mFakePolicy->assertUserActivityNotPoked(); |
| 6739 | } |
| 6740 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6741 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceivePolicyConsumedKey) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6742 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6743 | sp<FakeWindowHandle> window = |
| 6744 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6745 | ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6746 | |
| 6747 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6748 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6749 | setFocusedWindow(window); |
| 6750 | |
| 6751 | window->consumeFocusEvent(true); |
| 6752 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6753 | mFakePolicy->setConsumeKeyBeforeDispatching(true); |
| 6754 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6755 | mDispatcher->notifyKey( |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6756 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6757 | mDispatcher->waitForIdle(); |
| 6758 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6759 | // Key is not passed down |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6760 | window->assertNoEvents(); |
| 6761 | |
| 6762 | // Should have poked user activity |
| 6763 | mFakePolicy->assertUserActivityPoked(); |
| 6764 | } |
| 6765 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6766 | TEST_F(InputDispatcherTest, FocusedWindow_PolicyConsumedKeyIgnoresDisableUserActivity) { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6767 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6768 | sp<FakeWindowHandle> window = |
| 6769 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6770 | ui::LogicalDisplayId::DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6771 | |
| 6772 | window->setDisableUserActivity(true); |
| 6773 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6774 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6775 | setFocusedWindow(window); |
| 6776 | |
| 6777 | window->consumeFocusEvent(true); |
| 6778 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6779 | mFakePolicy->setConsumeKeyBeforeDispatching(true); |
| 6780 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6781 | mDispatcher->notifyKey( |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6782 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 6783 | mDispatcher->waitForIdle(); |
| 6784 | |
| 6785 | // System key is not passed down |
| 6786 | window->assertNoEvents(); |
| 6787 | |
| 6788 | // Should have poked user activity |
| 6789 | mFakePolicy->assertUserActivityPoked(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6790 | } |
| 6791 | |
Josep del Rio | c8fdedb | 2024-05-21 13:32:43 +0000 | [diff] [blame] | 6792 | class DisableUserActivityInputDispatcherTest : public InputDispatcherTest, |
| 6793 | public ::testing::WithParamInterface<bool> {}; |
| 6794 | |
| 6795 | TEST_P(DisableUserActivityInputDispatcherTest, NotPassedToUserUserActivity) { |
| 6796 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6797 | sp<FakeWindowHandle> window = |
| 6798 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6799 | ui::LogicalDisplayId::DEFAULT); |
| 6800 | |
| 6801 | window->setDisableUserActivity(GetParam()); |
| 6802 | |
| 6803 | window->setFocusable(true); |
| 6804 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 6805 | setFocusedWindow(window); |
| 6806 | |
| 6807 | window->consumeFocusEvent(true); |
| 6808 | |
| 6809 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 6810 | .keyCode(AKEYCODE_A) |
| 6811 | .policyFlags(0) |
| 6812 | .build()); |
| 6813 | mDispatcher->waitForIdle(); |
| 6814 | |
| 6815 | // Key is not passed down |
| 6816 | window->assertNoEvents(); |
| 6817 | |
| 6818 | // Should not have poked user activity |
| 6819 | mFakePolicy->assertUserActivityNotPoked(); |
| 6820 | } |
| 6821 | |
| 6822 | INSTANTIATE_TEST_CASE_P(DisableUserActivity, DisableUserActivityInputDispatcherTest, |
| 6823 | ::testing::Bool()); |
| 6824 | |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6825 | TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) { |
| 6826 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6827 | sp<FakeWindowHandle> window = |
| 6828 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6829 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6830 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6831 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6832 | |
| 6833 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6834 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6835 | ui::LogicalDisplayId::DEFAULT, {100, 100})) |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6836 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6837 | |
| 6838 | window->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6839 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6840 | |
| 6841 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 6842 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 6843 | mFakePolicy->assertUserActivityPoked(); |
| 6844 | } |
| 6845 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6846 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6847 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6848 | sp<FakeWindowHandle> window = |
| 6849 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6850 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6851 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6852 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6853 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6854 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6855 | mDispatcher->waitForIdle(); |
| 6856 | |
| 6857 | window->assertNoEvents(); |
| 6858 | } |
| 6859 | |
| 6860 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 6861 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6862 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6863 | sp<FakeWindowHandle> window = |
| 6864 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 6865 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6866 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6867 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6868 | |
| 6869 | // Send key |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6870 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6871 | // Send motion |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6872 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6873 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6874 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6875 | |
| 6876 | // Window should receive only the motion event |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6877 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6878 | window->assertNoEvents(); // Key event or focus event will not be received |
| 6879 | } |
| 6880 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6881 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 6882 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6883 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6884 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6885 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6886 | ui::LogicalDisplayId::DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6887 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6888 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6889 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6890 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6891 | ui::LogicalDisplayId::DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6892 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6893 | |
| 6894 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6895 | mDispatcher->onWindowInfosChanged( |
| 6896 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6897 | |
| 6898 | PointF pointInFirst = {300, 200}; |
| 6899 | PointF pointInSecond = {300, 600}; |
| 6900 | |
| 6901 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6902 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6903 | AINPUT_SOURCE_TOUCHSCREEN, |
| 6904 | ui::LogicalDisplayId::DEFAULT, {pointInFirst})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6905 | // Only the first window should get the down event |
| 6906 | firstWindow->consumeMotionDown(); |
| 6907 | secondWindow->assertNoEvents(); |
| 6908 | |
| 6909 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6910 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6911 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6912 | {pointInFirst, pointInSecond})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6913 | // The first window gets a move and the second a down |
| 6914 | firstWindow->consumeMotionMove(); |
| 6915 | secondWindow->consumeMotionDown(); |
| 6916 | |
| 6917 | // Send pointer cancel to the second window |
| 6918 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6919 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6920 | ui::LogicalDisplayId::DEFAULT, {pointInFirst, pointInSecond}); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6921 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6922 | mDispatcher->notifyMotion(pointerUpMotionArgs); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6923 | // The first window gets move and the second gets cancel. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6924 | firstWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 6925 | secondWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6926 | |
| 6927 | // Send up event. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6928 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6929 | ui::LogicalDisplayId::DEFAULT)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6930 | // The first window gets up and the second gets nothing. |
| 6931 | firstWindow->consumeMotionUp(); |
| 6932 | secondWindow->assertNoEvents(); |
| 6933 | } |
| 6934 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6935 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 6936 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6937 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6938 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 6939 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6940 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6941 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 6942 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 6943 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 6944 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6945 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6946 | window->assertNoEvents(); |
| 6947 | mDispatcher->waitForIdle(); |
| 6948 | } |
| 6949 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6950 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 6951 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6952 | /** |
| 6953 | * Two entities that receive touch: A window, and a global monitor. |
| 6954 | * The touch goes to the window, and then the window disappears. |
| 6955 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 6956 | * for the monitor, as well. |
| 6957 | * 1. foregroundWindow |
| 6958 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 6959 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6960 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6961 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6962 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 6963 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6964 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6965 | FakeMonitorReceiver monitor = |
| 6966 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6967 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6968 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6969 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6970 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 6971 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6972 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6973 | |
| 6974 | // Both the foreground window and the global monitor should receive the touch down |
| 6975 | window->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6976 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6977 | |
| 6978 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6979 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6980 | ui::LogicalDisplayId::DEFAULT, {110, 200})) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6981 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6982 | |
| 6983 | window->consumeMotionMove(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6984 | monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6985 | |
| 6986 | // Now the foreground window goes away |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6987 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6988 | window->consumeMotionCancel(); |
| 6989 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 6990 | |
| 6991 | // If more events come in, there will be no more foreground window to send them to. This will |
| 6992 | // cause a cancel for the monitor, as well. |
| 6993 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6994 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6995 | ui::LogicalDisplayId::DEFAULT, {120, 200})) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6996 | << "Injection should fail because the window was removed"; |
| 6997 | window->assertNoEvents(); |
| 6998 | // Global monitor now gets the cancel |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 6999 | monitor.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 7000 | } |
| 7001 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7002 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7003 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7004 | sp<FakeWindowHandle> window = |
| 7005 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7006 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7007 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 7008 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7009 | FakeMonitorReceiver monitor = |
| 7010 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 7011 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7012 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7013 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7014 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7015 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7016 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 7017 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 7018 | } |
| 7019 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7020 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7021 | FakeMonitorReceiver monitor = |
| 7022 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 7023 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7024 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7025 | sp<FakeWindowHandle> window = |
| 7026 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7027 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7028 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 7029 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7030 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7031 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7032 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7033 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7034 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 7035 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 7036 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7037 | // Pilfer pointers from the monitor. |
| 7038 | // This should not do anything and the window should continue to receive events. |
| 7039 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 7040 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7041 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7042 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7043 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7044 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7045 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7046 | monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
| 7047 | window->consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 7048 | } |
| 7049 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7050 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 7051 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7052 | sp<FakeWindowHandle> window = |
| 7053 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7054 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7055 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 7056 | window->setWindowOffset(20, 40); |
| 7057 | window->setWindowTransform(0, 1, -1, 0); |
| 7058 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7059 | FakeMonitorReceiver monitor = |
| 7060 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 7061 | |
| 7062 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7063 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7064 | ui::LogicalDisplayId::DEFAULT)) |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 7065 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7066 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7067 | std::unique_ptr<MotionEvent> event = monitor.consumeMotion(); |
| 7068 | ASSERT_NE(nullptr, event); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 7069 | // Even though window has transform, gesture monitor must not. |
| 7070 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 7071 | } |
| 7072 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7073 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 7074 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7075 | FakeMonitorReceiver monitor = |
| 7076 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 7077 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7078 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7079 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7080 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 7081 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 7082 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 7083 | } |
| 7084 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7085 | /** |
| 7086 | * Two displays |
| 7087 | * The first monitor has a foreground window, a monitor |
| 7088 | * The second window has only one monitor. |
| 7089 | * We first inject a Down event into the first display, this injection should succeed and both |
| 7090 | * the foreground window and monitor should receive a down event, then inject a Down event into |
| 7091 | * the second display as well, this injection should fail, at this point, the first display |
| 7092 | * window and monitor should not receive a cancel or any other event. |
| 7093 | * Continue to inject Move and UP events to the first display, the events should be received |
| 7094 | * normally by the foreground window and monitor. |
| 7095 | */ |
| 7096 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCanceledWhenAnotherEmptyDisplayReceiveEvents) { |
| 7097 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7098 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 7099 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7100 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7101 | FakeMonitorReceiver monitor = |
| 7102 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7103 | FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 7104 | |
| 7105 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 7106 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7107 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7108 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7109 | << "The down event injected into the first display should succeed"; |
| 7110 | |
| 7111 | window->consumeMotionDown(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7112 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7113 | |
| 7114 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 7115 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 7116 | {100, 200})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7117 | << "The down event injected into the second display should fail since there's no " |
| 7118 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7119 | |
| 7120 | // Continue to inject event to first display. |
| 7121 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7122 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7123 | ui::LogicalDisplayId::DEFAULT, {110, 220})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7124 | << "The move event injected into the first display should succeed"; |
| 7125 | |
| 7126 | window->consumeMotionMove(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7127 | monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7128 | |
| 7129 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7130 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7131 | {110, 220})) |
| 7132 | << "The up event injected into the first display should succeed"; |
| 7133 | |
| 7134 | window->consumeMotionUp(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7135 | monitor.consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7136 | |
| 7137 | window->assertNoEvents(); |
| 7138 | monitor.assertNoEvents(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7139 | secondMonitor.assertNoEvents(); |
| 7140 | } |
| 7141 | |
| 7142 | /** |
| 7143 | * Two displays |
| 7144 | * There is a monitor and foreground window on each display. |
| 7145 | * First, we inject down events into each of the two displays, at this point, the foreground windows |
| 7146 | * and monitors on both displays should receive down events. |
| 7147 | * At this point, the foreground window of the second display goes away, the gone window should |
| 7148 | * receive the cancel event, and the other windows and monitors should not receive any events. |
| 7149 | * Inject a move event into the second display. At this point, the injection should fail because |
| 7150 | * the second display no longer has a foreground window. At this point, the monitor on the second |
| 7151 | * display should receive a cancel event, and any windows or monitors on the first display should |
| 7152 | * not receive any events, and any subsequent injection of events into the second display should |
| 7153 | * also fail. |
| 7154 | * Continue to inject events into the first display, and the events should all be injected |
| 7155 | * successfully and received normally. |
| 7156 | */ |
| 7157 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCancelWhenAnotherDisplayMonitorTouchCanceled) { |
| 7158 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7159 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 7160 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7161 | sp<FakeWindowHandle> secondWindow = |
| 7162 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondForeground", |
| 7163 | SECOND_DISPLAY_ID); |
| 7164 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7165 | FakeMonitorReceiver monitor = |
| 7166 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7167 | FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 7168 | |
| 7169 | // There is a foreground window on both displays. |
| 7170 | mDispatcher->onWindowInfosChanged({{*window->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
| 7171 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7172 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7173 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7174 | << "The down event injected into the first display should succeed"; |
| 7175 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7176 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 7177 | monitor.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7178 | |
| 7179 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7180 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 7181 | {100, 200})) |
| 7182 | << "The down event injected into the second display should succeed"; |
| 7183 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7184 | secondWindow->consumeMotionDown(SECOND_DISPLAY_ID); |
| 7185 | secondMonitor.consumeMotionDown(SECOND_DISPLAY_ID); |
| 7186 | |
| 7187 | // Now second window is gone away. |
| 7188 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 7189 | |
| 7190 | // The gone window should receive a cancel, and the monitor on the second display should not |
| 7191 | // receive any events. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7192 | secondWindow->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 7193 | secondMonitor.assertNoEvents(); |
| 7194 | |
| 7195 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 7196 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 7197 | SECOND_DISPLAY_ID, {110, 220})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7198 | << "The move event injected into the second display should fail because there's no " |
| 7199 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7200 | // Now the monitor on the second display should receive a cancel event. |
| 7201 | secondMonitor.consumeMotionCancel(SECOND_DISPLAY_ID); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7202 | |
| 7203 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7204 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7205 | ui::LogicalDisplayId::DEFAULT, {110, 200})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7206 | << "The move event injected into the first display should succeed"; |
| 7207 | |
| 7208 | window->consumeMotionMove(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7209 | monitor.consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7210 | |
| 7211 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7212 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 7213 | {110, 220})) |
| 7214 | << "The up event injected into the second display should fail because there's no " |
| 7215 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7216 | |
| 7217 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7218 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7219 | {110, 220})) |
| 7220 | << "The up event injected into the first display should succeed"; |
| 7221 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7222 | window->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
| 7223 | monitor.consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7224 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7225 | window->assertNoEvents(); |
| 7226 | monitor.assertNoEvents(); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7227 | secondWindow->assertNoEvents(); |
| 7228 | secondMonitor.assertNoEvents(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7229 | } |
| 7230 | |
| 7231 | /** |
| 7232 | * One display with transform |
| 7233 | * There is a foreground window and a monitor on the display |
| 7234 | * Inject down event and move event sequentially, the foreground window and monitor can receive down |
| 7235 | * event and move event, then let the foreground window go away, the foreground window receives |
| 7236 | * cancel event, inject move event again, the monitor receives cancel event, all the events received |
| 7237 | * by the monitor should be with the same transform as the display |
| 7238 | */ |
| 7239 | TEST_F(InputDispatcherMonitorTest, MonitorTouchCancelEventWithDisplayTransform) { |
| 7240 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7241 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", |
| 7242 | ui::LogicalDisplayId::DEFAULT); |
| 7243 | FakeMonitorReceiver monitor = |
| 7244 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7245 | |
| 7246 | ui::Transform transform; |
| 7247 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 7248 | |
| 7249 | gui::DisplayInfo displayInfo; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7250 | displayInfo.displayId = ui::LogicalDisplayId::DEFAULT; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7251 | displayInfo.transform = transform; |
| 7252 | |
| 7253 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
| 7254 | |
| 7255 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7256 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 7257 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7258 | << "The down event injected should succeed"; |
| 7259 | |
| 7260 | window->consumeMotionDown(); |
| 7261 | std::unique_ptr<MotionEvent> downMotionEvent = monitor.consumeMotion(); |
| 7262 | EXPECT_EQ(transform, downMotionEvent->getTransform()); |
| 7263 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, downMotionEvent->getAction()); |
| 7264 | |
| 7265 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7266 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7267 | ui::LogicalDisplayId::DEFAULT, {110, 220})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7268 | << "The move event injected should succeed"; |
| 7269 | |
| 7270 | window->consumeMotionMove(); |
| 7271 | std::unique_ptr<MotionEvent> moveMotionEvent = monitor.consumeMotion(); |
| 7272 | EXPECT_EQ(transform, moveMotionEvent->getTransform()); |
| 7273 | EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, moveMotionEvent->getAction()); |
| 7274 | |
| 7275 | // Let foreground window gone |
| 7276 | mDispatcher->onWindowInfosChanged({{}, {displayInfo}, 0, 0}); |
| 7277 | |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7278 | // Foreground window should receive a cancel event, but not the monitor. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7279 | window->consumeMotionCancel(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7280 | |
| 7281 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 7282 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7283 | ui::LogicalDisplayId::DEFAULT, {110, 220})) |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7284 | << "The move event injected should failed"; |
| 7285 | // Now foreground should not receive any events, but monitor should receive a cancel event |
| 7286 | // with transform that same as display's display. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7287 | std::unique_ptr<MotionEvent> cancelMotionEvent = monitor.consumeMotion(); |
| 7288 | EXPECT_EQ(transform, cancelMotionEvent->getTransform()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7289 | EXPECT_EQ(ui::LogicalDisplayId::DEFAULT, cancelMotionEvent->getDisplayId()); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7290 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, cancelMotionEvent->getAction()); |
| 7291 | |
| 7292 | // Other event inject to this display should fail. |
| 7293 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 7294 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7295 | ui::LogicalDisplayId::DEFAULT, {110, 220})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 7296 | << "The up event injected should fail because the touched window was removed"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 7297 | window->assertNoEvents(); |
| 7298 | monitor.assertNoEvents(); |
| 7299 | } |
| 7300 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7301 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7302 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7303 | sp<FakeWindowHandle> window = |
| 7304 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 7305 | ui::LogicalDisplayId::DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7306 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7307 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7308 | |
| 7309 | NotifyMotionArgs motionArgs = |
| 7310 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7311 | ui::LogicalDisplayId::DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7312 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7313 | mDispatcher->notifyMotion(motionArgs); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7314 | // Window should receive motion down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7315 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7316 | |
| 7317 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7318 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7319 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 7320 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 7321 | motionArgs.pointerCoords[0].getX() - 10); |
| 7322 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7323 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7324 | window->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 7325 | } |
| 7326 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7327 | /** |
| 7328 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 7329 | * the device default right away. In the test scenario, we check both the default value, |
| 7330 | * and the action of enabling / disabling. |
| 7331 | */ |
| 7332 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7333 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7334 | sp<FakeWindowHandle> window = |
| 7335 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 7336 | ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7337 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7338 | |
| 7339 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7340 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7341 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7342 | |
| 7343 | SCOPED_TRACE("Check default value of touch mode"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7344 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7345 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7346 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7347 | |
| 7348 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7349 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7350 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7351 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7352 | |
| 7353 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7354 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7355 | /*hasPermission=*/true, ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7356 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7357 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7358 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7359 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7360 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7361 | |
| 7362 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7363 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7364 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7365 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7366 | |
| 7367 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 7368 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7369 | /*hasPermission=*/true, ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 7370 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7371 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7372 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7373 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7374 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7375 | |
| 7376 | window->assertNoEvents(); |
| 7377 | } |
| 7378 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7379 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7380 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7381 | sp<FakeWindowHandle> window = |
| 7382 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 7383 | ui::LogicalDisplayId::DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7384 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7385 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7386 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7387 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7388 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7389 | setFocusedWindow(window); |
| 7390 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7391 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7392 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7393 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 7394 | mDispatcher->notifyKey(keyArgs); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7395 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7396 | std::unique_ptr<KeyEvent> event = window->consumeKey(); |
| 7397 | ASSERT_NE(event, nullptr); |
| 7398 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7399 | ASSERT_NE(verified, nullptr); |
| 7400 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 7401 | |
| 7402 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 7403 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 7404 | ASSERT_EQ(keyArgs.source, verified->source); |
| 7405 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 7406 | |
| 7407 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 7408 | |
| 7409 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7410 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 7411 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 7412 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 7413 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 7414 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 7415 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 7416 | } |
| 7417 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7418 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7419 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7420 | sp<FakeWindowHandle> window = |
| 7421 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 7422 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7423 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7424 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7425 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 7426 | ui::Transform transform; |
| 7427 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 7428 | |
| 7429 | gui::DisplayInfo displayInfo; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7430 | displayInfo.displayId = ui::LogicalDisplayId::DEFAULT; |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 7431 | displayInfo.transform = transform; |
| 7432 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 7433 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7434 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7435 | const NotifyMotionArgs motionArgs = |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7436 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7437 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7438 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7439 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7440 | std::unique_ptr<MotionEvent> event = window->consumeMotionEvent(); |
| 7441 | ASSERT_NE(nullptr, event); |
| 7442 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7443 | ASSERT_NE(verified, nullptr); |
| 7444 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 7445 | |
| 7446 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 7447 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 7448 | EXPECT_EQ(motionArgs.source, verified->source); |
| 7449 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 7450 | |
| 7451 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 7452 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 7453 | const vec2 rawXY = |
| 7454 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 7455 | motionArgs.pointerCoords[0].getXYValue()); |
| 7456 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 7457 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7458 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7459 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 7460 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 7461 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 7462 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 7463 | } |
| 7464 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 7465 | /** |
| 7466 | * Ensure that separate calls to sign the same data are generating the same key. |
| 7467 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 7468 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 7469 | * tests. |
| 7470 | */ |
| 7471 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 7472 | KeyEvent event = getTestKeyEvent(); |
| 7473 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 7474 | |
| 7475 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 7476 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 7477 | ASSERT_EQ(hmac1, hmac2); |
| 7478 | } |
| 7479 | |
| 7480 | /** |
| 7481 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 7482 | */ |
| 7483 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 7484 | KeyEvent event = getTestKeyEvent(); |
| 7485 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 7486 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 7487 | |
| 7488 | verifiedEvent.deviceId += 1; |
| 7489 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7490 | |
| 7491 | verifiedEvent.source += 1; |
| 7492 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7493 | |
| 7494 | verifiedEvent.eventTimeNanos += 1; |
| 7495 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7496 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 7497 | verifiedEvent.displayId = ui::LogicalDisplayId{verifiedEvent.displayId.val() + 1}; |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 7498 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7499 | |
| 7500 | verifiedEvent.action += 1; |
| 7501 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7502 | |
| 7503 | verifiedEvent.downTimeNanos += 1; |
| 7504 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7505 | |
| 7506 | verifiedEvent.flags += 1; |
| 7507 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7508 | |
| 7509 | verifiedEvent.keyCode += 1; |
| 7510 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7511 | |
| 7512 | verifiedEvent.scanCode += 1; |
| 7513 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7514 | |
| 7515 | verifiedEvent.metaState += 1; |
| 7516 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7517 | |
| 7518 | verifiedEvent.repeatCount += 1; |
| 7519 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 7520 | } |
| 7521 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7522 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 7523 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7524 | sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 7525 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7526 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7527 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 7528 | ui::LogicalDisplayId::DEFAULT); |
| 7529 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7530 | |
| 7531 | // Top window is also focusable but is not granted focus. |
| 7532 | windowTop->setFocusable(true); |
| 7533 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7534 | mDispatcher->onWindowInfosChanged( |
| 7535 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7536 | setFocusedWindow(windowSecond); |
| 7537 | |
| 7538 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7539 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7540 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7541 | |
| 7542 | // Focused window should receive event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7543 | windowSecond->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7544 | windowTop->assertNoEvents(); |
| 7545 | } |
| 7546 | |
| 7547 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 7548 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7549 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
| 7550 | ui::LogicalDisplayId::DEFAULT); |
| 7551 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7552 | |
| 7553 | window->setFocusable(true); |
| 7554 | // Release channel for window is no longer valid. |
| 7555 | window->releaseChannel(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7556 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7557 | setFocusedWindow(window); |
| 7558 | |
| 7559 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7560 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7561 | |
| 7562 | // window channel is invalid, so it should not receive any input event. |
| 7563 | window->assertNoEvents(); |
| 7564 | } |
| 7565 | |
| 7566 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 7567 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7568 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
| 7569 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7570 | window->setFocusable(false); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7571 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7572 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7573 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7574 | setFocusedWindow(window); |
| 7575 | |
| 7576 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7577 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7578 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 7579 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7580 | window->assertNoEvents(); |
| 7581 | } |
| 7582 | |
| 7583 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 7584 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7585 | sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 7586 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7587 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7588 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 7589 | ui::LogicalDisplayId::DEFAULT); |
| 7590 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7591 | |
| 7592 | windowTop->setFocusable(true); |
| 7593 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7594 | mDispatcher->onWindowInfosChanged( |
| 7595 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7596 | setFocusedWindow(windowTop); |
| 7597 | windowTop->consumeFocusEvent(true); |
| 7598 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 7599 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7600 | mDispatcher->onWindowInfosChanged( |
| 7601 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7602 | windowSecond->consumeFocusEvent(true); |
| 7603 | windowTop->consumeFocusEvent(false); |
| 7604 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7605 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7606 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7607 | |
| 7608 | // Focused window should receive event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7609 | windowSecond->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7610 | } |
| 7611 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 7612 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7613 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7614 | sp<FakeWindowHandle> windowTop = sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 7615 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7616 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7617 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 7618 | ui::LogicalDisplayId::DEFAULT); |
| 7619 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7620 | |
| 7621 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 7622 | windowSecond->setFocusable(false); |
| 7623 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7624 | mDispatcher->onWindowInfosChanged( |
| 7625 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 7626 | setFocusedWindow(windowTop); |
| 7627 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7628 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7629 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 7630 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7631 | |
| 7632 | // Event should be dropped. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7633 | windowTop->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7634 | windowSecond->assertNoEvents(); |
| 7635 | } |
| 7636 | |
| 7637 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 7638 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7639 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
| 7640 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7641 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7642 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7643 | ui::LogicalDisplayId::DEFAULT); |
| 7644 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7645 | |
| 7646 | window->setFocusable(true); |
| 7647 | previousFocusedWindow->setFocusable(true); |
| 7648 | window->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7649 | mDispatcher->onWindowInfosChanged( |
| 7650 | {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7651 | setFocusedWindow(previousFocusedWindow); |
| 7652 | previousFocusedWindow->consumeFocusEvent(true); |
| 7653 | |
| 7654 | // Requesting focus on invisible window takes focus from currently focused window. |
| 7655 | setFocusedWindow(window); |
| 7656 | previousFocusedWindow->consumeFocusEvent(false); |
| 7657 | |
| 7658 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7659 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7660 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7661 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7662 | |
| 7663 | // Window does not get focus event or key down. |
| 7664 | window->assertNoEvents(); |
| 7665 | |
| 7666 | // Window becomes visible. |
| 7667 | window->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7668 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7669 | |
| 7670 | // Window receives focus event. |
| 7671 | window->consumeFocusEvent(true); |
| 7672 | // Focused window receives key down. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7673 | window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7674 | } |
| 7675 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 7676 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 7677 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7678 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "window", |
| 7679 | ui::LogicalDisplayId::DEFAULT); |
| 7680 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 7681 | |
| 7682 | // window is granted focus. |
| 7683 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7684 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 7685 | setFocusedWindow(window); |
| 7686 | window->consumeFocusEvent(true); |
| 7687 | |
| 7688 | // When a display is removed window loses focus. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7689 | mDispatcher->displayRemoved(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 7690 | window->consumeFocusEvent(false); |
| 7691 | } |
| 7692 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7693 | /** |
| 7694 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 7695 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 7696 | * of the 'slipperyEnterWindow'. |
| 7697 | * |
| 7698 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 7699 | * a way so that the touched location is no longer covered by the top window. |
| 7700 | * |
| 7701 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 7702 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 7703 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 7704 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 7705 | * with ACTION_DOWN). |
| 7706 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 7707 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 7708 | * |
| 7709 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 7710 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 7711 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 7712 | * |
| 7713 | * In this test, we ensure that the event received by the bottom window has |
| 7714 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 7715 | */ |
| 7716 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 7717 | constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7718 | constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1}; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7719 | |
| 7720 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7721 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7722 | |
| 7723 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7724 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 7725 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 7726 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7727 | // Make sure this one overlaps the bottom window |
| 7728 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 7729 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 7730 | // one. Windows with the same owner are not considered to be occluding each other. |
| 7731 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 7732 | |
| 7733 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7734 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 7735 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7736 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7737 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7738 | mDispatcher->onWindowInfosChanged( |
| 7739 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7740 | |
| 7741 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7742 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7743 | AINPUT_SOURCE_TOUCHSCREEN, |
| 7744 | ui::LogicalDisplayId::DEFAULT, {{50, 50}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7745 | slipperyExitWindow->consumeMotionDown(); |
| 7746 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7747 | mDispatcher->onWindowInfosChanged( |
| 7748 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7749 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7750 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7751 | AINPUT_SOURCE_TOUCHSCREEN, |
| 7752 | ui::LogicalDisplayId::DEFAULT, {{51, 51}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7753 | |
| 7754 | slipperyExitWindow->consumeMotionCancel(); |
| 7755 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7756 | slipperyEnterWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7757 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 7758 | } |
| 7759 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 7760 | /** |
| 7761 | * Two windows, one on the left and another on the right. The left window is slippery. The right |
| 7762 | * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the |
| 7763 | * touch moves from the left window into the right window, the gesture should continue to go to the |
| 7764 | * left window. Touch shouldn't slip because the right window can't receive touches. This test |
| 7765 | * reproduces a crash. |
| 7766 | */ |
| 7767 | TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { |
| 7768 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7769 | |
| 7770 | sp<FakeWindowHandle> leftSlipperyWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7771 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 7772 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 7773 | leftSlipperyWindow->setSlippery(true); |
| 7774 | leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7775 | |
| 7776 | sp<FakeWindowHandle> rightDropTouchesWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7777 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 7778 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 7779 | rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100)); |
| 7780 | rightDropTouchesWindow->setDropInput(true); |
| 7781 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7782 | mDispatcher->onWindowInfosChanged( |
| 7783 | {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 7784 | |
| 7785 | // Start touch in the left window |
| 7786 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7787 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7788 | .build()); |
| 7789 | leftSlipperyWindow->consumeMotionDown(); |
| 7790 | |
| 7791 | // And move it into the right window |
| 7792 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7793 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 7794 | .build()); |
| 7795 | |
| 7796 | // Since the right window isn't eligible to receive input, touch does not slip. |
| 7797 | // The left window continues to receive the gesture. |
| 7798 | leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 7799 | rightDropTouchesWindow->assertNoEvents(); |
| 7800 | } |
| 7801 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 7802 | /** |
| 7803 | * A single window is on screen first. Touch is injected into that window. Next, a second window |
| 7804 | * appears. Since the first window is slippery, touch will move from the first window to the second. |
| 7805 | */ |
| 7806 | TEST_F(InputDispatcherTest, InjectedTouchSlips) { |
| 7807 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7808 | sp<FakeWindowHandle> originalWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7809 | sp<FakeWindowHandle>::make(application, mDispatcher, "Original", |
| 7810 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 7811 | originalWindow->setFrame(Rect(0, 0, 200, 200)); |
| 7812 | originalWindow->setSlippery(true); |
| 7813 | |
| 7814 | sp<FakeWindowHandle> appearingWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7815 | sp<FakeWindowHandle>::make(application, mDispatcher, "Appearing", |
| 7816 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 7817 | appearingWindow->setFrame(Rect(0, 0, 200, 200)); |
| 7818 | |
| 7819 | mDispatcher->onWindowInfosChanged({{*originalWindow->getInfo()}, {}, 0, 0}); |
| 7820 | |
| 7821 | // Touch down on the original window |
| 7822 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7823 | injectMotionEvent(*mDispatcher, |
| 7824 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7825 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 7826 | .build())); |
| 7827 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 7828 | |
| 7829 | // Now, a new window appears. This could be, for example, a notification shade that appears |
| 7830 | // after user starts to drag down on the launcher window. |
| 7831 | mDispatcher->onWindowInfosChanged( |
| 7832 | {{*appearingWindow->getInfo(), *originalWindow->getInfo()}, {}, 0, 0}); |
| 7833 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7834 | injectMotionEvent(*mDispatcher, |
| 7835 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7836 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(110)) |
| 7837 | .build())); |
| 7838 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 7839 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 7840 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7841 | injectMotionEvent(*mDispatcher, |
| 7842 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7843 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 7844 | .build())); |
| 7845 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 7846 | |
| 7847 | originalWindow->assertNoEvents(); |
| 7848 | appearingWindow->assertNoEvents(); |
| 7849 | } |
| 7850 | |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7851 | /** |
| 7852 | * Three windows: |
| 7853 | * - left window, which has FLAG_SLIPPERY, so it supports slippery exit |
| 7854 | * - right window |
| 7855 | * - spy window |
| 7856 | * The three windows do not overlap. |
| 7857 | * |
| 7858 | * We have two devices reporting events: |
| 7859 | * - Device A reports ACTION_DOWN, which lands in the left window |
| 7860 | * - Device B reports ACTION_DOWN, which lands in the spy window. |
| 7861 | * - Now, device B reports ACTION_MOVE events which move to the right window. |
| 7862 | * |
| 7863 | * The right window should not receive any events because the spy window is not a foreground window, |
| 7864 | * and also it does not support slippery touches. |
| 7865 | */ |
| 7866 | TEST_F(InputDispatcherTest, MultiDeviceSpyWindowSlipTest) { |
| 7867 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7868 | sp<FakeWindowHandle> leftWindow = |
| 7869 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7870 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7871 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7872 | leftWindow->setSlippery(true); |
| 7873 | |
| 7874 | sp<FakeWindowHandle> rightWindow = |
| 7875 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7876 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7877 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 7878 | |
| 7879 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7880 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", |
| 7881 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7882 | spyWindow->setFrame(Rect(200, 0, 300, 100)); |
| 7883 | spyWindow->setSpy(true); |
| 7884 | spyWindow->setTrustedOverlay(true); |
| 7885 | |
| 7886 | mDispatcher->onWindowInfosChanged( |
| 7887 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *spyWindow->getInfo()}, {}, 0, 0}); |
| 7888 | |
| 7889 | const DeviceId deviceA = 9; |
| 7890 | const DeviceId deviceB = 3; |
| 7891 | |
| 7892 | // Tap on left window with device A |
| 7893 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7894 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7895 | .deviceId(deviceA) |
| 7896 | .build()); |
| 7897 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 7898 | |
| 7899 | // Tap on spy window with device B |
| 7900 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7901 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(50)) |
| 7902 | .deviceId(deviceB) |
| 7903 | .build()); |
| 7904 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 7905 | |
| 7906 | // Move to right window with device B. Touches should not slip to the right window, because spy |
| 7907 | // window is not a foreground window, and it does not have FLAG_SLIPPERY |
| 7908 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7909 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 7910 | .deviceId(deviceB) |
| 7911 | .build()); |
| 7912 | leftWindow->assertNoEvents(); |
| 7913 | rightWindow->assertNoEvents(); |
| 7914 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB))); |
| 7915 | } |
| 7916 | |
| 7917 | /** |
| 7918 | * Three windows arranged horizontally and without any overlap. |
| 7919 | * The left and right windows have FLAG_SLIPPERY. The middle window does not have any special flags. |
| 7920 | * |
| 7921 | * We have two devices reporting events: |
| 7922 | * - Device A reports ACTION_DOWN which lands in the left window |
| 7923 | * - Device B reports ACTION_DOWN which lands in the right window |
| 7924 | * - Device B reports ACTION_MOVE that shifts to the middle window. |
| 7925 | * This should cause touches for Device B to slip from the right window to the middle window. |
| 7926 | * The right window should receive ACTION_CANCEL for device B and the |
| 7927 | * middle window should receive down event for Device B. |
| 7928 | * If device B reports more ACTION_MOVE events, the middle window should receive remaining events. |
| 7929 | */ |
| 7930 | TEST_F(InputDispatcherTest, MultiDeviceSlipperyWindowTest) { |
Siarhei Vishniakou | dd56df1 | 2024-05-20 14:56:38 -0700 | [diff] [blame] | 7931 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7932 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7933 | sp<FakeWindowHandle> leftWindow = |
| 7934 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7935 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7936 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7937 | leftWindow->setSlippery(true); |
| 7938 | |
| 7939 | sp<FakeWindowHandle> middleWindow = |
| 7940 | sp<FakeWindowHandle>::make(application, mDispatcher, "middle window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7941 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7942 | middleWindow->setFrame(Rect(100, 0, 200, 100)); |
| 7943 | |
| 7944 | sp<FakeWindowHandle> rightWindow = |
| 7945 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 7946 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | 49b2b20 | 2024-04-12 12:46:40 +0800 | [diff] [blame] | 7947 | rightWindow->setFrame(Rect(200, 0, 300, 100)); |
| 7948 | rightWindow->setSlippery(true); |
| 7949 | |
| 7950 | mDispatcher->onWindowInfosChanged( |
| 7951 | {{*leftWindow->getInfo(), *middleWindow->getInfo(), *rightWindow->getInfo()}, |
| 7952 | {}, |
| 7953 | 0, |
| 7954 | 0}); |
| 7955 | |
| 7956 | const DeviceId deviceA = 9; |
| 7957 | const DeviceId deviceB = 3; |
| 7958 | |
| 7959 | // Tap on left window with device A |
| 7960 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7961 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7962 | .deviceId(deviceA) |
| 7963 | .build()); |
| 7964 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 7965 | |
| 7966 | // Tap on right window with device B |
| 7967 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7968 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(50)) |
| 7969 | .deviceId(deviceB) |
| 7970 | .build()); |
| 7971 | rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 7972 | |
| 7973 | // Move to middle window with device B. Touches should slip to middle window, because right |
| 7974 | // window is a foreground window that's associated with device B and has FLAG_SLIPPERY. |
| 7975 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7976 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 7977 | .deviceId(deviceB) |
| 7978 | .build()); |
| 7979 | rightWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceB))); |
| 7980 | middleWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceB))); |
| 7981 | |
| 7982 | // Move to middle window with device A. Touches should slip to middle window, because left |
| 7983 | // window is a foreground window that's associated with device A and has FLAG_SLIPPERY. |
| 7984 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7985 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 7986 | .deviceId(deviceA) |
| 7987 | .build()); |
| 7988 | leftWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(deviceA))); |
| 7989 | middleWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 7990 | |
| 7991 | // Ensure that middle window can receive the remaining move events. |
| 7992 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7993 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(51)) |
| 7994 | .deviceId(deviceB) |
| 7995 | .build()); |
| 7996 | leftWindow->assertNoEvents(); |
| 7997 | middleWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(deviceB))); |
| 7998 | rightWindow->assertNoEvents(); |
| 7999 | } |
| 8000 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8001 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 8002 | using Uid = gui::Uid; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8003 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 8004 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8005 | sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", |
| 8006 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8007 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8008 | leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8009 | |
| 8010 | sp<FakeWindowHandle> rightSpy = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8011 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", |
| 8012 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8013 | rightSpy->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8014 | rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8015 | rightSpy->setSpy(true); |
| 8016 | rightSpy->setTrustedOverlay(true); |
| 8017 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8018 | sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", |
| 8019 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8020 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8021 | rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8022 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8023 | mDispatcher->onWindowInfosChanged( |
| 8024 | {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8025 | |
| 8026 | // Touch in the left window |
| 8027 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8028 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8029 | .build()); |
| 8030 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown()); |
| 8031 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 8032 | ASSERT_NO_FATAL_FAILURE( |
| 8033 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8034 | |
| 8035 | // Touch another finger over the right windows |
| 8036 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8037 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8038 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 8039 | .build()); |
| 8040 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown()); |
| 8041 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown()); |
| 8042 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove()); |
| 8043 | mDispatcher->waitForIdle(); |
| 8044 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 8045 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, |
| 8046 | {Uid{101}, Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8047 | |
| 8048 | // Release finger over left window. The UP actions are not treated as device interaction. |
| 8049 | // The windows that did not receive the UP pointer will receive MOVE events, but since this |
| 8050 | // is part of the UP action, we do not treat this as device interaction. |
| 8051 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8052 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8053 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 8054 | .build()); |
| 8055 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp()); |
| 8056 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 8057 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 8058 | mDispatcher->waitForIdle(); |
| 8059 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 8060 | |
| 8061 | // Move remaining finger |
| 8062 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8063 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 8064 | .build()); |
| 8065 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 8066 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 8067 | mDispatcher->waitForIdle(); |
| 8068 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 8069 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8070 | |
| 8071 | // Release all fingers |
| 8072 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8073 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 8074 | .build()); |
| 8075 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp()); |
| 8076 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp()); |
| 8077 | mDispatcher->waitForIdle(); |
| 8078 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 8079 | } |
| 8080 | |
| 8081 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) { |
| 8082 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 8083 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8084 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 8085 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8086 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 8087 | window->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8088 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8089 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8090 | setFocusedWindow(window); |
| 8091 | ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true)); |
| 8092 | |
| 8093 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8094 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT)); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8095 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 8096 | ASSERT_NO_FATAL_FAILURE( |
| 8097 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8098 | |
| 8099 | // The UP actions are not treated as device interaction. |
| 8100 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8101 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT)); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 8102 | mDispatcher->waitForIdle(); |
| 8103 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 8104 | } |
| 8105 | |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8106 | TEST_F(InputDispatcherTest, HoverEnterExitSynthesisUsesNewEventId) { |
| 8107 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 8108 | |
| 8109 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8110 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8111 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8112 | sp<FakeWindowHandle> right = |
| 8113 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 8114 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8115 | right->setFrame(Rect(100, 0, 200, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8116 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", |
| 8117 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8118 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 8119 | spy->setTrustedOverlay(true); |
| 8120 | spy->setSpy(true); |
| 8121 | |
| 8122 | mDispatcher->onWindowInfosChanged( |
| 8123 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 8124 | |
| 8125 | // 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] | 8126 | NotifyMotionArgs notifyArgs = |
| 8127 | generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 8128 | ui::LogicalDisplayId::DEFAULT, {PointF{50, 50}}); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8129 | mDispatcher->notifyMotion(notifyArgs); |
| 8130 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8131 | std::unique_ptr<MotionEvent> leftEnter = left->consumeMotionEvent( |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8132 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), Not(WithEventId(notifyArgs.id)), |
| 8133 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8134 | ASSERT_NE(nullptr, leftEnter); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8135 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 8136 | Not(WithEventId(notifyArgs.id)), |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8137 | Not(WithEventId(leftEnter->getId())), |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8138 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 8139 | |
| 8140 | // 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] | 8141 | notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 8142 | ui::LogicalDisplayId::DEFAULT, {PointF{150, 50}}); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8143 | mDispatcher->notifyMotion(notifyArgs); |
| 8144 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8145 | std::unique_ptr<MotionEvent> leftExit = left->consumeMotionEvent( |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8146 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), Not(WithEventId(notifyArgs.id)), |
| 8147 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8148 | ASSERT_NE(nullptr, leftExit); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8149 | right->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 8150 | Not(WithEventId(notifyArgs.id)), |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8151 | Not(WithEventId(leftExit->getId())), |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 8152 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 8153 | |
| 8154 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithEventId(notifyArgs.id))); |
| 8155 | } |
| 8156 | |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 8157 | /** |
| 8158 | * When a device reports a DOWN event, which lands in a window that supports splits, and then the |
| 8159 | * device then reports a POINTER_DOWN, which lands in the location of a non-existing window, then |
| 8160 | * the previous window should receive this event and not be dropped. |
| 8161 | */ |
| 8162 | TEST_F(InputDispatcherMultiDeviceTest, SingleDevicePointerDownEventRetentionWithoutWindowTarget) { |
| 8163 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8164 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 8165 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 8166 | window->setFrame(Rect(0, 0, 100, 100)); |
| 8167 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 8168 | |
| 8169 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8170 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8171 | .build()); |
| 8172 | |
| 8173 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 8174 | |
| 8175 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8176 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8177 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 8178 | .build()); |
| 8179 | |
| 8180 | window->consumeMotionEvent(AllOf(WithMotionAction(POINTER_1_DOWN))); |
| 8181 | } |
| 8182 | |
| 8183 | /** |
| 8184 | * When deviceA reports a DOWN event, which lands in a window that supports splits, and then deviceB |
| 8185 | * also reports a DOWN event, which lands in the location of a non-existing window, then the |
| 8186 | * previous window should receive deviceB's event and it should be dropped. |
| 8187 | */ |
| 8188 | TEST_F(InputDispatcherMultiDeviceTest, SecondDeviceDownEventDroppedWithoutWindowTarget) { |
| 8189 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8190 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", |
| 8191 | ui::LogicalDisplayId::DEFAULT); |
Linnan Li | ccf6ce3 | 2024-04-11 20:32:13 +0800 | [diff] [blame] | 8192 | window->setFrame(Rect(0, 0, 100, 100)); |
| 8193 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 8194 | |
| 8195 | const DeviceId deviceA = 9; |
| 8196 | const DeviceId deviceB = 3; |
| 8197 | |
| 8198 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8199 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8200 | .deviceId(deviceA) |
| 8201 | .build()); |
| 8202 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(deviceA))); |
| 8203 | |
| 8204 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8205 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
| 8206 | .deviceId(deviceB) |
| 8207 | .build()); |
| 8208 | window->assertNoEvents(); |
| 8209 | } |
| 8210 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 8211 | class InputDispatcherFallbackKeyTest : public InputDispatcherTest { |
| 8212 | protected: |
| 8213 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 8214 | sp<FakeWindowHandle> mWindow; |
| 8215 | |
| 8216 | virtual void SetUp() override { |
| 8217 | InputDispatcherTest::SetUp(); |
| 8218 | |
| 8219 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 8220 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8221 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Window", |
| 8222 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 8223 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
| 8224 | |
| 8225 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 8226 | setFocusedWindow(mWindow); |
| 8227 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeFocusEvent(/*hasFocus=*/true)); |
| 8228 | } |
| 8229 | |
| 8230 | void setFallback(int32_t keycode) { |
| 8231 | mFakePolicy->setUnhandledKeyHandler([keycode](const KeyEvent& event) { |
| 8232 | return KeyEventBuilder(event).keyCode(keycode).build(); |
| 8233 | }); |
| 8234 | } |
| 8235 | |
| 8236 | void consumeKey(bool handled, const ::testing::Matcher<KeyEvent>& matcher) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8237 | std::unique_ptr<KeyEvent> event = mWindow->consumeKey(handled); |
| 8238 | ASSERT_NE(nullptr, event); |
| 8239 | ASSERT_THAT(*event, matcher); |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 8240 | } |
| 8241 | }; |
| 8242 | |
| 8243 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotNotifiedForHandledKey) { |
| 8244 | mDispatcher->notifyKey( |
| 8245 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8246 | consumeKey(/*handled=*/true, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 8247 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8248 | } |
| 8249 | |
| 8250 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotifiedForUnhandledKey) { |
| 8251 | mDispatcher->notifyKey( |
| 8252 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8253 | consumeKey(/*handled=*/false, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 8254 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8255 | } |
| 8256 | |
| 8257 | TEST_F(InputDispatcherFallbackKeyTest, NoFallbackRequestedByPolicy) { |
| 8258 | mDispatcher->notifyKey( |
| 8259 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8260 | |
| 8261 | // Do not handle this key event. |
| 8262 | consumeKey(/*handled=*/false, |
| 8263 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8264 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8265 | |
| 8266 | // Since the policy did not request any fallback to be generated, ensure there are no events. |
| 8267 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8268 | } |
| 8269 | |
| 8270 | TEST_F(InputDispatcherFallbackKeyTest, FallbackDispatchForUnhandledKey) { |
| 8271 | setFallback(AKEYCODE_B); |
| 8272 | mDispatcher->notifyKey( |
| 8273 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8274 | |
| 8275 | // Do not handle this key event. |
| 8276 | consumeKey(/*handled=*/false, |
| 8277 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8278 | |
| 8279 | // Since the key was not handled, ensure the fallback event was dispatched instead. |
| 8280 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8281 | consumeKey(/*handled=*/true, |
| 8282 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8283 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8284 | |
| 8285 | // Release the original key, and ensure the fallback key is also released. |
| 8286 | mDispatcher->notifyKey( |
| 8287 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8288 | consumeKey(/*handled=*/false, |
| 8289 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8290 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8291 | consumeKey(/*handled=*/true, |
| 8292 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8293 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8294 | |
| 8295 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8296 | mWindow->assertNoEvents(); |
| 8297 | } |
| 8298 | |
| 8299 | TEST_F(InputDispatcherFallbackKeyTest, AppHandlesPreviouslyUnhandledKey) { |
| 8300 | setFallback(AKEYCODE_B); |
| 8301 | mDispatcher->notifyKey( |
| 8302 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8303 | |
| 8304 | // Do not handle this key event, but handle the fallback. |
| 8305 | consumeKey(/*handled=*/false, |
| 8306 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8307 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8308 | consumeKey(/*handled=*/true, |
| 8309 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8310 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8311 | |
| 8312 | // Release the original key, and ensure the fallback key is also released. |
| 8313 | mDispatcher->notifyKey( |
| 8314 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8315 | // But this time, the app handles the original key. |
| 8316 | consumeKey(/*handled=*/true, |
| 8317 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8318 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8319 | // Ensure the fallback key is canceled. |
| 8320 | consumeKey(/*handled=*/true, |
| 8321 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8322 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 8323 | |
| 8324 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8325 | mWindow->assertNoEvents(); |
| 8326 | } |
| 8327 | |
| 8328 | TEST_F(InputDispatcherFallbackKeyTest, AppDoesNotHandleFallback) { |
| 8329 | setFallback(AKEYCODE_B); |
| 8330 | mDispatcher->notifyKey( |
| 8331 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8332 | |
| 8333 | // Do not handle this key event. |
| 8334 | consumeKey(/*handled=*/false, |
| 8335 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8336 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8337 | // App does not handle the fallback either, so ensure another fallback is not generated. |
| 8338 | setFallback(AKEYCODE_C); |
| 8339 | consumeKey(/*handled=*/false, |
| 8340 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8341 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8342 | |
| 8343 | // Release the original key, and ensure the fallback key is also released. |
| 8344 | setFallback(AKEYCODE_B); |
| 8345 | mDispatcher->notifyKey( |
| 8346 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8347 | consumeKey(/*handled=*/false, |
| 8348 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8349 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8350 | consumeKey(/*handled=*/false, |
| 8351 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8352 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8353 | |
| 8354 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8355 | mWindow->assertNoEvents(); |
| 8356 | } |
| 8357 | |
| 8358 | TEST_F(InputDispatcherFallbackKeyTest, InconsistentPolicyCancelsFallback) { |
| 8359 | setFallback(AKEYCODE_B); |
| 8360 | mDispatcher->notifyKey( |
| 8361 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8362 | |
| 8363 | // Do not handle this key event, so fallback is generated. |
| 8364 | consumeKey(/*handled=*/false, |
| 8365 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8366 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8367 | consumeKey(/*handled=*/true, |
| 8368 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8369 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8370 | |
| 8371 | // Release the original key, but assume the policy is misbehaving and it |
| 8372 | // generates an inconsistent fallback to the one from the DOWN event. |
| 8373 | setFallback(AKEYCODE_C); |
| 8374 | mDispatcher->notifyKey( |
| 8375 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8376 | consumeKey(/*handled=*/false, |
| 8377 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8378 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8379 | // Ensure the fallback key reported before as DOWN is canceled due to the inconsistency. |
| 8380 | consumeKey(/*handled=*/true, |
| 8381 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8382 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 8383 | |
| 8384 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8385 | mWindow->assertNoEvents(); |
| 8386 | } |
| 8387 | |
| 8388 | TEST_F(InputDispatcherFallbackKeyTest, CanceledKeyCancelsFallback) { |
| 8389 | setFallback(AKEYCODE_B); |
| 8390 | mDispatcher->notifyKey( |
| 8391 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8392 | |
| 8393 | // Do not handle this key event, so fallback is generated. |
| 8394 | consumeKey(/*handled=*/false, |
| 8395 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8396 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8397 | consumeKey(/*handled=*/true, |
| 8398 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8399 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8400 | |
| 8401 | // The original key is canceled. |
| 8402 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
| 8403 | .keyCode(AKEYCODE_A) |
| 8404 | .addFlag(AKEY_EVENT_FLAG_CANCELED) |
| 8405 | .build()); |
| 8406 | consumeKey(/*handled=*/false, |
| 8407 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), |
| 8408 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 8409 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8410 | // Ensure the fallback key is also canceled due to the original key being canceled. |
| 8411 | consumeKey(/*handled=*/true, |
| 8412 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8413 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 8414 | |
| 8415 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 8416 | mWindow->assertNoEvents(); |
| 8417 | } |
| 8418 | |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 8419 | TEST_F(InputDispatcherFallbackKeyTest, InputChannelRemovedDuringPolicyCall) { |
Prabir Pradhan | b13da8f | 2024-01-09 23:10:13 +0000 | [diff] [blame] | 8420 | setFallback(AKEYCODE_B); |
| 8421 | mDispatcher->notifyKey( |
| 8422 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8423 | |
| 8424 | // Do not handle this key event. |
| 8425 | consumeKey(/*handled=*/false, |
| 8426 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8427 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8428 | consumeKey(/*handled=*/true, |
| 8429 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8430 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8431 | |
| 8432 | mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) { |
| 8433 | // When the unhandled key is reported to the policy next, remove the input channel. |
| 8434 | mDispatcher->removeInputChannel(mWindow->getToken()); |
| 8435 | return KeyEventBuilder(event).keyCode(AKEYCODE_B).build(); |
| 8436 | }); |
| 8437 | // Release the original key, and let the app now handle the previously unhandled key. |
| 8438 | // This should result in the previously generated fallback key to be cancelled. |
| 8439 | // Since the policy was notified of the unhandled DOWN event earlier, it will also be notified |
| 8440 | // of the UP event for consistency. The Dispatcher calls into the policy from its own thread |
| 8441 | // without holding the lock, because it need to synchronously fetch the fallback key. While in |
| 8442 | // the policy call, we will now remove the input channel. Once the policy call returns, the |
| 8443 | // Dispatcher will no longer have a channel to send cancellation events to. Ensure this does |
| 8444 | // not cause any crashes. |
| 8445 | mDispatcher->notifyKey( |
| 8446 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8447 | consumeKey(/*handled=*/true, |
| 8448 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8449 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8450 | } |
| 8451 | |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 8452 | TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedDuringPolicyCall) { |
| 8453 | setFallback(AKEYCODE_B); |
| 8454 | mDispatcher->notifyKey( |
| 8455 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8456 | |
| 8457 | // Do not handle this key event. |
| 8458 | consumeKey(/*handled=*/false, |
| 8459 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8460 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8461 | consumeKey(/*handled=*/true, |
| 8462 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8463 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8464 | |
| 8465 | mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) { |
| 8466 | // When the unhandled key is reported to the policy next, remove the window. |
| 8467 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 8468 | return KeyEventBuilder(event).keyCode(AKEYCODE_B).build(); |
| 8469 | }); |
| 8470 | // Release the original key, which the app will not handle. When this unhandled key is reported |
| 8471 | // to the policy, the window will be removed. |
| 8472 | mDispatcher->notifyKey( |
| 8473 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8474 | consumeKey(/*handled=*/false, |
| 8475 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8476 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8477 | |
| 8478 | // Since the window was removed, it loses focus, and the channel state will be reset. |
| 8479 | consumeKey(/*handled=*/true, |
| 8480 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8481 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 8482 | mWindow->consumeFocusEvent(false); |
| 8483 | mWindow->assertNoEvents(); |
| 8484 | } |
| 8485 | |
| 8486 | TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedWhileAwaitingFinishedSignal) { |
| 8487 | setFallback(AKEYCODE_B); |
| 8488 | mDispatcher->notifyKey( |
| 8489 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 8490 | |
| 8491 | // Do not handle this key event. |
| 8492 | consumeKey(/*handled=*/false, |
| 8493 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 8494 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 8495 | const auto [seq, event] = mWindow->receiveEvent(); |
| 8496 | ASSERT_TRUE(seq.has_value() && event != nullptr) << "Failed to receive fallback event"; |
| 8497 | ASSERT_EQ(event->getType(), InputEventType::KEY); |
| 8498 | ASSERT_THAT(static_cast<const KeyEvent&>(*event), |
| 8499 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 8500 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 8501 | |
| 8502 | // Remove the window now, which should generate a cancellations and make the window lose focus. |
| 8503 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 8504 | consumeKey(/*handled=*/true, |
| 8505 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), |
| 8506 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 8507 | consumeKey(/*handled=*/true, |
| 8508 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 8509 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 8510 | mWindow->consumeFocusEvent(false); |
| 8511 | |
| 8512 | // Finish the event by reporting it as handled. |
| 8513 | mWindow->finishEvent(*seq); |
| 8514 | mWindow->assertNoEvents(); |
| 8515 | } |
| 8516 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8517 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 8518 | protected: |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 8519 | static constexpr std::chrono::nanoseconds KEY_REPEAT_TIMEOUT = 40ms; |
| 8520 | static constexpr std::chrono::nanoseconds KEY_REPEAT_DELAY = 40ms; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8521 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8522 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8523 | sp<FakeWindowHandle> mWindow; |
| 8524 | |
| 8525 | virtual void SetUp() override { |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 8526 | InputDispatcherTest::SetUp(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8527 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 8528 | mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8529 | setUpWindow(); |
| 8530 | } |
| 8531 | |
| 8532 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8533 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8534 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", |
| 8535 | ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8536 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8537 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8538 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8539 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8540 | mWindow->consumeFocusEvent(true); |
| 8541 | } |
| 8542 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8543 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8544 | NotifyKeyArgs keyArgs = |
| 8545 | generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8546 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8547 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8548 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8549 | |
| 8550 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8551 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8552 | } |
| 8553 | |
| 8554 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 8555 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8556 | mWindow->consumeKeyEvent( |
| 8557 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount))); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8558 | } |
| 8559 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8560 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8561 | NotifyKeyArgs keyArgs = |
| 8562 | generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8563 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8564 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8565 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8566 | |
| 8567 | // Window should receive key down event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8568 | mWindow->consumeKeyUp(ui::LogicalDisplayId::DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8569 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8570 | } |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 8571 | |
| 8572 | void injectKeyRepeat(int32_t repeatCount) { |
| 8573 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8574 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, repeatCount, |
| 8575 | ui::LogicalDisplayId::DEFAULT)) |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 8576 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 8577 | } |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8578 | }; |
| 8579 | |
| 8580 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8581 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8582 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 8583 | expectKeyRepeatOnce(repeatCount); |
| 8584 | } |
| 8585 | } |
| 8586 | |
| 8587 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8588 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8589 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 8590 | expectKeyRepeatOnce(repeatCount); |
| 8591 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8592 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8593 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8594 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 8595 | expectKeyRepeatOnce(repeatCount); |
| 8596 | } |
| 8597 | } |
| 8598 | |
| 8599 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8600 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 8601 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 8602 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8603 | mWindow->assertNoEvents(); |
| 8604 | } |
| 8605 | |
| 8606 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8607 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 8608 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 8609 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 8610 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8611 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8612 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8613 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8614 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 8615 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8616 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8617 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8618 | mWindow->assertNoEvents(); |
| 8619 | } |
| 8620 | |
| 8621 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8622 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 8623 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 8624 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 8625 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8626 | // 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] | 8627 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 8628 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8629 | mWindow->assertNoEvents(); |
| 8630 | } |
| 8631 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 8632 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 8633 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8634 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8635 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8636 | mWindow->consumeKeyUp(ui::LogicalDisplayId::DEFAULT, |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 8637 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 8638 | mWindow->assertNoEvents(); |
| 8639 | } |
| 8640 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8641 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 8642 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8643 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8644 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8645 | std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey(); |
| 8646 | ASSERT_NE(nullptr, repeatEvent); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8647 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8648 | IdGenerator::getSource(repeatEvent->getId())); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8649 | } |
| 8650 | } |
| 8651 | |
| 8652 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 8653 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8654 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8655 | |
| 8656 | std::unordered_set<int32_t> idSet; |
| 8657 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8658 | std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey(); |
| 8659 | ASSERT_NE(nullptr, repeatEvent); |
| 8660 | int32_t id = repeatEvent->getId(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 8661 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 8662 | idSet.insert(id); |
| 8663 | } |
| 8664 | } |
| 8665 | |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 8666 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_CorrectRepeatCountWhenInjectKeyRepeat) { |
| 8667 | injectKeyRepeat(0); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8668 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 8669 | for (int32_t repeatCount = 1; repeatCount <= 2; ++repeatCount) { |
| 8670 | expectKeyRepeatOnce(repeatCount); |
| 8671 | } |
| 8672 | injectKeyRepeat(1); |
| 8673 | // Expect repeatCount to be 3 instead of 1 |
| 8674 | expectKeyRepeatOnce(3); |
| 8675 | } |
| 8676 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8677 | /* Test InputDispatcher for MultiDisplay */ |
| 8678 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 8679 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 8680 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8681 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8682 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8683 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8684 | windowInPrimary = sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", |
| 8685 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8686 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8687 | // 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] | 8688 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8689 | windowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8690 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
| 8691 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8692 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8693 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8694 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8695 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8696 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8697 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8698 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8699 | // Set focus display to second one. |
| 8700 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
Arpit Singh | b65e2bd | 2024-06-03 09:48:16 +0000 | [diff] [blame] | 8701 | mFakePolicy->assertFocusedDisplayNotified(SECOND_DISPLAY_ID); |
| 8702 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8703 | // Set focus window for second display. |
| 8704 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8705 | windowInSecondary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8706 | mDispatcher->onWindowInfosChanged( |
| 8707 | {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8708 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8709 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8710 | } |
| 8711 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 8712 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8713 | InputDispatcherTest::TearDown(); |
| 8714 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8715 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8716 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8717 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8718 | windowInSecondary.clear(); |
| 8719 | } |
| 8720 | |
| 8721 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8722 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8723 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8724 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8725 | sp<FakeWindowHandle> windowInSecondary; |
| 8726 | }; |
| 8727 | |
| 8728 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 8729 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8730 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8731 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 8732 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8733 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8734 | windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8735 | windowInSecondary->assertNoEvents(); |
| 8736 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8737 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8738 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8739 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8740 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8741 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 8742 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8743 | } |
| 8744 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8745 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 8746 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 8747 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8748 | injectKeyDownNoRepeat(*mDispatcher, ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8749 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8750 | windowInPrimary->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 8751 | windowInSecondary->assertNoEvents(); |
| 8752 | |
| 8753 | // Test inject a key down without display id specified. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8754 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8755 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8756 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8757 | windowInSecondary->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8758 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8759 | // Remove all windows in secondary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8760 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8761 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8762 | // Old focus should receive a cancel event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8763 | windowInSecondary->consumeKeyUp(ui::LogicalDisplayId::INVALID, AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8764 | |
| 8765 | // Test inject a key down, should timeout because of no target window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8766 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8767 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8768 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 8769 | windowInSecondary->assertNoEvents(); |
| 8770 | } |
| 8771 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8772 | // Test per-display input monitors for motion event. |
| 8773 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8774 | FakeMonitorReceiver monitorInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8775 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8776 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 8777 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8778 | |
| 8779 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8780 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8781 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 8782 | ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8783 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8784 | windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 8785 | monitorInPrimary.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8786 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8787 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8788 | |
| 8789 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8790 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8791 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8792 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8793 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8794 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 8795 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8796 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8797 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 8798 | // Lift up the touch from the second display |
| 8799 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8800 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 8801 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8802 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 8803 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 8804 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8805 | // Test inject a non-pointer motion event. |
| 8806 | // If specific a display, it will dispatch to the focused window of particular display, |
| 8807 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8808 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8809 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, |
| 8810 | ui::LogicalDisplayId::INVALID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8811 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8812 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8813 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8814 | windowInSecondary->consumeMotionDown(ui::LogicalDisplayId::INVALID); |
| 8815 | monitorInSecondary.consumeMotionDown(ui::LogicalDisplayId::INVALID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8816 | } |
| 8817 | |
| 8818 | // Test per-display input monitors for key event. |
| 8819 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8820 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8821 | FakeMonitorReceiver monitorInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8822 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8823 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 8824 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8825 | |
| 8826 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8827 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8828 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8829 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 8830 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8831 | windowInSecondary->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
| 8832 | monitorInSecondary.consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 8833 | } |
| 8834 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8835 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 8836 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8837 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", |
| 8838 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8839 | secondWindowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8840 | mDispatcher->onWindowInfosChanged( |
| 8841 | {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(), |
| 8842 | *windowInSecondary->getInfo()}, |
| 8843 | {}, |
| 8844 | 0, |
| 8845 | 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8846 | setFocusedWindow(secondWindowInPrimary); |
| 8847 | windowInPrimary->consumeFocusEvent(false); |
| 8848 | secondWindowInPrimary->consumeFocusEvent(true); |
| 8849 | |
| 8850 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8851 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8852 | injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8853 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8854 | windowInPrimary->assertNoEvents(); |
| 8855 | windowInSecondary->assertNoEvents(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8856 | secondWindowInPrimary->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8857 | } |
| 8858 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8859 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 8860 | FakeMonitorReceiver monitorInPrimary = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8861 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8862 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 8863 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8864 | |
| 8865 | // Test touch down on primary display. |
| 8866 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8867 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 8868 | ui::LogicalDisplayId::DEFAULT)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8869 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8870 | windowInPrimary->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 8871 | monitorInPrimary.consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8872 | |
| 8873 | // Test touch down on second display. |
| 8874 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8875 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8876 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8877 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 8878 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 8879 | |
| 8880 | // Trigger cancel touch. |
| 8881 | mDispatcher->cancelCurrentTouch(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8882 | windowInPrimary->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT); |
| 8883 | monitorInPrimary.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8884 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 8885 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 8886 | |
| 8887 | // Test inject a move motion event, no window/monitor should receive the event. |
| 8888 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8889 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8890 | ui::LogicalDisplayId::DEFAULT, {110, 200})) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8891 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 8892 | windowInPrimary->assertNoEvents(); |
| 8893 | monitorInPrimary.assertNoEvents(); |
| 8894 | |
| 8895 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8896 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 8897 | SECOND_DISPLAY_ID, {110, 200})) |
| 8898 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 8899 | windowInSecondary->assertNoEvents(); |
| 8900 | monitorInSecondary.assertNoEvents(); |
| 8901 | } |
| 8902 | |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8903 | /** |
| 8904 | * Send a key to the primary display and to the secondary display. |
| 8905 | * Then cause the key on the primary display to be canceled by sending in a stale key. |
| 8906 | * Ensure that the key on the primary display is canceled, and that the key on the secondary display |
| 8907 | * does not get canceled. |
| 8908 | */ |
| 8909 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture) { |
| 8910 | // Send a key down on primary display |
| 8911 | mDispatcher->notifyKey( |
| 8912 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8913 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8914 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 8915 | .build()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8916 | windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), |
| 8917 | WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8918 | windowInSecondary->assertNoEvents(); |
| 8919 | |
| 8920 | // Send a key down on second display |
| 8921 | mDispatcher->notifyKey( |
| 8922 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 8923 | .displayId(SECOND_DISPLAY_ID) |
| 8924 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 8925 | .build()); |
| 8926 | windowInSecondary->consumeKeyEvent( |
| 8927 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID))); |
| 8928 | windowInPrimary->assertNoEvents(); |
| 8929 | |
| 8930 | // Send a valid key up event on primary display that will be dropped because it is stale |
| 8931 | NotifyKeyArgs staleKeyUp = |
| 8932 | KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8933 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8934 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 8935 | .build(); |
| 8936 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms; |
| 8937 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
| 8938 | std::this_thread::sleep_for(STALE_EVENT_TIMEOUT); |
| 8939 | mDispatcher->notifyKey(staleKeyUp); |
| 8940 | |
| 8941 | // Only the key gesture corresponding to the dropped event should receive the cancel event. |
| 8942 | // Therefore, windowInPrimary should get the cancel event and windowInSecondary should not |
| 8943 | // receive any events. |
| 8944 | windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8945 | WithDisplayId(ui::LogicalDisplayId::DEFAULT), |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8946 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 8947 | windowInSecondary->assertNoEvents(); |
| 8948 | } |
| 8949 | |
| 8950 | /** |
| 8951 | * Similar to 'WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture' but for motion events. |
| 8952 | */ |
| 8953 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropMotionEvent_OnlyCancelCorrespondingGesture) { |
| 8954 | // Send touch down on primary display. |
| 8955 | mDispatcher->notifyMotion( |
| 8956 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8957 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8958 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8959 | .build()); |
| 8960 | windowInPrimary->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8961 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8962 | windowInSecondary->assertNoEvents(); |
| 8963 | |
| 8964 | // Send touch down on second display. |
| 8965 | mDispatcher->notifyMotion( |
| 8966 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8967 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 8968 | .displayId(SECOND_DISPLAY_ID) |
| 8969 | .build()); |
| 8970 | windowInPrimary->assertNoEvents(); |
| 8971 | windowInSecondary->consumeMotionEvent( |
| 8972 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID))); |
| 8973 | |
| 8974 | // inject a valid MotionEvent on primary display that will be stale when it arrives. |
| 8975 | NotifyMotionArgs staleMotionUp = |
| 8976 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 8977 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 8978 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 8979 | .build(); |
| 8980 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms; |
| 8981 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
| 8982 | std::this_thread::sleep_for(STALE_EVENT_TIMEOUT); |
| 8983 | mDispatcher->notifyMotion(staleMotionUp); |
| 8984 | |
| 8985 | // For stale motion events, we let the gesture to complete. This behaviour is different from key |
| 8986 | // events, where we would cancel the current keys instead. |
| 8987 | windowInPrimary->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 8988 | windowInSecondary->assertNoEvents(); |
| 8989 | } |
| 8990 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8991 | class InputFilterTest : public InputDispatcherTest { |
| 8992 | protected: |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 8993 | void testNotifyMotion(ui::LogicalDisplayId displayId, bool expectToBeFiltered, |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 8994 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 8995 | NotifyMotionArgs motionArgs; |
| 8996 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8997 | motionArgs = |
| 8998 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8999 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 9000 | motionArgs = |
| 9001 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9002 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 9003 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9004 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 9005 | const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue()); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 9006 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9007 | } else { |
| 9008 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 9009 | } |
| 9010 | } |
| 9011 | |
| 9012 | void testNotifyKey(bool expectToBeFiltered) { |
| 9013 | NotifyKeyArgs keyArgs; |
| 9014 | |
| 9015 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9016 | mDispatcher->notifyKey(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9017 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9018 | mDispatcher->notifyKey(keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 9019 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9020 | |
| 9021 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 9022 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9023 | } else { |
| 9024 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 9025 | } |
| 9026 | } |
| 9027 | }; |
| 9028 | |
| 9029 | // Test InputFilter for MotionEvent |
| 9030 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 9031 | // 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] | 9032 | testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/false); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 9033 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9034 | |
| 9035 | // Enable InputFilter |
| 9036 | mDispatcher->setInputFilterEnabled(true); |
| 9037 | // 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] | 9038 | testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/true); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 9039 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9040 | |
| 9041 | // Disable InputFilter |
| 9042 | mDispatcher->setInputFilterEnabled(false); |
| 9043 | // 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] | 9044 | testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/false); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 9045 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9046 | } |
| 9047 | |
| 9048 | // Test InputFilter for KeyEvent |
| 9049 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 9050 | // 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] | 9051 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9052 | |
| 9053 | // Enable InputFilter |
| 9054 | mDispatcher->setInputFilterEnabled(true); |
| 9055 | // Send a key event, and check if it is filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 9056 | testNotifyKey(/*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9057 | |
| 9058 | // Disable InputFilter |
| 9059 | mDispatcher->setInputFilterEnabled(false); |
| 9060 | // Send a key event, and check if it isn't filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 9061 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 9062 | } |
| 9063 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 9064 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 9065 | // logical display coordinate space. |
| 9066 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 9067 | ui::Transform firstDisplayTransform; |
| 9068 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 9069 | ui::Transform secondDisplayTransform; |
| 9070 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 9071 | |
| 9072 | std::vector<gui::DisplayInfo> displayInfos(2); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9073 | displayInfos[0].displayId = ui::LogicalDisplayId::DEFAULT; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 9074 | displayInfos[0].transform = firstDisplayTransform; |
| 9075 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 9076 | displayInfos[1].transform = secondDisplayTransform; |
| 9077 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 9078 | mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0}); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 9079 | |
| 9080 | // Enable InputFilter |
| 9081 | mDispatcher->setInputFilterEnabled(true); |
| 9082 | |
| 9083 | // Ensure the correct transforms are used for the displays. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9084 | testNotifyMotion(ui::LogicalDisplayId::DEFAULT, /*expectToBeFiltered=*/true, |
| 9085 | firstDisplayTransform); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 9086 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 9087 | } |
| 9088 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9089 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 9090 | protected: |
| 9091 | virtual void SetUp() override { |
| 9092 | InputDispatcherTest::SetUp(); |
| 9093 | |
| 9094 | /** |
| 9095 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 9096 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 9097 | * on. |
| 9098 | */ |
| 9099 | mDispatcher->setInputFilterEnabled(true); |
| 9100 | |
| 9101 | std::shared_ptr<InputApplicationHandle> application = |
| 9102 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9103 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9104 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9105 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9106 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9107 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9108 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9109 | setFocusedWindow(mWindow); |
| 9110 | mWindow->consumeFocusEvent(true); |
| 9111 | } |
| 9112 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9113 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 9114 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9115 | KeyEvent event; |
| 9116 | |
| 9117 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 9118 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9119 | ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, |
| 9120 | AKEYCODE_A, KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9121 | const int32_t additionalPolicyFlags = |
| 9122 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 9123 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9124 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 9125 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9126 | policyFlags | additionalPolicyFlags)); |
| 9127 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9128 | mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags))); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9129 | } |
| 9130 | |
| 9131 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 9132 | int32_t flags) { |
| 9133 | MotionEvent event; |
| 9134 | PointerProperties pointerProperties[1]; |
| 9135 | PointerCoords pointerCoords[1]; |
| 9136 | pointerProperties[0].clear(); |
| 9137 | pointerProperties[0].id = 0; |
| 9138 | pointerCoords[0].clear(); |
| 9139 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 9140 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 9141 | |
| 9142 | ui::Transform identityTransform; |
| 9143 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 9144 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 9145 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 9146 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 9147 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 9148 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 9149 | eventTime, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 9150 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9151 | |
| 9152 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 9153 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9154 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 9155 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9156 | policyFlags | additionalPolicyFlags)); |
| 9157 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9158 | mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId))); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9159 | } |
| 9160 | |
| 9161 | private: |
| 9162 | sp<FakeWindowHandle> mWindow; |
| 9163 | }; |
| 9164 | |
| 9165 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9166 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 9167 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 9168 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9169 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 9170 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9171 | } |
| 9172 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9173 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9174 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9175 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9176 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 9177 | } |
| 9178 | |
| 9179 | TEST_F(InputFilterInjectionPolicyTest, |
| 9180 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 9181 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9182 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 9183 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9184 | } |
| 9185 | |
| 9186 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9187 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 9188 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 9189 | } |
| 9190 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9191 | class InputDispatcherUserActivityPokeTests : public InputDispatcherTest { |
| 9192 | protected: |
| 9193 | virtual void SetUp() override { |
| 9194 | InputDispatcherTest::SetUp(); |
| 9195 | |
| 9196 | std::shared_ptr<FakeApplicationHandle> application = |
| 9197 | std::make_shared<FakeApplicationHandle>(); |
| 9198 | application->setDispatchingTimeout(100ms); |
| 9199 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9200 | ui::LogicalDisplayId::DEFAULT); |
Yeabkal Wubshit | 222d83d | 2024-01-24 18:00:09 +0000 | [diff] [blame] | 9201 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9202 | mWindow->setDispatchingTimeout(100ms); |
| 9203 | mWindow->setFocusable(true); |
| 9204 | |
| 9205 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9206 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9207 | |
| 9208 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 9209 | setFocusedWindow(mWindow); |
| 9210 | mWindow->consumeFocusEvent(true); |
| 9211 | } |
| 9212 | |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 9213 | void notifyAndConsumeMotion(int32_t action, uint32_t source, ui::LogicalDisplayId displayId, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9214 | nsecs_t eventTime) { |
| 9215 | mDispatcher->notifyMotion(MotionArgsBuilder(action, source) |
| 9216 | .displayId(displayId) |
| 9217 | .eventTime(eventTime) |
| 9218 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 9219 | .build()); |
| 9220 | mWindow->consumeMotionEvent(WithMotionAction(action)); |
| 9221 | } |
| 9222 | |
| 9223 | private: |
| 9224 | sp<FakeWindowHandle> mWindow; |
| 9225 | }; |
| 9226 | |
| 9227 | TEST_F_WITH_FLAGS( |
| 9228 | InputDispatcherUserActivityPokeTests, MinPokeTimeObserved, |
| 9229 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 9230 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 9231 | mDispatcher->setMinTimeBetweenUserActivityPokes(50ms); |
| 9232 | |
| 9233 | // First event of type TOUCH. Should poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9234 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9235 | milliseconds_to_nanoseconds(50)); |
| 9236 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9237 | {{milliseconds_to_nanoseconds(50), USER_ACTIVITY_EVENT_TOUCH, |
| 9238 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9239 | |
| 9240 | // 80ns > 50ns has passed since previous TOUCH event. Should poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9241 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9242 | milliseconds_to_nanoseconds(130)); |
| 9243 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9244 | {{milliseconds_to_nanoseconds(130), USER_ACTIVITY_EVENT_TOUCH, |
| 9245 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9246 | |
| 9247 | // 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] | 9248 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, |
| 9249 | ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(135)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9250 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9251 | {{milliseconds_to_nanoseconds(135), USER_ACTIVITY_EVENT_OTHER, |
| 9252 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9253 | |
| 9254 | // Within 50ns of previous TOUCH event. Should NOT poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9255 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9256 | milliseconds_to_nanoseconds(140)); |
| 9257 | mFakePolicy->assertUserActivityNotPoked(); |
| 9258 | |
| 9259 | // Within 50ns of previous OTHER event. Should NOT poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9260 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, |
| 9261 | ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(150)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9262 | mFakePolicy->assertUserActivityNotPoked(); |
| 9263 | |
| 9264 | // Within 50ns of previous TOUCH event (which was at time 130). Should NOT poke. |
| 9265 | // 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] | 9266 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_STYLUS, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9267 | milliseconds_to_nanoseconds(160)); |
| 9268 | mFakePolicy->assertUserActivityNotPoked(); |
| 9269 | |
| 9270 | // 65ns > 50ns has passed since previous OTHER event. Should poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9271 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, |
| 9272 | ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(200)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9273 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9274 | {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_OTHER, |
| 9275 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9276 | |
| 9277 | // 170ns > 50ns has passed since previous TOUCH event. Should poke. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9278 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_STYLUS, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9279 | milliseconds_to_nanoseconds(300)); |
| 9280 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9281 | {{milliseconds_to_nanoseconds(300), USER_ACTIVITY_EVENT_TOUCH, |
| 9282 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9283 | |
| 9284 | // Assert that there's no more user activity poke event. |
| 9285 | mFakePolicy->assertUserActivityNotPoked(); |
| 9286 | } |
| 9287 | |
| 9288 | TEST_F_WITH_FLAGS( |
| 9289 | InputDispatcherUserActivityPokeTests, DefaultMinPokeTimeOf100MsUsed, |
| 9290 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 9291 | rate_limit_user_activity_poke_in_dispatcher))) { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9292 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9293 | milliseconds_to_nanoseconds(200)); |
| 9294 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9295 | {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_TOUCH, |
| 9296 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9297 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9298 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9299 | milliseconds_to_nanoseconds(280)); |
| 9300 | mFakePolicy->assertUserActivityNotPoked(); |
| 9301 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9302 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9303 | milliseconds_to_nanoseconds(340)); |
| 9304 | mFakePolicy->assertUserActivityPoked( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9305 | {{milliseconds_to_nanoseconds(340), USER_ACTIVITY_EVENT_TOUCH, |
| 9306 | ui::LogicalDisplayId::DEFAULT}}); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9307 | } |
| 9308 | |
| 9309 | TEST_F_WITH_FLAGS( |
| 9310 | InputDispatcherUserActivityPokeTests, ZeroMinPokeTimeDisablesRateLimiting, |
| 9311 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 9312 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 9313 | mDispatcher->setMinTimeBetweenUserActivityPokes(0ms); |
| 9314 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9315 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
| 9316 | 20); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9317 | mFakePolicy->assertUserActivityPoked(); |
| 9318 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9319 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
| 9320 | 30); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 9321 | mFakePolicy->assertUserActivityPoked(); |
| 9322 | } |
| 9323 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9324 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 9325 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9326 | InputDispatcherTest::SetUp(); |
| 9327 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9328 | std::shared_ptr<FakeApplicationHandle> application = |
| 9329 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9330 | mUnfocusedWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Top", |
| 9331 | ui::LogicalDisplayId::DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9332 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9333 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9334 | mFocusedWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Second", |
| 9335 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 9336 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9337 | |
| 9338 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9339 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9340 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9341 | |
| 9342 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9343 | mDispatcher->onWindowInfosChanged( |
| 9344 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9345 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 9346 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9347 | } |
| 9348 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 9349 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9350 | InputDispatcherTest::TearDown(); |
| 9351 | |
| 9352 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 9353 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9354 | } |
| 9355 | |
| 9356 | protected: |
| 9357 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 9358 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 9359 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9360 | }; |
| 9361 | |
| 9362 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 9363 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 9364 | // the onPointerDownOutsideFocus callback. |
| 9365 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9366 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9367 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9368 | ui::LogicalDisplayId::DEFAULT, {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9369 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 9370 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9371 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 9372 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9373 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 9374 | } |
| 9375 | |
| 9376 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 9377 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 9378 | // onPointerDownOutsideFocus callback. |
| 9379 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9380 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9381 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9382 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9383 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 9384 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9385 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 9386 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9387 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9388 | } |
| 9389 | |
| 9390 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 9391 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 9392 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 9393 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9394 | injectKeyDownNoRepeat(*mDispatcher, ui::LogicalDisplayId::DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9395 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9396 | mFocusedWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9397 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 9398 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9399 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9400 | } |
| 9401 | |
| 9402 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 9403 | // DOWN on the window that already has focus. Ensure no window received the |
| 9404 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 9405 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9406 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9407 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9408 | ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9409 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 9410 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9411 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 9412 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9413 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 9414 | } |
| 9415 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 9416 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 9417 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 9418 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 9419 | const MotionEvent event = |
| 9420 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 9421 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 9422 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 9423 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 9424 | .build(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9425 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 9426 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9427 | mUnfocusedWindow->consumeAnyMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 9428 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 9429 | |
| 9430 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9431 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 9432 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 9433 | mUnfocusedWindow->assertNoEvents(); |
| 9434 | } |
| 9435 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9436 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 9437 | // windows that point to the same client token. |
| 9438 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 9439 | virtual void SetUp() override { |
| 9440 | InputDispatcherTest::SetUp(); |
| 9441 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9442 | std::shared_ptr<FakeApplicationHandle> application = |
| 9443 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9444 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9445 | ui::LogicalDisplayId::DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9446 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 9447 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9448 | mWindow2 = mWindow1->clone(ui::LogicalDisplayId::DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9449 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 9450 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9451 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9452 | } |
| 9453 | |
| 9454 | protected: |
| 9455 | sp<FakeWindowHandle> mWindow1; |
| 9456 | sp<FakeWindowHandle> mWindow2; |
| 9457 | |
| 9458 | // 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] | 9459 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 9460 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 9461 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9462 | } |
| 9463 | |
| 9464 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 9465 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 9466 | const std::string name = window->getName(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9467 | std::unique_ptr<MotionEvent> motionEvent = |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 9468 | window->consumeMotionEvent(WithMotionAction(expectedAction)); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9469 | ASSERT_NE(nullptr, motionEvent); |
| 9470 | ASSERT_EQ(points.size(), motionEvent->getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9471 | |
| 9472 | for (size_t i = 0; i < points.size(); i++) { |
| 9473 | float expectedX = points[i].x; |
| 9474 | float expectedY = points[i].y; |
| 9475 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9476 | EXPECT_EQ(expectedX, motionEvent->getX(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9477 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9478 | << ", got " << motionEvent->getX(i); |
| 9479 | EXPECT_EQ(expectedY, motionEvent->getY(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9480 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9481 | << ", got " << motionEvent->getY(i); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9482 | } |
| 9483 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9484 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9485 | void touchAndAssertPositions(sp<FakeWindowHandle> touchedWindow, int32_t action, |
| 9486 | const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9487 | std::vector<PointF> expectedPoints) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9488 | mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9489 | ui::LogicalDisplayId::DEFAULT, touchedPoints)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9490 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9491 | consumeMotionEvent(touchedWindow, action, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9492 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9493 | }; |
| 9494 | |
| 9495 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 9496 | // Touch Window 1 |
| 9497 | PointF touchedPoint = {10, 10}; |
| 9498 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9499 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9500 | |
| 9501 | // Release touch on Window 1 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9502 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9503 | |
| 9504 | // Touch Window 2 |
| 9505 | touchedPoint = {150, 150}; |
| 9506 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9507 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9508 | } |
| 9509 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9510 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 9511 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9512 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9513 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9514 | |
| 9515 | // Touch Window 1 |
| 9516 | PointF touchedPoint = {10, 10}; |
| 9517 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9518 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9519 | // Release touch on Window 1 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9520 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9521 | |
| 9522 | // Touch Window 2 |
| 9523 | touchedPoint = {150, 150}; |
| 9524 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9525 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 9526 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9527 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9528 | // Update the transform so rotation is set |
| 9529 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9530 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9531 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9532 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 9533 | } |
| 9534 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9535 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9536 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9537 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9538 | |
| 9539 | // Touch Window 1 |
| 9540 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 9541 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9542 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9543 | |
| 9544 | // Touch Window 2 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9545 | // Since this is part of the same touch gesture that has already been dispatched to Window 1, |
| 9546 | // the touch stream from Window 2 will be merged with the stream in Window 1. The merged stream |
| 9547 | // will continue to be dispatched through Window 1. |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9548 | touchedPoints.push_back(PointF{150, 150}); |
| 9549 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9550 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9551 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9552 | // Release Window 2 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9553 | touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9554 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9555 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9556 | // Update the transform so rotation is set for Window 2 |
| 9557 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9558 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9559 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9560 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9561 | } |
| 9562 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9563 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9564 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9565 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9566 | |
| 9567 | // Touch Window 1 |
| 9568 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 9569 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9570 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9571 | |
| 9572 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9573 | touchedPoints.push_back(PointF{150, 150}); |
| 9574 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9575 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9576 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9577 | |
| 9578 | // Move both windows |
| 9579 | touchedPoints = {{20, 20}, {175, 175}}; |
| 9580 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 9581 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 9582 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9583 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9584 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9585 | // Release Window 2 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9586 | touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9587 | expectedPoints.pop_back(); |
| 9588 | |
| 9589 | // Touch Window 2 |
| 9590 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9591 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9592 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9593 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9594 | |
| 9595 | // Move both windows |
| 9596 | touchedPoints = {{20, 20}, {175, 175}}; |
| 9597 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 9598 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 9599 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9600 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9601 | } |
| 9602 | |
| 9603 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 9604 | mWindow1->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9605 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9606 | |
| 9607 | // Touch Window 1 |
| 9608 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 9609 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9610 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9611 | |
| 9612 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 9613 | touchedPoints.push_back(PointF{150, 150}); |
| 9614 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9615 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9616 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9617 | |
| 9618 | // Move both windows |
| 9619 | touchedPoints = {{20, 20}, {175, 175}}; |
| 9620 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 9621 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 9622 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9623 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 9624 | } |
| 9625 | |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 9626 | /** |
| 9627 | * When one of the windows is slippery, the touch should not slip into the other window with the |
| 9628 | * same input channel. |
| 9629 | */ |
| 9630 | TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) { |
| 9631 | mWindow1->setSlippery(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9632 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 9633 | |
| 9634 | // Touch down in window 1 |
| 9635 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9636 | ui::LogicalDisplayId::DEFAULT, {{50, 50}})); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 9637 | consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}}); |
| 9638 | |
| 9639 | // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip. |
| 9640 | // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN |
| 9641 | // getting generated. |
| 9642 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9643 | ui::LogicalDisplayId::DEFAULT, {{150, 150}})); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 9644 | |
| 9645 | consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}}); |
| 9646 | } |
| 9647 | |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 9648 | /** |
| 9649 | * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and |
| 9650 | * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window |
| 9651 | * that the pointer is hovering over may have a different transform. |
| 9652 | */ |
| 9653 | TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) { |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9654 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 9655 | |
| 9656 | // Start hover in window 1 |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 9657 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN) |
| 9658 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 9659 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 9660 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 9661 | {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 9662 | // Move hover to window 2. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 9663 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 9664 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 9665 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 9666 | consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}}); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9667 | consumeMotionEvent(mWindow2, ACTION_HOVER_ENTER, |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 9668 | {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})}); |
| 9669 | } |
| 9670 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9671 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 9672 | virtual void SetUp() override { |
| 9673 | InputDispatcherTest::SetUp(); |
| 9674 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9675 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9676 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9677 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9678 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9679 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9680 | mWindow->setDispatchingTimeout(100ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9681 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9682 | |
| 9683 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9684 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9685 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9686 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9687 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9688 | mWindow->consumeFocusEvent(true); |
| 9689 | } |
| 9690 | |
| 9691 | virtual void TearDown() override { |
| 9692 | InputDispatcherTest::TearDown(); |
| 9693 | mWindow.clear(); |
| 9694 | } |
| 9695 | |
| 9696 | protected: |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9697 | static constexpr std::chrono::duration SPY_TIMEOUT = 200ms; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9698 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9699 | sp<FakeWindowHandle> mWindow; |
| 9700 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 9701 | |
| 9702 | void tapOnWindow() { |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 9703 | const auto touchingPointer = PointerBuilder(/*id=*/0, ToolType::FINGER) |
| 9704 | .x(WINDOW_LOCATION.x) |
| 9705 | .y(WINDOW_LOCATION.y); |
| 9706 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9707 | .pointer(touchingPointer) |
| 9708 | .build()); |
| 9709 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 9710 | .pointer(touchingPointer) |
| 9711 | .build()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9712 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9713 | |
| 9714 | sp<FakeWindowHandle> addSpyWindow() { |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9715 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", |
| 9716 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9717 | spy->setTrustedOverlay(true); |
| 9718 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9719 | spy->setSpy(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9720 | spy->setDispatchingTimeout(SPY_TIMEOUT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9721 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9722 | return spy; |
| 9723 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9724 | }; |
| 9725 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9726 | // Send a tap and respond, which should not cause an ANR. |
| 9727 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 9728 | tapOnWindow(); |
| 9729 | mWindow->consumeMotionDown(); |
| 9730 | mWindow->consumeMotionUp(); |
| 9731 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9732 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9733 | } |
| 9734 | |
| 9735 | // Send a regular key and respond, which should not cause an ANR. |
| 9736 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9737 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9738 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9739 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9740 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9741 | } |
| 9742 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 9743 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 9744 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9745 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 9746 | mWindow->consumeFocusEvent(false); |
| 9747 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9748 | InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9749 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 9750 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
| 9751 | CONSUME_TIMEOUT_EVENT_EXPECTED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9752 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9753 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 9754 | // Key will not go to window because we have no focused window. |
| 9755 | // The 'no focused window' ANR timer should start instead. |
| 9756 | |
| 9757 | // Now, the focused application goes away. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9758 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, nullptr); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 9759 | // The key should get dropped and there should be no ANR. |
| 9760 | |
| 9761 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9762 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9763 | } |
| 9764 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9765 | // 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] | 9766 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 9767 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9768 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9769 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9770 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9771 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9772 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9773 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9774 | ASSERT_TRUE(sequenceNum); |
| 9775 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9776 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9777 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9778 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 9779 | mWindow->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9780 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9781 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9782 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9783 | } |
| 9784 | |
| 9785 | // Send a key to the app and have the app not respond right away. |
| 9786 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 9787 | // Inject a key, and don't respond - expect that ANR is called. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9788 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9789 | const auto [sequenceNum, _] = mWindow->receiveEvent(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9790 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9791 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9792 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 9793 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9794 | } |
| 9795 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9796 | // We have a focused application, but no focused window |
| 9797 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9798 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9799 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9800 | mWindow->consumeFocusEvent(false); |
| 9801 | |
| 9802 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9803 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9804 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9805 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9806 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 9807 | mDispatcher->waitForIdle(); |
| 9808 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9809 | |
| 9810 | // Once a focused event arrives, we get an ANR for this application |
| 9811 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 9812 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9813 | const InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9814 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 9815 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 9816 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9817 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9818 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 9819 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9820 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9821 | } |
| 9822 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 9823 | /** |
| 9824 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 9825 | * there will not be an ANR. |
| 9826 | */ |
| 9827 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 9828 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9829 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 9830 | mWindow->consumeFocusEvent(false); |
| 9831 | |
| 9832 | KeyEvent event; |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 9833 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 9834 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 9835 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 9836 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 9837 | |
| 9838 | // Define a valid key down event that is stale (too old). |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9839 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, |
| 9840 | ui::LogicalDisplayId::INVALID, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, |
| 9841 | /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, |
| 9842 | eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 9843 | |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 9844 | const int32_t policyFlags = |
| 9845 | 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] | 9846 | |
| 9847 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9848 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 9849 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 9850 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 9851 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 9852 | << "Injection should fail because the event is stale"; |
| 9853 | |
| 9854 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9855 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9856 | mWindow->assertNoEvents(); |
| 9857 | } |
| 9858 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9859 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9860 | // Make sure that we don't notify policy twice about the same ANR. |
| 9861 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 9862 | const std::chrono::duration appTimeout = 400ms; |
| 9863 | mApplication->setDispatchingTimeout(appTimeout); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9864 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication); |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 9865 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9866 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9867 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9868 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9869 | |
| 9870 | // Once a focused event arrives, we get an ANR for this application |
| 9871 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 9872 | // injection times out (instead of failing). |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 9873 | const std::chrono::duration eventInjectionTimeout = 100ms; |
| 9874 | ASSERT_LT(eventInjectionTimeout, appTimeout); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9875 | const InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9876 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 9877 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 9878 | eventInjectionTimeout, |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 9879 | /*allowKeyRepeat=*/false); |
| 9880 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result) |
| 9881 | << "result=" << ftl::enum_string(result); |
| 9882 | // We already waited for 'eventInjectionTimeout`, because the countdown started when the event |
| 9883 | // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait. |
| 9884 | std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout; |
| 9885 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9886 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 9887 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9888 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 9889 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9890 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9891 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 9892 | // '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] | 9893 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9894 | } |
| 9895 | |
| 9896 | // We have a focused application, but no focused window |
| 9897 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9898 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9899 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9900 | mWindow->consumeFocusEvent(false); |
| 9901 | |
| 9902 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9903 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9904 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 9905 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 9906 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9907 | |
| 9908 | // Future focused events get dropped right away |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9909 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9910 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9911 | mWindow->assertNoEvents(); |
| 9912 | } |
| 9913 | |
| 9914 | /** |
| 9915 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 9916 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 9917 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 9918 | * the ANR mechanism should still work. |
| 9919 | * |
| 9920 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 9921 | * DOWN event, while not responding on the second one. |
| 9922 | */ |
| 9923 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 9924 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9925 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9926 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9927 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 9928 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9929 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9930 | |
| 9931 | // Now send ACTION_UP, with identical timestamp |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9932 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9933 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9934 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 9935 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9936 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9937 | |
| 9938 | // 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] | 9939 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9940 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9941 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9942 | } |
| 9943 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9944 | // A spy window can receive an ANR |
| 9945 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 9946 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 9947 | |
| 9948 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9949 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 9950 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9951 | mWindow->consumeMotionDown(); |
| 9952 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9953 | const auto [sequenceNum, _] = spy->receiveEvent(); // ACTION_DOWN |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9954 | ASSERT_TRUE(sequenceNum); |
| 9955 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9956 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9957 | |
| 9958 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 9959 | spy->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9960 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9961 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9962 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9963 | } |
| 9964 | |
| 9965 | // 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] | 9966 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9967 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 9968 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9969 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9970 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9971 | injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT)); |
| 9972 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
| 9973 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9974 | injectKeyUp(*mDispatcher, ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9975 | |
| 9976 | // Stuck on the ACTION_UP |
| 9977 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9978 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9979 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9980 | // 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] | 9981 | tapOnWindow(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9982 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 9983 | spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9984 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9985 | mWindow->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); // still the previous motion |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9986 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9987 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9988 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9989 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9990 | } |
| 9991 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9992 | // 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] | 9993 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 9994 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 9995 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9996 | |
| 9997 | tapOnWindow(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 9998 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 9999 | spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10000 | |
| 10001 | mWindow->consumeMotionDown(); |
| 10002 | // Stuck on the ACTION_UP |
| 10003 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10004 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10005 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10006 | // 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] | 10007 | tapOnWindow(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10008 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 10009 | spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10010 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10011 | mWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); // still the previous motion |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10012 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10013 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10014 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10015 | spy->assertNoEvents(); |
| 10016 | } |
| 10017 | |
| 10018 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10019 | mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10020 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10021 | FakeMonitorReceiver monitor = |
| 10022 | FakeMonitorReceiver(*mDispatcher, "M_1", ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10023 | |
| 10024 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10025 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10026 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10027 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10028 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10029 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 10030 | ASSERT_TRUE(consumeSeq); |
| 10031 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10032 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(), |
| 10033 | MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10034 | |
| 10035 | monitor.finishEvent(*consumeSeq); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10036 | monitor.consumeMotionCancel(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10037 | |
| 10038 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10039 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10040 | } |
| 10041 | |
| 10042 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 10043 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 10044 | // get an ANR again. |
| 10045 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 10046 | // 2. consume all pending events (= queue becomes healthy again) |
| 10047 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 10048 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 10049 | tapOnWindow(); |
| 10050 | |
| 10051 | mWindow->consumeMotionDown(); |
| 10052 | // Block on ACTION_UP |
| 10053 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10054 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10055 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 10056 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10057 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10058 | mWindow->assertNoEvents(); |
| 10059 | |
| 10060 | tapOnWindow(); |
| 10061 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10062 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10063 | mWindow->consumeMotionUp(); |
| 10064 | |
| 10065 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10066 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10067 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10068 | mWindow->assertNoEvents(); |
| 10069 | } |
| 10070 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10071 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 10072 | // it. |
| 10073 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10074 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10075 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10076 | ui::LogicalDisplayId::DEFAULT, WINDOW_LOCATION)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10077 | |
| 10078 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10079 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10080 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10081 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 10082 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 10083 | // 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] | 10084 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 10085 | mWindow->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10086 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10087 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10088 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10089 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10090 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10091 | } |
| 10092 | |
| 10093 | /** |
| 10094 | * 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] | 10095 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10096 | */ |
| 10097 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 10098 | // The timeouts in this test are established by relying on the fact that the "key waiting for |
| 10099 | // events timeout" is equal to 500ms. |
| 10100 | ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10101 | 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] | 10102 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10103 | |
| 10104 | tapOnWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10105 | const auto& [downSequenceNum, downEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10106 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10107 | const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10108 | ASSERT_TRUE(upSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10109 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 10110 | // Don't finish the events yet, and send a key |
| 10111 | mDispatcher->notifyKey( |
| 10112 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 10113 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 10114 | .build()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10115 | // Key will not be sent to the window, yet, because the window is still processing events |
| 10116 | // 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] | 10117 | // 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] | 10118 | mWindow->assertNoEvents(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10119 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 10120 | std::this_thread::sleep_for(400ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10121 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 10122 | // 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] | 10123 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10124 | mWindow->finishEvent(*downSequenceNum); |
| 10125 | mWindow->finishEvent(*upSequenceNum); |
| 10126 | } |
| 10127 | |
| 10128 | /** |
| 10129 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 10130 | * not go to the focused window until the motion is processed. |
| 10131 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 10132 | * focused window right away. |
| 10133 | */ |
| 10134 | TEST_F(InputDispatcherSingleWindowAnr, |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 10135 | PendingKey_IsDeliveredWhileMotionIsProcessingAndNewTouchComesIn) { |
| 10136 | // The timeouts in this test are established by relying on the fact that the "key waiting for |
| 10137 | // events timeout" is equal to 500ms. |
| 10138 | ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10139 | 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] | 10140 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10141 | |
| 10142 | tapOnWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10143 | const auto& [downSequenceNum, _] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10144 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10145 | const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10146 | ASSERT_TRUE(upSequenceNum); |
| 10147 | // Don't finish the events yet, and send a key |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 10148 | mDispatcher->notifyKey( |
| 10149 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 10150 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 10151 | .build()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10152 | // 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] | 10153 | mWindow->assertNoEvents(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10154 | |
| 10155 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 10156 | tapOnWindow(); |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 10157 | // Now that we tapped, we should receive the key immediately. |
| 10158 | // Since there's still room for slowness, we use 200ms, which is much less than |
| 10159 | // the "key waiting for events' timeout of 500ms minus the already waited 100ms duration. |
| 10160 | std::unique_ptr<InputEvent> keyEvent = mWindow->consume(200ms); |
| 10161 | ASSERT_NE(nullptr, keyEvent); |
| 10162 | ASSERT_EQ(InputEventType::KEY, keyEvent->getType()); |
| 10163 | ASSERT_THAT(static_cast<KeyEvent&>(*keyEvent), WithKeyAction(AKEY_EVENT_ACTION_DOWN)); |
| 10164 | // it doesn't matter that we haven't ack'd the other events yet. We can finish events in any |
| 10165 | // order. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10166 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 10167 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 10168 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 10169 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10170 | mWindow->assertNoEvents(); |
| 10171 | } |
| 10172 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 10173 | /** |
| 10174 | * Send an event to the app and have the app not respond right away. |
| 10175 | * When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 10176 | * So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 10177 | * At some point, the window becomes responsive again. |
| 10178 | * Ensure that subsequent events get dropped, and the next gesture is delivered. |
| 10179 | */ |
| 10180 | TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) { |
| 10181 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10182 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 10183 | .build()); |
| 10184 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10185 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 10186 | ASSERT_TRUE(sequenceNum); |
| 10187 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 10188 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 10189 | |
| 10190 | mWindow->finishEvent(*sequenceNum); |
| 10191 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 10192 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10193 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
| 10194 | |
| 10195 | // Now that the window is responsive, let's continue the gesture. |
| 10196 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 10197 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 10198 | .build()); |
| 10199 | |
| 10200 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10201 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 10202 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 10203 | .build()); |
| 10204 | |
| 10205 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 10206 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 10207 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 10208 | .build()); |
| 10209 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 10210 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 10211 | .build()); |
| 10212 | // We already canceled this pointer, so the window shouldn't get any new events. |
| 10213 | mWindow->assertNoEvents(); |
| 10214 | |
| 10215 | // Start another one. |
| 10216 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10217 | .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15)) |
| 10218 | .build()); |
| 10219 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 10220 | } |
| 10221 | |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10222 | // Send an event to the app and have the app not respond right away. Then remove the app window. |
| 10223 | // When the window is removed, the dispatcher will cancel the events for that window. |
| 10224 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 10225 | TEST_F(InputDispatcherSingleWindowAnr, AnrAfterWindowRemoval) { |
| 10226 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10227 | AINPUT_SOURCE_TOUCHSCREEN, |
| 10228 | ui::LogicalDisplayId::DEFAULT, {WINDOW_LOCATION})); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10229 | |
| 10230 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
| 10231 | ASSERT_TRUE(sequenceNum); |
| 10232 | |
| 10233 | // Remove the window, but the input channel should remain alive. |
| 10234 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 10235 | |
| 10236 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 10237 | // Since the window was removed, Dispatcher does not know the PID associated with the window |
| 10238 | // anymore, so the policy is notified without the PID. |
| 10239 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken(), |
| 10240 | /*pid=*/std::nullopt); |
| 10241 | |
| 10242 | mWindow->finishEvent(*sequenceNum); |
| 10243 | // The cancellation was generated when the window was removed, along with the focus event. |
| 10244 | mWindow->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10245 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10246 | mWindow->consumeFocusEvent(false); |
| 10247 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10248 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt); |
| 10249 | } |
| 10250 | |
| 10251 | // Send an event to the app and have the app not respond right away. Wait for the policy to be |
| 10252 | // notified of the unresponsive window, then remove the app window. |
| 10253 | TEST_F(InputDispatcherSingleWindowAnr, AnrFollowedByWindowRemoval) { |
| 10254 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10255 | AINPUT_SOURCE_TOUCHSCREEN, |
| 10256 | ui::LogicalDisplayId::DEFAULT, {WINDOW_LOCATION})); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10257 | |
| 10258 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
| 10259 | ASSERT_TRUE(sequenceNum); |
| 10260 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 10261 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 10262 | |
| 10263 | // Remove the window, but the input channel should remain alive. |
| 10264 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 10265 | |
| 10266 | mWindow->finishEvent(*sequenceNum); |
| 10267 | // The cancellation was generated during the ANR, and the window lost focus when it was removed. |
| 10268 | mWindow->consumeMotionEvent( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10269 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ui::LogicalDisplayId::DEFAULT))); |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 10270 | mWindow->consumeFocusEvent(false); |
| 10271 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10272 | // Since the window was removed, Dispatcher does not know the PID associated with the window |
| 10273 | // becoming responsive, so the policy is notified without the PID. |
| 10274 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt); |
| 10275 | } |
| 10276 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10277 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 10278 | virtual void SetUp() override { |
| 10279 | InputDispatcherTest::SetUp(); |
| 10280 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 10281 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 10282 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10283 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10284 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10285 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10286 | // 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] | 10287 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10288 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10289 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10290 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 10291 | mFocusedWindow->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10292 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10293 | |
| 10294 | // Set focused application. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10295 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 10296 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10297 | |
| 10298 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10299 | mDispatcher->onWindowInfosChanged( |
| 10300 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10301 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10302 | mFocusedWindow->consumeFocusEvent(true); |
| 10303 | } |
| 10304 | |
| 10305 | virtual void TearDown() override { |
| 10306 | InputDispatcherTest::TearDown(); |
| 10307 | |
| 10308 | mUnfocusedWindow.clear(); |
| 10309 | mFocusedWindow.clear(); |
| 10310 | } |
| 10311 | |
| 10312 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 10313 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10314 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 10315 | sp<FakeWindowHandle> mFocusedWindow; |
| 10316 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 10317 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 10318 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 10319 | |
| 10320 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 10321 | |
| 10322 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 10323 | |
| 10324 | private: |
| 10325 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10326 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10327 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10328 | ui::LogicalDisplayId::DEFAULT, location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10329 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10330 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10331 | ui::LogicalDisplayId::DEFAULT, location)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10332 | } |
| 10333 | }; |
| 10334 | |
| 10335 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 10336 | // should be ANR'd first. |
| 10337 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10338 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 10339 | injectMotionEvent(*mDispatcher, |
| 10340 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10341 | AINPUT_SOURCE_TOUCHSCREEN) |
| 10342 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 10343 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 10344 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 10345 | .build())); |
| 10346 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10347 | injectMotionEvent(*mDispatcher, |
| 10348 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 10349 | AINPUT_SOURCE_TOUCHSCREEN) |
| 10350 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 10351 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 10352 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 10353 | .build())); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10354 | mFocusedWindow->consumeMotionDown(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 10355 | mFocusedWindow->consumeMotionUp(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10356 | mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10357 | // We consumed all events, so no ANR |
| 10358 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10359 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 10360 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10361 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 10362 | injectMotionEvent(*mDispatcher, |
| 10363 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10364 | AINPUT_SOURCE_TOUCHSCREEN) |
| 10365 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 10366 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 10367 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 10368 | .build())); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10369 | const auto [unfocusedSequenceNum, _] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10370 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10371 | |
| 10372 | const std::chrono::duration timeout = |
| 10373 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10374 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 10375 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10376 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10377 | mFocusedWindow->consumeMotionDown(); |
| 10378 | // This cancel is generated because the connection was unresponsive |
| 10379 | mFocusedWindow->consumeMotionCancel(); |
| 10380 | mFocusedWindow->assertNoEvents(); |
| 10381 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10382 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10383 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 10384 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10385 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10386 | } |
| 10387 | |
| 10388 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 10389 | // it doesn't matter which order they should have ANR. |
| 10390 | // But we should receive ANR for both. |
| 10391 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 10392 | // Set the timeout for unfocused window to match the focused window |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 10393 | mUnfocusedWindow->setDispatchingTimeout( |
| 10394 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10395 | mDispatcher->onWindowInfosChanged( |
| 10396 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10397 | |
| 10398 | tapOnFocusedWindow(); |
| 10399 | // 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] | 10400 | // 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] | 10401 | std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken( |
| 10402 | mFocusedWindow->getDispatchingTimeout( |
| 10403 | DISPATCHING_TIMEOUT)), |
| 10404 | mFakePolicy->getUnresponsiveWindowToken(0ms)}; |
| 10405 | |
| 10406 | ASSERT_THAT(anrConnectionTokens, |
| 10407 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 10408 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10409 | |
| 10410 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10411 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10412 | |
| 10413 | mFocusedWindow->consumeMotionDown(); |
| 10414 | mFocusedWindow->consumeMotionUp(); |
| 10415 | mUnfocusedWindow->consumeMotionOutside(); |
| 10416 | |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 10417 | std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(), |
| 10418 | mFakePolicy->getResponsiveWindowToken()}; |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10419 | |
| 10420 | // Both applications should be marked as responsive, in any order |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 10421 | ASSERT_THAT(responsiveTokens, |
| 10422 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 10423 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10424 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10425 | } |
| 10426 | |
| 10427 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 10428 | // We should also log an error to account for the dropped event (not tested here). |
| 10429 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 10430 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 10431 | tapOnFocusedWindow(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10432 | mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10433 | // Receive the events, but don't respond |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10434 | const auto [downEventSequenceNum, downEvent] = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10435 | ASSERT_TRUE(downEventSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10436 | const auto [upEventSequenceNum, upEvent] = mFocusedWindow->receiveEvent(); // ACTION_UP |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10437 | ASSERT_TRUE(upEventSequenceNum); |
| 10438 | const std::chrono::duration timeout = |
| 10439 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10440 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10441 | |
| 10442 | // Tap once again |
| 10443 | // 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] | 10444 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10445 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10446 | ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10447 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10448 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10449 | FOCUSED_WINDOW_LOCATION)); |
| 10450 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 10451 | // valid touch target |
| 10452 | mUnfocusedWindow->assertNoEvents(); |
| 10453 | |
| 10454 | // Consume the first tap |
| 10455 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 10456 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 10457 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10458 | // The second tap did not go to the focused window |
| 10459 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10460 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10461 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 10462 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10463 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 10464 | } |
| 10465 | |
| 10466 | // If you tap outside of all windows, there will not be ANR |
| 10467 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10468 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10469 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10470 | ui::LogicalDisplayId::DEFAULT, LOCATION_OUTSIDE_ALL_WINDOWS)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10471 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10472 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 10473 | } |
| 10474 | |
| 10475 | // Since the focused window is paused, tapping on it should not produce any events |
| 10476 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 10477 | mFocusedWindow->setPaused(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10478 | mDispatcher->onWindowInfosChanged( |
| 10479 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10480 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10481 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10482 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 10483 | ui::LogicalDisplayId::DEFAULT, FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10484 | |
| 10485 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 10486 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10487 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 10488 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 10489 | |
| 10490 | mFocusedWindow->assertNoEvents(); |
| 10491 | mUnfocusedWindow->assertNoEvents(); |
| 10492 | } |
| 10493 | |
| 10494 | /** |
| 10495 | * 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] | 10496 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10497 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 10498 | * |
| 10499 | * Warning!!! |
| 10500 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 10501 | * and the injection timeout that we specify when injecting the key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10502 | * We must have the injection timeout (100ms) be smaller than |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10503 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 10504 | * |
| 10505 | * If that value changes, this test should also change. |
| 10506 | */ |
| 10507 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 10508 | // Set a long ANR timeout to prevent it from triggering |
| 10509 | mFocusedWindow->setDispatchingTimeout(2s); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10510 | mDispatcher->onWindowInfosChanged( |
| 10511 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10512 | |
| 10513 | tapOnUnfocusedWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10514 | const auto [downSequenceNum, downEvent] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10515 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10516 | const auto [upSequenceNum, upEvent] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10517 | ASSERT_TRUE(upSequenceNum); |
| 10518 | // Don't finish the events yet, and send a key |
| 10519 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 10520 | // window even if motions are still being processed. |
| 10521 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10522 | InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10523 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 10524 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 10525 | /*injectionTimeout=*/100ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10526 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10527 | // 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] | 10528 | // and the key remains pending, waiting for the touch events to be processed. |
| 10529 | // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED |
| 10530 | // under the hood. |
| 10531 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 10532 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10533 | |
| 10534 | // 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] | 10535 | mFocusedWindow->setFocusable(false); |
| 10536 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10537 | mDispatcher->onWindowInfosChanged( |
| 10538 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10539 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10540 | |
| 10541 | // Focus events should precede the key events |
| 10542 | mUnfocusedWindow->consumeFocusEvent(true); |
| 10543 | mFocusedWindow->consumeFocusEvent(false); |
| 10544 | |
| 10545 | // Finish the tap events, which should unblock dispatcher |
| 10546 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 10547 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 10548 | |
| 10549 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 10550 | // can finally go to the newly focused "mUnfocusedWindow". |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10551 | mUnfocusedWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10552 | mFocusedWindow->assertNoEvents(); |
| 10553 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10554 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10555 | } |
| 10556 | |
| 10557 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 10558 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 10559 | // The other window should not be affected by that. |
| 10560 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 10561 | // Touch Window 1 |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10562 | mDispatcher->notifyMotion( |
| 10563 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 10564 | ui::LogicalDisplayId::DEFAULT, {FOCUSED_WINDOW_LOCATION})); |
| 10565 | mUnfocusedWindow->consumeMotionOutside(ui::LogicalDisplayId::DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10566 | |
| 10567 | // Touch Window 2 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10568 | mDispatcher->notifyMotion( |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10569 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 10570 | ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10571 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10572 | |
| 10573 | const std::chrono::duration timeout = |
| 10574 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10575 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10576 | |
| 10577 | mUnfocusedWindow->consumeMotionDown(); |
| 10578 | mFocusedWindow->consumeMotionDown(); |
| 10579 | // Focused window may or may not receive ACTION_MOVE |
| 10580 | // But it should definitely receive ACTION_CANCEL due to the ANR |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 10581 | const auto [moveOrCancelSequenceNum, event] = mFocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10582 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 10583 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 10584 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 10585 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10586 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 10587 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 10588 | mFocusedWindow->consumeMotionCancel(); |
| 10589 | } else { |
| 10590 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 10591 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10592 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 10593 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 10594 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10595 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10596 | mUnfocusedWindow->assertNoEvents(); |
| 10597 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 10598 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 10599 | } |
| 10600 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10601 | /** |
| 10602 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 10603 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 10604 | * |
| 10605 | * If the user touches another application during this time, the key should be dropped. |
| 10606 | * Next, if a new focused window comes in, without toggling the focused application, |
| 10607 | * then no ANR should occur. |
| 10608 | * |
| 10609 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 10610 | * but in some cases the policy may not update the focused application. |
| 10611 | */ |
| 10612 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 10613 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 10614 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | c033dfb | 2023-10-03 10:45:16 -0700 | [diff] [blame] | 10615 | focusedApplication->setDispatchingTimeout(300ms); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10616 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, focusedApplication); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10617 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 10618 | mFocusedWindow->setFocusable(false); |
| 10619 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10620 | mDispatcher->onWindowInfosChanged( |
| 10621 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10622 | mFocusedWindow->consumeFocusEvent(false); |
| 10623 | |
| 10624 | // Send a key. The ANR timer should start because there is no focused window. |
| 10625 | // 'focusedApplication' will get blamed if this timer completes. |
| 10626 | // 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] | 10627 | InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10628 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 10629 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 10630 | /*injectionTimeout=*/100ms, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10631 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10632 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10633 | |
| 10634 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 10635 | // then the injected touches won't cause the focused event to get dropped. |
| 10636 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 10637 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 10638 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 10639 | // For this test, it means that the key would get delivered to the window once it becomes |
| 10640 | // focused. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10641 | std::this_thread::sleep_for(100ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10642 | |
| 10643 | // Touch unfocused window. This should force the pending key to get dropped. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10644 | mDispatcher->notifyMotion( |
| 10645 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 10646 | ui::LogicalDisplayId::DEFAULT, {UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10647 | |
| 10648 | // We do not consume the motion right away, because that would require dispatcher to first |
| 10649 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 10650 | // Set the focused window first. |
| 10651 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10652 | mDispatcher->onWindowInfosChanged( |
| 10653 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 10654 | setFocusedWindow(mFocusedWindow); |
| 10655 | mFocusedWindow->consumeFocusEvent(true); |
| 10656 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 10657 | // to another application. This could be a bug / behaviour in the policy. |
| 10658 | |
| 10659 | mUnfocusedWindow->consumeMotionDown(); |
| 10660 | |
| 10661 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10662 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 10663 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 10664 | } |
| 10665 | |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 10666 | /** |
| 10667 | * If we are pruning input queue, we should never drop pointer events. Otherwise, we risk having |
| 10668 | * an inconsistent event stream inside the dispatcher. In this test, we make sure that the |
| 10669 | * dispatcher doesn't prune pointer events incorrectly. |
| 10670 | * |
| 10671 | * This test reproduces a crash in InputDispatcher. |
| 10672 | * To reproduce the crash, we need to simulate the conditions for "pruning input queue" to occur. |
| 10673 | * |
| 10674 | * Keep the currently focused application (mApplication), and have no focused window. |
| 10675 | * We set up two additional windows: |
| 10676 | * 1) The navigation bar window. This simulates the system "NavigationBar", which is used in the |
| 10677 | * 3-button navigation mode. This window injects a BACK button when it's touched. 2) The application |
| 10678 | * window. This window is not focusable, but is touchable. |
| 10679 | * |
| 10680 | * We first touch the navigation bar, which causes it to inject a key. Since there's no focused |
| 10681 | * window, the dispatcher doesn't process this key, and all other events inside dispatcher are now |
| 10682 | * blocked. The dispatcher is waiting for 'mApplication' to add a focused window. |
| 10683 | * |
| 10684 | * Now, we touch "Another window". This window is owned by a different application than |
| 10685 | * 'mApplication'. This causes the dispatcher to stop waiting for 'mApplication' to add a focused |
| 10686 | * window. Now, the "pruning input queue" behaviour should kick in, and the dispatcher should start |
| 10687 | * dropping the events from its queue. Ensure that no crash occurs. |
| 10688 | * |
| 10689 | * In this test, we are setting long timeouts to prevent ANRs and events dropped due to being stale. |
| 10690 | * This does not affect the test running time. |
| 10691 | */ |
| 10692 | TEST_F(InputDispatcherMultiWindowAnr, PruningInputQueueShouldNotDropPointerEvents) { |
| 10693 | std::shared_ptr<FakeApplicationHandle> systemUiApplication = |
| 10694 | std::make_shared<FakeApplicationHandle>(); |
| 10695 | systemUiApplication->setDispatchingTimeout(3000ms); |
| 10696 | mFakePolicy->setStaleEventTimeout(3000ms); |
| 10697 | sp<FakeWindowHandle> navigationBar = |
| 10698 | sp<FakeWindowHandle>::make(systemUiApplication, mDispatcher, "NavigationBar", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10699 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 10700 | navigationBar->setFocusable(false); |
| 10701 | navigationBar->setWatchOutsideTouch(true); |
| 10702 | navigationBar->setFrame(Rect(0, 0, 100, 100)); |
| 10703 | |
| 10704 | mApplication->setDispatchingTimeout(3000ms); |
| 10705 | // '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] | 10706 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApplication); |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 10707 | |
| 10708 | std::shared_ptr<FakeApplicationHandle> anotherApplication = |
| 10709 | std::make_shared<FakeApplicationHandle>(); |
| 10710 | sp<FakeWindowHandle> appWindow = |
| 10711 | sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Another window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10712 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 10713 | appWindow->setFocusable(false); |
| 10714 | appWindow->setFrame(Rect(100, 100, 200, 200)); |
| 10715 | |
| 10716 | mDispatcher->onWindowInfosChanged( |
| 10717 | {{*navigationBar->getInfo(), *appWindow->getInfo()}, {}, 0, 0}); |
| 10718 | // 'mFocusedWindow' is no longer in the dispatcher window list, and therefore loses focus |
| 10719 | mFocusedWindow->consumeFocusEvent(false); |
| 10720 | |
| 10721 | // Touch down the navigation bar. It consumes the touch and injects a key into the dispatcher |
| 10722 | // in response. |
| 10723 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10724 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 10725 | .build()); |
| 10726 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 10727 | |
| 10728 | // Key will not be sent anywhere because we have no focused window. It will remain pending. |
| 10729 | // Pretend we are injecting KEYCODE_BACK, but it doesn't actually matter what key it is. |
| 10730 | InputEventInjectionResult result = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10731 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 10732 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 10733 | /*injectionTimeout=*/100ms, |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 10734 | /*allowKeyRepeat=*/false); |
| 10735 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
| 10736 | |
| 10737 | // Finish the gesture - lift up finger and inject ACTION_UP key event |
| 10738 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 10739 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 10740 | .build()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10741 | result = injectKey(*mDispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, |
| 10742 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE, |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 10743 | /*injectionTimeout=*/100ms, |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 10744 | /*allowKeyRepeat=*/false); |
| 10745 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
| 10746 | // The key that was injected is blocking the dispatcher, so the navigation bar shouldn't be |
| 10747 | // getting any events yet. |
| 10748 | navigationBar->assertNoEvents(); |
| 10749 | |
| 10750 | // Now touch "Another window". This touch is going to a different application than the one we |
| 10751 | // are waiting for (which is 'mApplication'). |
| 10752 | // This should cause the dispatcher to drop the pending focus-dispatched events (like the key |
| 10753 | // trying to be injected) and to continue processing the rest of the events in the original |
| 10754 | // order. |
| 10755 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10756 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 10757 | .build()); |
| 10758 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 10759 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_OUTSIDE)); |
| 10760 | appWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 10761 | |
| 10762 | appWindow->assertNoEvents(); |
| 10763 | navigationBar->assertNoEvents(); |
| 10764 | } |
| 10765 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10766 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 10767 | // that has feature NO_INPUT_CHANNEL. |
| 10768 | // Layout: |
| 10769 | // Top (closest to user) |
| 10770 | // mNoInputWindow (above all windows) |
| 10771 | // mBottomWindow |
| 10772 | // Bottom (furthest from user) |
| 10773 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 10774 | virtual void SetUp() override { |
| 10775 | InputDispatcherTest::SetUp(); |
| 10776 | |
| 10777 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10778 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 10779 | "Window without input channel", |
| 10780 | ui::LogicalDisplayId::DEFAULT, |
| 10781 | /*createInputChannel=*/false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10782 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10783 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 10784 | // It's perfectly valid for this window to not have an associated input channel |
| 10785 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10786 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10787 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10788 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 10789 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10790 | mDispatcher->onWindowInfosChanged( |
| 10791 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10792 | } |
| 10793 | |
| 10794 | protected: |
| 10795 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 10796 | sp<FakeWindowHandle> mNoInputWindow; |
| 10797 | sp<FakeWindowHandle> mBottomWindow; |
| 10798 | }; |
| 10799 | |
| 10800 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 10801 | PointF touchedPoint = {10, 10}; |
| 10802 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10803 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10804 | AINPUT_SOURCE_TOUCHSCREEN, |
| 10805 | ui::LogicalDisplayId::DEFAULT, {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10806 | |
| 10807 | mNoInputWindow->assertNoEvents(); |
| 10808 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 10809 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 10810 | // and therefore should prevent mBottomWindow from receiving touches |
| 10811 | mBottomWindow->assertNoEvents(); |
| 10812 | } |
| 10813 | |
| 10814 | /** |
| 10815 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 10816 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 10817 | */ |
| 10818 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 10819 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10820 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 10821 | "Window with input channel and NO_INPUT_CHANNEL", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10822 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10823 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10824 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10825 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10826 | mDispatcher->onWindowInfosChanged( |
| 10827 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10828 | |
| 10829 | PointF touchedPoint = {10, 10}; |
| 10830 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10831 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10832 | AINPUT_SOURCE_TOUCHSCREEN, |
| 10833 | ui::LogicalDisplayId::DEFAULT, {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 10834 | |
| 10835 | mNoInputWindow->assertNoEvents(); |
| 10836 | mBottomWindow->assertNoEvents(); |
| 10837 | } |
| 10838 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10839 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 10840 | protected: |
| 10841 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 10842 | sp<FakeWindowHandle> mWindow; |
| 10843 | sp<FakeWindowHandle> mMirror; |
| 10844 | |
| 10845 | virtual void SetUp() override { |
| 10846 | InputDispatcherTest::SetUp(); |
| 10847 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10848 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", |
| 10849 | ui::LogicalDisplayId::DEFAULT); |
| 10850 | mMirror = mWindow->clone(ui::LogicalDisplayId::DEFAULT); |
| 10851 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10852 | mWindow->setFocusable(true); |
| 10853 | mMirror->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10854 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10855 | } |
| 10856 | }; |
| 10857 | |
| 10858 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 10859 | // Request focus on a mirrored window |
| 10860 | setFocusedWindow(mMirror); |
| 10861 | |
| 10862 | // window gets focused |
| 10863 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10864 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10865 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10866 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10867 | } |
| 10868 | |
| 10869 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 10870 | // focusable. |
| 10871 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 10872 | setFocusedWindow(mMirror); |
| 10873 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 10874 | // window gets focused because it is above the mirror |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10875 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10876 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10877 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10878 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10879 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10880 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10881 | mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10882 | |
| 10883 | mMirror->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10884 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10885 | |
| 10886 | // window loses focus since one of the windows associated with the token in not focusable |
| 10887 | mWindow->consumeFocusEvent(false); |
| 10888 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10889 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10890 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10891 | mWindow->assertNoEvents(); |
| 10892 | } |
| 10893 | |
| 10894 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 10895 | // invisible. |
| 10896 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 10897 | setFocusedWindow(mMirror); |
| 10898 | |
| 10899 | // window gets focused |
| 10900 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10901 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10902 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10903 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10904 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10905 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10906 | mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10907 | |
| 10908 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10909 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10910 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10911 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10912 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10913 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10914 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10915 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10916 | mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10917 | |
| 10918 | mWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10919 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10920 | |
| 10921 | // window loses focus only after all windows associated with the token become invisible. |
| 10922 | mWindow->consumeFocusEvent(false); |
| 10923 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10924 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10925 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10926 | mWindow->assertNoEvents(); |
| 10927 | } |
| 10928 | |
| 10929 | // A focused & mirrored window remains focused until both windows are removed. |
| 10930 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 10931 | setFocusedWindow(mMirror); |
| 10932 | |
| 10933 | // window gets focused |
| 10934 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10935 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10936 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10937 | mWindow->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10938 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10939 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10940 | mWindow->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10941 | |
| 10942 | // single window is removed but the window token remains focused |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10943 | mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10944 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10945 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10946 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10947 | mMirror->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10948 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10949 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10950 | mMirror->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10951 | |
| 10952 | // Both windows are removed |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10953 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10954 | mWindow->consumeFocusEvent(false); |
| 10955 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10956 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10957 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10958 | mWindow->assertNoEvents(); |
| 10959 | } |
| 10960 | |
| 10961 | // Focus request can be pending until one window becomes visible. |
| 10962 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 10963 | // Request focus on an invisible mirror. |
| 10964 | mWindow->setVisible(false); |
| 10965 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10966 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10967 | setFocusedWindow(mMirror); |
| 10968 | |
| 10969 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 10970 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10971 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10972 | ui::LogicalDisplayId::DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10973 | |
| 10974 | mMirror->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10975 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10976 | |
| 10977 | // window gets focused |
| 10978 | mWindow->consumeFocusEvent(true); |
| 10979 | // window gets the pending key event |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10980 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 10981 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10982 | |
| 10983 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 10984 | protected: |
| 10985 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 10986 | sp<FakeWindowHandle> mWindow; |
| 10987 | sp<FakeWindowHandle> mSecondWindow; |
| 10988 | |
| 10989 | void SetUp() override { |
| 10990 | InputDispatcherTest::SetUp(); |
| 10991 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10992 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", |
| 10993 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10994 | mWindow->setFocusable(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10995 | mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", |
| 10996 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 10997 | mSecondWindow->setFocusable(true); |
| 10998 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 10999 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11000 | mDispatcher->onWindowInfosChanged( |
| 11001 | {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11002 | |
| 11003 | setFocusedWindow(mWindow); |
| 11004 | mWindow->consumeFocusEvent(true); |
| 11005 | } |
| 11006 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11007 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11008 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11009 | } |
| 11010 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11011 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 11012 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11013 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11014 | auto request = mFakePolicy->assertSetPointerCaptureCalled(window, enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11015 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11016 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11017 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11018 | } |
| 11019 | }; |
| 11020 | |
| 11021 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 11022 | // Ensure that capture cannot be obtained for unfocused windows. |
| 11023 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 11024 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 11025 | mSecondWindow->assertNoEvents(); |
| 11026 | |
| 11027 | // Ensure that capture can be enabled from the focus window. |
| 11028 | requestAndVerifyPointerCapture(mWindow, true); |
| 11029 | |
| 11030 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 11031 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 11032 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 11033 | |
| 11034 | // Ensure that capture can be disabled from the window with capture. |
| 11035 | requestAndVerifyPointerCapture(mWindow, false); |
| 11036 | } |
| 11037 | |
| 11038 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11039 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11040 | |
| 11041 | setFocusedWindow(mSecondWindow); |
| 11042 | |
| 11043 | // Ensure that the capture disabled event was sent first. |
| 11044 | mWindow->consumeCaptureEvent(false); |
| 11045 | mWindow->consumeFocusEvent(false); |
| 11046 | mSecondWindow->consumeFocusEvent(true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11047 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11048 | |
| 11049 | // 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] | 11050 | notifyPointerCaptureChanged({}); |
| 11051 | notifyPointerCaptureChanged(request); |
| 11052 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11053 | mWindow->assertNoEvents(); |
| 11054 | mSecondWindow->assertNoEvents(); |
| 11055 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 11056 | } |
| 11057 | |
| 11058 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11059 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11060 | |
| 11061 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11062 | notifyPointerCaptureChanged({}); |
| 11063 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11064 | |
| 11065 | // Ensure that Pointer Capture is disabled. |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11066 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 11067 | mWindow->consumeCaptureEvent(false); |
| 11068 | mWindow->assertNoEvents(); |
| 11069 | } |
| 11070 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 11071 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 11072 | requestAndVerifyPointerCapture(mWindow, true); |
| 11073 | |
| 11074 | // The first window loses focus. |
| 11075 | setFocusedWindow(mSecondWindow); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11076 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 11077 | mWindow->consumeCaptureEvent(false); |
| 11078 | |
| 11079 | // Request Pointer Capture from the second window before the notification from InputReader |
| 11080 | // arrives. |
| 11081 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11082 | auto request = mFakePolicy->assertSetPointerCaptureCalled(mSecondWindow, true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 11083 | |
| 11084 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11085 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 11086 | |
| 11087 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11088 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 11089 | |
| 11090 | mSecondWindow->consumeFocusEvent(true); |
| 11091 | mSecondWindow->consumeCaptureEvent(true); |
| 11092 | } |
| 11093 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11094 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 11095 | // App repeatedly enables and disables capture. |
| 11096 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11097 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11098 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11099 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11100 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11101 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 11102 | |
| 11103 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 11104 | // first request is now stale, this should do nothing. |
| 11105 | notifyPointerCaptureChanged(firstRequest); |
| 11106 | mWindow->assertNoEvents(); |
| 11107 | |
| 11108 | // InputReader notifies that the second request was enabled. |
| 11109 | notifyPointerCaptureChanged(secondRequest); |
| 11110 | mWindow->consumeCaptureEvent(true); |
| 11111 | } |
| 11112 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 11113 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 11114 | requestAndVerifyPointerCapture(mWindow, true); |
| 11115 | |
| 11116 | // App toggles pointer capture off and on. |
| 11117 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11118 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 11119 | |
| 11120 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11121 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 11122 | |
| 11123 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 11124 | // preceding "disable" request. |
| 11125 | notifyPointerCaptureChanged(enableRequest); |
| 11126 | |
| 11127 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 11128 | // any notifications. |
| 11129 | mWindow->assertNoEvents(); |
| 11130 | } |
| 11131 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11132 | /** |
| 11133 | * One window. Hover mouse in the window, and then start capture. Make sure that the relative |
| 11134 | * mouse movements don't affect the previous mouse hovering state. |
| 11135 | * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no |
| 11136 | * HOVER_MOVE events). |
| 11137 | */ |
| 11138 | TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) { |
| 11139 | // Mouse hover on the window |
| 11140 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 11141 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 11142 | .build()); |
| 11143 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 11144 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 11145 | .build()); |
| 11146 | |
| 11147 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER))); |
| 11148 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE))); |
| 11149 | |
| 11150 | // Start pointer capture |
| 11151 | requestAndVerifyPointerCapture(mWindow, true); |
| 11152 | |
| 11153 | // Send some relative mouse movements and receive them in the window. |
| 11154 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE) |
| 11155 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11)) |
| 11156 | .build()); |
| 11157 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11), |
| 11158 | WithSource(AINPUT_SOURCE_MOUSE_RELATIVE))); |
| 11159 | |
| 11160 | // Stop pointer capture |
| 11161 | requestAndVerifyPointerCapture(mWindow, false); |
| 11162 | |
| 11163 | // Continue hovering on the window |
| 11164 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 11165 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115)) |
| 11166 | .build()); |
| 11167 | mWindow->consumeMotionEvent( |
| 11168 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 11169 | |
| 11170 | mWindow->assertNoEvents(); |
| 11171 | } |
| 11172 | |
Prabir Pradhan | 9d3d561 | 2024-06-06 20:34:26 +0000 | [diff] [blame] | 11173 | TEST_F(InputDispatcherPointerCaptureTests, MultiDisplayPointerCapture) { |
| 11174 | // The default display is the focused display to begin with. |
| 11175 | requestAndVerifyPointerCapture(mWindow, true); |
| 11176 | |
| 11177 | // Move the second window to a second display, make it the focused window on that display. |
| 11178 | mSecondWindow->editInfo()->displayId = SECOND_DISPLAY_ID; |
| 11179 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
| 11180 | setFocusedWindow(mSecondWindow); |
| 11181 | mSecondWindow->consumeFocusEvent(true); |
| 11182 | |
| 11183 | mWindow->assertNoEvents(); |
| 11184 | |
| 11185 | // The second window cannot gain capture because it is not on the focused display. |
| 11186 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 11187 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 11188 | mSecondWindow->assertNoEvents(); |
| 11189 | |
| 11190 | // Make the second display the focused display. |
| 11191 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
Arpit Singh | b65e2bd | 2024-06-03 09:48:16 +0000 | [diff] [blame] | 11192 | mFakePolicy->assertFocusedDisplayNotified(SECOND_DISPLAY_ID); |
Prabir Pradhan | 9d3d561 | 2024-06-06 20:34:26 +0000 | [diff] [blame] | 11193 | |
| 11194 | // This causes the first window to lose pointer capture, and it's unable to request capture. |
| 11195 | mWindow->consumeCaptureEvent(false); |
| 11196 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
| 11197 | |
| 11198 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 11199 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 11200 | |
| 11201 | // The second window is now able to gain pointer capture successfully. |
| 11202 | requestAndVerifyPointerCapture(mSecondWindow, true); |
| 11203 | } |
| 11204 | |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 11205 | using InputDispatcherPointerCaptureDeathTest = InputDispatcherPointerCaptureTests; |
| 11206 | |
| 11207 | TEST_F(InputDispatcherPointerCaptureDeathTest, |
| 11208 | NotifyPointerCaptureChangedWithWrongTokenAbortsDispatcher) { |
| 11209 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
| 11210 | ScopedSilentDeath _silentDeath; |
| 11211 | |
| 11212 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 11213 | auto request = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
| 11214 | |
| 11215 | // Dispatch a pointer changed event with a wrong token. |
| 11216 | request.window = mSecondWindow->getToken(); |
| 11217 | ASSERT_DEATH( |
| 11218 | { |
| 11219 | notifyPointerCaptureChanged(request); |
| 11220 | mSecondWindow->consumeCaptureEvent(true); |
| 11221 | }, |
| 11222 | "Unexpected requested window for Pointer Capture."); |
| 11223 | } |
| 11224 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11225 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 11226 | protected: |
| 11227 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11228 | |
| 11229 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 11230 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 11231 | |
| 11232 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 11233 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 11234 | |
| 11235 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 11236 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 11237 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 11238 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 11239 | MAXIMUM_OBSCURING_OPACITY); |
| 11240 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11241 | static constexpr gui::Uid TOUCHED_APP_UID{10001}; |
| 11242 | static constexpr gui::Uid APP_B_UID{10002}; |
| 11243 | static constexpr gui::Uid APP_C_UID{10003}; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11244 | |
| 11245 | sp<FakeWindowHandle> mTouchWindow; |
| 11246 | |
| 11247 | virtual void SetUp() override { |
| 11248 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11249 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11250 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 11251 | } |
| 11252 | |
| 11253 | virtual void TearDown() override { |
| 11254 | InputDispatcherTest::TearDown(); |
| 11255 | mTouchWindow.clear(); |
| 11256 | } |
| 11257 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11258 | sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 11259 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11260 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11261 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11262 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11263 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11264 | return window; |
| 11265 | } |
| 11266 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11267 | sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11268 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 11269 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11270 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ui::LogicalDisplayId::DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11271 | // Generate an arbitrary PID based on the UID |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11272 | 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] | 11273 | return window; |
| 11274 | } |
| 11275 | |
| 11276 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11277 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11278 | AINPUT_SOURCE_TOUCHSCREEN, |
| 11279 | ui::LogicalDisplayId::DEFAULT, points)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11280 | } |
| 11281 | }; |
| 11282 | |
| 11283 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11284 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11285 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11286 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11287 | |
| 11288 | touch(); |
| 11289 | |
| 11290 | mTouchWindow->assertNoEvents(); |
| 11291 | } |
| 11292 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11293 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11294 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 11295 | const sp<FakeWindowHandle>& w = |
| 11296 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11297 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11298 | |
| 11299 | touch(); |
| 11300 | |
| 11301 | mTouchWindow->assertNoEvents(); |
| 11302 | } |
| 11303 | |
| 11304 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11305 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 11306 | const sp<FakeWindowHandle>& w = |
| 11307 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11308 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11309 | |
| 11310 | touch(); |
| 11311 | |
| 11312 | w->assertNoEvents(); |
| 11313 | } |
| 11314 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11315 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11316 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11317 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11318 | |
| 11319 | touch(); |
| 11320 | |
| 11321 | mTouchWindow->consumeAnyMotionDown(); |
| 11322 | } |
| 11323 | |
| 11324 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11325 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11326 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11327 | w->setFrame(Rect(0, 0, 50, 50)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11328 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11329 | |
| 11330 | touch({PointF{100, 100}}); |
| 11331 | |
| 11332 | mTouchWindow->consumeAnyMotionDown(); |
| 11333 | } |
| 11334 | |
| 11335 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11336 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11337 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11338 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11339 | |
| 11340 | touch(); |
| 11341 | |
| 11342 | mTouchWindow->consumeAnyMotionDown(); |
| 11343 | } |
| 11344 | |
| 11345 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 11346 | const sp<FakeWindowHandle>& w = |
| 11347 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11348 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11349 | |
| 11350 | touch(); |
| 11351 | |
| 11352 | mTouchWindow->consumeAnyMotionDown(); |
| 11353 | } |
| 11354 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11355 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 11356 | const sp<FakeWindowHandle>& w = |
| 11357 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11358 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11359 | |
| 11360 | touch(); |
| 11361 | |
| 11362 | w->assertNoEvents(); |
| 11363 | } |
| 11364 | |
| 11365 | /** |
| 11366 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 11367 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 11368 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 11369 | */ |
| 11370 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11371 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 11372 | const sp<FakeWindowHandle>& w = |
| 11373 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11374 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11375 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11376 | |
| 11377 | touch(); |
| 11378 | |
| 11379 | w->consumeMotionOutside(); |
| 11380 | } |
| 11381 | |
| 11382 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 11383 | const sp<FakeWindowHandle>& w = |
| 11384 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11385 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11386 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11387 | |
| 11388 | touch(); |
| 11389 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 11390 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 11391 | } |
| 11392 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11393 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11394 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11395 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11396 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11397 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11398 | |
| 11399 | touch(); |
| 11400 | |
| 11401 | mTouchWindow->consumeAnyMotionDown(); |
| 11402 | } |
| 11403 | |
| 11404 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 11405 | const sp<FakeWindowHandle>& w = |
| 11406 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11407 | MAXIMUM_OBSCURING_OPACITY); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11408 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11409 | |
| 11410 | touch(); |
| 11411 | |
| 11412 | mTouchWindow->consumeAnyMotionDown(); |
| 11413 | } |
| 11414 | |
| 11415 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11416 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11417 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11418 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11419 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11420 | |
| 11421 | touch(); |
| 11422 | |
| 11423 | mTouchWindow->assertNoEvents(); |
| 11424 | } |
| 11425 | |
| 11426 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 11427 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 11428 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11429 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 11430 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11431 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11432 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 11433 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11434 | mDispatcher->onWindowInfosChanged( |
| 11435 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11436 | |
| 11437 | touch(); |
| 11438 | |
| 11439 | mTouchWindow->assertNoEvents(); |
| 11440 | } |
| 11441 | |
| 11442 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 11443 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 11444 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11445 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 11446 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11447 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11448 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 11449 | OPACITY_FAR_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11450 | mDispatcher->onWindowInfosChanged( |
| 11451 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11452 | |
| 11453 | touch(); |
| 11454 | |
| 11455 | mTouchWindow->consumeAnyMotionDown(); |
| 11456 | } |
| 11457 | |
| 11458 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11459 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 11460 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11461 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11462 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11463 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11464 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 11465 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11466 | mDispatcher->onWindowInfosChanged( |
| 11467 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11468 | |
| 11469 | touch(); |
| 11470 | |
| 11471 | mTouchWindow->consumeAnyMotionDown(); |
| 11472 | } |
| 11473 | |
| 11474 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 11475 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11476 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11477 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 11478 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11479 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 11480 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11481 | mDispatcher->onWindowInfosChanged( |
| 11482 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 11483 | |
| 11484 | touch(); |
| 11485 | |
| 11486 | mTouchWindow->assertNoEvents(); |
| 11487 | } |
| 11488 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11489 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11490 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 11491 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11492 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 11493 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11494 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11495 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11496 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11497 | mDispatcher->onWindowInfosChanged( |
| 11498 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11499 | |
| 11500 | touch(); |
| 11501 | |
| 11502 | mTouchWindow->assertNoEvents(); |
| 11503 | } |
| 11504 | |
| 11505 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11506 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 11507 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11508 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 11509 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11510 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11511 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11512 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11513 | mDispatcher->onWindowInfosChanged( |
| 11514 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11515 | |
| 11516 | touch(); |
| 11517 | |
| 11518 | mTouchWindow->consumeAnyMotionDown(); |
| 11519 | } |
| 11520 | |
| 11521 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 11522 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11523 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 11524 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11525 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11526 | |
| 11527 | touch(); |
| 11528 | |
| 11529 | mTouchWindow->consumeAnyMotionDown(); |
| 11530 | } |
| 11531 | |
| 11532 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 11533 | const sp<FakeWindowHandle>& w = |
| 11534 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11535 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 11536 | |
| 11537 | touch(); |
| 11538 | |
| 11539 | mTouchWindow->consumeAnyMotionDown(); |
| 11540 | } |
| 11541 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 11542 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11543 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 11544 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 11545 | const sp<FakeWindowHandle>& w = |
| 11546 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11547 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 11548 | |
| 11549 | touch(); |
| 11550 | |
| 11551 | mTouchWindow->assertNoEvents(); |
| 11552 | } |
| 11553 | |
| 11554 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 11555 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 11556 | const sp<FakeWindowHandle>& w = |
| 11557 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11558 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 11559 | |
| 11560 | touch(); |
| 11561 | |
| 11562 | mTouchWindow->consumeAnyMotionDown(); |
| 11563 | } |
| 11564 | |
| 11565 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11566 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 11567 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 11568 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11569 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11570 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11571 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11572 | |
| 11573 | touch(); |
| 11574 | |
| 11575 | mTouchWindow->consumeAnyMotionDown(); |
| 11576 | } |
| 11577 | |
| 11578 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11579 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 11580 | const sp<FakeWindowHandle>& w1 = |
| 11581 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 11582 | OPACITY_BELOW_THRESHOLD); |
| 11583 | const sp<FakeWindowHandle>& w2 = |
| 11584 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 11585 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11586 | mDispatcher->onWindowInfosChanged( |
| 11587 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11588 | |
| 11589 | touch(); |
| 11590 | |
| 11591 | mTouchWindow->assertNoEvents(); |
| 11592 | } |
| 11593 | |
| 11594 | /** |
| 11595 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 11596 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 11597 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 11598 | */ |
| 11599 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11600 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 11601 | const sp<FakeWindowHandle>& wB = |
| 11602 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 11603 | OPACITY_BELOW_THRESHOLD); |
| 11604 | const sp<FakeWindowHandle>& wC = |
| 11605 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 11606 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11607 | mDispatcher->onWindowInfosChanged( |
| 11608 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 11609 | |
| 11610 | touch(); |
| 11611 | |
| 11612 | mTouchWindow->assertNoEvents(); |
| 11613 | } |
| 11614 | |
| 11615 | /** |
| 11616 | * This test is testing that a window from a different UID but with same application token doesn't |
| 11617 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 11618 | */ |
| 11619 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 11620 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 11621 | const sp<FakeWindowHandle>& w = |
| 11622 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 11623 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11624 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 11625 | |
| 11626 | touch(); |
| 11627 | |
| 11628 | mTouchWindow->consumeAnyMotionDown(); |
| 11629 | } |
| 11630 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11631 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 11632 | protected: |
| 11633 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 11634 | sp<FakeWindowHandle> mWindow; |
| 11635 | sp<FakeWindowHandle> mSecondWindow; |
| 11636 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11637 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11638 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 11639 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11640 | |
| 11641 | void SetUp() override { |
| 11642 | InputDispatcherTest::SetUp(); |
| 11643 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11644 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", |
| 11645 | ui::LogicalDisplayId::DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11646 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11647 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11648 | mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", |
| 11649 | ui::LogicalDisplayId::DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11650 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11651 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11652 | mSpyWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", |
| 11653 | ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11654 | mSpyWindow->setSpy(true); |
| 11655 | mSpyWindow->setTrustedOverlay(true); |
| 11656 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 11657 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11658 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11659 | mDispatcher->onWindowInfosChanged( |
| 11660 | {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 11661 | {}, |
| 11662 | 0, |
| 11663 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11664 | } |
| 11665 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11666 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 11667 | switch (fromSource) { |
| 11668 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 11669 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11670 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11671 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11672 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11673 | break; |
| 11674 | case AINPUT_SOURCE_STYLUS: |
| 11675 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11676 | injectMotionEvent(*mDispatcher, |
| 11677 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 11678 | AINPUT_SOURCE_STYLUS) |
| 11679 | .buttonState( |
| 11680 | AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 11681 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
| 11682 | .x(50) |
| 11683 | .y(50)) |
| 11684 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11685 | break; |
| 11686 | case AINPUT_SOURCE_MOUSE: |
| 11687 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11688 | injectMotionEvent(*mDispatcher, |
| 11689 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 11690 | AINPUT_SOURCE_MOUSE) |
| 11691 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 11692 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 11693 | ToolType::MOUSE) |
| 11694 | .x(50) |
| 11695 | .y(50)) |
| 11696 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11697 | break; |
| 11698 | default: |
| 11699 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 11700 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11701 | |
| 11702 | // Window should receive motion event. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11703 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11704 | // Spy window should also receive motion event |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11705 | mSpyWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11706 | } |
| 11707 | |
| 11708 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 11709 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 11710 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11711 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11712 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11713 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11714 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11715 | |
| 11716 | // The drag window covers the entire display |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11717 | mDragWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", |
| 11718 | ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11719 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11720 | mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), |
| 11721 | *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 11722 | {}, |
| 11723 | 0, |
| 11724 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11725 | |
| 11726 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11727 | bool transferred = |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 11728 | mDispatcher->transferTouchGesture(mWindow->getToken(), mDragWindow->getToken(), |
| 11729 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11730 | if (transferred) { |
| 11731 | mWindow->consumeMotionCancel(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11732 | mDragWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 11733 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11734 | } |
| 11735 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11736 | } |
| 11737 | }; |
| 11738 | |
| 11739 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11740 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11741 | |
| 11742 | // Move on window. |
| 11743 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11744 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11745 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11746 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11747 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11748 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11749 | mWindow->consumeDragEvent(false, 50, 50); |
| 11750 | mSecondWindow->assertNoEvents(); |
| 11751 | |
| 11752 | // Move to another window. |
| 11753 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11754 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11755 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11756 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11757 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11758 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11759 | mWindow->consumeDragEvent(true, 150, 50); |
| 11760 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 11761 | |
| 11762 | // Move back to original window. |
| 11763 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11764 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11765 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11766 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11767 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11768 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11769 | mWindow->consumeDragEvent(false, 50, 50); |
| 11770 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 11771 | |
| 11772 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11773 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11774 | {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11775 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11776 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 11777 | mWindow->assertNoEvents(); |
| 11778 | mSecondWindow->assertNoEvents(); |
| 11779 | } |
| 11780 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11781 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11782 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11783 | |
| 11784 | // No cancel event after drag start |
| 11785 | mSpyWindow->assertNoEvents(); |
| 11786 | |
| 11787 | const MotionEvent secondFingerDownEvent = |
| 11788 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11789 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11790 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11791 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11792 | .build(); |
| 11793 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11794 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 11795 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11796 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11797 | |
| 11798 | // Receives cancel for first pointer after next pointer down |
| 11799 | mSpyWindow->consumeMotionCancel(); |
| 11800 | mSpyWindow->consumeMotionDown(); |
| 11801 | |
| 11802 | mSpyWindow->assertNoEvents(); |
| 11803 | } |
| 11804 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11805 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11806 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11807 | |
| 11808 | // Move on window. |
| 11809 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11810 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11811 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11812 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11813 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11814 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11815 | mWindow->consumeDragEvent(false, 50, 50); |
| 11816 | mSecondWindow->assertNoEvents(); |
| 11817 | |
| 11818 | // Move to another window. |
| 11819 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11820 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11821 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11822 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11823 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11824 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11825 | mWindow->consumeDragEvent(true, 150, 50); |
| 11826 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 11827 | |
| 11828 | // drop to another window. |
| 11829 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11830 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11831 | {150, 50})) |
| 11832 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11833 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 11834 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 11835 | mWindow->assertNoEvents(); |
| 11836 | mSecondWindow->assertNoEvents(); |
| 11837 | } |
| 11838 | |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 11839 | TEST_F(InputDispatcherDragTests, DragAndDropNotCancelledIfSomeOtherPointerIsPilfered) { |
| 11840 | startDrag(); |
| 11841 | |
| 11842 | // No cancel event after drag start |
| 11843 | mSpyWindow->assertNoEvents(); |
| 11844 | |
| 11845 | const MotionEvent secondFingerDownEvent = |
| 11846 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11847 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 11848 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11849 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 11850 | .build(); |
| 11851 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11852 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 11853 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11854 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11855 | |
| 11856 | // Receives cancel for first pointer after next pointer down |
| 11857 | mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 11858 | mSpyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithPointerIds({1}))); |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 11859 | mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 11860 | |
| 11861 | mSpyWindow->assertNoEvents(); |
| 11862 | |
| 11863 | // Spy window calls pilfer pointers |
| 11864 | EXPECT_EQ(OK, mDispatcher->pilferPointers(mSpyWindow->getToken())); |
| 11865 | mDragWindow->assertNoEvents(); |
| 11866 | |
| 11867 | const MotionEvent firstFingerMoveEvent = |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 11868 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 11869 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 11870 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(60).y(60)) |
| 11871 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 11872 | .build(); |
| 11873 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 11874 | injectMotionEvent(*mDispatcher, firstFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 11875 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11876 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11877 | |
| 11878 | // Drag window should still receive the new event |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 11879 | mDragWindow->consumeMotionEvent( |
| 11880 | AllOf(WithMotionAction(ACTION_MOVE), WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 11881 | mDragWindow->assertNoEvents(); |
| 11882 | } |
| 11883 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11884 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11885 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11886 | |
| 11887 | // Move on window and keep button pressed. |
| 11888 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11889 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11890 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 11891 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11892 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11893 | .build())) |
| 11894 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11895 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11896 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11897 | mWindow->consumeDragEvent(false, 50, 50); |
| 11898 | mSecondWindow->assertNoEvents(); |
| 11899 | |
| 11900 | // Move to another window and release button, expect to drop item. |
| 11901 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11902 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11903 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 11904 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11905 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11906 | .build())) |
| 11907 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11908 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11909 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11910 | mWindow->assertNoEvents(); |
| 11911 | mSecondWindow->assertNoEvents(); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 11912 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11913 | |
| 11914 | // nothing to the window. |
| 11915 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11916 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11917 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 11918 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11919 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11920 | .build())) |
| 11921 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11922 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 11923 | mWindow->assertNoEvents(); |
| 11924 | mSecondWindow->assertNoEvents(); |
| 11925 | } |
| 11926 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11927 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11928 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11929 | |
| 11930 | // Set second window invisible. |
| 11931 | mSecondWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11932 | mDispatcher->onWindowInfosChanged( |
| 11933 | {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11934 | |
| 11935 | // Move on window. |
| 11936 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11937 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11938 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11939 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11940 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11941 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11942 | mWindow->consumeDragEvent(false, 50, 50); |
| 11943 | mSecondWindow->assertNoEvents(); |
| 11944 | |
| 11945 | // Move to another window. |
| 11946 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11947 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11948 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11949 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11950 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 11951 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11952 | mWindow->consumeDragEvent(true, 150, 50); |
| 11953 | mSecondWindow->assertNoEvents(); |
| 11954 | |
| 11955 | // drop to another window. |
| 11956 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11957 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11958 | {150, 50})) |
| 11959 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11960 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 11961 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 11962 | mWindow->assertNoEvents(); |
| 11963 | mSecondWindow->assertNoEvents(); |
| 11964 | } |
| 11965 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11966 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11967 | // Ensure window could track pointerIds if it didn't support split touch. |
| 11968 | mWindow->setPreventSplitting(true); |
| 11969 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11970 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11971 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 11972 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11973 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11974 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11975 | |
| 11976 | const MotionEvent secondFingerDownEvent = |
| 11977 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11978 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11979 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11980 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11981 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11982 | .build(); |
| 11983 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11984 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11985 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11986 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11987 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11988 | |
| 11989 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 11990 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11991 | } |
| 11992 | |
| 11993 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 11994 | // First down on second window. |
| 11995 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 11996 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 11997 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 11998 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11999 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12000 | mSecondWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12001 | |
| 12002 | // Second down on first window. |
| 12003 | const MotionEvent secondFingerDownEvent = |
| 12004 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12005 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12006 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12007 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 12008 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12009 | .build(); |
| 12010 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12011 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12012 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12013 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12014 | mWindow->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 12015 | mSecondWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12016 | |
| 12017 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12018 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12019 | |
| 12020 | // Move on window. |
| 12021 | const MotionEvent secondFingerMoveEvent = |
| 12022 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 12023 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12024 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 12025 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12026 | .build(); |
| 12027 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12028 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12029 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12030 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12031 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12032 | mWindow->consumeDragEvent(false, 50, 50); |
| 12033 | mSecondWindow->consumeMotionMove(); |
| 12034 | |
| 12035 | // Release the drag pointer should perform drop. |
| 12036 | const MotionEvent secondFingerUpEvent = |
| 12037 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 12038 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12039 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 12040 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12041 | .build(); |
| 12042 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12043 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12044 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12045 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 12046 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken()); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 12047 | mWindow->assertNoEvents(); |
| 12048 | mSecondWindow->consumeMotionMove(); |
| 12049 | } |
| 12050 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12051 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12052 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12053 | |
| 12054 | // Update window of second display. |
| 12055 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 12056 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12057 | mDispatcher->onWindowInfosChanged( |
| 12058 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 12059 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 12060 | {}, |
| 12061 | 0, |
| 12062 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12063 | |
| 12064 | // Let second display has a touch state. |
| 12065 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12066 | injectMotionEvent(*mDispatcher, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12067 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 12068 | AINPUT_SOURCE_TOUCHSCREEN) |
| 12069 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12070 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12071 | .build())); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 12072 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12073 | // Update window again. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12074 | mDispatcher->onWindowInfosChanged( |
| 12075 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 12076 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 12077 | {}, |
| 12078 | 0, |
| 12079 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12080 | |
| 12081 | // Move on window. |
| 12082 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12083 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12084 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12085 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12086 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12087 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12088 | mWindow->consumeDragEvent(false, 50, 50); |
| 12089 | mSecondWindow->assertNoEvents(); |
| 12090 | |
| 12091 | // Move to another window. |
| 12092 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12093 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12094 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12095 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12096 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12097 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12098 | mWindow->consumeDragEvent(true, 150, 50); |
| 12099 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 12100 | |
| 12101 | // drop to another window. |
| 12102 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12103 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12104 | {150, 50})) |
| 12105 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12106 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 12107 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 12108 | mWindow->assertNoEvents(); |
| 12109 | mSecondWindow->assertNoEvents(); |
| 12110 | } |
| 12111 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12112 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 12113 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 12114 | // Move on window. |
| 12115 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12116 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12117 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 12118 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12119 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12120 | .x(50) |
| 12121 | .y(50)) |
| 12122 | .build())) |
| 12123 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12124 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12125 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12126 | mWindow->consumeDragEvent(false, 50, 50); |
| 12127 | mSecondWindow->assertNoEvents(); |
| 12128 | |
| 12129 | // Move to another window. |
| 12130 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12131 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12132 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 12133 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12134 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12135 | .x(150) |
| 12136 | .y(50)) |
| 12137 | .build())) |
| 12138 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12139 | mDragWindow->consumeMotionMove(ui::LogicalDisplayId::DEFAULT, |
| 12140 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12141 | mWindow->consumeDragEvent(true, 150, 50); |
| 12142 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 12143 | |
| 12144 | // drop to another window. |
| 12145 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12146 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12147 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 12148 | .buttonState(0) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12149 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12150 | .x(150) |
| 12151 | .y(50)) |
| 12152 | .build())) |
| 12153 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12154 | mDragWindow->consumeMotionUp(ui::LogicalDisplayId::DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 12155 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 12156 | mWindow->assertNoEvents(); |
| 12157 | mSecondWindow->assertNoEvents(); |
| 12158 | } |
| 12159 | |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12160 | /** |
| 12161 | * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag |
| 12162 | * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash. |
| 12163 | */ |
| 12164 | TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) { |
| 12165 | // Down on second window |
| 12166 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12167 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12168 | ui::LogicalDisplayId::DEFAULT, {150, 50})) |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12169 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12170 | |
| 12171 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown()); |
| 12172 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown()); |
| 12173 | |
| 12174 | // Down on first window |
| 12175 | const MotionEvent secondFingerDownEvent = |
| 12176 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12177 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12178 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 12179 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 12180 | .build(); |
| 12181 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12182 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 12183 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12184 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12185 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 12186 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove()); |
| 12187 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1)); |
| 12188 | |
| 12189 | // Start drag on first window |
| 12190 | ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN)); |
| 12191 | |
| 12192 | // Trigger cancel |
| 12193 | mDispatcher->cancelCurrentTouch(); |
| 12194 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel()); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12195 | ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel(ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 12196 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)); |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12197 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel()); |
| 12198 | |
| 12199 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 12200 | // The D&D finished with nullptr |
| 12201 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
| 12202 | |
| 12203 | // Remove drag window |
| 12204 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
| 12205 | |
| 12206 | // Inject a simple gesture, ensure dispatcher not crashed |
| 12207 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12208 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12209 | ui::LogicalDisplayId::DEFAULT, PointF{50, 50})) |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12210 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12211 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 12212 | |
| 12213 | const MotionEvent moveEvent = |
| 12214 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12215 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12216 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12217 | .build(); |
| 12218 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12219 | injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT, |
| 12220 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12221 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12222 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove()); |
| 12223 | |
| 12224 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12225 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT, |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 12226 | {50, 50})) |
| 12227 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12228 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp()); |
| 12229 | } |
| 12230 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 12231 | TEST_F(InputDispatcherDragTests, NoDragAndDropWithHoveringPointer) { |
| 12232 | // Start hovering over the window. |
| 12233 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12234 | injectMotionEvent(*mDispatcher, ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12235 | ui::LogicalDisplayId::DEFAULT, {50, 50})); |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 12236 | |
| 12237 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 12238 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 12239 | |
| 12240 | ASSERT_FALSE(startDrag(/*sendDown=*/false)) |
| 12241 | << "Drag and drop should not work with a hovering pointer"; |
| 12242 | } |
| 12243 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12244 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 12245 | |
| 12246 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 12247 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12248 | sp<FakeWindowHandle> window = |
| 12249 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 12250 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12251 | window->setDropInput(true); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12252 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12253 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12254 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12255 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12256 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12257 | |
| 12258 | // With the flag set, window should not get any input |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12259 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12260 | window->assertNoEvents(); |
| 12261 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12262 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12263 | AINPUT_SOURCE_TOUCHSCREEN, |
| 12264 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 12265 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12266 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 12267 | mDispatcher->waitForIdle(); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12268 | window->assertNoEvents(); |
| 12269 | |
| 12270 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12271 | window->setDropInput(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12272 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12273 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12274 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT)); |
| 12275 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12276 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12277 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12278 | AINPUT_SOURCE_TOUCHSCREEN, |
| 12279 | ui::LogicalDisplayId::DEFAULT)); |
| 12280 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12281 | window->assertNoEvents(); |
| 12282 | } |
| 12283 | |
| 12284 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 12285 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 12286 | std::make_shared<FakeApplicationHandle>(); |
| 12287 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 12288 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12289 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12290 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12291 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12292 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12293 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12294 | sp<FakeWindowHandle> window = |
| 12295 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 12296 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12297 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12298 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12299 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12300 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12301 | mDispatcher->onWindowInfosChanged( |
| 12302 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12303 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12304 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12305 | |
| 12306 | // With the flag set, window should not get any input |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12307 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12308 | window->assertNoEvents(); |
| 12309 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12310 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12311 | AINPUT_SOURCE_TOUCHSCREEN, |
| 12312 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 12313 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12314 | ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12315 | window->assertNoEvents(); |
| 12316 | |
| 12317 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12318 | window->setDropInputIfObscured(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12319 | mDispatcher->onWindowInfosChanged( |
| 12320 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12321 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12322 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT)); |
| 12323 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12324 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12325 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12326 | AINPUT_SOURCE_TOUCHSCREEN, |
| 12327 | ui::LogicalDisplayId::DEFAULT)); |
| 12328 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT, |
| 12329 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12330 | window->assertNoEvents(); |
| 12331 | } |
| 12332 | |
| 12333 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 12334 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 12335 | std::make_shared<FakeApplicationHandle>(); |
| 12336 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 12337 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12338 | ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12339 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12340 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12341 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12342 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12343 | sp<FakeWindowHandle> window = |
| 12344 | sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", |
| 12345 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12346 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12347 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12348 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12349 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12350 | mDispatcher->onWindowInfosChanged( |
| 12351 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12352 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12353 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12354 | |
| 12355 | // With the flag set, window should not get any input |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12356 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12357 | window->assertNoEvents(); |
| 12358 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12359 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12360 | AINPUT_SOURCE_TOUCHSCREEN, |
| 12361 | ui::LogicalDisplayId::DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 12362 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12363 | ui::LogicalDisplayId::DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12364 | window->assertNoEvents(); |
| 12365 | |
| 12366 | // 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] | 12367 | mDispatcher->onWindowInfosChanged( |
| 12368 | {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12369 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12370 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ui::LogicalDisplayId::DEFAULT)); |
| 12371 | window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12372 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 12373 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12374 | AINPUT_SOURCE_TOUCHSCREEN, |
| 12375 | ui::LogicalDisplayId::DEFAULT)); |
| 12376 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 12377 | window->assertNoEvents(); |
| 12378 | } |
| 12379 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12380 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 12381 | protected: |
| 12382 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12383 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12384 | sp<FakeWindowHandle> mWindow; |
| 12385 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12386 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12387 | |
| 12388 | void SetUp() override { |
| 12389 | InputDispatcherTest::SetUp(); |
| 12390 | |
| 12391 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12392 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12393 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", |
| 12394 | ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12395 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12396 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12397 | mSecondWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", |
| 12398 | ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12399 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12400 | mThirdWindow = |
| 12401 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 12402 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 12403 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12404 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12405 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12406 | mDispatcher->onWindowInfosChanged( |
| 12407 | {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()}, |
| 12408 | {}, |
| 12409 | 0, |
| 12410 | 0}); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12411 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12412 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12413 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12414 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12415 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12416 | WINDOW_UID, /*hasPermission=*/true, |
| 12417 | ui::LogicalDisplayId::DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12418 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 12419 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12420 | mThirdWindow->assertNoEvents(); |
| 12421 | } |
| 12422 | |
| 12423 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 12424 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12425 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12426 | SECOND_DISPLAY_ID)) { |
| 12427 | mWindow->assertNoEvents(); |
| 12428 | mSecondWindow->assertNoEvents(); |
| 12429 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12430 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12431 | } |
| 12432 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12433 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12434 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 12435 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12436 | ui::LogicalDisplayId::DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12437 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 12438 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12439 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12440 | } |
| 12441 | }; |
| 12442 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12443 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 12444 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12445 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 12446 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12447 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12448 | } |
| 12449 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12450 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 12451 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12452 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 12453 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12454 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12455 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12456 | ownerUid, /*hasPermission=*/false, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12457 | ui::LogicalDisplayId::DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12458 | mWindow->assertNoEvents(); |
| 12459 | mSecondWindow->assertNoEvents(); |
| 12460 | } |
| 12461 | |
| 12462 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 12463 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12464 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 12465 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12466 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12467 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12468 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12469 | } |
| 12470 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12471 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 12472 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 12473 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 12474 | windowInfo.ownerPid, windowInfo.ownerUid, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12475 | /*hasPermission=*/true, |
| 12476 | ui::LogicalDisplayId::DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 12477 | mWindow->assertNoEvents(); |
| 12478 | mSecondWindow->assertNoEvents(); |
| 12479 | } |
| 12480 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12481 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 12482 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 12483 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 12484 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12485 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 12486 | mWindow->assertNoEvents(); |
| 12487 | mSecondWindow->assertNoEvents(); |
| 12488 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 12489 | } |
| 12490 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12491 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 12492 | // Interact with the window first. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12493 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12494 | injectKeyDown(*mDispatcher, ui::LogicalDisplayId::DEFAULT)) |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12495 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12496 | mWindow->consumeKeyDown(ui::LogicalDisplayId::DEFAULT); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12497 | |
| 12498 | // Then remove focus. |
| 12499 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12500 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12501 | |
| 12502 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 12503 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 12504 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 12505 | windowInfo.ownerPid, windowInfo.ownerUid, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12506 | /*hasPermission=*/false, |
| 12507 | ui::LogicalDisplayId::DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 12508 | } |
| 12509 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12510 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 12511 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12512 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12513 | std::shared_ptr<FakeApplicationHandle> application = |
| 12514 | std::make_shared<FakeApplicationHandle>(); |
| 12515 | std::string name = "Fake Spy "; |
| 12516 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12517 | sp<FakeWindowHandle> spy = |
| 12518 | sp<FakeWindowHandle>::make(application, mDispatcher, name.c_str(), |
| 12519 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 12520 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 12521 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12522 | return spy; |
| 12523 | } |
| 12524 | |
| 12525 | sp<FakeWindowHandle> createForeground() { |
| 12526 | std::shared_ptr<FakeApplicationHandle> application = |
| 12527 | std::make_shared<FakeApplicationHandle>(); |
| 12528 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 12529 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12530 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12531 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12532 | return window; |
| 12533 | } |
| 12534 | |
| 12535 | private: |
| 12536 | int mSpyCount{0}; |
| 12537 | }; |
| 12538 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 12539 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12540 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 12541 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 12542 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 12543 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
Siarhei Vishniakou | 21f77bd | 2023-07-18 17:51:35 -0700 | [diff] [blame] | 12544 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 12545 | ScopedSilentDeath _silentDeath; |
| 12546 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12547 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 12548 | spy->setTrustedOverlay(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12549 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 12550 | ".* not a trusted overlay"); |
| 12551 | } |
| 12552 | |
| 12553 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12554 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 12555 | */ |
| 12556 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12557 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12558 | mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12559 | |
| 12560 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12561 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12562 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12563 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12564 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12565 | } |
| 12566 | |
| 12567 | /** |
| 12568 | * Verify the order in which different input windows receive events. The touched foreground window |
| 12569 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 12570 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 12571 | * receive events before ones belows it. |
| 12572 | * |
| 12573 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 12574 | * spy1, spy2, window, spy3. |
| 12575 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 12576 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 12577 | * window. |
| 12578 | */ |
| 12579 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 12580 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12581 | auto spy1 = createSpy(); |
| 12582 | auto spy2 = createSpy(); |
| 12583 | auto spy3 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12584 | mDispatcher->onWindowInfosChanged( |
| 12585 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12586 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 12587 | const size_t numChannels = channels.size(); |
| 12588 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 12589 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12590 | if (!epollFd.ok()) { |
| 12591 | FAIL() << "Failed to create epoll fd"; |
| 12592 | } |
| 12593 | |
| 12594 | for (size_t i = 0; i < numChannels; i++) { |
| 12595 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 12596 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 12597 | FAIL() << "Failed to add fd to epoll"; |
| 12598 | } |
| 12599 | } |
| 12600 | |
| 12601 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12602 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12603 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12604 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12605 | |
| 12606 | std::vector<size_t> eventOrder; |
| 12607 | std::vector<struct epoll_event> events(numChannels); |
| 12608 | for (;;) { |
| 12609 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 12610 | (100ms).count()); |
| 12611 | if (nFds < 0) { |
| 12612 | FAIL() << "Failed to call epoll_wait"; |
| 12613 | } |
| 12614 | if (nFds == 0) { |
| 12615 | break; // epoll_wait timed out |
| 12616 | } |
| 12617 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 12618 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 12619 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12620 | channels[i]->consumeMotionDown(); |
| 12621 | } |
| 12622 | } |
| 12623 | |
| 12624 | // Verify the order in which the events were received. |
| 12625 | EXPECT_EQ(3u, eventOrder.size()); |
| 12626 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 12627 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 12628 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 12629 | } |
| 12630 | |
| 12631 | /** |
| 12632 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 12633 | */ |
| 12634 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 12635 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12636 | auto spy = createSpy(); |
| 12637 | spy->setTouchable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12638 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12639 | |
| 12640 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12641 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12642 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12643 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12644 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12645 | spy->assertNoEvents(); |
| 12646 | } |
| 12647 | |
| 12648 | /** |
| 12649 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 12650 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 12651 | * to the window. |
| 12652 | */ |
| 12653 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 12654 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12655 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12656 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12657 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12658 | |
| 12659 | // Inject an event outside the spy window's touchable region. |
| 12660 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12661 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12662 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12663 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12664 | window->consumeMotionDown(); |
| 12665 | spy->assertNoEvents(); |
| 12666 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12667 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12668 | ui::LogicalDisplayId::DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12669 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12670 | window->consumeMotionUp(); |
| 12671 | spy->assertNoEvents(); |
| 12672 | |
| 12673 | // Inject an event inside the spy window's touchable region. |
| 12674 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12675 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12676 | ui::LogicalDisplayId::DEFAULT, {5, 10})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12677 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12678 | window->consumeMotionDown(); |
| 12679 | spy->consumeMotionDown(); |
| 12680 | } |
| 12681 | |
| 12682 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12683 | * 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] | 12684 | * 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] | 12685 | */ |
| 12686 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 12687 | auto window = createForeground(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12688 | window->setOwnerInfo(gui::Pid{12}, gui::Uid{34}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12689 | auto spy = createSpy(); |
| 12690 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12691 | spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12692 | spy->setFrame(Rect{0, 0, 20, 20}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12693 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12694 | |
| 12695 | // Inject an event outside the spy window's frame and touchable region. |
| 12696 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12697 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12698 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12699 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12700 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 12701 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12702 | } |
| 12703 | |
| 12704 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12705 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 12706 | * pointers that are down within its bounds. |
| 12707 | */ |
| 12708 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 12709 | auto windowLeft = createForeground(); |
| 12710 | windowLeft->setFrame({0, 0, 100, 200}); |
| 12711 | auto windowRight = createForeground(); |
| 12712 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12713 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12714 | spy->setFrame({0, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12715 | mDispatcher->onWindowInfosChanged( |
| 12716 | {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12717 | |
| 12718 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12719 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12720 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12721 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12722 | windowLeft->consumeMotionDown(); |
| 12723 | spy->consumeMotionDown(); |
| 12724 | |
| 12725 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 12726 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12727 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12728 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12729 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12730 | .build(); |
| 12731 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12732 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12733 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12734 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12735 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12736 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12737 | } |
| 12738 | |
| 12739 | /** |
| 12740 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 12741 | * the spy should receive the second pointer with ACTION_DOWN. |
| 12742 | */ |
| 12743 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 12744 | auto window = createForeground(); |
| 12745 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12746 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12747 | spyRight->setFrame({100, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12748 | mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12749 | |
| 12750 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12751 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12752 | ui::LogicalDisplayId::DEFAULT, {50, 50})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12753 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12754 | window->consumeMotionDown(); |
| 12755 | spyRight->assertNoEvents(); |
| 12756 | |
| 12757 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 12758 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12759 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12760 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12761 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12762 | .build(); |
| 12763 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12764 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12765 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12766 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12767 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 12768 | spyRight->consumeMotionDown(); |
| 12769 | } |
| 12770 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12771 | /** |
| 12772 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 12773 | * windows should be allowed to control split touch. |
| 12774 | */ |
| 12775 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 12776 | // 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] | 12777 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12778 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 12779 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12780 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12781 | auto window = createForeground(); |
| 12782 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12783 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12784 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12785 | |
| 12786 | // First finger down, no window touched. |
| 12787 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12788 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12789 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12790 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12791 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12792 | window->assertNoEvents(); |
| 12793 | |
| 12794 | // Second finger down on window, the window should receive touch down. |
| 12795 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 12796 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12797 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12798 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12799 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 12800 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12801 | .build(); |
| 12802 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12803 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12804 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12805 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12806 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12807 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12808 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12809 | } |
| 12810 | |
| 12811 | /** |
| 12812 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 12813 | * do not receive key events. |
| 12814 | */ |
| 12815 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 12816 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12817 | spy->setFocusable(false); |
| 12818 | |
| 12819 | auto window = createForeground(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12820 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12821 | setFocusedWindow(window); |
| 12822 | window->consumeFocusEvent(true); |
| 12823 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12824 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12825 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12826 | window->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12827 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12828 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12829 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12830 | window->consumeKeyUp(ui::LogicalDisplayId::INVALID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 12831 | |
| 12832 | spy->assertNoEvents(); |
| 12833 | } |
| 12834 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12835 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 12836 | |
| 12837 | /** |
| 12838 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 12839 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 12840 | */ |
| 12841 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 12842 | auto window = createForeground(); |
| 12843 | auto spy1 = createSpy(); |
| 12844 | auto spy2 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12845 | mDispatcher->onWindowInfosChanged( |
| 12846 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12847 | |
| 12848 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12849 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12850 | ui::LogicalDisplayId::DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12851 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12852 | window->consumeMotionDown(); |
| 12853 | spy1->consumeMotionDown(); |
| 12854 | spy2->consumeMotionDown(); |
| 12855 | |
| 12856 | // Pilfer pointers from the second spy window. |
| 12857 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 12858 | spy2->assertNoEvents(); |
| 12859 | spy1->consumeMotionCancel(); |
| 12860 | window->consumeMotionCancel(); |
| 12861 | |
| 12862 | // The rest of the gesture should only be sent to the second spy window. |
| 12863 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12864 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12865 | ui::LogicalDisplayId::DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12866 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12867 | spy2->consumeMotionMove(); |
| 12868 | spy1->assertNoEvents(); |
| 12869 | window->assertNoEvents(); |
| 12870 | } |
| 12871 | |
| 12872 | /** |
| 12873 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 12874 | * in the middle of the gesture. |
| 12875 | */ |
| 12876 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 12877 | auto window = createForeground(); |
| 12878 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12879 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12880 | |
| 12881 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12882 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12883 | ui::LogicalDisplayId::DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12884 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12885 | window->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
| 12886 | spy->consumeMotionDown(ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12887 | |
| 12888 | window->releaseChannel(); |
| 12889 | |
| 12890 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 12891 | |
| 12892 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12893 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12894 | ui::LogicalDisplayId::DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12895 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12896 | spy->consumeMotionUp(ui::LogicalDisplayId::DEFAULT); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12897 | } |
| 12898 | |
| 12899 | /** |
| 12900 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 12901 | * the spy, but not to any other windows. |
| 12902 | */ |
| 12903 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 12904 | auto spy = createSpy(); |
| 12905 | auto window = createForeground(); |
| 12906 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12907 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12908 | |
| 12909 | // First finger down on the window and the spy. |
| 12910 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12911 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12912 | ui::LogicalDisplayId::DEFAULT, {100, 200})) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12913 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12914 | spy->consumeMotionDown(); |
| 12915 | window->consumeMotionDown(); |
| 12916 | |
| 12917 | // Spy window pilfers the pointers. |
| 12918 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 12919 | window->consumeMotionCancel(); |
| 12920 | |
| 12921 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 12922 | const MotionEvent secondFingerDownEvent = |
| 12923 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12924 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12925 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12926 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 12927 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12928 | .build(); |
| 12929 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12930 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12931 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12932 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12933 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 12934 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12935 | |
| 12936 | // Third finger goes down outside all windows, so injection should fail. |
| 12937 | const MotionEvent thirdFingerDownEvent = |
| 12938 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12939 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12940 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12941 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 12942 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 12943 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12944 | .build(); |
| 12945 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12946 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12947 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 12948 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12949 | |
| 12950 | spy->assertNoEvents(); |
| 12951 | window->assertNoEvents(); |
| 12952 | } |
| 12953 | |
| 12954 | /** |
| 12955 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 12956 | */ |
| 12957 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 12958 | auto spy = createSpy(); |
| 12959 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 12960 | auto window = createForeground(); |
| 12961 | window->setFrame(Rect(0, 0, 200, 200)); |
| 12962 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12963 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12964 | |
| 12965 | // First finger down on the window only |
| 12966 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12967 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 12968 | ui::LogicalDisplayId::DEFAULT, {150, 150})) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12969 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12970 | window->consumeMotionDown(); |
| 12971 | |
| 12972 | // Second finger down on the spy and window |
| 12973 | const MotionEvent secondFingerDownEvent = |
| 12974 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12975 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12976 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12977 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 12978 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12979 | .build(); |
| 12980 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12981 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12982 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12983 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 12984 | spy->consumeMotionDown(); |
| 12985 | window->consumeMotionPointerDown(1); |
| 12986 | |
| 12987 | // Third finger down on the spy and window |
| 12988 | const MotionEvent thirdFingerDownEvent = |
| 12989 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 12990 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12991 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 12992 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 12993 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 12994 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12995 | .build(); |
| 12996 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12997 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 12998 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 12999 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13000 | spy->consumeMotionPointerDown(1); |
| 13001 | window->consumeMotionPointerDown(2); |
| 13002 | |
| 13003 | // Spy window pilfers the pointers. |
| 13004 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Prabir Pradhan | 33cfc6d | 2024-06-11 20:17:44 +0000 | [diff] [blame] | 13005 | window->consumeMotionPointerUp(/*pointerIdx=*/2, |
| 13006 | AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT), |
| 13007 | WithFlags(AMOTION_EVENT_FLAG_CANCELED), |
| 13008 | WithPointerCount(3))); |
| 13009 | window->consumeMotionPointerUp(/*pointerIdx=*/1, |
| 13010 | AllOf(WithDisplayId(ui::LogicalDisplayId::DEFAULT), |
| 13011 | WithFlags(AMOTION_EVENT_FLAG_CANCELED), |
| 13012 | WithPointerCount(2))); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13013 | |
| 13014 | spy->assertNoEvents(); |
| 13015 | window->assertNoEvents(); |
| 13016 | } |
| 13017 | |
| 13018 | /** |
| 13019 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 13020 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 13021 | * window, then that window should receive ACTION_CANCEL. |
| 13022 | */ |
| 13023 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 13024 | auto spy = createSpy(); |
| 13025 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 13026 | auto window = createForeground(); |
| 13027 | window->setFrame(Rect(0, 0, 200, 200)); |
| 13028 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13029 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13030 | |
| 13031 | // First finger down on both spy and window |
| 13032 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13033 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 13034 | ui::LogicalDisplayId::DEFAULT, {10, 10})) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13035 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13036 | window->consumeMotionDown(); |
| 13037 | spy->consumeMotionDown(); |
| 13038 | |
| 13039 | // Second finger down on the spy and window |
| 13040 | const MotionEvent secondFingerDownEvent = |
| 13041 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13042 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13043 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 13044 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 13045 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13046 | .build(); |
| 13047 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13048 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13049 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 13050 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13051 | spy->consumeMotionPointerDown(1); |
| 13052 | window->consumeMotionPointerDown(1); |
| 13053 | |
| 13054 | // Spy window pilfers the pointers. |
| 13055 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 13056 | window->consumeMotionCancel(); |
| 13057 | |
| 13058 | spy->assertNoEvents(); |
| 13059 | window->assertNoEvents(); |
| 13060 | } |
| 13061 | |
| 13062 | /** |
| 13063 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 13064 | * be sent to other windows |
| 13065 | */ |
| 13066 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 13067 | auto spy = createSpy(); |
| 13068 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 13069 | auto window = createForeground(); |
| 13070 | window->setFrame(Rect(0, 0, 200, 200)); |
| 13071 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13072 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13073 | |
| 13074 | // First finger down on both window and spy |
| 13075 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13076 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
| 13077 | ui::LogicalDisplayId::DEFAULT, {10, 10})) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13078 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13079 | window->consumeMotionDown(); |
| 13080 | spy->consumeMotionDown(); |
| 13081 | |
| 13082 | // Spy window pilfers the pointers. |
| 13083 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 13084 | window->consumeMotionCancel(); |
| 13085 | |
| 13086 | // Second finger down on the window only |
| 13087 | const MotionEvent secondFingerDownEvent = |
| 13088 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13089 | .displayId(ui::LogicalDisplayId::DEFAULT) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13090 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 13091 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 13092 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13093 | .build(); |
| 13094 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13095 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 13096 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 13097 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 13098 | window->consumeMotionDown(); |
| 13099 | window->assertNoEvents(); |
| 13100 | |
| 13101 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 13102 | spy->consumeMotionMove(); |
| 13103 | spy->assertNoEvents(); |
| 13104 | } |
| 13105 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 13106 | /** |
| 13107 | * A window on the left and a window on the right. Also, a spy window that's above all of the |
| 13108 | * windows, and spanning both left and right windows. |
| 13109 | * Send simultaneous motion streams from two different devices, one to the left window, and another |
| 13110 | * to the right window. |
| 13111 | * Pilfer from spy window. |
| 13112 | * Check that the pilfering only affects the pointers that are actually being received by the spy. |
| 13113 | */ |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13114 | TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer_legacy) { |
| 13115 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 13116 | sp<FakeWindowHandle> spy = createSpy(); |
| 13117 | spy->setFrame(Rect(0, 0, 200, 200)); |
| 13118 | sp<FakeWindowHandle> leftWindow = createForeground(); |
| 13119 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 13120 | |
| 13121 | sp<FakeWindowHandle> rightWindow = createForeground(); |
| 13122 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 13123 | |
| 13124 | constexpr int32_t stylusDeviceId = 1; |
| 13125 | constexpr int32_t touchDeviceId = 2; |
| 13126 | |
| 13127 | mDispatcher->onWindowInfosChanged( |
| 13128 | {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 13129 | |
| 13130 | // Stylus down on left window and spy |
| 13131 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 13132 | .deviceId(stylusDeviceId) |
| 13133 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 13134 | .build()); |
| 13135 | leftWindow->consumeMotionEvent( |
| 13136 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 13137 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 13138 | |
| 13139 | // Finger down on right window and spy - but spy already has stylus |
| 13140 | mDispatcher->notifyMotion( |
| 13141 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 13142 | .deviceId(touchDeviceId) |
| 13143 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 13144 | .build()); |
| 13145 | rightWindow->consumeMotionEvent( |
| 13146 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 13147 | spy->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 13148 | |
| 13149 | // Act: pilfer from spy. Spy is currently receiving touch events. |
| 13150 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 13151 | leftWindow->consumeMotionEvent( |
| 13152 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 13153 | rightWindow->consumeMotionEvent( |
| 13154 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 13155 | |
| 13156 | // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus |
| 13157 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 13158 | .deviceId(stylusDeviceId) |
| 13159 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52)) |
| 13160 | .build()); |
| 13161 | mDispatcher->notifyMotion( |
| 13162 | MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 13163 | .deviceId(touchDeviceId) |
| 13164 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52)) |
| 13165 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 13166 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 13167 | |
| 13168 | spy->assertNoEvents(); |
| 13169 | leftWindow->assertNoEvents(); |
| 13170 | rightWindow->assertNoEvents(); |
| 13171 | } |
| 13172 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13173 | /** |
| 13174 | * A window on the left and a window on the right. Also, a spy window that's above all of the |
| 13175 | * windows, and spanning both left and right windows. |
| 13176 | * Send simultaneous motion streams from two different devices, one to the left window, and another |
| 13177 | * to the right window. |
| 13178 | * Pilfer from spy window. |
| 13179 | * Check that the pilfering affects all of the pointers that are actually being received by the spy. |
| 13180 | * The spy should receive both the touch and the stylus events after pilfer. |
| 13181 | */ |
| 13182 | TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) { |
| 13183 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 13184 | sp<FakeWindowHandle> spy = createSpy(); |
| 13185 | spy->setFrame(Rect(0, 0, 200, 200)); |
| 13186 | sp<FakeWindowHandle> leftWindow = createForeground(); |
| 13187 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 13188 | |
| 13189 | sp<FakeWindowHandle> rightWindow = createForeground(); |
| 13190 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 13191 | |
| 13192 | constexpr int32_t stylusDeviceId = 1; |
| 13193 | constexpr int32_t touchDeviceId = 2; |
| 13194 | |
| 13195 | mDispatcher->onWindowInfosChanged( |
| 13196 | {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 13197 | |
| 13198 | // Stylus down on left window and spy |
| 13199 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 13200 | .deviceId(stylusDeviceId) |
| 13201 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 13202 | .build()); |
| 13203 | leftWindow->consumeMotionEvent( |
| 13204 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 13205 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 13206 | |
| 13207 | // Finger down on right window and spy |
| 13208 | mDispatcher->notifyMotion( |
| 13209 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 13210 | .deviceId(touchDeviceId) |
| 13211 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 13212 | .build()); |
| 13213 | rightWindow->consumeMotionEvent( |
| 13214 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 13215 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 13216 | |
| 13217 | // Act: pilfer from spy. Spy is currently receiving touch events. |
| 13218 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 13219 | leftWindow->consumeMotionEvent( |
| 13220 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId))); |
| 13221 | rightWindow->consumeMotionEvent( |
| 13222 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 13223 | |
| 13224 | // 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] | 13225 | // Instead of sending the two MOVE events for each input device together, and then receiving |
| 13226 | // them both, process them one at at time. InputConsumer is always in the batching mode, which |
| 13227 | // means that the two MOVE events will be initially put into a batch. Once the events are |
| 13228 | // batched, the 'consume' call may result in any of the MOVE events to be sent first (depending |
| 13229 | // on the implementation of InputConsumer), which would mean that the order of the received |
| 13230 | // events could be different depending on whether there are 1 or 2 events pending in the |
| 13231 | // InputChannel at the time the test calls 'consume'. To make assertions simpler here, and to |
| 13232 | // avoid this confusing behaviour, send and receive each MOVE event separately. |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13233 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 13234 | .deviceId(stylusDeviceId) |
| 13235 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52)) |
| 13236 | .build()); |
Siarhei Vishniakou | 92bca1c | 2024-04-01 14:06:59 -0700 | [diff] [blame] | 13237 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13238 | mDispatcher->notifyMotion( |
| 13239 | MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 13240 | .deviceId(touchDeviceId) |
| 13241 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52)) |
| 13242 | .build()); |
Siarhei Vishniakou | 92bca1c | 2024-04-01 14:06:59 -0700 | [diff] [blame] | 13243 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13244 | |
| 13245 | spy->assertNoEvents(); |
| 13246 | leftWindow->assertNoEvents(); |
| 13247 | rightWindow->assertNoEvents(); |
| 13248 | } |
| 13249 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 13250 | TEST_F(InputDispatcherPilferPointersTest, NoPilferingWithHoveringPointers) { |
| 13251 | auto window = createForeground(); |
| 13252 | auto spy = createSpy(); |
| 13253 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 13254 | |
| 13255 | mDispatcher->notifyMotion( |
| 13256 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 13257 | .deviceId(1) |
| 13258 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(100).y(200)) |
| 13259 | .build()); |
| 13260 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13261 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13262 | |
| 13263 | // Pilfer pointers from the spy window should fail. |
| 13264 | EXPECT_NE(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 13265 | spy->assertNoEvents(); |
| 13266 | window->assertNoEvents(); |
| 13267 | } |
| 13268 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13269 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 13270 | public: |
| 13271 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 13272 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 13273 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13274 | sp<FakeWindowHandle> overlay = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 13275 | "Stylus interceptor window", |
| 13276 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13277 | overlay->setFocusable(false); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13278 | overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 13279 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 13280 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13281 | overlay->setTrustedOverlay(true); |
| 13282 | |
| 13283 | std::shared_ptr<FakeApplicationHandle> application = |
| 13284 | std::make_shared<FakeApplicationHandle>(); |
| 13285 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 13286 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13287 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13288 | window->setFocusable(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13289 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13290 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13291 | mDispatcher->setFocusedApplication(ui::LogicalDisplayId::DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13292 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13293 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 13294 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13295 | return {std::move(overlay), std::move(window)}; |
| 13296 | } |
| 13297 | |
| 13298 | void sendFingerEvent(int32_t action) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 13299 | mDispatcher->notifyMotion( |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13300 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13301 | ui::LogicalDisplayId::DEFAULT, {PointF{20, 20}})); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13302 | } |
| 13303 | |
| 13304 | void sendStylusEvent(int32_t action) { |
| 13305 | NotifyMotionArgs motionArgs = |
| 13306 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13307 | ui::LogicalDisplayId::DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 13308 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 13309 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13310 | } |
| 13311 | }; |
| 13312 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 13313 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 13314 | |
| 13315 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
Siarhei Vishniakou | ad3b682 | 2023-06-22 14:17:35 -0700 | [diff] [blame] | 13316 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 13317 | ScopedSilentDeath _silentDeath; |
| 13318 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13319 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 13320 | overlay->setTrustedOverlay(false); |
| 13321 | // 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] | 13322 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 13323 | {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13324 | ".* not a trusted overlay"); |
| 13325 | } |
| 13326 | |
| 13327 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 13328 | auto [overlay, window] = setupStylusOverlayScenario(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13329 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13330 | |
| 13331 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 13332 | overlay->consumeMotionDown(); |
| 13333 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 13334 | overlay->consumeMotionUp(); |
| 13335 | |
| 13336 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 13337 | window->consumeMotionDown(); |
| 13338 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 13339 | window->consumeMotionUp(); |
| 13340 | |
| 13341 | overlay->assertNoEvents(); |
| 13342 | window->assertNoEvents(); |
| 13343 | } |
| 13344 | |
| 13345 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 13346 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 13347 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13348 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 13349 | |
| 13350 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 13351 | overlay->consumeMotionDown(); |
| 13352 | window->consumeMotionDown(); |
| 13353 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 13354 | overlay->consumeMotionUp(); |
| 13355 | window->consumeMotionUp(); |
| 13356 | |
| 13357 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 13358 | window->consumeMotionDown(); |
| 13359 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 13360 | window->consumeMotionUp(); |
| 13361 | |
| 13362 | overlay->assertNoEvents(); |
| 13363 | window->assertNoEvents(); |
| 13364 | } |
| 13365 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 13366 | /** |
| 13367 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 13368 | * The scenario is as follows: |
| 13369 | * - The stylus interceptor overlay is configured as a spy window. |
| 13370 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 13371 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 13372 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 13373 | */ |
| 13374 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 13375 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 13376 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13377 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 13378 | |
| 13379 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 13380 | overlay->consumeMotionDown(); |
| 13381 | window->consumeMotionDown(); |
| 13382 | |
| 13383 | // The interceptor pilfers the pointers. |
| 13384 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 13385 | window->consumeMotionCancel(); |
| 13386 | |
| 13387 | // The interceptor configures itself so that it is no longer a spy. |
| 13388 | overlay->setSpy(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13389 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 13390 | |
| 13391 | // It continues to receive the rest of the stylus gesture. |
| 13392 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 13393 | overlay->consumeMotionMove(); |
| 13394 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 13395 | overlay->consumeMotionUp(); |
| 13396 | |
| 13397 | window->assertNoEvents(); |
| 13398 | } |
| 13399 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13400 | struct User { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13401 | gui::Pid mPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 13402 | gui::Uid mUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13403 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 13404 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 13405 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13406 | User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid) |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13407 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 13408 | |
| 13409 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13410 | return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13411 | ui::LogicalDisplayId::DEFAULT, {100, 200}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13412 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 13413 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 13414 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 13415 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 13416 | } |
| 13417 | |
| 13418 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 13419 | return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13420 | ui::LogicalDisplayId::INVALID, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13421 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 13422 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13423 | mPolicyFlags); |
| 13424 | } |
| 13425 | |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13426 | sp<FakeWindowHandle> createWindow(const char* name) const { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13427 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 13428 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13429 | sp<FakeWindowHandle> window = |
| 13430 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, name, |
| 13431 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13432 | window->setOwnerInfo(mPid, mUid); |
| 13433 | return window; |
| 13434 | } |
| 13435 | }; |
| 13436 | |
| 13437 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 13438 | |
| 13439 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13440 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13441 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13442 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13443 | |
| 13444 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 13445 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 13446 | window->consumeMotionDown(); |
| 13447 | |
| 13448 | setFocusedWindow(window); |
| 13449 | window->consumeFocusEvent(true); |
| 13450 | |
| 13451 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 13452 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13453 | window->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13454 | } |
| 13455 | |
| 13456 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13457 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13458 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13459 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13460 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13461 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13462 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 13463 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 13464 | |
| 13465 | setFocusedWindow(window); |
| 13466 | window->consumeFocusEvent(true); |
| 13467 | |
| 13468 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 13469 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 13470 | window->assertNoEvents(); |
| 13471 | } |
| 13472 | |
| 13473 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13474 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13475 | auto window = owner.createWindow("Owned window"); |
| 13476 | auto spy = owner.createWindow("Owned spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13477 | spy->setSpy(true); |
| 13478 | spy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13479 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13480 | |
| 13481 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 13482 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 13483 | spy->consumeMotionDown(); |
| 13484 | window->consumeMotionDown(); |
| 13485 | } |
| 13486 | |
| 13487 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13488 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13489 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13490 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13491 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13492 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13493 | randosSpy->setSpy(true); |
| 13494 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13495 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13496 | |
| 13497 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 13498 | // not receive the event. |
| 13499 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 13500 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 13501 | randosSpy->assertNoEvents(); |
| 13502 | window->consumeMotionDown(); |
| 13503 | } |
| 13504 | |
| 13505 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13506 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13507 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13508 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13509 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13510 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13511 | randosSpy->setSpy(true); |
| 13512 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13513 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13514 | |
| 13515 | // A user that has injection permission can inject into any window. |
| 13516 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13517 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13518 | ui::LogicalDisplayId::DEFAULT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13519 | randosSpy->consumeMotionDown(); |
| 13520 | window->consumeMotionDown(); |
| 13521 | |
| 13522 | setFocusedWindow(randosSpy); |
| 13523 | randosSpy->consumeFocusEvent(true); |
| 13524 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 13525 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13526 | randosSpy->consumeKeyDown(ui::LogicalDisplayId::INVALID); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13527 | window->assertNoEvents(); |
| 13528 | } |
| 13529 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 13530 | TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13531 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13532 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13533 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 13534 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 13535 | auto randosWindow = rando.createWindow("Rando's window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13536 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 13537 | randosWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 13538 | mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13539 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 13540 | // 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] | 13541 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 13542 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 13543 | window->consumeMotionDown(); |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 13544 | randosWindow->assertNoEvents(); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 13545 | } |
| 13546 | |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13547 | using InputDispatcherPointerInWindowTest = InputDispatcherTest; |
| 13548 | |
| 13549 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWhenHovering) { |
| 13550 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 13551 | |
| 13552 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13553 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13554 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13555 | sp<FakeWindowHandle> right = |
| 13556 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 13557 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13558 | right->setFrame(Rect(100, 0, 200, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13559 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", |
| 13560 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13561 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 13562 | spy->setTrustedOverlay(true); |
| 13563 | spy->setSpy(true); |
| 13564 | |
| 13565 | mDispatcher->onWindowInfosChanged( |
| 13566 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 13567 | |
| 13568 | // Hover into the left window. |
| 13569 | mDispatcher->notifyMotion( |
| 13570 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 13571 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(50).y(50)) |
| 13572 | .build()); |
| 13573 | |
| 13574 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13575 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13576 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13577 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13578 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13579 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13580 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13581 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13582 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13583 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13584 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13585 | /*pointerId=*/0)); |
| 13586 | |
| 13587 | // Hover move to the right window. |
| 13588 | mDispatcher->notifyMotion( |
| 13589 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 13590 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 13591 | .build()); |
| 13592 | |
| 13593 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 13594 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13595 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 13596 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13597 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13598 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13599 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13600 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->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_TRUE(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 | // Stop hovering. |
| 13608 | mDispatcher->notifyMotion( |
| 13609 | MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 13610 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 13611 | .build()); |
| 13612 | |
| 13613 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 13614 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 13615 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13616 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13617 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13618 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13619 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13620 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13621 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13622 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13623 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13624 | /*pointerId=*/0)); |
| 13625 | } |
| 13626 | |
| 13627 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWithSplitTouch) { |
| 13628 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 13629 | |
| 13630 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13631 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13632 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13633 | sp<FakeWindowHandle> right = |
| 13634 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 13635 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13636 | right->setFrame(Rect(100, 0, 200, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13637 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", |
| 13638 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13639 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 13640 | spy->setTrustedOverlay(true); |
| 13641 | spy->setSpy(true); |
| 13642 | |
| 13643 | mDispatcher->onWindowInfosChanged( |
| 13644 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 13645 | |
| 13646 | // First pointer down on left window. |
| 13647 | mDispatcher->notifyMotion( |
| 13648 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 13649 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 13650 | .build()); |
| 13651 | |
| 13652 | left->consumeMotionDown(); |
| 13653 | spy->consumeMotionDown(); |
| 13654 | |
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_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13659 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13660 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13661 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13662 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13663 | /*pointerId=*/0)); |
| 13664 | |
| 13665 | // Second pointer down on right window. |
| 13666 | mDispatcher->notifyMotion( |
| 13667 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 13668 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 13669 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 13670 | .build()); |
| 13671 | |
| 13672 | left->consumeMotionMove(); |
| 13673 | right->consumeMotionDown(); |
| 13674 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 13675 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13676 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13677 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13678 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13679 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13680 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13681 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13682 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13683 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13684 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13685 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13686 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13687 | /*pointerId=*/1)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13688 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13689 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13690 | /*pointerId=*/1)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13691 | ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13692 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13693 | /*pointerId=*/1)); |
| 13694 | |
| 13695 | // Second pointer up. |
| 13696 | mDispatcher->notifyMotion( |
| 13697 | MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 13698 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 13699 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 13700 | .build()); |
| 13701 | |
| 13702 | left->consumeMotionMove(); |
| 13703 | right->consumeMotionUp(); |
| 13704 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 13705 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13706 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13707 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13708 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13709 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13710 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13711 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13712 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13713 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13714 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13715 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13716 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13717 | /*pointerId=*/1)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13718 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13719 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13720 | /*pointerId=*/1)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13721 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13722 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13723 | /*pointerId=*/1)); |
| 13724 | |
| 13725 | // First pointer up. |
| 13726 | mDispatcher->notifyMotion( |
| 13727 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 13728 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 13729 | .build()); |
| 13730 | |
| 13731 | left->consumeMotionUp(); |
| 13732 | spy->consumeMotionUp(); |
| 13733 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13734 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13735 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13736 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13737 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13738 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13739 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13740 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13741 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13742 | /*pointerId=*/0)); |
| 13743 | } |
| 13744 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13745 | TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse_legacy) { |
| 13746 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, false); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13747 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 13748 | |
| 13749 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13750 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13751 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13752 | sp<FakeWindowHandle> right = |
| 13753 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 13754 | ui::LogicalDisplayId::DEFAULT); |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13755 | right->setFrame(Rect(100, 0, 200, 100)); |
| 13756 | |
| 13757 | mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 13758 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13759 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13760 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13761 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13762 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13763 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13764 | /*pointerId=*/0)); |
| 13765 | |
| 13766 | // Hover move into the window. |
| 13767 | mDispatcher->notifyMotion( |
| 13768 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 13769 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50)) |
| 13770 | .rawXCursorPosition(50) |
| 13771 | .rawYCursorPosition(50) |
| 13772 | .deviceId(DEVICE_ID) |
| 13773 | .build()); |
| 13774 | |
| 13775 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13776 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13777 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13778 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13779 | /*pointerId=*/0)); |
| 13780 | |
| 13781 | // Move the mouse with another device. This cancels the hovering pointer from the first device. |
| 13782 | mDispatcher->notifyMotion( |
| 13783 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 13784 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50)) |
| 13785 | .rawXCursorPosition(51) |
| 13786 | .rawYCursorPosition(50) |
| 13787 | .deviceId(SECOND_DEVICE_ID) |
| 13788 | .build()); |
| 13789 | |
| 13790 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 13791 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13792 | |
| 13793 | // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets |
| 13794 | // a HOVER_EXIT from the first device. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13795 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13796 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13797 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13798 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13799 | SECOND_DEVICE_ID, |
| 13800 | /*pointerId=*/0)); |
| 13801 | |
| 13802 | // Move the mouse outside the window. Document the current behavior, where the window does not |
| 13803 | // receive HOVER_EXIT even though the mouse left the window. |
| 13804 | mDispatcher->notifyMotion( |
| 13805 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 13806 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50)) |
| 13807 | .rawXCursorPosition(150) |
| 13808 | .rawYCursorPosition(50) |
| 13809 | .deviceId(SECOND_DEVICE_ID) |
| 13810 | .build()); |
| 13811 | |
| 13812 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 13813 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13814 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13815 | DEVICE_ID, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13816 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13817 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 13818 | SECOND_DEVICE_ID, |
| 13819 | /*pointerId=*/0)); |
| 13820 | } |
| 13821 | |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13822 | /** |
| 13823 | * TODO(b/313689709) - correctly support multiple mouse devices, because they should be controlling |
| 13824 | * the same cursor, and therefore have a shared motion event stream. |
| 13825 | */ |
| 13826 | TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse) { |
| 13827 | SCOPED_FLAG_OVERRIDE(enable_multi_device_same_window_stream, true); |
| 13828 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 13829 | |
| 13830 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13831 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13832 | left->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13833 | sp<FakeWindowHandle> right = |
| 13834 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right Window", |
| 13835 | ui::LogicalDisplayId::DEFAULT); |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13836 | right->setFrame(Rect(100, 0, 200, 100)); |
| 13837 | |
| 13838 | mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 13839 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13840 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13841 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13842 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13843 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13844 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13845 | /*pointerId=*/0)); |
| 13846 | |
| 13847 | // Hover move into the window. |
| 13848 | mDispatcher->notifyMotion( |
| 13849 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 13850 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50)) |
| 13851 | .rawXCursorPosition(50) |
| 13852 | .rawYCursorPosition(50) |
| 13853 | .deviceId(DEVICE_ID) |
| 13854 | .build()); |
| 13855 | |
| 13856 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13857 | |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13858 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13859 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13860 | /*pointerId=*/0)); |
| 13861 | |
| 13862 | // Move the mouse with another device |
| 13863 | mDispatcher->notifyMotion( |
| 13864 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 13865 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50)) |
| 13866 | .rawXCursorPosition(51) |
| 13867 | .rawYCursorPosition(50) |
| 13868 | .deviceId(SECOND_DEVICE_ID) |
| 13869 | .build()); |
| 13870 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 13871 | |
| 13872 | // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets |
| 13873 | // a HOVER_EXIT from the first device. |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13874 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13875 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13876 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13877 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13878 | SECOND_DEVICE_ID, |
| 13879 | /*pointerId=*/0)); |
| 13880 | |
| 13881 | // Move the mouse outside the window. Document the current behavior, where the window does not |
| 13882 | // receive HOVER_EXIT even though the mouse left the window. |
| 13883 | mDispatcher->notifyMotion( |
| 13884 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 13885 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50)) |
| 13886 | .rawXCursorPosition(150) |
| 13887 | .rawYCursorPosition(50) |
| 13888 | .deviceId(SECOND_DEVICE_ID) |
| 13889 | .build()); |
| 13890 | |
| 13891 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13892 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
| 13893 | DEVICE_ID, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13894 | /*pointerId=*/0)); |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 13895 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ui::LogicalDisplayId::DEFAULT, |
Siarhei Vishniakou | ed89cbb | 2023-10-13 10:42:44 -0700 | [diff] [blame] | 13896 | SECOND_DEVICE_ID, |
| 13897 | /*pointerId=*/0)); |
| 13898 | } |
| 13899 | |
Arpit Singh | b65e2bd | 2024-06-03 09:48:16 +0000 | [diff] [blame] | 13900 | TEST_F(InputDispatcherTest, FocusedDisplayChangeIsNotified) { |
| 13901 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 13902 | mFakePolicy->assertFocusedDisplayNotified(SECOND_DISPLAY_ID); |
| 13903 | } |
| 13904 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 13905 | } // namespace android::inputdispatcher |