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 | e3b28dd | 2023-10-06 04:19:29 +0000 | [diff] [blame] | 21 | #include "TestEventMatchers.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 22 | |
Cody Heiner | 166a5af | 2023-07-07 12:25:00 -0700 | [diff] [blame] | 23 | #include <NotifyArgsBuilders.h> |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 24 | #include <android-base/logging.h> |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 25 | #include <android-base/properties.h> |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 26 | #include <android-base/silent_death_test.h> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 27 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 28 | #include <android-base/thread_annotations.h> |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 29 | #include <binder/Binder.h> |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 30 | #include <com_android_input_flags.h> |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 31 | #include <fcntl.h> |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 32 | #include <flag_macros.h> |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 33 | #include <gmock/gmock.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 34 | #include <gtest/gtest.h> |
Siarhei Vishniakou | 3782af6 | 2024-03-07 21:56:39 -0800 | [diff] [blame] | 35 | #include <input/BlockingQueue.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 36 | #include <input/Input.h> |
Siarhei Vishniakou | 0438ca8 | 2024-03-12 14:27:25 -0700 | [diff] [blame] | 37 | #include <input/InputConsumer.h> |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 38 | #include <input/PrintTools.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 39 | #include <linux/input.h> |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 40 | #include <sys/epoll.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 41 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 42 | #include <cinttypes> |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 43 | #include <compare> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 44 | #include <thread> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 45 | #include <unordered_set> |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 46 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 47 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 48 | using android::base::StringPrintf; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 49 | using android::gui::FocusRequest; |
| 50 | using android::gui::TouchOcclusionMode; |
| 51 | using android::gui::WindowInfo; |
| 52 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 53 | using android::os::InputEventInjectionResult; |
| 54 | using android::os::InputEventInjectionSync; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 55 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 56 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 57 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 58 | using namespace ftl::flag_operators; |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 59 | using testing::AllOf; |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 60 | using testing::Not; |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 61 | |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 62 | namespace { |
| 63 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 64 | // An arbitrary time value. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 65 | static constexpr nsecs_t ARBITRARY_TIME = 1234; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 66 | |
| 67 | // An arbitrary device id. |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 68 | static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 69 | static constexpr int32_t SECOND_DEVICE_ID = 2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 70 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 71 | // An arbitrary display id. |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 72 | static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
| 73 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 74 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 75 | // Ensure common actions are interchangeable between keys and motions for convenience. |
| 76 | static_assert(AMOTION_EVENT_ACTION_DOWN == AKEY_EVENT_ACTION_DOWN); |
| 77 | static_assert(AMOTION_EVENT_ACTION_UP == AKEY_EVENT_ACTION_UP); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 78 | static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN; |
| 79 | static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE; |
| 80 | static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP; |
| 81 | static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 82 | static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 83 | static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 84 | static constexpr int32_t ACTION_SCROLL = AMOTION_EVENT_ACTION_SCROLL; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 85 | static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 86 | static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 87 | /** |
| 88 | * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the |
| 89 | * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at |
| 90 | * index 0) is the new pointer going down. The same pointer could have been placed at a different |
| 91 | * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In |
| 92 | * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if |
| 93 | * pointer id=0 leaves but the pointer id=1 remains. |
| 94 | */ |
| 95 | static constexpr int32_t POINTER_0_DOWN = |
| 96 | AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 97 | static constexpr int32_t POINTER_1_DOWN = |
| 98 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 99 | static constexpr int32_t POINTER_2_DOWN = |
| 100 | AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 101 | static constexpr int32_t POINTER_3_DOWN = |
| 102 | AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 103 | static constexpr int32_t POINTER_0_UP = |
| 104 | AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 105 | static constexpr int32_t POINTER_1_UP = |
| 106 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 107 | static constexpr int32_t POINTER_2_UP = |
| 108 | AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 109 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 110 | // The default pid and uid for windows created on the primary display by the test. |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 111 | static constexpr gui::Pid WINDOW_PID{999}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 112 | static constexpr gui::Uid WINDOW_UID{1001}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 113 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 114 | // 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] | 115 | static constexpr gui::Pid SECONDARY_WINDOW_PID{1010}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 116 | static constexpr gui::Uid SECONDARY_WINDOW_UID{1012}; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 117 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 118 | // An arbitrary pid of the gesture monitor window |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 119 | static constexpr gui::Pid MONITOR_PID{2001}; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 120 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 121 | /** |
| 122 | * If we expect to receive the event, the timeout can be made very long. When the test are running |
| 123 | * correctly, we will actually never wait until the end of the timeout because the wait will end |
| 124 | * when the event comes in. Still, this value shouldn't be infinite. During development, a local |
| 125 | * change may cause the test to fail. This timeout should be short enough to not annoy so that the |
| 126 | * developer can see the failure quickly (on human scale). |
| 127 | */ |
| 128 | static constexpr std::chrono::duration CONSUME_TIMEOUT_EVENT_EXPECTED = 1000ms; |
| 129 | /** |
| 130 | * When no event is expected, we can have a very short timeout. A large value here would slow down |
| 131 | * the tests. In the unlikely event of system being too slow, the event may still be present but the |
| 132 | * timeout would complete before it is consumed. This would result in test flakiness. If this |
| 133 | * occurs, the flakiness rate would be high. Since the flakes are treated with high priority, this |
| 134 | * would get noticed and addressed quickly. |
| 135 | */ |
| 136 | static constexpr std::chrono::duration CONSUME_TIMEOUT_NO_EVENT_EXPECTED = 10ms; |
| 137 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 138 | static constexpr int expectedWallpaperFlags = |
| 139 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 140 | |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 141 | using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID; |
| 142 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 143 | /** |
| 144 | * Return a DOWN key event with KEYCODE_A. |
| 145 | */ |
| 146 | static KeyEvent getTestKeyEvent() { |
| 147 | KeyEvent event; |
| 148 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 149 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 150 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 151 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 152 | return event; |
| 153 | } |
| 154 | |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 155 | } // namespace |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 156 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 157 | // --- InputDispatcherTest --- |
| 158 | |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 159 | // The trace is a global variable for now, to avoid having to pass it into all of the |
| 160 | // FakeWindowHandles created throughout the tests. |
| 161 | // TODO(b/210460522): Update the tests to avoid the need to have the trace be a global variable. |
| 162 | static std::shared_ptr<VerifyingTrace> gVerifyingTrace = std::make_shared<VerifyingTrace>(); |
| 163 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 164 | class InputDispatcherTest : public testing::Test { |
| 165 | protected: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 166 | std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 167 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 168 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 169 | void SetUp() override { |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 170 | gVerifyingTrace->reset(); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 171 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 172 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, |
| 173 | std::make_unique<FakeInputTracingBackend>( |
| 174 | gVerifyingTrace)); |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 175 | |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 176 | mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 177 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 178 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 181 | void TearDown() override { |
Prabir Pradhan | 0eaf140 | 2024-02-05 22:43:04 +0000 | [diff] [blame] | 182 | ASSERT_NO_FATAL_FAILURE(gVerifyingTrace->verifyExpectedEventsTraced()); |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 183 | ASSERT_EQ(OK, mDispatcher->stop()); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 184 | mFakePolicy.reset(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 185 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 186 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 187 | |
| 188 | /** |
| 189 | * Used for debugging when writing the test |
| 190 | */ |
| 191 | void dumpDispatcherState() { |
| 192 | std::string dump; |
| 193 | mDispatcher->dump(dump); |
| 194 | std::stringstream ss(dump); |
| 195 | std::string to; |
| 196 | |
| 197 | while (std::getline(ss, to, '\n')) { |
| 198 | ALOGE("%s", to.c_str()); |
| 199 | } |
| 200 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 201 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 202 | void setFocusedWindow(const sp<WindowInfoHandle>& window) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 203 | FocusRequest request; |
| 204 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 205 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 206 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 207 | request.displayId = window->getInfo()->displayId; |
| 208 | mDispatcher->setFocusedWindow(request); |
| 209 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 210 | }; |
| 211 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 212 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 213 | KeyEvent event; |
| 214 | |
| 215 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 216 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 217 | INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 218 | /*action=*/-1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 219 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 220 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 221 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 222 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 223 | << "Should reject key events with undefined action."; |
| 224 | |
| 225 | // Rejects ACTION_MULTIPLE since it is not supported despite being defined in the API. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 226 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 227 | INVALID_HMAC, AKEY_EVENT_ACTION_MULTIPLE, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 228 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 229 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 230 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 231 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 232 | << "Should reject key events with ACTION_MULTIPLE."; |
| 233 | } |
| 234 | |
| 235 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 236 | MotionEvent event; |
| 237 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 238 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 239 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 240 | pointerProperties[i].clear(); |
| 241 | pointerProperties[i].id = i; |
| 242 | pointerCoords[i].clear(); |
| 243 | } |
| 244 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 245 | // Some constants commonly used below |
| 246 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 247 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 248 | constexpr int32_t metaState = AMETA_NONE; |
| 249 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 250 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 251 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 252 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 253 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 254 | /*action=*/-1, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 255 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 256 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 257 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 258 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
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 motion events with undefined action."; |
| 263 | |
| 264 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 265 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 266 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 267 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 268 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 269 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 270 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 271 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 272 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 273 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 274 | << "Should reject motion events with pointer down index too large."; |
| 275 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 276 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 277 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 278 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 279 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 280 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 281 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 282 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 283 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 284 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 285 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 286 | << "Should reject motion events with pointer down index too small."; |
| 287 | |
| 288 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 289 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 290 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 291 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 292 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 293 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 294 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 295 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 296 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 297 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 298 | << "Should reject motion events with pointer up index too large."; |
| 299 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 300 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 301 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 302 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 303 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 304 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 305 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 306 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 307 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 308 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 309 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 310 | << "Should reject motion events with pointer up index too small."; |
| 311 | |
| 312 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 313 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 314 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 315 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 316 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 317 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 318 | /*pointerCount=*/0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 319 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 320 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 321 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 322 | << "Should reject motion events with 0 pointers."; |
| 323 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 324 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 325 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 326 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 327 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 328 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 329 | /*pointerCount=*/MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 330 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 331 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 332 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 333 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 334 | |
| 335 | // Rejects motion events with invalid pointer ids. |
| 336 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 337 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 338 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 339 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 340 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 341 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 342 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 343 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 344 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 345 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 346 | << "Should reject motion events with pointer ids less than 0."; |
| 347 | |
| 348 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 349 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 350 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 351 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 352 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 353 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 354 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 355 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 356 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 357 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 358 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 359 | |
| 360 | // Rejects motion events with duplicate pointer ids. |
| 361 | pointerProperties[0].id = 1; |
| 362 | pointerProperties[1].id = 1; |
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=*/2, 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 duplicate pointer ids."; |
| 373 | } |
| 374 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 375 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 376 | |
| 377 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 378 | constexpr nsecs_t eventTime = 20; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 379 | mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 380 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 381 | |
| 382 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 383 | } |
| 384 | |
| 385 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 386 | NotifySwitchArgs args(InputEvent::nextId(), /*eventTime=*/20, /*policyFlags=*/0, |
| 387 | /*switchValues=*/1, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 388 | /*switchMask=*/2); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 389 | mDispatcher->notifySwitch(args); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 390 | |
| 391 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 392 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 393 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 394 | } |
| 395 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 396 | namespace { |
| 397 | |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 398 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 399 | // Default input dispatching timeout if there is no focused application or paused window |
| 400 | // from which to determine an appropriate dispatching timeout. |
| 401 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 402 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 403 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 404 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 405 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 406 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 407 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 408 | : mConsumer(std::move(clientChannel)), mName(name) {} |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 409 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 410 | std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = false) { |
| 411 | auto [consumeSeq, event] = receiveEvent(timeout); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 412 | if (!consumeSeq) { |
| 413 | return nullptr; |
| 414 | } |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 415 | finishEvent(*consumeSeq, handled); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 416 | return std::move(event); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Receive an event without acknowledging it. |
| 421 | * Return the sequence number that could later be used to send finished signal. |
| 422 | */ |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 423 | std::pair<std::optional<uint32_t>, std::unique_ptr<InputEvent>> receiveEvent( |
| 424 | std::chrono::milliseconds timeout) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 425 | uint32_t consumeSeq; |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 426 | std::unique_ptr<InputEvent> event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 427 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 428 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 429 | status_t status = WOULD_BLOCK; |
| 430 | while (status == WOULD_BLOCK) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 431 | InputEvent* rawEventPtr = nullptr; |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 432 | status = mConsumer.consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 433 | &rawEventPtr); |
| 434 | event = std::unique_ptr<InputEvent>(rawEventPtr); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 435 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 436 | if (elapsed > timeout) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 437 | break; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | if (status == WOULD_BLOCK) { |
| 442 | // Just means there's no event available. |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 443 | return std::make_pair(std::nullopt, nullptr); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | if (status != OK) { |
| 447 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 448 | return std::make_pair(std::nullopt, nullptr); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 449 | } |
| 450 | if (event == nullptr) { |
| 451 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 452 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 453 | return std::make_pair(consumeSeq, std::move(event)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 454 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 455 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 456 | /** |
| 457 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 458 | */ |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 459 | void finishEvent(uint32_t consumeSeq, bool handled = true) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 460 | const status_t status = mConsumer.sendFinishedSignal(consumeSeq, handled); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 461 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 464 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 465 | const status_t status = mConsumer.sendTimeline(inputEventId, timeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 466 | ASSERT_EQ(OK, status); |
| 467 | } |
| 468 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 469 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 470 | std::optional<int32_t> expectedDisplayId, |
| 471 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 472 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 473 | |
| 474 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 475 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 476 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 477 | << mName.c_str() << " expected " << ftl::enum_string(expectedEventType) |
| 478 | << " event, got " << *event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 479 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 480 | if (expectedDisplayId.has_value()) { |
| 481 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 482 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 483 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 484 | switch (expectedEventType) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 485 | case InputEventType::KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 486 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 487 | ASSERT_THAT(keyEvent, WithKeyAction(expectedAction)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 488 | if (expectedFlags.has_value()) { |
| 489 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 490 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 491 | break; |
| 492 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 493 | case InputEventType::MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 494 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 495 | ASSERT_THAT(motionEvent, WithMotionAction(expectedAction)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 496 | if (expectedFlags.has_value()) { |
| 497 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 498 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 499 | break; |
| 500 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 501 | case InputEventType::FOCUS: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 502 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 503 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 504 | case InputEventType::CAPTURE: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 505 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 506 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 507 | case InputEventType::TOUCH_MODE: { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 508 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 509 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 510 | case InputEventType::DRAG: { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 511 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 512 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 513 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 514 | } |
| 515 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 516 | std::unique_ptr<MotionEvent> consumeMotion() { |
| 517 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 518 | |
| 519 | if (event == nullptr) { |
| 520 | ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one."; |
| 521 | return nullptr; |
| 522 | } |
| 523 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 524 | if (event->getType() != InputEventType::MOTION) { |
| 525 | ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 526 | return nullptr; |
| 527 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 528 | return std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event.release())); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 532 | std::unique_ptr<MotionEvent> motionEvent = consumeMotion(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 533 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; |
| 534 | ASSERT_THAT(*motionEvent, matcher); |
| 535 | } |
| 536 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 537 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 538 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 539 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 540 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 541 | ASSERT_EQ(InputEventType::FOCUS, event->getType()) |
| 542 | << "Instead of FocusEvent, got " << *event; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 543 | |
| 544 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 545 | << mName.c_str() << ": event displayId should always be NONE."; |
| 546 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 547 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 548 | EXPECT_EQ(hasFocus, focusEvent.getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 549 | } |
| 550 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 551 | void consumeCaptureEvent(bool hasCapture) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 552 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 553 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 554 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 555 | ASSERT_EQ(InputEventType::CAPTURE, event->getType()) |
| 556 | << "Instead of CaptureEvent, got " << *event; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 557 | |
| 558 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 559 | << mName.c_str() << ": event displayId should always be NONE."; |
| 560 | |
| 561 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 562 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 563 | } |
| 564 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 565 | void consumeDragEvent(bool isExiting, float x, float y) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 566 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 567 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 568 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 569 | ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 570 | |
| 571 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 572 | << mName.c_str() << ": event displayId should always be NONE."; |
| 573 | |
| 574 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 575 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 576 | EXPECT_EQ(x, dragEvent.getX()); |
| 577 | EXPECT_EQ(y, dragEvent.getY()); |
| 578 | } |
| 579 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 580 | void consumeTouchModeEvent(bool inTouchMode) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 581 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 582 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 583 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 584 | ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType()) |
| 585 | << "Instead of TouchModeEvent, got " << *event; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 586 | |
| 587 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 588 | << mName.c_str() << ": event displayId should always be NONE."; |
| 589 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 590 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 591 | } |
| 592 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 593 | void assertNoEvents(std::chrono::milliseconds timeout) { |
| 594 | std::unique_ptr<InputEvent> event = consume(timeout); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 595 | if (event == nullptr) { |
| 596 | return; |
| 597 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 598 | if (event->getType() == InputEventType::KEY) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 599 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 600 | ADD_FAILURE() << "Received key event " << keyEvent; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 601 | } else if (event->getType() == InputEventType::MOTION) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 602 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 603 | ADD_FAILURE() << "Received motion event " << motionEvent; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 604 | } else if (event->getType() == InputEventType::FOCUS) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 605 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 606 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 607 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 608 | } else if (event->getType() == InputEventType::CAPTURE) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 609 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 610 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 611 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 612 | } else if (event->getType() == InputEventType::TOUCH_MODE) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 613 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 614 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 615 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 616 | } |
| 617 | FAIL() << mName.c_str() |
| 618 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 619 | } |
| 620 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 621 | sp<IBinder> getToken() { return mConsumer.getChannel()->getConnectionToken(); } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 622 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 623 | int getChannelFd() { return mConsumer.getChannel()->getFd(); } |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 624 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 625 | private: |
| 626 | InputConsumer mConsumer; |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 627 | DynamicInputEventFactory mEventFactory; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 628 | |
| 629 | std::string mName; |
| 630 | }; |
| 631 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 632 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 633 | public: |
| 634 | static const int32_t WIDTH = 600; |
| 635 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 636 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 637 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 638 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 639 | int32_t displayId, bool createInputChannel = true) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 640 | : mName(name) { |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 641 | sp<IBinder> token; |
| 642 | if (createInputChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 643 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 644 | dispatcher->createInputChannel(name); |
| 645 | token = (*channel)->getConnectionToken(); |
| 646 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | inputApplicationHandle->updateInfo(); |
| 650 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 651 | |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 652 | mInfo.token = token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 653 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 654 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 655 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 656 | mInfo.alpha = 1.0; |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 657 | mInfo.frame = Rect(0, 0, WIDTH, HEIGHT); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 658 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 659 | mInfo.globalScaleFactor = 1.0; |
| 660 | mInfo.touchableRegion.clear(); |
| 661 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 662 | mInfo.ownerPid = WINDOW_PID; |
| 663 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 664 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 665 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 666 | } |
| 667 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 668 | sp<FakeWindowHandle> clone(int32_t displayId) { |
| 669 | sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)"); |
| 670 | handle->mInfo = mInfo; |
| 671 | handle->mInfo.displayId = displayId; |
| 672 | handle->mInfo.id = sId++; |
| 673 | handle->mInputReceiver = mInputReceiver; |
| 674 | return handle; |
| 675 | } |
| 676 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 677 | void setTouchable(bool touchable) { |
| 678 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 679 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 680 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 681 | void setFocusable(bool focusable) { |
| 682 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 683 | } |
| 684 | |
| 685 | void setVisible(bool visible) { |
| 686 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 687 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 688 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 689 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 690 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 693 | void setPaused(bool paused) { |
| 694 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 695 | } |
| 696 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 697 | void setPreventSplitting(bool preventSplitting) { |
| 698 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | void setSlippery(bool slippery) { |
| 702 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 703 | } |
| 704 | |
| 705 | void setWatchOutsideTouch(bool watchOutside) { |
| 706 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 707 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 708 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 709 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 710 | |
| 711 | void setInterceptsStylus(bool interceptsStylus) { |
| 712 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 713 | } |
| 714 | |
| 715 | void setDropInput(bool dropInput) { |
| 716 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 717 | } |
| 718 | |
| 719 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 720 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 721 | } |
| 722 | |
| 723 | void setNoInputChannel(bool noInputChannel) { |
| 724 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 725 | } |
| 726 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 727 | void setDisableUserActivity(bool disableUserActivity) { |
| 728 | mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity); |
| 729 | } |
| 730 | |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 731 | void setGlobalStylusBlocksTouch(bool shouldGlobalStylusBlockTouch) { |
| 732 | mInfo.setInputConfig(WindowInfo::InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH, |
| 733 | shouldGlobalStylusBlockTouch); |
| 734 | } |
| 735 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 736 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 737 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 738 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 739 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 740 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 741 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 742 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 743 | mInfo.frame = frame; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 744 | mInfo.touchableRegion.clear(); |
| 745 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 746 | |
| 747 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 748 | ui::Transform translate; |
| 749 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 750 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 751 | } |
| 752 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 753 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 754 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 755 | void setIsWallpaper(bool isWallpaper) { |
| 756 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 757 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 758 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 759 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 760 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 761 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 762 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 763 | void setTrustedOverlay(bool trustedOverlay) { |
| 764 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 765 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 766 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 767 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 768 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 769 | } |
| 770 | |
| 771 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 772 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 773 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 774 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 775 | std::unique_ptr<KeyEvent> consumeKey(bool handled = true) { |
| 776 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED, handled); |
| 777 | if (event == nullptr) { |
| 778 | ADD_FAILURE() << "No event"; |
| 779 | return nullptr; |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 780 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 781 | if (event->getType() != InputEventType::KEY) { |
| 782 | ADD_FAILURE() << "Instead of key event, got " << event; |
| 783 | return nullptr; |
| 784 | } |
| 785 | return std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event.release())); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | void consumeKeyEvent(const ::testing::Matcher<KeyEvent>& matcher) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 789 | std::unique_ptr<KeyEvent> keyEvent = consumeKey(); |
| 790 | ASSERT_NE(nullptr, keyEvent); |
| 791 | ASSERT_THAT(*keyEvent, matcher); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 794 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 795 | consumeKeyEvent(AllOf(WithKeyAction(ACTION_DOWN), WithDisplayId(expectedDisplayId), |
| 796 | WithFlags(expectedFlags))); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 797 | } |
| 798 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 799 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 800 | consumeKeyEvent(AllOf(WithKeyAction(ACTION_UP), WithDisplayId(expectedDisplayId), |
| 801 | WithFlags(expectedFlags))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 802 | } |
| 803 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 804 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 805 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 806 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId), |
| 807 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 811 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 812 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 813 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 817 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 818 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 819 | } |
| 820 | |
| 821 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 822 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 823 | consumeMotionEvent( |
| 824 | AllOf(WithMotionAction(ACTION_DOWN), |
| 825 | testing::Conditional(expectedDisplayId.has_value(), |
| 826 | WithDisplayId(*expectedDisplayId), testing::_), |
| 827 | testing::Conditional(expectedFlags.has_value(), WithFlags(*expectedFlags), |
| 828 | testing::_))); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 829 | } |
| 830 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 831 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 832 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 833 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 834 | const int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 835 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 836 | consumeMotionEvent(AllOf(WithMotionAction(action), WithDisplayId(expectedDisplayId), |
| 837 | WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 841 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 842 | const int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 843 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 844 | consumeMotionEvent(AllOf(WithMotionAction(action), WithDisplayId(expectedDisplayId), |
| 845 | WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 849 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 850 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDisplayId(expectedDisplayId), |
| 851 | WithFlags(expectedFlags))); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 854 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 855 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 856 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE), |
| 857 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 858 | } |
| 859 | |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 860 | void consumeMotionOutsideWithZeroedCoords() { |
| 861 | consumeMotionEvent( |
| 862 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE), WithRawCoords(0, 0))); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 863 | } |
| 864 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 865 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 866 | ASSERT_NE(mInputReceiver, nullptr) |
| 867 | << "Cannot consume events from a window with no receiver"; |
| 868 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 869 | } |
| 870 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 871 | void consumeCaptureEvent(bool hasCapture) { |
| 872 | ASSERT_NE(mInputReceiver, nullptr) |
| 873 | << "Cannot consume events from a window with no receiver"; |
| 874 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 875 | } |
| 876 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 877 | std::unique_ptr<MotionEvent> consumeMotionEvent( |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 878 | const ::testing::Matcher<MotionEvent>& matcher = testing::_) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 879 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
| 880 | if (event == nullptr) { |
| 881 | ADD_FAILURE() << "No event"; |
| 882 | return nullptr; |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 883 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 884 | if (event->getType() != InputEventType::MOTION) { |
| 885 | ADD_FAILURE() << "Instead of motion event, got " << *event; |
| 886 | return nullptr; |
| 887 | } |
| 888 | std::unique_ptr<MotionEvent> motionEvent = |
| 889 | std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event.release())); |
| 890 | EXPECT_THAT(*motionEvent, matcher); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 891 | return motionEvent; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 892 | } |
| 893 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 894 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 895 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 896 | } |
| 897 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 898 | void consumeTouchModeEvent(bool inTouchMode) { |
| 899 | ASSERT_NE(mInputReceiver, nullptr) |
| 900 | << "Cannot consume events from a window with no receiver"; |
| 901 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 902 | } |
| 903 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 904 | std::pair<std::optional<uint32_t>, std::unique_ptr<InputEvent>> receiveEvent() { |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 905 | return receive(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | void finishEvent(uint32_t sequenceNum) { |
| 909 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 910 | mInputReceiver->finishEvent(sequenceNum); |
| 911 | } |
| 912 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 913 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 914 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 915 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 916 | } |
| 917 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 918 | void assertNoEvents(std::chrono::milliseconds timeout = CONSUME_TIMEOUT_NO_EVENT_EXPECTED) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 919 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 920 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 921 | return; // Can't receive events if the window does not have input channel |
| 922 | } |
| 923 | ASSERT_NE(nullptr, mInputReceiver) |
| 924 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 925 | mInputReceiver->assertNoEvents(timeout); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 926 | } |
| 927 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 928 | sp<IBinder> getToken() { return mInfo.token; } |
| 929 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 930 | const std::string& getName() { return mName; } |
| 931 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 932 | void setOwnerInfo(gui::Pid ownerPid, gui::Uid ownerUid) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 933 | mInfo.ownerPid = ownerPid; |
| 934 | mInfo.ownerUid = ownerUid; |
| 935 | } |
| 936 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 937 | gui::Pid getPid() const { return mInfo.ownerPid; } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 938 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 939 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 940 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 941 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 942 | |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 943 | // FakeWindowHandle uses this consume method to ensure received events are added to the trace. |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 944 | std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = true) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 945 | if (mInputReceiver == nullptr) { |
| 946 | LOG(FATAL) << "Cannot consume event from a window with no input event receiver"; |
| 947 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 948 | std::unique_ptr<InputEvent> event = mInputReceiver->consume(timeout, handled); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 949 | if (event == nullptr) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 950 | ADD_FAILURE() << "Consume failed: no event"; |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 951 | } |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 952 | expectReceivedEventTraced(event); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 953 | return event; |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 954 | } |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 955 | |
| 956 | private: |
| 957 | FakeWindowHandle(std::string name) : mName(name){}; |
| 958 | const std::string mName; |
| 959 | std::shared_ptr<FakeInputReceiver> mInputReceiver; |
| 960 | static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger |
| 961 | friend class sp<FakeWindowHandle>; |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 962 | |
| 963 | // FakeWindowHandle uses this receive method to ensure received events are added to the trace. |
| 964 | std::pair<std::optional<uint32_t /*seq*/>, std::unique_ptr<InputEvent>> receive() { |
| 965 | if (mInputReceiver == nullptr) { |
| 966 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 967 | return std::make_pair(std::nullopt, nullptr); |
| 968 | } |
| 969 | auto out = mInputReceiver->receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED); |
| 970 | const auto& [_, event] = out; |
| 971 | expectReceivedEventTraced(event); |
| 972 | return std::move(out); |
| 973 | } |
| 974 | |
| 975 | void expectReceivedEventTraced(const std::unique_ptr<InputEvent>& event) { |
| 976 | if (!event) { |
| 977 | return; |
| 978 | } |
| 979 | |
| 980 | switch (event->getType()) { |
| 981 | case InputEventType::KEY: { |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 982 | gVerifyingTrace->expectKeyDispatchTraced(static_cast<KeyEvent&>(*event), mInfo.id); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 983 | break; |
| 984 | } |
| 985 | case InputEventType::MOTION: { |
Prabir Pradhan | 4497c86 | 2023-12-15 07:13:30 +0000 | [diff] [blame] | 986 | gVerifyingTrace->expectMotionDispatchTraced(static_cast<MotionEvent&>(*event), |
| 987 | mInfo.id); |
Prabir Pradhan | dc3a2ad | 2024-02-05 19:03:51 +0000 | [diff] [blame] | 988 | break; |
| 989 | } |
| 990 | default: |
| 991 | break; |
| 992 | } |
| 993 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 994 | }; |
| 995 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 996 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 997 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 998 | class FakeMonitorReceiver { |
| 999 | public: |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1000 | FakeMonitorReceiver(InputDispatcher& dispatcher, const std::string name, int32_t displayId) |
| 1001 | : mInputReceiver(*dispatcher.createInputMonitor(displayId, name, MONITOR_PID), name) {} |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1002 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1003 | sp<IBinder> getToken() { return mInputReceiver.getToken(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1004 | |
| 1005 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1006 | mInputReceiver.consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1007 | expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | std::optional<int32_t> receiveEvent() { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1011 | const auto [sequenceNum, _] = mInputReceiver.receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED); |
| 1012 | return sequenceNum; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1015 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver.finishEvent(consumeSeq); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1016 | |
| 1017 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1018 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 1019 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1023 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 1024 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1028 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, |
| 1029 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1033 | mInputReceiver.consumeMotionEvent( |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1034 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 1035 | WithDisplayId(expectedDisplayId), |
| 1036 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
| 1037 | } |
| 1038 | |
| 1039 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 1040 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1041 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1042 | mInputReceiver.consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT, |
| 1043 | /*expectedFlags=*/0); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1047 | mInputReceiver.consumeMotionEvent(matcher); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1050 | std::unique_ptr<MotionEvent> consumeMotion() { return mInputReceiver.consumeMotion(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1051 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 1052 | void assertNoEvents() { mInputReceiver.assertNoEvents(CONSUME_TIMEOUT_NO_EVENT_EXPECTED); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1053 | |
| 1054 | private: |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1055 | FakeInputReceiver mInputReceiver; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1056 | }; |
| 1057 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1058 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1059 | InputDispatcher& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1060 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1061 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1062 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1063 | bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1064 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1065 | KeyEvent event; |
| 1066 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1067 | |
| 1068 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1069 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1070 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 1071 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1072 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1073 | if (!allowKeyRepeat) { |
| 1074 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1075 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1076 | // Inject event until dispatch out. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1077 | return dispatcher.injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1078 | } |
| 1079 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1080 | static void assertInjectedKeyTimesOut(InputDispatcher& dispatcher) { |
| 1081 | InputEventInjectionResult result = |
| 1082 | injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
| 1083 | InputEventInjectionSync::WAIT_FOR_RESULT, CONSUME_TIMEOUT_NO_EVENT_EXPECTED); |
| 1084 | if (result != InputEventInjectionResult::TIMED_OUT) { |
| 1085 | FAIL() << "Injection should have timed out, but got " << ftl::enum_string(result); |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | static InputEventInjectionResult injectKeyDown(InputDispatcher& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1090 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1091 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1094 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1095 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1096 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1097 | static InputEventInjectionResult injectKeyDownNoRepeat(InputDispatcher& dispatcher, |
| 1098 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1099 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1100 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1101 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1102 | } |
| 1103 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1104 | static InputEventInjectionResult injectKeyUp(InputDispatcher& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1105 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1106 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1109 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1110 | InputDispatcher& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1111 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1112 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1113 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1114 | return dispatcher.injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1115 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1118 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1119 | InputDispatcher& dispatcher, int32_t action, int32_t source, int32_t displayId, |
| 1120 | const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1121 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1122 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1123 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1124 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1125 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1126 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1127 | MotionEventBuilder motionBuilder = |
| 1128 | MotionEventBuilder(action, source) |
| 1129 | .displayId(displayId) |
| 1130 | .eventTime(eventTime) |
| 1131 | .rawXCursorPosition(cursorPosition.x) |
| 1132 | .rawYCursorPosition(cursorPosition.y) |
| 1133 | .pointer( |
| 1134 | PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y)); |
| 1135 | if (MotionEvent::getActionMasked(action) == ACTION_DOWN) { |
| 1136 | motionBuilder.downTime(eventTime); |
| 1137 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1138 | |
| 1139 | // Inject event until dispatch out. |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1140 | return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode, |
| 1141 | targetUid, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1142 | } |
| 1143 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1144 | static InputEventInjectionResult injectMotionDown(InputDispatcher& dispatcher, int32_t source, |
| 1145 | int32_t displayId, |
| 1146 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1147 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1150 | static InputEventInjectionResult injectMotionUp(InputDispatcher& dispatcher, int32_t source, |
| 1151 | int32_t displayId, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1152 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1153 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1156 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1157 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1158 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1159 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 1160 | AINPUT_SOURCE_KEYBOARD, displayId, POLICY_FLAG_PASS_TO_USER, action, |
| 1161 | /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1162 | |
| 1163 | return args; |
| 1164 | } |
| 1165 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1166 | static NotifyKeyArgs generateSystemShortcutArgs(int32_t action, |
| 1167 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1168 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1169 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1170 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 1171 | AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_C, KEY_C, |
| 1172 | AMETA_META_ON, currentTime); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1173 | |
| 1174 | return args; |
| 1175 | } |
| 1176 | |
| 1177 | static NotifyKeyArgs generateAssistantKeyArgs(int32_t action, |
| 1178 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1179 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1180 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1181 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 1182 | AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_ASSIST, |
| 1183 | KEY_ASSISTANT, AMETA_NONE, currentTime); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1184 | |
| 1185 | return args; |
| 1186 | } |
| 1187 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1188 | [[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
| 1189 | int32_t displayId, |
| 1190 | const std::vector<PointF>& points) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1191 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1192 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1193 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1194 | } |
| 1195 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1196 | PointerProperties pointerProperties[pointerCount]; |
| 1197 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1198 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1199 | for (size_t i = 0; i < pointerCount; i++) { |
| 1200 | pointerProperties[i].clear(); |
| 1201 | pointerProperties[i].id = i; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1202 | pointerProperties[i].toolType = ToolType::FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1203 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1204 | pointerCoords[i].clear(); |
| 1205 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1206 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1207 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1208 | |
| 1209 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1210 | // Define a valid motion event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1211 | NotifyMotionArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, source, |
| 1212 | displayId, POLICY_FLAG_PASS_TO_USER, action, /*actionButton=*/0, |
| 1213 | /*flags=*/0, AMETA_NONE, /*buttonState=*/0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1214 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1215 | pointerCoords, /*xPrecision=*/0, /*yPrecision=*/0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1216 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1217 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /*videoFrames=*/{}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1218 | |
| 1219 | return args; |
| 1220 | } |
| 1221 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1222 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1223 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1224 | } |
| 1225 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1226 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1227 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1228 | } |
| 1229 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1230 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1231 | const PointerCaptureRequest& request) { |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1232 | return NotifyPointerCaptureChangedArgs(InputEvent::nextId(), systemTime(SYSTEM_TIME_MONOTONIC), |
| 1233 | request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1234 | } |
| 1235 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 1236 | } // namespace |
| 1237 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1238 | /** |
| 1239 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1240 | * broken channel. |
| 1241 | */ |
| 1242 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1243 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1244 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1245 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1246 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1247 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1248 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1249 | |
| 1250 | // Window closes its channel, but the window remains. |
| 1251 | window->destroyReceiver(); |
| 1252 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1253 | } |
| 1254 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1255 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1256 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1257 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1258 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1259 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1260 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1261 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1262 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1263 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1264 | |
| 1265 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1266 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1267 | } |
| 1268 | |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 1269 | using InputDispatcherDeathTest = InputDispatcherTest; |
| 1270 | |
| 1271 | /** |
| 1272 | * When 'onWindowInfosChanged' arguments contain a duplicate entry for the same window, dispatcher |
| 1273 | * should crash. |
| 1274 | */ |
| 1275 | TEST_F(InputDispatcherDeathTest, DuplicateWindowInfosAbortDispatcher) { |
| 1276 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
| 1277 | ScopedSilentDeath _silentDeath; |
| 1278 | |
| 1279 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1280 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1281 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1282 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 1283 | {{*window->getInfo(), *window->getInfo()}, {}, 0, 0}), |
| 1284 | "Incorrect WindowInfosUpdate provided"); |
| 1285 | } |
| 1286 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1287 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1288 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1289 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1290 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1291 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1292 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1293 | // Inject a MotionEvent to an unknown display. |
| 1294 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1295 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1296 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1297 | |
| 1298 | // Window should receive motion event. |
| 1299 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1300 | } |
| 1301 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1302 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1303 | * Calling onWindowInfosChanged once should not cause any issues. |
| 1304 | * 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] | 1305 | * called twice. |
| 1306 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1307 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1308 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1309 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1310 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1311 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1312 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1313 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1314 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1315 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1316 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1317 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1318 | |
| 1319 | // Window should receive motion event. |
| 1320 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1321 | } |
| 1322 | |
| 1323 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1324 | * Calling onWindowInfosChanged twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1325 | */ |
| 1326 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1327 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1328 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1329 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1330 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1331 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1332 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1333 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1334 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1335 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1336 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1337 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1338 | |
| 1339 | // Window should receive motion event. |
| 1340 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1341 | } |
| 1342 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1343 | // The foreground window should receive the first touch down event. |
| 1344 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1345 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1346 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1347 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1348 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1349 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1350 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1351 | mDispatcher->onWindowInfosChanged( |
| 1352 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1353 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1354 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1355 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1356 | |
| 1357 | // Top window should receive the touch down event. Second window should not receive anything. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1358 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1359 | windowSecond->assertNoEvents(); |
| 1360 | } |
| 1361 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1362 | /** |
| 1363 | * Two windows: A top window, and a wallpaper behind the window. |
| 1364 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1365 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1366 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1367 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1368 | */ |
| 1369 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1370 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1371 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1372 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1373 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1374 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1375 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1376 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1377 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1378 | mDispatcher->onWindowInfosChanged( |
| 1379 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1380 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1381 | injectMotionEvent(*mDispatcher, |
| 1382 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1383 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(200)) |
| 1384 | .build())) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1385 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1386 | |
| 1387 | // Both foreground window and its wallpaper should receive the touch down |
| 1388 | foregroundWindow->consumeMotionDown(); |
| 1389 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1390 | |
| 1391 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1392 | injectMotionEvent(*mDispatcher, |
| 1393 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1394 | .pointer(PointerBuilder(0, ToolType::FINGER).x(110).y(200)) |
| 1395 | .build())) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1396 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1397 | |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1398 | foregroundWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1399 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1400 | |
| 1401 | // Now the foreground window goes away, but the wallpaper stays |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1402 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1403 | foregroundWindow->consumeMotionCancel(); |
| 1404 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1405 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1406 | } |
| 1407 | |
| 1408 | /** |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1409 | * Two fingers down on the window, and lift off the first finger. |
| 1410 | * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event |
| 1411 | * contains a single pointer. |
| 1412 | */ |
| 1413 | TEST_F(InputDispatcherTest, CancelAfterPointer0Up) { |
| 1414 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1415 | sp<FakeWindowHandle> window = |
| 1416 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1417 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1418 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1419 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1420 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1421 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1422 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1423 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1424 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1425 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1426 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1427 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1428 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1429 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1430 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1431 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1432 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1433 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 1434 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1435 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 1436 | |
| 1437 | // Remove the window. The gesture should be canceled |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1438 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1439 | const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}}; |
| 1440 | window->consumeMotionEvent( |
| 1441 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers))); |
| 1442 | } |
| 1443 | |
| 1444 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1445 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1446 | * with the following differences: |
| 1447 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1448 | * clean up the connection. |
| 1449 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1450 | * Ensure that there's no crash in the dispatcher. |
| 1451 | */ |
| 1452 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1453 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1454 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1455 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1456 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1457 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1458 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1459 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1460 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1461 | mDispatcher->onWindowInfosChanged( |
| 1462 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1463 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1464 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1465 | {100, 200})) |
| 1466 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1467 | |
| 1468 | // Both foreground window and its wallpaper should receive the touch down |
| 1469 | foregroundWindow->consumeMotionDown(); |
| 1470 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1471 | |
| 1472 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1473 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1474 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1475 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1476 | |
| 1477 | foregroundWindow->consumeMotionMove(); |
| 1478 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1479 | |
| 1480 | // Wallpaper closes its channel, but the window remains. |
| 1481 | wallpaperWindow->destroyReceiver(); |
| 1482 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 1483 | |
| 1484 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 1485 | // is no longer valid. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1486 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1487 | foregroundWindow->consumeMotionCancel(); |
| 1488 | } |
| 1489 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1490 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 1491 | public ::testing::WithParamInterface<bool> {}; |
| 1492 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 1493 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1494 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1495 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1496 | * The top window gets a multitouch gesture. |
| 1497 | * Ensure that wallpaper gets the same gesture. |
| 1498 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1499 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1500 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1501 | sp<FakeWindowHandle> foregroundWindow = |
| 1502 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 1503 | foregroundWindow->setDupTouchToWallpaper(true); |
| 1504 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1505 | |
| 1506 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1507 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1508 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1509 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1510 | mDispatcher->onWindowInfosChanged( |
| 1511 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1512 | |
| 1513 | // Touch down on top window |
| 1514 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1515 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1516 | {100, 100})) |
| 1517 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1518 | |
| 1519 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1520 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1521 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1522 | |
| 1523 | // Second finger down on the top window |
| 1524 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1525 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1526 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1527 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1528 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1529 | .build(); |
| 1530 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1531 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1532 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1533 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1534 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1535 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
| 1536 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1537 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1538 | |
| 1539 | const MotionEvent secondFingerUpEvent = |
| 1540 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1541 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1542 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1543 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1544 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1545 | .build(); |
| 1546 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1547 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1548 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1549 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1550 | foregroundWindow->consumeMotionPointerUp(0); |
| 1551 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1552 | |
| 1553 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1554 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1555 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1556 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1557 | .displayId(ADISPLAY_ID_DEFAULT) |
| 1558 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1559 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 1560 | .x(100) |
| 1561 | .y(100)) |
| 1562 | .build(), |
| 1563 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1564 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1565 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 1566 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | /** |
| 1570 | * Two windows: a window on the left and window on the right. |
| 1571 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 1572 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 1573 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 1574 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 1575 | * ACTION_POINTER_DOWN(1). |
| 1576 | */ |
| 1577 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 1578 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1579 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1580 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1581 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1582 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1583 | |
| 1584 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1585 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1586 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1587 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1588 | |
| 1589 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1590 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1591 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1592 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1593 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1594 | mDispatcher->onWindowInfosChanged( |
| 1595 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 1596 | {}, |
| 1597 | 0, |
| 1598 | 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1599 | |
| 1600 | // Touch down on left window |
| 1601 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1602 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1603 | {100, 100})) |
| 1604 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1605 | |
| 1606 | // Both foreground window and its wallpaper should receive the touch down |
| 1607 | leftWindow->consumeMotionDown(); |
| 1608 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1609 | |
| 1610 | // Second finger down on the right window |
| 1611 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1612 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1613 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1614 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1615 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1616 | .build(); |
| 1617 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1618 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1619 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1620 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1621 | |
| 1622 | leftWindow->consumeMotionMove(); |
| 1623 | // Since the touch is split, right window gets ACTION_DOWN |
| 1624 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1625 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1626 | expectedWallpaperFlags); |
| 1627 | |
| 1628 | // Now, leftWindow, which received the first finger, disappears. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1629 | mDispatcher->onWindowInfosChanged( |
| 1630 | {{*rightWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1631 | leftWindow->consumeMotionCancel(); |
| 1632 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1633 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1634 | |
| 1635 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 1636 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 1637 | const MotionEvent secondFingerMoveEvent = |
| 1638 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1639 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1640 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1641 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1642 | .build(); |
| 1643 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1644 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1645 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 1646 | rightWindow->consumeMotionMove(); |
| 1647 | |
| 1648 | leftWindow->assertNoEvents(); |
| 1649 | rightWindow->assertNoEvents(); |
| 1650 | wallpaperWindow->assertNoEvents(); |
| 1651 | } |
| 1652 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1653 | /** |
| 1654 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 1655 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 1656 | * The right window should receive ACTION_DOWN. |
| 1657 | */ |
| 1658 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1659 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1660 | sp<FakeWindowHandle> leftWindow = |
| 1661 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 1662 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1663 | leftWindow->setDupTouchToWallpaper(true); |
| 1664 | leftWindow->setSlippery(true); |
| 1665 | |
| 1666 | sp<FakeWindowHandle> rightWindow = |
| 1667 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 1668 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1669 | |
| 1670 | sp<FakeWindowHandle> wallpaperWindow = |
| 1671 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 1672 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1673 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1674 | mDispatcher->onWindowInfosChanged( |
| 1675 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 1676 | {}, |
| 1677 | 0, |
| 1678 | 0}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1679 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1680 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1681 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1682 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1683 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1684 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1685 | |
| 1686 | // Both foreground window and its wallpaper should receive the touch down |
| 1687 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1688 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1689 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1690 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1691 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1692 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1693 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1694 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1695 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1696 | leftWindow->consumeMotionCancel(); |
| 1697 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1698 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1701 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1702 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 1703 | * interactive, it might stop sending this flag. |
| 1704 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 1705 | * to have a consistent input stream. |
| 1706 | * |
| 1707 | * Test procedure: |
| 1708 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 1709 | * DOWN (new gesture). |
| 1710 | * |
| 1711 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 1712 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 1713 | * |
| 1714 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 1715 | * regular window below) to emulate the actual situation where it happens on the device. |
| 1716 | */ |
| 1717 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 1718 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1719 | sp<FakeWindowHandle> spyWindow = |
| 1720 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 1721 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1722 | spyWindow->setTrustedOverlay(true); |
| 1723 | spyWindow->setSpy(true); |
| 1724 | |
| 1725 | sp<FakeWindowHandle> window = |
| 1726 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1727 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1728 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1729 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1730 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1731 | |
| 1732 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1733 | mDispatcher->notifyMotion( |
| 1734 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1735 | .deviceId(touchDeviceId) |
| 1736 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1737 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1738 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1739 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1740 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1741 | .deviceId(touchDeviceId) |
| 1742 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1743 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1744 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 1745 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1746 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1747 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1748 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1749 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1750 | |
| 1751 | // Cancel the current gesture. Send the cancel without the default policy flags. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1752 | mDispatcher->notifyMotion( |
| 1753 | MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 1754 | .deviceId(touchDeviceId) |
| 1755 | .policyFlags(0) |
| 1756 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1757 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 1758 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1759 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 1760 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 1761 | |
| 1762 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 1763 | // 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] | 1764 | mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1765 | |
| 1766 | // Start new gesture |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1767 | mDispatcher->notifyMotion( |
| 1768 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1769 | .deviceId(touchDeviceId) |
| 1770 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1771 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1772 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 1773 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1774 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1775 | |
| 1776 | // No more events |
| 1777 | spyWindow->assertNoEvents(); |
| 1778 | window->assertNoEvents(); |
| 1779 | } |
| 1780 | |
| 1781 | /** |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 1782 | * Same as the above 'TwoPointerCancelInconsistentPolicy' test, but for hovers. |
| 1783 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 1784 | * interactive, it might stop sending this flag. |
| 1785 | * We've already ensured the consistency of the touch event in this case, and we should also ensure |
| 1786 | * the consistency of the hover event in this case. |
| 1787 | * |
| 1788 | * Test procedure: |
| 1789 | * HOVER_ENTER -> HOVER_MOVE -> (stop sending POLICY_FLAG_PASS_TO_USER) -> HOVER_EXIT |
| 1790 | * HOVER_ENTER -> HOVER_MOVE -> HOVER_EXIT |
| 1791 | * |
| 1792 | * We expect to receive two full streams of hover events. |
| 1793 | */ |
| 1794 | TEST_F(InputDispatcherTest, HoverEventInconsistentPolicy) { |
| 1795 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1796 | |
| 1797 | sp<FakeWindowHandle> window = |
| 1798 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1799 | window->setFrame(Rect(0, 0, 300, 300)); |
| 1800 | |
| 1801 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1802 | |
| 1803 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1804 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1805 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 1806 | .build()); |
| 1807 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1808 | |
| 1809 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1810 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1811 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 1812 | .build()); |
| 1813 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 1814 | |
| 1815 | // Send hover exit without the default policy flags. |
| 1816 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 1817 | .policyFlags(0) |
| 1818 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 1819 | .build()); |
| 1820 | |
| 1821 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1822 | |
| 1823 | // Send a simple hover event stream, ensure dispatcher not crashed and window can receive |
| 1824 | // right event. |
| 1825 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1826 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1827 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(200).y(201)) |
| 1828 | .build()); |
| 1829 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1830 | |
| 1831 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 1832 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1833 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 1834 | .build()); |
| 1835 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 1836 | |
| 1837 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 1838 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 1839 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 1840 | .build()); |
| 1841 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1842 | } |
| 1843 | |
| 1844 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1845 | * Two windows: a window on the left and a window on the right. |
| 1846 | * Mouse is hovered from the right window into the left window. |
| 1847 | * Next, we tap on the left window, where the cursor was last seen. |
| 1848 | * The second tap is done onto the right window. |
| 1849 | * The mouse and tap are from two different devices. |
| 1850 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 1851 | * explicitly helps during debugging. |
| 1852 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 1853 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 1854 | */ |
| 1855 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 1856 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1857 | sp<FakeWindowHandle> leftWindow = |
| 1858 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 1859 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1860 | |
| 1861 | sp<FakeWindowHandle> rightWindow = |
| 1862 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 1863 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 1864 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1865 | mDispatcher->onWindowInfosChanged( |
| 1866 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1867 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 1868 | // stale. |
| 1869 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1870 | const int32_t mouseDeviceId = 6; |
| 1871 | const int32_t touchDeviceId = 4; |
| 1872 | // Move the cursor from right |
| 1873 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1874 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1875 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1876 | AINPUT_SOURCE_MOUSE) |
| 1877 | .deviceId(mouseDeviceId) |
| 1878 | .downTime(baseTime + 10) |
| 1879 | .eventTime(baseTime + 20) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1880 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1881 | .build())); |
| 1882 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 1883 | |
| 1884 | // .. to the left window |
| 1885 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1886 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1887 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 1888 | AINPUT_SOURCE_MOUSE) |
| 1889 | .deviceId(mouseDeviceId) |
| 1890 | .downTime(baseTime + 10) |
| 1891 | .eventTime(baseTime + 30) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1892 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1893 | .build())); |
| 1894 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 1895 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 1896 | // Now tap the left window |
| 1897 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1898 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1899 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 1900 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1901 | .deviceId(touchDeviceId) |
| 1902 | .downTime(baseTime + 40) |
| 1903 | .eventTime(baseTime + 40) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1904 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1905 | .build())); |
| 1906 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 1907 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1908 | |
| 1909 | // release tap |
| 1910 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1911 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1912 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1913 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1914 | .deviceId(touchDeviceId) |
| 1915 | .downTime(baseTime + 40) |
| 1916 | .eventTime(baseTime + 50) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1917 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1918 | .build())); |
| 1919 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 1920 | |
| 1921 | // Tap the window on the right |
| 1922 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1923 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1924 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 1925 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1926 | .deviceId(touchDeviceId) |
| 1927 | .downTime(baseTime + 60) |
| 1928 | .eventTime(baseTime + 60) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1929 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1930 | .build())); |
| 1931 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 1932 | |
| 1933 | // release tap |
| 1934 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1935 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1936 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 1937 | AINPUT_SOURCE_TOUCHSCREEN) |
| 1938 | .deviceId(touchDeviceId) |
| 1939 | .downTime(baseTime + 60) |
| 1940 | .eventTime(baseTime + 70) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1941 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 1942 | .build())); |
| 1943 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 1944 | |
| 1945 | // No more events |
| 1946 | leftWindow->assertNoEvents(); |
| 1947 | rightWindow->assertNoEvents(); |
| 1948 | } |
| 1949 | |
| 1950 | /** |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1951 | * Start hovering in a window. While this hover is still active, make another window appear on top. |
| 1952 | * The top, obstructing window has no input channel, so it's not supposed to receive input. |
| 1953 | * While the top window is present, the hovering is stopped. |
| 1954 | * Later, hovering gets resumed again. |
| 1955 | * Ensure that new hover gesture is handled correctly. |
| 1956 | * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly |
| 1957 | * to the window that's currently being hovered over. |
| 1958 | */ |
| 1959 | TEST_F(InputDispatcherTest, HoverWhileWindowAppears) { |
| 1960 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1961 | sp<FakeWindowHandle> window = |
| 1962 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1963 | window->setFrame(Rect(0, 0, 200, 200)); |
| 1964 | |
| 1965 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1966 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1967 | |
| 1968 | // Start hovering in the window |
| 1969 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1970 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1971 | .build()); |
| 1972 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 1973 | |
| 1974 | // Now, an obscuring window appears! |
| 1975 | sp<FakeWindowHandle> obscuringWindow = |
| 1976 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 1977 | ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1978 | /*createInputChannel=*/false); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1979 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 1980 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 1981 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 1982 | obscuringWindow->setNoInputChannel(true); |
| 1983 | obscuringWindow->setFocusable(false); |
| 1984 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1985 | mDispatcher->onWindowInfosChanged( |
| 1986 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1987 | |
| 1988 | // While this new obscuring window is present, the hovering is stopped |
| 1989 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 1990 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 1991 | .build()); |
| 1992 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 1993 | |
| 1994 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1995 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 1996 | |
| 1997 | // And a new hover gesture starts. |
| 1998 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 1999 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2000 | .build()); |
| 2001 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2002 | } |
| 2003 | |
| 2004 | /** |
| 2005 | * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to |
| 2006 | * the obscuring window. |
| 2007 | */ |
| 2008 | TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) { |
| 2009 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2010 | sp<FakeWindowHandle> window = |
| 2011 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2012 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2013 | |
| 2014 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2015 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2016 | |
| 2017 | // Start hovering in the window |
| 2018 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2019 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2020 | .build()); |
| 2021 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2022 | |
| 2023 | // Now, an obscuring window appears! |
| 2024 | sp<FakeWindowHandle> obscuringWindow = |
| 2025 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2026 | ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 2027 | /*createInputChannel=*/false); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2028 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2029 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2030 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2031 | obscuringWindow->setNoInputChannel(true); |
| 2032 | obscuringWindow->setFocusable(false); |
| 2033 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2034 | mDispatcher->onWindowInfosChanged( |
| 2035 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2036 | |
| 2037 | // While this new obscuring window is present, the hovering continues. The event can't go to the |
| 2038 | // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window. |
| 2039 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2040 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2041 | .build()); |
| 2042 | obscuringWindow->assertNoEvents(); |
| 2043 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2044 | |
| 2045 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2046 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2047 | |
| 2048 | // Hovering continues in the same position. The hovering pointer re-enters the bottom window, |
| 2049 | // so it should generate a HOVER_ENTER |
| 2050 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2051 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2052 | .build()); |
| 2053 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2054 | |
| 2055 | // Now the MOVE should be getting dispatched normally |
| 2056 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2057 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2058 | .build()); |
| 2059 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2060 | } |
| 2061 | |
| 2062 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2063 | * Hover mouse over a window, and then send ACTION_SCROLL. Ensure both the hover and the scroll |
| 2064 | * events are delivered to the window. |
| 2065 | */ |
| 2066 | TEST_F(InputDispatcherTest, HoverMoveAndScroll) { |
| 2067 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2068 | sp<FakeWindowHandle> window = |
| 2069 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2070 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2071 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2072 | |
| 2073 | // Start hovering in the window |
| 2074 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2075 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 2076 | .build()); |
| 2077 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2078 | |
| 2079 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 2080 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2081 | .build()); |
| 2082 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2083 | |
| 2084 | // Scroll with the mouse |
| 2085 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_SCROLL, AINPUT_SOURCE_MOUSE) |
| 2086 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2087 | .build()); |
| 2088 | window->consumeMotionEvent(WithMotionAction(ACTION_SCROLL)); |
| 2089 | } |
| 2090 | |
| 2091 | using InputDispatcherMultiDeviceTest = InputDispatcherTest; |
| 2092 | |
| 2093 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2094 | * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that |
| 2095 | * touch is dropped, because stylus should be preferred over touch. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2096 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2097 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2098 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2099 | sp<FakeWindowHandle> window = |
| 2100 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2101 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2102 | |
| 2103 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2104 | |
| 2105 | constexpr int32_t touchDeviceId = 4; |
| 2106 | constexpr int32_t stylusDeviceId = 2; |
| 2107 | |
| 2108 | // Stylus down |
| 2109 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2110 | .deviceId(stylusDeviceId) |
| 2111 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2112 | .build()); |
| 2113 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2114 | |
| 2115 | // Touch down |
| 2116 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2117 | .deviceId(touchDeviceId) |
| 2118 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2119 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2120 | |
| 2121 | // Touch move |
| 2122 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2123 | .deviceId(touchDeviceId) |
| 2124 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2125 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2126 | // Touch is ignored because stylus is already down |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2127 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2128 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2129 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2130 | .deviceId(stylusDeviceId) |
| 2131 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2132 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2133 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2134 | WithCoords(101, 111))); |
| 2135 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2136 | window->assertNoEvents(); |
| 2137 | } |
| 2138 | |
| 2139 | /** |
| 2140 | * 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] | 2141 | * 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] | 2142 | * Similar test as above, but with added SPY window. |
| 2143 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2144 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2145 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2146 | sp<FakeWindowHandle> window = |
| 2147 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2148 | sp<FakeWindowHandle> spyWindow = |
| 2149 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2150 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2151 | spyWindow->setTrustedOverlay(true); |
| 2152 | spyWindow->setSpy(true); |
| 2153 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2154 | |
| 2155 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 2156 | |
| 2157 | constexpr int32_t touchDeviceId = 4; |
| 2158 | constexpr int32_t stylusDeviceId = 2; |
| 2159 | |
| 2160 | // Stylus down |
| 2161 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2162 | .deviceId(stylusDeviceId) |
| 2163 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2164 | .build()); |
| 2165 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2166 | spyWindow->consumeMotionEvent( |
| 2167 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2168 | |
| 2169 | // Touch down |
| 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 | |
| 2175 | // Touch move |
| 2176 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2177 | .deviceId(touchDeviceId) |
| 2178 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2179 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2180 | |
| 2181 | // Touch is ignored because stylus is already down |
| 2182 | |
| 2183 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2184 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2185 | .deviceId(stylusDeviceId) |
| 2186 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2187 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2188 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2189 | WithCoords(101, 111))); |
| 2190 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2191 | WithCoords(101, 111))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2192 | |
| 2193 | window->assertNoEvents(); |
| 2194 | spyWindow->assertNoEvents(); |
| 2195 | } |
| 2196 | |
| 2197 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2198 | * 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] | 2199 | * touch is dropped, because stylus hover takes precedence. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2200 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2201 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2202 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2203 | sp<FakeWindowHandle> window = |
| 2204 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2205 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2206 | |
| 2207 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2208 | |
| 2209 | constexpr int32_t touchDeviceId = 4; |
| 2210 | constexpr int32_t stylusDeviceId = 2; |
| 2211 | |
| 2212 | // Stylus down on the window |
| 2213 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2214 | .deviceId(stylusDeviceId) |
| 2215 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2216 | .build()); |
| 2217 | window->consumeMotionEvent( |
| 2218 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2219 | |
| 2220 | // Touch down on window |
| 2221 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2222 | .deviceId(touchDeviceId) |
| 2223 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2224 | .build()); |
| 2225 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2226 | .deviceId(touchDeviceId) |
| 2227 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2228 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2229 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2230 | // Touch is ignored because stylus is hovering |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2231 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2232 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2233 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2234 | .deviceId(stylusDeviceId) |
| 2235 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2236 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2237 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2238 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2239 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2240 | // and subsequent touches continue to be ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2241 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2242 | .deviceId(touchDeviceId) |
| 2243 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2244 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2245 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | /** |
| 2249 | * 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] | 2250 | * Ensure that touch is canceled, because stylus hover should take precedence. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2251 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2252 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusHover) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2253 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2254 | sp<FakeWindowHandle> window = |
| 2255 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2256 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2257 | |
| 2258 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2259 | |
| 2260 | constexpr int32_t touchDeviceId = 4; |
| 2261 | constexpr int32_t stylusDeviceId = 2; |
| 2262 | |
| 2263 | // Touch down on window |
| 2264 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2265 | .deviceId(touchDeviceId) |
| 2266 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2267 | .build()); |
| 2268 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2269 | .deviceId(touchDeviceId) |
| 2270 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2271 | .build()); |
| 2272 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2273 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2274 | |
| 2275 | // Stylus hover on the window |
| 2276 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2277 | .deviceId(stylusDeviceId) |
| 2278 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2279 | .build()); |
| 2280 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2281 | .deviceId(stylusDeviceId) |
| 2282 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2283 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2284 | // Stylus hover movement causes touch to be canceled |
| 2285 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 2286 | WithCoords(141, 146))); |
| 2287 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 2288 | WithDeviceId(stylusDeviceId), WithCoords(100, 110))); |
| 2289 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2290 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2291 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2292 | // Subsequent touch movements are ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2293 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2294 | .deviceId(touchDeviceId) |
| 2295 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2296 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2297 | |
| 2298 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2299 | } |
| 2300 | |
| 2301 | /** |
| 2302 | * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that |
| 2303 | * the latest stylus takes over. That is, old stylus should be canceled and the new stylus should |
| 2304 | * become active. |
| 2305 | */ |
| 2306 | TEST_F(InputDispatcherMultiDeviceTest, LatestStylusWins) { |
| 2307 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2308 | sp<FakeWindowHandle> window = |
| 2309 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2310 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2311 | |
| 2312 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2313 | |
| 2314 | constexpr int32_t stylusDeviceId1 = 3; |
| 2315 | constexpr int32_t stylusDeviceId2 = 5; |
| 2316 | |
| 2317 | // Touch down on window |
| 2318 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2319 | .deviceId(stylusDeviceId1) |
| 2320 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100)) |
| 2321 | .build()); |
| 2322 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2323 | .deviceId(stylusDeviceId1) |
| 2324 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2325 | .build()); |
| 2326 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1))); |
| 2327 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2328 | |
| 2329 | // Second stylus down |
| 2330 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2331 | .deviceId(stylusDeviceId2) |
| 2332 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10)) |
| 2333 | .build()); |
| 2334 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2335 | .deviceId(stylusDeviceId2) |
| 2336 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11)) |
| 2337 | .build()); |
| 2338 | |
| 2339 | // First stylus is canceled, second one takes over. |
| 2340 | window->consumeMotionEvent( |
| 2341 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId1))); |
| 2342 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2))); |
| 2343 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2))); |
| 2344 | |
| 2345 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2346 | .deviceId(stylusDeviceId1) |
| 2347 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2348 | .build()); |
| 2349 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2350 | window->assertNoEvents(); |
| 2351 | } |
| 2352 | |
| 2353 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2354 | * One window. Touch down on the window. Then, stylus down on the window from another device. |
| 2355 | * Ensure that is canceled, because stylus down should be preferred over touch. |
| 2356 | */ |
| 2357 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusDown) { |
| 2358 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2359 | sp<FakeWindowHandle> window = |
| 2360 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2361 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2362 | |
| 2363 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2364 | |
| 2365 | constexpr int32_t touchDeviceId = 4; |
| 2366 | constexpr int32_t stylusDeviceId = 2; |
| 2367 | |
| 2368 | // Touch down on window |
| 2369 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2370 | .deviceId(touchDeviceId) |
| 2371 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2372 | .build()); |
| 2373 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2374 | .deviceId(touchDeviceId) |
| 2375 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2376 | .build()); |
| 2377 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2378 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2379 | |
| 2380 | // Stylus down on the window |
| 2381 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2382 | .deviceId(stylusDeviceId) |
| 2383 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2384 | .build()); |
| 2385 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 2386 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2387 | |
| 2388 | // Subsequent stylus movements are delivered correctly |
| 2389 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2390 | .deviceId(stylusDeviceId) |
| 2391 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2392 | .build()); |
| 2393 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2394 | WithCoords(101, 111))); |
| 2395 | } |
| 2396 | |
| 2397 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2398 | * Two windows: a window on the left and a window on the right. |
| 2399 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2400 | * down. Then, on the left window, also place second touch pointer down. |
| 2401 | * This test tries to reproduce a crash. |
| 2402 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2403 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2404 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2405 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2406 | sp<FakeWindowHandle> leftWindow = |
| 2407 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2408 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2409 | |
| 2410 | sp<FakeWindowHandle> rightWindow = |
| 2411 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2412 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2413 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2414 | mDispatcher->onWindowInfosChanged( |
| 2415 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2416 | |
| 2417 | const int32_t touchDeviceId = 4; |
| 2418 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2419 | |
| 2420 | // Start hovering over the left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2421 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2422 | .deviceId(mouseDeviceId) |
| 2423 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2424 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2425 | leftWindow->consumeMotionEvent( |
| 2426 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2427 | |
| 2428 | // Mouse down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2429 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2430 | .deviceId(mouseDeviceId) |
| 2431 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2432 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2433 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2434 | |
| 2435 | leftWindow->consumeMotionEvent( |
| 2436 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2437 | leftWindow->consumeMotionEvent( |
| 2438 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2439 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2440 | mDispatcher->notifyMotion( |
| 2441 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2442 | .deviceId(mouseDeviceId) |
| 2443 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2444 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2445 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2446 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2447 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2448 | |
| 2449 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2450 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2451 | .deviceId(touchDeviceId) |
| 2452 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2453 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2454 | leftWindow->assertNoEvents(); |
| 2455 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2456 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2457 | |
| 2458 | // Second touch pointer down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2459 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2460 | .deviceId(touchDeviceId) |
| 2461 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2462 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2463 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2464 | // Since this is now a new splittable pointer going down on the left window, and it's coming |
| 2465 | // from a different device, the current gesture in the left window (pointer down) should first |
| 2466 | // be canceled. |
| 2467 | leftWindow->consumeMotionEvent( |
| 2468 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2469 | leftWindow->consumeMotionEvent( |
| 2470 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2471 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2472 | // current implementation. |
| 2473 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2474 | rightWindow->consumeMotionEvent( |
| 2475 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2476 | |
| 2477 | leftWindow->assertNoEvents(); |
| 2478 | rightWindow->assertNoEvents(); |
| 2479 | } |
| 2480 | |
| 2481 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2482 | * Two windows: a window on the left and a window on the right. |
| 2483 | * Mouse is hovered on the left window and stylus is hovered on the right window. |
| 2484 | */ |
| 2485 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHover) { |
| 2486 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2487 | sp<FakeWindowHandle> leftWindow = |
| 2488 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2489 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2490 | |
| 2491 | sp<FakeWindowHandle> rightWindow = |
| 2492 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2493 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2494 | |
| 2495 | mDispatcher->onWindowInfosChanged( |
| 2496 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2497 | |
| 2498 | const int32_t stylusDeviceId = 3; |
| 2499 | const int32_t mouseDeviceId = 6; |
| 2500 | |
| 2501 | // Start hovering over the left window |
| 2502 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2503 | .deviceId(mouseDeviceId) |
| 2504 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 2505 | .build()); |
| 2506 | leftWindow->consumeMotionEvent( |
| 2507 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2508 | |
| 2509 | // Stylus hovered on right window |
| 2510 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2511 | .deviceId(stylusDeviceId) |
| 2512 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(300).y(100)) |
| 2513 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2514 | rightWindow->consumeMotionEvent( |
| 2515 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2516 | |
| 2517 | // Subsequent HOVER_MOVE events are dispatched correctly. |
| 2518 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 2519 | .deviceId(mouseDeviceId) |
| 2520 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2521 | .build()); |
| 2522 | leftWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2523 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2524 | |
| 2525 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2526 | .deviceId(stylusDeviceId) |
| 2527 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(310).y(110)) |
| 2528 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2529 | rightWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2530 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2531 | |
| 2532 | leftWindow->assertNoEvents(); |
| 2533 | rightWindow->assertNoEvents(); |
| 2534 | } |
| 2535 | |
| 2536 | /** |
| 2537 | * Three windows: a window on the left and a window on the right. |
| 2538 | * And a spy window that's positioned above all of them. |
| 2539 | * Stylus down on the left window and remains down. Touch goes down on the right and remains down. |
| 2540 | * Check the stream that's received by the spy. |
| 2541 | */ |
| 2542 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy) { |
| 2543 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2544 | |
| 2545 | sp<FakeWindowHandle> spyWindow = |
| 2546 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2547 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 2548 | spyWindow->setTrustedOverlay(true); |
| 2549 | spyWindow->setSpy(true); |
| 2550 | |
| 2551 | sp<FakeWindowHandle> leftWindow = |
| 2552 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2553 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2554 | |
| 2555 | sp<FakeWindowHandle> rightWindow = |
| 2556 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2557 | |
| 2558 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2559 | |
| 2560 | mDispatcher->onWindowInfosChanged( |
| 2561 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2562 | |
| 2563 | const int32_t stylusDeviceId = 1; |
| 2564 | const int32_t touchDeviceId = 2; |
| 2565 | |
| 2566 | // Stylus down on the left window |
| 2567 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2568 | .deviceId(stylusDeviceId) |
| 2569 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2570 | .build()); |
| 2571 | leftWindow->consumeMotionEvent( |
| 2572 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2573 | spyWindow->consumeMotionEvent( |
| 2574 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2575 | |
| 2576 | // Touch down on the right window |
| 2577 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2578 | .deviceId(touchDeviceId) |
| 2579 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2580 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2581 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2582 | rightWindow->consumeMotionEvent( |
| 2583 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2584 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2585 | // Spy window does not receive touch events, because stylus events take precedence, and it |
| 2586 | // already has an active stylus gesture. |
| 2587 | |
| 2588 | // 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] | 2589 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2590 | .deviceId(stylusDeviceId) |
| 2591 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2592 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2593 | leftWindow->consumeMotionEvent( |
| 2594 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 2595 | spyWindow->consumeMotionEvent( |
| 2596 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2597 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2598 | // Further MOVE events keep going to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2599 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2600 | .deviceId(touchDeviceId) |
| 2601 | .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110)) |
| 2602 | .build()); |
| 2603 | rightWindow->consumeMotionEvent( |
| 2604 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2605 | |
| 2606 | spyWindow->assertNoEvents(); |
| 2607 | leftWindow->assertNoEvents(); |
| 2608 | rightWindow->assertNoEvents(); |
| 2609 | } |
| 2610 | |
| 2611 | /** |
| 2612 | * Three windows: a window on the left, a window on the right, and a spy window positioned above |
| 2613 | * both. |
| 2614 | * Check hover in left window and touch down in the right window. |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2615 | * 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] | 2616 | * At the same time, left and right should be getting independent streams of hovering and touch, |
| 2617 | * respectively. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2618 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2619 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverBlocksTouchWithSpy) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2620 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2621 | |
| 2622 | sp<FakeWindowHandle> spyWindow = |
| 2623 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2624 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 2625 | spyWindow->setTrustedOverlay(true); |
| 2626 | spyWindow->setSpy(true); |
| 2627 | |
| 2628 | sp<FakeWindowHandle> leftWindow = |
| 2629 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2630 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2631 | |
| 2632 | sp<FakeWindowHandle> rightWindow = |
| 2633 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2634 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2635 | |
| 2636 | mDispatcher->onWindowInfosChanged( |
| 2637 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2638 | |
| 2639 | const int32_t stylusDeviceId = 1; |
| 2640 | const int32_t touchDeviceId = 2; |
| 2641 | |
| 2642 | // Stylus hover on the left window |
| 2643 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2644 | .deviceId(stylusDeviceId) |
| 2645 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2646 | .build()); |
| 2647 | leftWindow->consumeMotionEvent( |
| 2648 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2649 | spyWindow->consumeMotionEvent( |
| 2650 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2651 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2652 | // Touch down on the right window. Spy doesn't receive this touch because it already has |
| 2653 | // stylus hovering there. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2654 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2655 | .deviceId(touchDeviceId) |
| 2656 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2657 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2658 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2659 | spyWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2660 | rightWindow->consumeMotionEvent( |
| 2661 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2662 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2663 | // 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] | 2664 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2665 | .deviceId(stylusDeviceId) |
| 2666 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2667 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2668 | leftWindow->consumeMotionEvent( |
| 2669 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2670 | spyWindow->consumeMotionEvent( |
| 2671 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2672 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2673 | // Touch movements continue. They should be delivered to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2674 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2675 | .deviceId(touchDeviceId) |
| 2676 | .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101)) |
| 2677 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2678 | rightWindow->consumeMotionEvent( |
| 2679 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2680 | |
| 2681 | spyWindow->assertNoEvents(); |
| 2682 | leftWindow->assertNoEvents(); |
| 2683 | rightWindow->assertNoEvents(); |
| 2684 | } |
| 2685 | |
| 2686 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2687 | * On a single window, use two different devices: mouse and touch. |
| 2688 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 2689 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 2690 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 2691 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 2692 | * represent a new gesture. |
| 2693 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2694 | TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2695 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2696 | sp<FakeWindowHandle> window = |
| 2697 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2698 | window->setFrame(Rect(0, 0, 400, 400)); |
| 2699 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2700 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2701 | |
| 2702 | const int32_t touchDeviceId = 4; |
| 2703 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2704 | |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 2705 | // First touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2706 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2707 | .deviceId(touchDeviceId) |
| 2708 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2709 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2710 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2711 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2712 | .deviceId(touchDeviceId) |
| 2713 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2714 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2715 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2716 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2717 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 2718 | .deviceId(touchDeviceId) |
| 2719 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2720 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2721 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2722 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2723 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2724 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 2725 | |
| 2726 | // Mouse down. The touch should be canceled |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2727 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2728 | .deviceId(mouseDeviceId) |
| 2729 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2730 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 2731 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2732 | |
| 2733 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 2734 | WithPointerCount(1u))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2735 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2736 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2737 | mDispatcher->notifyMotion( |
| 2738 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2739 | .deviceId(mouseDeviceId) |
| 2740 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2741 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2742 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 2743 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2744 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2745 | |
| 2746 | // Second touch pointer down. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2747 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2748 | .deviceId(touchDeviceId) |
| 2749 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2750 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 2751 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2752 | // Since we already canceled this touch gesture, it will be ignored until a completely new |
| 2753 | // gesture is started. This is easier to implement than trying to keep track of the new pointer |
| 2754 | // and generating an ACTION_DOWN instead of ACTION_POINTER_DOWN. |
| 2755 | // However, mouse movements should continue to work. |
| 2756 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 2757 | .deviceId(mouseDeviceId) |
| 2758 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2759 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110)) |
| 2760 | .build()); |
| 2761 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 2762 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2763 | window->assertNoEvents(); |
| 2764 | } |
| 2765 | |
| 2766 | /** |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2767 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels |
| 2768 | * the injected event. |
| 2769 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2770 | TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent) { |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2771 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2772 | sp<FakeWindowHandle> window = |
| 2773 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2774 | window->setFrame(Rect(0, 0, 400, 400)); |
| 2775 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2776 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2777 | |
| 2778 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2779 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 2780 | // completion. |
| 2781 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2782 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2783 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2784 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2785 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2786 | .build())); |
| 2787 | window->consumeMotionEvent( |
| 2788 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 2789 | |
| 2790 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer |
| 2791 | // should be canceled and the new gesture should take over. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2792 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2793 | .deviceId(touchDeviceId) |
| 2794 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2795 | .build()); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 2796 | |
| 2797 | window->consumeMotionEvent( |
| 2798 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 2799 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2800 | } |
| 2801 | |
| 2802 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2803 | * This test is similar to the test above, but the sequence of injected events is different. |
| 2804 | * |
| 2805 | * Two windows: a window on the left and a window on the right. |
| 2806 | * Mouse is hovered over the left window. |
| 2807 | * Next, we tap on the left window, where the cursor was last seen. |
| 2808 | * |
| 2809 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 2810 | * the left window. |
| 2811 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 2812 | * window (first), and then another on the left window (second). |
| 2813 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2814 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 2815 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2816 | TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch) { |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2817 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2818 | sp<FakeWindowHandle> leftWindow = |
| 2819 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2820 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2821 | |
| 2822 | sp<FakeWindowHandle> rightWindow = |
| 2823 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2824 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2825 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2826 | mDispatcher->onWindowInfosChanged( |
| 2827 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2828 | |
| 2829 | const int32_t mouseDeviceId = 6; |
| 2830 | const int32_t touchDeviceId = 4; |
| 2831 | // Hover over the left window. Keep the cursor there. |
| 2832 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2833 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2834 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 2835 | AINPUT_SOURCE_MOUSE) |
| 2836 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2837 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2838 | .build())); |
| 2839 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2840 | |
| 2841 | // Tap on left window |
| 2842 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2843 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2844 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2845 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2846 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2847 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2848 | .build())); |
| 2849 | |
| 2850 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2851 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2852 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2853 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2854 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2855 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2856 | .build())); |
| 2857 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2858 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2859 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2860 | |
| 2861 | // First finger down on right window |
| 2862 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2863 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2864 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2865 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2866 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2867 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2868 | .build())); |
| 2869 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2870 | |
| 2871 | // Second finger down on the left window |
| 2872 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2873 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2874 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2875 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2876 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2877 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 2878 | .build())); |
| 2879 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2880 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 2881 | |
| 2882 | // No more events |
| 2883 | leftWindow->assertNoEvents(); |
| 2884 | rightWindow->assertNoEvents(); |
| 2885 | } |
| 2886 | |
| 2887 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2888 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 2889 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 2890 | * touch is gone, stylus hovering should start working again. |
| 2891 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2892 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverIgnoresTouchTap) { |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2893 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2894 | sp<FakeWindowHandle> window = |
| 2895 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2896 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2897 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2898 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2899 | |
| 2900 | const int32_t stylusDeviceId = 5; |
| 2901 | const int32_t touchDeviceId = 4; |
| 2902 | // Start hovering with stylus |
| 2903 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2904 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2905 | MotionEventBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2906 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2907 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2908 | .build())); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2909 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2910 | |
| 2911 | // Finger down on the window |
| 2912 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2913 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2914 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2915 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2916 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2917 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2918 | // The touch device should be ignored! |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2919 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2920 | // Continue hovering with stylus. |
| 2921 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2922 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2923 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2924 | AINPUT_SOURCE_STYLUS) |
| 2925 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 2926 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2927 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2928 | // Hovers continue to work |
| 2929 | window->consumeMotionEvent( |
| 2930 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2931 | |
| 2932 | // Lift up the finger |
| 2933 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2934 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2935 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2936 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2937 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2938 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2939 | .build())); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2940 | |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2941 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2942 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2943 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2944 | AINPUT_SOURCE_STYLUS) |
| 2945 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 2946 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2947 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2948 | window->consumeMotionEvent( |
| 2949 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 2950 | window->assertNoEvents(); |
| 2951 | } |
| 2952 | |
| 2953 | /** |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 2954 | * If stylus is down anywhere on the screen, then touches should not be delivered to windows that |
| 2955 | * have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 2956 | * |
| 2957 | * Two windows: one on the left and one on the right. |
| 2958 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 2959 | * Stylus down on the left window, and then touch down on the right window. |
| 2960 | * Check that the right window doesn't get touches while the stylus is down on the left window. |
| 2961 | */ |
| 2962 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusDownBlocksTouch) { |
| 2963 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2964 | sp<FakeWindowHandle> leftWindow = |
| 2965 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
| 2966 | ADISPLAY_ID_DEFAULT); |
| 2967 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 2968 | |
| 2969 | sp<FakeWindowHandle> sbtRightWindow = |
| 2970 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 2971 | "Stylus blocks touch (right) window", ADISPLAY_ID_DEFAULT); |
| 2972 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 2973 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 2974 | |
| 2975 | mDispatcher->onWindowInfosChanged( |
| 2976 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 2977 | |
| 2978 | const int32_t stylusDeviceId = 5; |
| 2979 | const int32_t touchDeviceId = 4; |
| 2980 | |
| 2981 | // Stylus down in the left window |
| 2982 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2983 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 2984 | .deviceId(stylusDeviceId) |
| 2985 | .build()); |
| 2986 | leftWindow->consumeMotionEvent( |
| 2987 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2988 | |
| 2989 | // Finger tap on the right window |
| 2990 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2991 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 2992 | .deviceId(touchDeviceId) |
| 2993 | .build()); |
| 2994 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 2995 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 2996 | .deviceId(touchDeviceId) |
| 2997 | .build()); |
| 2998 | |
| 2999 | // The touch should be blocked, because stylus is down somewhere else on screen! |
| 3000 | sbtRightWindow->assertNoEvents(); |
| 3001 | |
| 3002 | // Continue stylus motion, and ensure it's not impacted. |
| 3003 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 3004 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3005 | .deviceId(stylusDeviceId) |
| 3006 | .build()); |
| 3007 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3008 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3009 | .deviceId(stylusDeviceId) |
| 3010 | .build()); |
| 3011 | leftWindow->consumeMotionEvent( |
| 3012 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 3013 | leftWindow->consumeMotionEvent( |
| 3014 | AllOf(WithMotionAction(ACTION_UP), WithDeviceId(stylusDeviceId))); |
| 3015 | |
| 3016 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3017 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3018 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3019 | .deviceId(touchDeviceId) |
| 3020 | .build()); |
| 3021 | sbtRightWindow->consumeMotionEvent( |
| 3022 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3023 | } |
| 3024 | |
| 3025 | /** |
| 3026 | * If stylus is hovering anywhere on the screen, then touches should not be delivered to windows |
| 3027 | * that have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 3028 | * |
| 3029 | * Two windows: one on the left and one on the right. |
| 3030 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 3031 | * Stylus hover on the left window, and then touch down on the right window. |
| 3032 | * Check that the right window doesn't get touches while the stylus is hovering on the left window. |
| 3033 | */ |
| 3034 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusHoverBlocksTouch) { |
| 3035 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3036 | sp<FakeWindowHandle> leftWindow = |
| 3037 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
| 3038 | ADISPLAY_ID_DEFAULT); |
| 3039 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3040 | |
| 3041 | sp<FakeWindowHandle> sbtRightWindow = |
| 3042 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3043 | "Stylus blocks touch (right) window", ADISPLAY_ID_DEFAULT); |
| 3044 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3045 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 3046 | |
| 3047 | mDispatcher->onWindowInfosChanged( |
| 3048 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 3049 | |
| 3050 | const int32_t stylusDeviceId = 5; |
| 3051 | const int32_t touchDeviceId = 4; |
| 3052 | |
| 3053 | // Stylus hover in the left window |
| 3054 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3055 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 3056 | .deviceId(stylusDeviceId) |
| 3057 | .build()); |
| 3058 | leftWindow->consumeMotionEvent( |
| 3059 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3060 | |
| 3061 | // Finger tap on the right window |
| 3062 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3063 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3064 | .deviceId(touchDeviceId) |
| 3065 | .build()); |
| 3066 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3067 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3068 | .deviceId(touchDeviceId) |
| 3069 | .build()); |
| 3070 | |
| 3071 | // The touch should be blocked, because stylus is hovering somewhere else on screen! |
| 3072 | sbtRightWindow->assertNoEvents(); |
| 3073 | |
| 3074 | // Continue stylus motion, and ensure it's not impacted. |
| 3075 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3076 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3077 | .deviceId(stylusDeviceId) |
| 3078 | .build()); |
| 3079 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3080 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3081 | .deviceId(stylusDeviceId) |
| 3082 | .build()); |
| 3083 | leftWindow->consumeMotionEvent( |
| 3084 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3085 | leftWindow->consumeMotionEvent( |
| 3086 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId))); |
| 3087 | |
| 3088 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3089 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3090 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3091 | .deviceId(touchDeviceId) |
| 3092 | .build()); |
| 3093 | sbtRightWindow->consumeMotionEvent( |
| 3094 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3095 | } |
| 3096 | |
| 3097 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3098 | * A spy window above a window with no input channel. |
| 3099 | * Start hovering with a stylus device, and then tap with it. |
| 3100 | * Ensure spy window receives the entire sequence. |
| 3101 | */ |
| 3102 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 3103 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3104 | sp<FakeWindowHandle> spyWindow = |
| 3105 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3106 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3107 | spyWindow->setTrustedOverlay(true); |
| 3108 | spyWindow->setSpy(true); |
| 3109 | sp<FakeWindowHandle> window = |
| 3110 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3111 | window->setNoInputChannel(true); |
| 3112 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3113 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3114 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3115 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3116 | // Start hovering with stylus |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3117 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3118 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3119 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3120 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3121 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3122 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3123 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3124 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3125 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3126 | |
| 3127 | // Stylus touches down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3128 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3129 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3130 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3131 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3132 | |
| 3133 | // Stylus goes up |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3134 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3135 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3136 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3137 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 3138 | |
| 3139 | // Again hover |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3140 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3141 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3142 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3143 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3144 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3145 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3146 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3147 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3148 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3149 | |
| 3150 | // No more events |
| 3151 | spyWindow->assertNoEvents(); |
| 3152 | window->assertNoEvents(); |
| 3153 | } |
| 3154 | |
| 3155 | /** |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 3156 | * A stale stylus HOVER_EXIT event is injected. Since it's a stale event, it should generally be |
| 3157 | * rejected. But since we already have an ongoing gesture, this event should be processed. |
| 3158 | * This prevents inconsistent events being handled inside the dispatcher. |
| 3159 | */ |
| 3160 | TEST_F(InputDispatcherTest, StaleStylusHoverGestureIsComplete) { |
| 3161 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3162 | |
| 3163 | sp<FakeWindowHandle> window = |
| 3164 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3165 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3166 | |
| 3167 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3168 | |
| 3169 | // Start hovering with stylus |
| 3170 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3171 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3172 | .build()); |
| 3173 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3174 | |
| 3175 | NotifyMotionArgs hoverExit = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3176 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3177 | .build(); |
| 3178 | // Make this 'hoverExit' event stale |
| 3179 | mFakePolicy->setStaleEventTimeout(100ms); |
| 3180 | std::this_thread::sleep_for(100ms); |
| 3181 | |
| 3182 | // It shouldn't be dropped by the dispatcher, even though it's stale. |
| 3183 | mDispatcher->notifyMotion(hoverExit); |
| 3184 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3185 | |
| 3186 | // Stylus starts hovering again! There should be no crash. |
| 3187 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3188 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(51)) |
| 3189 | .build()); |
| 3190 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3191 | } |
| 3192 | |
| 3193 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3194 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 3195 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 3196 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 3197 | * While the mouse is down, new move events from the touch device should be ignored. |
| 3198 | */ |
| 3199 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 3200 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3201 | sp<FakeWindowHandle> spyWindow = |
| 3202 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3203 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3204 | spyWindow->setTrustedOverlay(true); |
| 3205 | spyWindow->setSpy(true); |
| 3206 | sp<FakeWindowHandle> window = |
| 3207 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3208 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3209 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3210 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3211 | |
| 3212 | const int32_t mouseDeviceId = 7; |
| 3213 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3214 | |
| 3215 | // Hover a bit with mouse first |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3216 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3217 | .deviceId(mouseDeviceId) |
| 3218 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3219 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3220 | spyWindow->consumeMotionEvent( |
| 3221 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3222 | window->consumeMotionEvent( |
| 3223 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3224 | |
| 3225 | // Start touching |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3226 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3227 | .deviceId(touchDeviceId) |
| 3228 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3229 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3230 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3231 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3232 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3233 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3234 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3235 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3236 | .deviceId(touchDeviceId) |
| 3237 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 3238 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3239 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3240 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3241 | |
| 3242 | // Pilfer the stream |
| 3243 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3244 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 3245 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3246 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3247 | .deviceId(touchDeviceId) |
| 3248 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 3249 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3250 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3251 | |
| 3252 | // Mouse down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3253 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3254 | .deviceId(mouseDeviceId) |
| 3255 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3256 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3257 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3258 | |
| 3259 | spyWindow->consumeMotionEvent( |
| 3260 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 3261 | spyWindow->consumeMotionEvent( |
| 3262 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3263 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3264 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3265 | mDispatcher->notifyMotion( |
| 3266 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3267 | .deviceId(mouseDeviceId) |
| 3268 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3269 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3270 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3271 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3272 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3273 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3274 | |
| 3275 | // Mouse move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3276 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3277 | .deviceId(mouseDeviceId) |
| 3278 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3279 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 3280 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3281 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3282 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3283 | |
| 3284 | // Touch move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3285 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3286 | .deviceId(touchDeviceId) |
| 3287 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 3288 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3289 | |
| 3290 | // No more events |
| 3291 | spyWindow->assertNoEvents(); |
| 3292 | window->assertNoEvents(); |
| 3293 | } |
| 3294 | |
| 3295 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3296 | * On the display, have a single window, and also an area where there's no window. |
| 3297 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 3298 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 3299 | */ |
| 3300 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 3301 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3302 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3303 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3304 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3305 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3306 | |
| 3307 | // Touch down on the empty space |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3308 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3309 | |
| 3310 | mDispatcher->waitForIdle(); |
| 3311 | window->assertNoEvents(); |
| 3312 | |
| 3313 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3314 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3315 | mDispatcher->waitForIdle(); |
| 3316 | window->consumeMotionDown(); |
| 3317 | } |
| 3318 | |
| 3319 | /** |
| 3320 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 3321 | * non-touchable window. |
| 3322 | */ |
| 3323 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 3324 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3325 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3326 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3327 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3328 | window1->setTouchable(false); |
| 3329 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3330 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3331 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3332 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3333 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3334 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3335 | // Touch down on the non-touchable window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3336 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3337 | |
| 3338 | mDispatcher->waitForIdle(); |
| 3339 | window1->assertNoEvents(); |
| 3340 | window2->assertNoEvents(); |
| 3341 | |
| 3342 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3343 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3344 | mDispatcher->waitForIdle(); |
| 3345 | window2->consumeMotionDown(); |
| 3346 | } |
| 3347 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3348 | /** |
| 3349 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 3350 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 3351 | */ |
| 3352 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 3353 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3354 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3355 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3356 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3357 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3358 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3359 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3360 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3361 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3362 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3363 | // Touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3364 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3365 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3366 | |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 3367 | const std::unique_ptr<MotionEvent> firstDown = |
| 3368 | window1->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 3369 | ASSERT_EQ(firstDown->getDownTime(), firstDown->getEventTime()); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3370 | window2->assertNoEvents(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3371 | |
| 3372 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3373 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3374 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 3375 | |
| 3376 | const std::unique_ptr<MotionEvent> secondDown = |
| 3377 | window2->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN))); |
| 3378 | ASSERT_EQ(secondDown->getDownTime(), secondDown->getEventTime()); |
| 3379 | ASSERT_NE(firstDown->getDownTime(), secondDown->getDownTime()); |
| 3380 | // We currently send MOVE events to all windows receiving a split touch when there is any change |
| 3381 | // in the touch state, even when none of the pointers in the split window actually moved. |
| 3382 | // Document this behavior in the test. |
| 3383 | window1->consumeMotionMove(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3384 | |
| 3385 | // Now move the pointer on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3386 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3387 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 3388 | |
| 3389 | window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime())); |
| 3390 | window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime())); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3391 | |
| 3392 | // Now add new touch down on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3393 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3394 | mDispatcher->waitForIdle(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3395 | |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 3396 | window2->consumeMotionEvent( |
| 3397 | AllOf(WithMotionAction(POINTER_1_DOWN), WithDownTime(secondDown->getDownTime()))); |
| 3398 | window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime())); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3399 | |
| 3400 | // Now move the pointer on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3401 | mDispatcher->notifyMotion( |
| 3402 | generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3403 | mDispatcher->waitForIdle(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3404 | |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 3405 | window1->consumeMotionEvent(WithDownTime(firstDown->getDownTime())); |
| 3406 | window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime())); |
| 3407 | |
| 3408 | // Now add new touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3409 | mDispatcher->notifyMotion( |
| 3410 | generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3411 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 3412 | |
| 3413 | window1->consumeMotionEvent( |
| 3414 | AllOf(WithMotionAction(POINTER_1_DOWN), WithDownTime(firstDown->getDownTime()))); |
| 3415 | window2->consumeMotionEvent(WithDownTime(secondDown->getDownTime())); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3418 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3419 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3420 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3421 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3422 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3423 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3424 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3425 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3426 | |
| 3427 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3428 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3429 | mDispatcher->onWindowInfosChanged( |
| 3430 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3431 | |
| 3432 | // 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] | 3433 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3434 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3435 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3436 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3437 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3438 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3439 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3440 | |
| 3441 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3442 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3443 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3444 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3445 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3446 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3447 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3448 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3449 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3450 | |
| 3451 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3452 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3453 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3454 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3455 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3456 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3457 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3458 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3459 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3460 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3461 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3462 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3463 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3464 | AINPUT_SOURCE_MOUSE) |
| 3465 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3466 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3467 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3468 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3469 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3470 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3471 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3472 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3473 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3474 | AINPUT_SOURCE_MOUSE) |
| 3475 | .buttonState(0) |
| 3476 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3477 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3478 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3479 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3480 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3481 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3482 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3483 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3484 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3485 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3486 | .build())); |
| 3487 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3488 | |
| 3489 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3490 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3491 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3492 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3493 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3494 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3495 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3496 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3497 | |
| 3498 | // No more events |
| 3499 | windowLeft->assertNoEvents(); |
| 3500 | windowRight->assertNoEvents(); |
| 3501 | } |
| 3502 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3503 | /** |
| 3504 | * Put two fingers down (and don't release them) and click the mouse button. |
| 3505 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 3506 | * currently active gesture should be canceled, and the new one should proceed. |
| 3507 | */ |
| 3508 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 3509 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3510 | sp<FakeWindowHandle> window = |
| 3511 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3512 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3513 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3514 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3515 | |
| 3516 | const int32_t touchDeviceId = 4; |
| 3517 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3518 | |
| 3519 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3520 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3521 | .deviceId(touchDeviceId) |
| 3522 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3523 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3524 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3525 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3526 | .deviceId(touchDeviceId) |
| 3527 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3528 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 3529 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3530 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3531 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3532 | |
| 3533 | // Inject a series of mouse events for a mouse click |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3534 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3535 | .deviceId(mouseDeviceId) |
| 3536 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3537 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3538 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3539 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 3540 | WithPointerCount(2u))); |
| 3541 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3542 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3543 | mDispatcher->notifyMotion( |
| 3544 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3545 | .deviceId(mouseDeviceId) |
| 3546 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3547 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3548 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3549 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3550 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3551 | |
| 3552 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 3553 | // already canceled gesture, it should be ignored. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3554 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3555 | .deviceId(touchDeviceId) |
| 3556 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 3557 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 3558 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3559 | window->assertNoEvents(); |
| 3560 | } |
| 3561 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3562 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 3563 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3564 | |
| 3565 | sp<FakeWindowHandle> spyWindow = |
| 3566 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3567 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3568 | spyWindow->setTrustedOverlay(true); |
| 3569 | spyWindow->setSpy(true); |
| 3570 | sp<FakeWindowHandle> window = |
| 3571 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3572 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3573 | |
| 3574 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3575 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3576 | |
| 3577 | // Send mouse cursor to the window |
| 3578 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3579 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3580 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3581 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3582 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3583 | .build())); |
| 3584 | |
| 3585 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3586 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3587 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3588 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3589 | |
| 3590 | window->assertNoEvents(); |
| 3591 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3592 | } |
| 3593 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3594 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 3595 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3596 | |
| 3597 | sp<FakeWindowHandle> spyWindow = |
| 3598 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3599 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 3600 | spyWindow->setTrustedOverlay(true); |
| 3601 | spyWindow->setSpy(true); |
| 3602 | sp<FakeWindowHandle> window = |
| 3603 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3604 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3605 | |
| 3606 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3607 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3608 | |
| 3609 | // Send mouse cursor to the window |
| 3610 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3611 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3612 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3613 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3614 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3615 | .build())); |
| 3616 | |
| 3617 | // Move mouse cursor |
| 3618 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3619 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3620 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3621 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3622 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3623 | .build())); |
| 3624 | |
| 3625 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3626 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3627 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3628 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3629 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3630 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3631 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3632 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3633 | // Touch down on the window |
| 3634 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3635 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3636 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3637 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3638 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3639 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3640 | .build())); |
| 3641 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3642 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3643 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3644 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 3645 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3646 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3647 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3648 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3649 | |
| 3650 | // pilfer the motion, retaining the gesture on the spy window. |
| 3651 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3652 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 3653 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3654 | |
| 3655 | // Touch UP on the window |
| 3656 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3657 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3658 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3659 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3660 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3661 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3662 | .build())); |
| 3663 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3664 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3665 | |
| 3666 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 3667 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 3668 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 3669 | |
| 3670 | // One more tap - DOWN |
| 3671 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3672 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3673 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3674 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3675 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3676 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3677 | .build())); |
| 3678 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3679 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3680 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3681 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3682 | |
| 3683 | // Touch UP on the window |
| 3684 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3685 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3686 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3687 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3688 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3689 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 3690 | .build())); |
| 3691 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3692 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3693 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3694 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 3695 | |
| 3696 | window->assertNoEvents(); |
| 3697 | spyWindow->assertNoEvents(); |
| 3698 | } |
| 3699 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3700 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 3701 | // directly in this test. |
| 3702 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3703 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3704 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3705 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3706 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3707 | |
| 3708 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3709 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3710 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3711 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3712 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3713 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3714 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3715 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3716 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3717 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3718 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3719 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3720 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3721 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3722 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3723 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3724 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3725 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3726 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3727 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3728 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3729 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3730 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3731 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3732 | AINPUT_SOURCE_MOUSE) |
| 3733 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3734 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3735 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3736 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3737 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3738 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3739 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3740 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3741 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3742 | AINPUT_SOURCE_MOUSE) |
| 3743 | .buttonState(0) |
| 3744 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3745 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3746 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3747 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3748 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3749 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3750 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3751 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3752 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3753 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3754 | .build())); |
| 3755 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3756 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 3757 | // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the |
| 3758 | // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail. |
| 3759 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3760 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3761 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 3762 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3763 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3764 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3765 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3766 | } |
| 3767 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3768 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3769 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 3770 | * is generated. |
| 3771 | */ |
| 3772 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 3773 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3774 | sp<FakeWindowHandle> window = |
| 3775 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3776 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 3777 | |
| 3778 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3779 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3780 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3781 | |
| 3782 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3783 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3784 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3785 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3786 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3787 | .build())); |
| 3788 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3789 | |
| 3790 | // Remove the window, but keep the channel. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3791 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3792 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3793 | } |
| 3794 | |
| 3795 | /** |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 3796 | * Test that invalid HOVER events sent by accessibility do not cause a fatal crash. |
| 3797 | */ |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 3798 | TEST_F_WITH_FLAGS(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash, |
| 3799 | REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(com::android::input::flags, |
| 3800 | a11y_crash_on_inconsistent_event_stream))) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 3801 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3802 | sp<FakeWindowHandle> window = |
| 3803 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3804 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 3805 | |
| 3806 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3807 | |
| 3808 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3809 | |
| 3810 | MotionEventBuilder hoverEnterBuilder = |
| 3811 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3812 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3813 | .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 3814 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3815 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 3816 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 3817 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 3818 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3819 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3820 | } |
| 3821 | |
| 3822 | /** |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3823 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. |
| 3824 | */ |
| 3825 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { |
| 3826 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3827 | sp<FakeWindowHandle> window = |
| 3828 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3829 | window->setFrame(Rect(0, 0, 100, 100)); |
| 3830 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3831 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3832 | |
| 3833 | const int32_t mouseDeviceId = 7; |
| 3834 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3835 | |
| 3836 | // Start hovering with the mouse |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3837 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3838 | .deviceId(mouseDeviceId) |
| 3839 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 3840 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3841 | window->consumeMotionEvent( |
| 3842 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3843 | |
| 3844 | // Touch goes down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3845 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3846 | .deviceId(touchDeviceId) |
| 3847 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3848 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3849 | |
| 3850 | window->consumeMotionEvent( |
| 3851 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 3852 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3853 | } |
| 3854 | |
| 3855 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3856 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3857 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 3858 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3859 | */ |
| 3860 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 3861 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3862 | sp<FakeWindowHandle> window = |
| 3863 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3864 | window->setFrame(Rect(0, 0, 100, 100)); |
| 3865 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3866 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3867 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 3868 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 3869 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3870 | ASSERT_NO_FATAL_FAILURE( |
| 3871 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 3872 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3873 | |
| 3874 | // Tap on the window |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3875 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3876 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 3877 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3878 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 3879 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 3880 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 3881 | |
| 3882 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3883 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 3884 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 3885 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3886 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3887 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 3888 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 3889 | ASSERT_NO_FATAL_FAILURE( |
| 3890 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 3891 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 3892 | } |
| 3893 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3894 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 3895 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3896 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 3897 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 3898 | ADISPLAY_ID_DEFAULT); |
| 3899 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 3900 | sp<FakeWindowHandle> windowSecondDisplay = |
| 3901 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 3902 | SECOND_DISPLAY_ID); |
| 3903 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 3904 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3905 | mDispatcher->onWindowInfosChanged( |
| 3906 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3907 | |
| 3908 | // Set cursor position in window in default display and check that hover enter and move |
| 3909 | // events are generated. |
| 3910 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3911 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3912 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3913 | AINPUT_SOURCE_MOUSE) |
| 3914 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3915 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3916 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3917 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3918 | |
| 3919 | // Remove all windows in secondary display and check that no event happens on window in |
| 3920 | // primary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3921 | mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0}); |
| 3922 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3923 | windowDefaultDisplay->assertNoEvents(); |
| 3924 | |
| 3925 | // Move cursor position in window in default display and check that only hover move |
| 3926 | // event is generated and not hover enter event. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3927 | mDispatcher->onWindowInfosChanged( |
| 3928 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3929 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3930 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3931 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3932 | AINPUT_SOURCE_MOUSE) |
| 3933 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3934 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3935 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3936 | windowDefaultDisplay->consumeMotionEvent( |
| 3937 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 3938 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 3939 | windowDefaultDisplay->assertNoEvents(); |
| 3940 | } |
| 3941 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3942 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3943 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3944 | |
| 3945 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3946 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3947 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3948 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3949 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3950 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3951 | |
| 3952 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3953 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3954 | mDispatcher->onWindowInfosChanged( |
| 3955 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3956 | |
| 3957 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 3958 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3959 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3960 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 3961 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 3962 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 3963 | windowRight->assertNoEvents(); |
| 3964 | } |
| 3965 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3966 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3967 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3968 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3969 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 3970 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3971 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3972 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 3973 | setFocusedWindow(window); |
| 3974 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 3975 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3976 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3977 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3978 | |
| 3979 | // Window should receive key down event. |
| 3980 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 3981 | |
| 3982 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 3983 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3984 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 3985 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT, AKEY_EVENT_FLAG_CANCELED); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3986 | } |
| 3987 | |
| 3988 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3989 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3990 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3991 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3992 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3993 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3994 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3995 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 3996 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 3997 | |
| 3998 | // Window should receive motion down event. |
| 3999 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4000 | |
| 4001 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 4002 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4003 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 4004 | window->consumeMotionEvent( |
| 4005 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4006 | } |
| 4007 | |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 4008 | TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) { |
| 4009 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4010 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4011 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4012 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4013 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 4014 | |
| 4015 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 4016 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10)) |
| 4017 | .build()); |
| 4018 | |
| 4019 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 4020 | |
| 4021 | // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT |
| 4022 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
| 4023 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4024 | |
| 4025 | // After the device has been reset, a new hovering stream can be sent to the window |
| 4026 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 4027 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15)) |
| 4028 | .build()); |
| 4029 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 4030 | } |
| 4031 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4032 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 4033 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4034 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4035 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4036 | window->setFocusable(true); |
| 4037 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4038 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4039 | setFocusedWindow(window); |
| 4040 | |
| 4041 | window->consumeFocusEvent(true); |
| 4042 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4043 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4044 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 4045 | const nsecs_t injectTime = keyArgs.eventTime; |
| 4046 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4047 | mDispatcher->notifyKey(keyArgs); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4048 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 4049 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4050 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 4051 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 4052 | } |
| 4053 | |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 4054 | /** |
| 4055 | * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set. |
| 4056 | */ |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4057 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 4058 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4059 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4060 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4061 | window->setFocusable(true); |
| 4062 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4063 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4064 | setFocusedWindow(window); |
| 4065 | |
| 4066 | window->consumeFocusEvent(true); |
| 4067 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4068 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4069 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 4070 | |
| 4071 | // Set a value that's significantly larger than the default consumption timeout. If the |
| 4072 | // implementation is correct, the actual value doesn't matter; it won't slow down the test. |
| 4073 | mFakePolicy->setInterceptKeyTimeout(600ms); |
| 4074 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
| 4075 | // Window should receive key event immediately when same key up. |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4076 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 4077 | } |
| 4078 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4079 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4080 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 4081 | * WATCH_OUTSIDE_TOUCH. |
| 4082 | * Both windows are owned by the same UID. |
| 4083 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 4084 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 4085 | */ |
| 4086 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 4087 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4088 | sp<FakeWindowHandle> window = |
| 4089 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4090 | window->setFrame(Rect{0, 0, 100, 100}); |
| 4091 | |
| 4092 | sp<FakeWindowHandle> outsideWindow = |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4093 | sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window", |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4094 | ADISPLAY_ID_DEFAULT); |
| 4095 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 4096 | outsideWindow->setWatchOutsideTouch(true); |
| 4097 | // 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] | 4098 | mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4099 | |
| 4100 | // Tap on first window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4101 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4102 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4103 | {PointF{50, 50}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4104 | window->consumeMotionDown(); |
| 4105 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 4106 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 4107 | outsideWindow->consumeMotionEvent( |
| 4108 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 4109 | |
| 4110 | // Ensure outsideWindow doesn't get any more events for the gesture. |
| 4111 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4112 | ADISPLAY_ID_DEFAULT, {PointF{51, 51}})); |
| 4113 | window->consumeMotionMove(); |
| 4114 | outsideWindow->assertNoEvents(); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4115 | } |
| 4116 | |
| 4117 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4118 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 4119 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 4120 | * ACTION_OUTSIDE event is sent per gesture. |
| 4121 | */ |
| 4122 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 4123 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 4124 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4125 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4126 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4127 | window->setWatchOutsideTouch(true); |
| 4128 | window->setFrame(Rect{0, 0, 100, 100}); |
| 4129 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4130 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4131 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4132 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 4133 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4134 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 4135 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4136 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4137 | mDispatcher->onWindowInfosChanged( |
| 4138 | {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4139 | |
| 4140 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4141 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4142 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4143 | {PointF{-10, -10}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4144 | window->assertNoEvents(); |
| 4145 | secondWindow->assertNoEvents(); |
| 4146 | |
| 4147 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 4148 | // Now, `window` should get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4149 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4150 | ADISPLAY_ID_DEFAULT, |
| 4151 | {PointF{-10, -10}, PointF{105, 105}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4152 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 4153 | window->consumeMotionEvent( |
| 4154 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4155 | secondWindow->consumeMotionDown(); |
| 4156 | thirdWindow->assertNoEvents(); |
| 4157 | |
| 4158 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 4159 | // 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] | 4160 | mDispatcher->notifyMotion( |
| 4161 | generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4162 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4163 | window->assertNoEvents(); |
| 4164 | secondWindow->consumeMotionMove(); |
| 4165 | thirdWindow->consumeMotionDown(); |
| 4166 | } |
| 4167 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4168 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 4169 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4170 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4171 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4172 | window->setFocusable(true); |
| 4173 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4174 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4175 | setFocusedWindow(window); |
| 4176 | |
| 4177 | window->consumeFocusEvent(true); |
| 4178 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4179 | const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 4180 | const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 4181 | mDispatcher->notifyKey(keyDown); |
| 4182 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4183 | |
| 4184 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4185 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 4186 | |
| 4187 | // 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] | 4188 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4189 | |
| 4190 | window->consumeFocusEvent(false); |
| 4191 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4192 | mDispatcher->notifyKey(keyDown); |
| 4193 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4194 | window->assertNoEvents(); |
| 4195 | } |
| 4196 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4197 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 4198 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4199 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4200 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4201 | // Ensure window is non-split and have some transform. |
| 4202 | window->setPreventSplitting(true); |
| 4203 | window->setWindowOffset(20, 40); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4204 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4205 | |
| 4206 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4207 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4208 | {50, 50})) |
| 4209 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4210 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4211 | |
| 4212 | const MotionEvent secondFingerDownEvent = |
| 4213 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4214 | .displayId(ADISPLAY_ID_DEFAULT) |
| 4215 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4216 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 4217 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4218 | .build(); |
| 4219 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4220 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4221 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 4222 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4223 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 4224 | std::unique_ptr<MotionEvent> event = window->consumeMotionEvent(); |
| 4225 | ASSERT_NE(nullptr, event); |
| 4226 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 4227 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 4228 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 4229 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 4230 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4231 | } |
| 4232 | |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 4233 | /** |
| 4234 | * Two windows: a splittable and a non-splittable. |
| 4235 | * The non-splittable window shouldn't receive any "incomplete" gestures. |
| 4236 | * Send the first pointer to the splittable window, and then touch the non-splittable window. |
| 4237 | * The second pointer should be dropped because the initial window is splittable, so it won't get |
| 4238 | * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any |
| 4239 | * "incomplete" gestures. |
| 4240 | */ |
| 4241 | TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) { |
| 4242 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4243 | sp<FakeWindowHandle> leftWindow = |
| 4244 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window", |
| 4245 | ADISPLAY_ID_DEFAULT); |
| 4246 | leftWindow->setPreventSplitting(false); |
| 4247 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4248 | sp<FakeWindowHandle> rightWindow = |
| 4249 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window", |
| 4250 | ADISPLAY_ID_DEFAULT); |
| 4251 | rightWindow->setPreventSplitting(true); |
| 4252 | rightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 4253 | mDispatcher->onWindowInfosChanged( |
| 4254 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 4255 | |
| 4256 | // Touch down on left, splittable window |
| 4257 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4258 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4259 | .build()); |
| 4260 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4261 | |
| 4262 | mDispatcher->notifyMotion( |
| 4263 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4264 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 4265 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
| 4266 | .build()); |
| 4267 | leftWindow->assertNoEvents(); |
| 4268 | rightWindow->assertNoEvents(); |
| 4269 | } |
| 4270 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4271 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) { |
| 4272 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4273 | sp<FakeWindowHandle> window = |
| 4274 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4275 | window->setFrame(Rect(0, 0, 400, 400)); |
| 4276 | sp<FakeWindowHandle> trustedOverlay = |
| 4277 | sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay", |
| 4278 | ADISPLAY_ID_DEFAULT); |
| 4279 | trustedOverlay->setSpy(true); |
| 4280 | trustedOverlay->setTrustedOverlay(true); |
| 4281 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4282 | mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4283 | |
| 4284 | // Start a three-finger touchpad swipe |
| 4285 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4286 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4287 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4288 | .build()); |
| 4289 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 4290 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4291 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4292 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4293 | .build()); |
| 4294 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 4295 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4296 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4297 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 4298 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4299 | .build()); |
| 4300 | |
| 4301 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4302 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4303 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN)); |
| 4304 | |
| 4305 | // Move the swipe a bit |
| 4306 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4307 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4308 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4309 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4310 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4311 | .build()); |
| 4312 | |
| 4313 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 4314 | |
| 4315 | // End the swipe |
| 4316 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 4317 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4318 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4319 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4320 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4321 | .build()); |
| 4322 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 4323 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4324 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4325 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4326 | .build()); |
| 4327 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4328 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4329 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4330 | .build()); |
| 4331 | |
| 4332 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP)); |
| 4333 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 4334 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 4335 | |
| 4336 | window->assertNoEvents(); |
| 4337 | } |
| 4338 | |
| 4339 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) { |
| 4340 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4341 | sp<FakeWindowHandle> window = |
| 4342 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4343 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4344 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4345 | |
| 4346 | // Start a three-finger touchpad swipe |
| 4347 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4348 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4349 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4350 | .build()); |
| 4351 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 4352 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4353 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4354 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4355 | .build()); |
| 4356 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 4357 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4358 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4359 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 4360 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4361 | .build()); |
| 4362 | |
| 4363 | // Move the swipe a bit |
| 4364 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4365 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4366 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4367 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4368 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4369 | .build()); |
| 4370 | |
| 4371 | // End the swipe |
| 4372 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 4373 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4374 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4375 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4376 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4377 | .build()); |
| 4378 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 4379 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4380 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4381 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4382 | .build()); |
| 4383 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4384 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4385 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4386 | .build()); |
| 4387 | |
| 4388 | window->assertNoEvents(); |
| 4389 | } |
| 4390 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4391 | /** |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4392 | * Send a two-pointer gesture to a single window. The window's orientation changes in response to |
| 4393 | * the first pointer. |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4394 | * 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] | 4395 | */ |
| 4396 | TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) { |
| 4397 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4398 | sp<FakeWindowHandle> window = |
| 4399 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4400 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4401 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4402 | |
| 4403 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4404 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4405 | .downTime(baseTime + 10) |
| 4406 | .eventTime(baseTime + 10) |
| 4407 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4408 | .build()); |
| 4409 | |
| 4410 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4411 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4412 | // Change the transform so that the orientation is now different from original. |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4413 | window->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4414 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4415 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4416 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4417 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4418 | .downTime(baseTime + 10) |
| 4419 | .eventTime(baseTime + 30) |
| 4420 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4421 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 4422 | .build()); |
| 4423 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4424 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4425 | |
| 4426 | // 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] | 4427 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4428 | .downTime(baseTime + 10) |
| 4429 | .eventTime(baseTime + 40) |
| 4430 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4431 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 4432 | .build()); |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4433 | |
| 4434 | window->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 4435 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4436 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4437 | .downTime(baseTime + 10) |
| 4438 | .eventTime(baseTime + 50) |
| 4439 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4440 | .build()); |
| 4441 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4442 | window->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 4443 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4444 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4445 | .downTime(baseTime + 60) |
| 4446 | .eventTime(baseTime + 60) |
| 4447 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40)) |
| 4448 | .build()); |
| 4449 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4450 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4451 | } |
| 4452 | |
| 4453 | /** |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 4454 | * When there are multiple screens, such as screen projection to TV or screen recording, if the |
| 4455 | * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and |
| 4456 | * its coordinates should be converted by the transform of the windows of target screen. |
| 4457 | */ |
| 4458 | TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) { |
| 4459 | // This case will create a window and a spy window on the default display and mirror |
| 4460 | // window on the second display. cancel event is sent through spy window pilferPointers |
| 4461 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4462 | |
| 4463 | sp<FakeWindowHandle> spyWindowDefaultDisplay = |
| 4464 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 4465 | spyWindowDefaultDisplay->setTrustedOverlay(true); |
| 4466 | spyWindowDefaultDisplay->setSpy(true); |
| 4467 | |
| 4468 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 4469 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 4470 | ADISPLAY_ID_DEFAULT); |
| 4471 | windowDefaultDisplay->setWindowTransform(1, 0, 0, 1); |
| 4472 | |
| 4473 | sp<FakeWindowHandle> windowSecondDisplay = windowDefaultDisplay->clone(SECOND_DISPLAY_ID); |
| 4474 | windowSecondDisplay->setWindowTransform(2, 0, 0, 2); |
| 4475 | |
| 4476 | // Add the windows to the dispatcher |
| 4477 | mDispatcher->onWindowInfosChanged( |
| 4478 | {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(), |
| 4479 | *windowSecondDisplay->getInfo()}, |
| 4480 | {}, |
| 4481 | 0, |
| 4482 | 0}); |
| 4483 | |
| 4484 | // Send down to ADISPLAY_ID_DEFAULT |
| 4485 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4486 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4487 | {100, 100})) |
| 4488 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4489 | |
| 4490 | spyWindowDefaultDisplay->consumeMotionDown(); |
| 4491 | windowDefaultDisplay->consumeMotionDown(); |
| 4492 | |
| 4493 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken())); |
| 4494 | |
| 4495 | // windowDefaultDisplay gets cancel |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 4496 | std::unique_ptr<MotionEvent> event = windowDefaultDisplay->consumeMotionEvent(); |
| 4497 | ASSERT_NE(nullptr, event); |
| 4498 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event->getAction()); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 4499 | |
| 4500 | // The cancel event is sent to windowDefaultDisplay of the ADISPLAY_ID_DEFAULT display, so the |
| 4501 | // coordinates of the cancel are converted by windowDefaultDisplay's transform, the x and y |
| 4502 | // coordinates are both 100, otherwise if the cancel event is sent to windowSecondDisplay of |
| 4503 | // SECOND_DISPLAY_ID, the x and y coordinates are 200 |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 4504 | EXPECT_EQ(100, event->getX(0)); |
| 4505 | EXPECT_EQ(100, event->getY(0)); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 4506 | } |
| 4507 | |
| 4508 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4509 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 4510 | * |
| 4511 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 4512 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 4513 | * space. |
| 4514 | */ |
| 4515 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 4516 | public: |
| 4517 | void SetUp() override { |
| 4518 | InputDispatcherTest::SetUp(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4519 | removeAllWindowsAndDisplays(); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4520 | } |
| 4521 | |
| 4522 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 4523 | gui::DisplayInfo info; |
| 4524 | info.displayId = displayId; |
| 4525 | info.transform = transform; |
| 4526 | mDisplayInfos.push_back(std::move(info)); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4527 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4528 | } |
| 4529 | |
| 4530 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 4531 | mWindowInfos.push_back(*windowHandle->getInfo()); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4532 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4533 | } |
| 4534 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4535 | void removeAllWindowsAndDisplays() { |
| 4536 | mDisplayInfos.clear(); |
| 4537 | mWindowInfos.clear(); |
| 4538 | } |
| 4539 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4540 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 4541 | // on the display. |
| 4542 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 4543 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 4544 | // respectively. |
| 4545 | ui::Transform displayTransform; |
| 4546 | displayTransform.set(2, 0, 0, 4); |
| 4547 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 4548 | |
| 4549 | std::shared_ptr<FakeApplicationHandle> application = |
| 4550 | std::make_shared<FakeApplicationHandle>(); |
| 4551 | |
| 4552 | // Add two windows to the display. Their frames are represented in the display space. |
| 4553 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4554 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4555 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4556 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 4557 | addWindow(firstWindow); |
| 4558 | |
| 4559 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4560 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4561 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4562 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 4563 | addWindow(secondWindow); |
| 4564 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 4565 | } |
| 4566 | |
| 4567 | private: |
| 4568 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 4569 | std::vector<gui::WindowInfo> mWindowInfos; |
| 4570 | }; |
| 4571 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4572 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4573 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4574 | // 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] | 4575 | // selected so that if the hit test was performed with the point and the bounds being in |
| 4576 | // different coordinate spaces, the event would end up in the incorrect window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4577 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4578 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4579 | {PointF{75, 55}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4580 | |
| 4581 | firstWindow->consumeMotionDown(); |
| 4582 | secondWindow->assertNoEvents(); |
| 4583 | } |
| 4584 | |
| 4585 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 4586 | // the event should be treated as being in the logical display space. |
| 4587 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 4588 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4589 | // Send down to the first window. The point is represented in the logical display space. The |
| 4590 | // point is selected so that if the hit test was done in logical display space, then it would |
| 4591 | // end up in the incorrect window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4592 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4593 | PointF{75 * 2, 55 * 4}); |
| 4594 | |
| 4595 | firstWindow->consumeMotionDown(); |
| 4596 | secondWindow->assertNoEvents(); |
| 4597 | } |
| 4598 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4599 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 4600 | // event should be treated as being in the logical display space. |
| 4601 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 4602 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4603 | |
| 4604 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 4605 | ui::Transform injectedEventTransform; |
| 4606 | injectedEventTransform.set(matrix); |
| 4607 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 4608 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 4609 | |
| 4610 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4611 | .displayId(ADISPLAY_ID_DEFAULT) |
| 4612 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4613 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4614 | .x(untransformedPoint.x) |
| 4615 | .y(untransformedPoint.y)) |
| 4616 | .build(); |
| 4617 | event.transform(matrix); |
| 4618 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4619 | injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 4620 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 4621 | |
| 4622 | firstWindow->consumeMotionDown(); |
| 4623 | secondWindow->assertNoEvents(); |
| 4624 | } |
| 4625 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4626 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 4627 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4628 | |
| 4629 | // Send down to the second window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4630 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4631 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4632 | {PointF{150, 220}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4633 | |
| 4634 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 4635 | std::unique_ptr<MotionEvent> event = secondWindow->consumeMotionEvent(); |
| 4636 | ASSERT_NE(nullptr, event); |
| 4637 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4638 | |
| 4639 | // 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] | 4640 | EXPECT_EQ(300, event->getRawX(0)); |
| 4641 | EXPECT_EQ(880, event->getRawY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4642 | |
| 4643 | // Ensure that the x and y values are in the window's coordinate space. |
| 4644 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 4645 | // 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] | 4646 | EXPECT_EQ(100, event->getX(0)); |
| 4647 | EXPECT_EQ(80, event->getY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4648 | } |
| 4649 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4650 | TEST_F(InputDispatcherDisplayProjectionTest, CancelMotionWithCorrectCoordinates) { |
| 4651 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4652 | // The monitor will always receive events in the logical display's coordinate space, because |
| 4653 | // it does not have a window. |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 4654 | FakeMonitorReceiver monitor{*mDispatcher, "Monitor", ADISPLAY_ID_DEFAULT}; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 4655 | |
| 4656 | // Send down to the first window. |
| 4657 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4658 | ADISPLAY_ID_DEFAULT, {PointF{50, 100}})); |
| 4659 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 4660 | monitor.consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 4661 | |
| 4662 | // Second pointer goes down on second window. |
| 4663 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4664 | ADISPLAY_ID_DEFAULT, |
| 4665 | {PointF{50, 100}, PointF{150, 220}})); |
| 4666 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80))); |
| 4667 | const std::map<int32_t, PointF> expectedMonitorPointers{{0, PointF{100, 400}}, |
| 4668 | {1, PointF{300, 880}}}; |
| 4669 | monitor.consumeMotionEvent( |
| 4670 | AllOf(WithMotionAction(POINTER_1_DOWN), WithPointers(expectedMonitorPointers))); |
| 4671 | |
| 4672 | mDispatcher->cancelCurrentTouch(); |
| 4673 | |
| 4674 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 4675 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 80))); |
| 4676 | monitor.consumeMotionEvent( |
| 4677 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedMonitorPointers))); |
| 4678 | } |
| 4679 | |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4680 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeDownWithCorrectCoordinates) { |
| 4681 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4682 | |
| 4683 | // Send down to the first window. |
| 4684 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4685 | ADISPLAY_ID_DEFAULT, {PointF{50, 100}})); |
| 4686 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 4687 | |
| 4688 | // The pointer is transferred to the second window, and the second window receives it in the |
| 4689 | // correct coordinate space. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 4690 | mDispatcher->transferTouchGesture(firstWindow->getToken(), secondWindow->getToken()); |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 4691 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 4692 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(-100, -400))); |
| 4693 | } |
| 4694 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 4695 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverEnterExitWithCorrectCoordinates) { |
| 4696 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4697 | |
| 4698 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 4699 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 4700 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 4701 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 4702 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 4703 | |
| 4704 | // Touch down at the same location and ensure a hover exit is synthesized. |
| 4705 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
| 4706 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 4707 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 4708 | WithRawCoords(300, 880))); |
| 4709 | secondWindow->consumeMotionEvent( |
| 4710 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 4711 | secondWindow->assertNoEvents(); |
| 4712 | firstWindow->assertNoEvents(); |
| 4713 | } |
| 4714 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 4715 | // Same as above, but while the window is being mirrored. |
| 4716 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 4717 | SynthesizeHoverEnterExitWithCorrectCoordinatesWhenMirrored) { |
| 4718 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4719 | |
| 4720 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 4721 | ui::Transform secondDisplayTransform; |
| 4722 | secondDisplayTransform.set(matrix); |
| 4723 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 4724 | |
| 4725 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 4726 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 4727 | addWindow(secondWindowClone); |
| 4728 | |
| 4729 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 4730 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 4731 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 4732 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 4733 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 4734 | |
| 4735 | // Touch down at the same location and ensure a hover exit is synthesized for the correct |
| 4736 | // display. |
| 4737 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
| 4738 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 4739 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 4740 | WithRawCoords(300, 880))); |
| 4741 | secondWindow->consumeMotionEvent( |
| 4742 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 4743 | secondWindow->assertNoEvents(); |
| 4744 | firstWindow->assertNoEvents(); |
| 4745 | } |
| 4746 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 4747 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverCancelationWithCorrectCoordinates) { |
| 4748 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4749 | |
| 4750 | // Send hover enter to second window |
| 4751 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
| 4752 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 4753 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 4754 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 4755 | |
| 4756 | mDispatcher->cancelCurrentTouch(); |
| 4757 | |
| 4758 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 4759 | WithRawCoords(300, 880))); |
| 4760 | secondWindow->assertNoEvents(); |
| 4761 | firstWindow->assertNoEvents(); |
| 4762 | } |
| 4763 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 4764 | // Same as above, but while the window is being mirrored. |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 4765 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 4766 | SynthesizeHoverCancelationWithCorrectCoordinatesWhenMirrored) { |
| 4767 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 4768 | |
| 4769 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 4770 | ui::Transform secondDisplayTransform; |
| 4771 | secondDisplayTransform.set(matrix); |
| 4772 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 4773 | |
| 4774 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 4775 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 4776 | addWindow(secondWindowClone); |
| 4777 | |
| 4778 | // Send hover enter to second window |
| 4779 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
| 4780 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 4781 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 4782 | WithCoords(100, 80), WithRawCoords(300, 880), |
| 4783 | WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 4784 | |
| 4785 | mDispatcher->cancelCurrentTouch(); |
| 4786 | |
| 4787 | // Ensure the cancelation happens with the correct displayId and the correct coordinates. |
| 4788 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 4789 | WithRawCoords(300, 880), |
| 4790 | WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 4791 | secondWindow->assertNoEvents(); |
| 4792 | firstWindow->assertNoEvents(); |
| 4793 | } |
| 4794 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4795 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ |
| 4796 | class InputDispatcherDisplayOrientationFixture |
| 4797 | : public InputDispatcherDisplayProjectionTest, |
| 4798 | public ::testing::WithParamInterface<ui::Rotation> {}; |
| 4799 | |
| 4800 | // This test verifies the touchable region of a window for all rotations of the display by tapping |
| 4801 | // in different locations on the display, specifically points close to the four corners of a |
| 4802 | // window. |
| 4803 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { |
| 4804 | constexpr static int32_t displayWidth = 400; |
| 4805 | constexpr static int32_t displayHeight = 800; |
| 4806 | |
| 4807 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4808 | |
| 4809 | const auto rotation = GetParam(); |
| 4810 | |
| 4811 | // Set up the display with the specified rotation. |
| 4812 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 4813 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 4814 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 4815 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 4816 | logicalDisplayWidth, logicalDisplayHeight); |
| 4817 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 4818 | |
| 4819 | // Create a window with its bounds determined in the logical display. |
| 4820 | const Rect frameInLogicalDisplay(100, 100, 200, 300); |
| 4821 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); |
| 4822 | sp<FakeWindowHandle> window = |
| 4823 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4824 | window->setFrame(frameInDisplay, displayTransform); |
| 4825 | addWindow(window); |
| 4826 | |
| 4827 | // The following points in logical display space should be inside the window. |
| 4828 | static const std::array<vec2, 4> insidePoints{ |
| 4829 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 4830 | for (const auto pointInsideWindow : insidePoints) { |
| 4831 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); |
| 4832 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4833 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4834 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4835 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4836 | window->consumeMotionDown(); |
| 4837 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4838 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 4839 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4840 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4841 | window->consumeMotionUp(); |
| 4842 | } |
| 4843 | |
| 4844 | // The following points in logical display space should be outside the window. |
| 4845 | static const std::array<vec2, 5> outsidePoints{ |
| 4846 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 4847 | for (const auto pointOutsideWindow : outsidePoints) { |
| 4848 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); |
| 4849 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4850 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4851 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4852 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4853 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4854 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 4855 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4856 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4857 | } |
| 4858 | window->assertNoEvents(); |
| 4859 | } |
| 4860 | |
Linnan Li | 5e5645e | 2024-03-05 14:43:05 +0000 | [diff] [blame] | 4861 | // This test verifies the occlusion detection for all rotations of the display by tapping |
| 4862 | // in different locations on the display, specifically points close to the four corners of a |
| 4863 | // window. |
| 4864 | TEST_P(InputDispatcherDisplayOrientationFixture, BlockUntrustClickInDifferentOrientations) { |
| 4865 | constexpr static int32_t displayWidth = 400; |
| 4866 | constexpr static int32_t displayHeight = 800; |
| 4867 | |
| 4868 | std::shared_ptr<FakeApplicationHandle> untrustedWindowApplication = |
| 4869 | std::make_shared<FakeApplicationHandle>(); |
| 4870 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4871 | |
| 4872 | const auto rotation = GetParam(); |
| 4873 | |
| 4874 | // Set up the display with the specified rotation. |
| 4875 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 4876 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 4877 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 4878 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 4879 | logicalDisplayWidth, logicalDisplayHeight); |
| 4880 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 4881 | |
| 4882 | // Create a window that not trusted. |
| 4883 | const Rect untrustedWindowFrameInLogicalDisplay(100, 100, 200, 300); |
| 4884 | |
| 4885 | const Rect untrustedWindowFrameInDisplay = |
| 4886 | displayTransform.inverse().transform(untrustedWindowFrameInLogicalDisplay); |
| 4887 | |
| 4888 | sp<FakeWindowHandle> untrustedWindow = |
| 4889 | sp<FakeWindowHandle>::make(untrustedWindowApplication, mDispatcher, "UntrustedWindow", |
| 4890 | ADISPLAY_ID_DEFAULT); |
| 4891 | untrustedWindow->setFrame(untrustedWindowFrameInDisplay, displayTransform); |
| 4892 | untrustedWindow->setTrustedOverlay(false); |
| 4893 | untrustedWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 4894 | untrustedWindow->setTouchable(false); |
| 4895 | untrustedWindow->setAlpha(1.0f); |
| 4896 | untrustedWindow->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
| 4897 | addWindow(untrustedWindow); |
| 4898 | |
| 4899 | // Create a simple app window below the untrusted window. |
| 4900 | const Rect simpleAppWindowFrameInLogicalDisplay(0, 0, 300, 600); |
| 4901 | const Rect simpleAppWindowFrameInDisplay = |
| 4902 | displayTransform.inverse().transform(simpleAppWindowFrameInLogicalDisplay); |
| 4903 | |
| 4904 | sp<FakeWindowHandle> simpleAppWindow = |
| 4905 | sp<FakeWindowHandle>::make(application, mDispatcher, "SimpleAppWindow", |
| 4906 | ADISPLAY_ID_DEFAULT); |
| 4907 | simpleAppWindow->setFrame(simpleAppWindowFrameInDisplay, displayTransform); |
| 4908 | simpleAppWindow->setOwnerInfo(gui::Pid{2}, gui::Uid{202}); |
| 4909 | addWindow(simpleAppWindow); |
| 4910 | |
| 4911 | // The following points in logical display space should be inside the untrusted window, so |
| 4912 | // the simple window could not receive events that coordinate is these point. |
| 4913 | static const std::array<vec2, 4> untrustedPoints{ |
| 4914 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 4915 | |
| 4916 | for (const auto untrustedPoint : untrustedPoints) { |
| 4917 | const vec2 p = displayTransform.inverse().transform(untrustedPoint); |
| 4918 | const PointF pointInDisplaySpace{p.x, p.y}; |
| 4919 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4920 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4921 | {pointInDisplaySpace})); |
| 4922 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 4923 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4924 | {pointInDisplaySpace})); |
| 4925 | } |
| 4926 | untrustedWindow->assertNoEvents(); |
| 4927 | simpleAppWindow->assertNoEvents(); |
| 4928 | // The following points in logical display space should be outside the untrusted window, so |
| 4929 | // the simple window should receive events that coordinate is these point. |
| 4930 | static const std::array<vec2, 5> trustedPoints{ |
| 4931 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 4932 | for (const auto trustedPoint : trustedPoints) { |
| 4933 | const vec2 p = displayTransform.inverse().transform(trustedPoint); |
| 4934 | const PointF pointInDisplaySpace{p.x, p.y}; |
| 4935 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4936 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4937 | {pointInDisplaySpace})); |
| 4938 | simpleAppWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 4939 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 4940 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 4941 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4942 | {pointInDisplaySpace})); |
| 4943 | simpleAppWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, |
| 4944 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 4945 | } |
| 4946 | untrustedWindow->assertNoEvents(); |
| 4947 | } |
| 4948 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4949 | // Run the precision tests for all rotations. |
| 4950 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, |
| 4951 | InputDispatcherDisplayOrientationFixture, |
| 4952 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, |
| 4953 | ui::ROTATION_270), |
| 4954 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { |
| 4955 | return ftl::enum_string(testParamInfo.param); |
| 4956 | }); |
| 4957 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 4958 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 4959 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4960 | |
| 4961 | class TransferTouchFixture : public InputDispatcherTest, |
| 4962 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 4963 | |
| 4964 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4965 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4966 | |
| 4967 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4968 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4969 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 4970 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4971 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 4972 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4973 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4974 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4975 | sp<FakeWindowHandle> wallpaper = |
| 4976 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 4977 | wallpaper->setIsWallpaper(true); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 4978 | // 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] | 4979 | mDispatcher->onWindowInfosChanged( |
| 4980 | {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 4981 | setFocusedWindow(firstWindow); |
| 4982 | firstWindow->consumeFocusEvent(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4983 | |
| 4984 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4985 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4986 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4987 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4988 | // Only the first window should get the down event |
| 4989 | firstWindow->consumeMotionDown(); |
| 4990 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 4991 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 4992 | // Dispatcher reports pointer down outside focus for the wallpaper |
| 4993 | mFakePolicy->assertOnPointerDownEquals(wallpaper->getToken()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4994 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 4995 | // Transfer touch to the second window |
| 4996 | TransferFunction f = GetParam(); |
| 4997 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 4998 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 4999 | // The first window gets cancel and the second gets down |
| 5000 | firstWindow->consumeMotionCancel(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5001 | secondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5002 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5003 | // There should not be any changes to the focused window when transferring touch |
| 5004 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertOnPointerDownWasNotCalled()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5005 | |
| 5006 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5007 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5008 | ADISPLAY_ID_DEFAULT)); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5009 | // The first window gets no events and the second gets up |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5010 | firstWindow->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5011 | secondWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5012 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5013 | } |
| 5014 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5015 | /** |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5016 | * When 'transferTouchGesture' API is invoked, dispatcher needs to find the "best" window to take |
| 5017 | * touch from. When we have spy windows, there are several windows to choose from: either spy, or |
| 5018 | * 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] | 5019 | * natural to the user. |
| 5020 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 5021 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 5022 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 5023 | * the first window to the second. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5024 | * The main goal here is to test the behaviour of 'transferTouchGesture' API, but it's still valid |
| 5025 | * to test the other API, as well. |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5026 | */ |
| 5027 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 5028 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5029 | |
| 5030 | // Create a couple of windows + a spy window |
| 5031 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5032 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5033 | spyWindow->setTrustedOverlay(true); |
| 5034 | spyWindow->setSpy(true); |
| 5035 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5036 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5037 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5038 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5039 | |
| 5040 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5041 | mDispatcher->onWindowInfosChanged( |
| 5042 | {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5043 | |
| 5044 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5045 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5046 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5047 | // Only the first window and spy should get the down event |
| 5048 | spyWindow->consumeMotionDown(); |
| 5049 | firstWindow->consumeMotionDown(); |
| 5050 | |
| 5051 | // 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] | 5052 | // if f === 'transferTouchGesture'. |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5053 | TransferFunction f = GetParam(); |
| 5054 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5055 | ASSERT_TRUE(success); |
| 5056 | // The first window gets cancel and the second gets down |
| 5057 | firstWindow->consumeMotionCancel(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5058 | secondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5059 | |
| 5060 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5061 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5062 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5063 | // The first window gets no events and the second+spy get up |
| 5064 | firstWindow->assertNoEvents(); |
| 5065 | spyWindow->consumeMotionUp(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5066 | secondWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5067 | } |
| 5068 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5069 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5070 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5071 | |
| 5072 | PointF touchPoint = {10, 10}; |
| 5073 | |
| 5074 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5075 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5076 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5077 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5078 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5079 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5080 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5081 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5082 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5083 | |
| 5084 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5085 | mDispatcher->onWindowInfosChanged( |
| 5086 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5087 | |
| 5088 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5089 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5090 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5091 | {touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5092 | // Only the first window should get the down event |
| 5093 | firstWindow->consumeMotionDown(); |
| 5094 | secondWindow->assertNoEvents(); |
| 5095 | |
| 5096 | // Send pointer down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5097 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5098 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5099 | // Only the first window should get the pointer down event |
| 5100 | firstWindow->consumeMotionPointerDown(1); |
| 5101 | secondWindow->assertNoEvents(); |
| 5102 | |
| 5103 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5104 | TransferFunction f = GetParam(); |
| 5105 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5106 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5107 | // The first window gets cancel and the second gets down and pointer down |
| 5108 | firstWindow->consumeMotionCancel(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5109 | secondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 5110 | secondWindow->consumeMotionPointerDown(1, ADISPLAY_ID_DEFAULT, |
| 5111 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5112 | |
| 5113 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5114 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5115 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5116 | // The first window gets nothing and the second gets pointer up |
| 5117 | firstWindow->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5118 | secondWindow->consumeMotionPointerUp(1, ADISPLAY_ID_DEFAULT, |
| 5119 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5120 | |
| 5121 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5122 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5123 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5124 | // The first window gets nothing and the second gets up |
| 5125 | firstWindow->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5126 | secondWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5127 | } |
| 5128 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5129 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 5130 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5131 | |
| 5132 | // Create a couple of windows |
| 5133 | sp<FakeWindowHandle> firstWindow = |
| 5134 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5135 | ADISPLAY_ID_DEFAULT); |
| 5136 | firstWindow->setDupTouchToWallpaper(true); |
| 5137 | sp<FakeWindowHandle> secondWindow = |
| 5138 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5139 | ADISPLAY_ID_DEFAULT); |
| 5140 | secondWindow->setDupTouchToWallpaper(true); |
| 5141 | |
| 5142 | sp<FakeWindowHandle> wallpaper1 = |
| 5143 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 5144 | wallpaper1->setIsWallpaper(true); |
| 5145 | |
| 5146 | sp<FakeWindowHandle> wallpaper2 = |
| 5147 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 5148 | wallpaper2->setIsWallpaper(true); |
| 5149 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5150 | mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(), |
| 5151 | *secondWindow->getInfo(), *wallpaper2->getInfo()}, |
| 5152 | {}, |
| 5153 | 0, |
| 5154 | 0}); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5155 | |
| 5156 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5157 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5158 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5159 | |
| 5160 | // Only the first window should get the down event |
| 5161 | firstWindow->consumeMotionDown(); |
| 5162 | secondWindow->assertNoEvents(); |
| 5163 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5164 | wallpaper2->assertNoEvents(); |
| 5165 | |
| 5166 | // Transfer touch focus to the second window |
| 5167 | TransferFunction f = GetParam(); |
| 5168 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5169 | ASSERT_TRUE(success); |
| 5170 | |
| 5171 | // The first window gets cancel and the second gets down |
| 5172 | firstWindow->consumeMotionCancel(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5173 | secondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5174 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5175 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 5176 | expectedWallpaperFlags | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5177 | |
| 5178 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5179 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5180 | ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5181 | // The first window gets no events and the second gets up |
| 5182 | firstWindow->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5183 | secondWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5184 | wallpaper1->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5185 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, |
| 5186 | expectedWallpaperFlags | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5187 | } |
| 5188 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5189 | // 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] | 5190 | // 'transferTouchGesture' and 'transferTouchOnDisplay' are equivalent in behaviour. They only differ |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5191 | // for the case where there are multiple pointers split across several windows. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5192 | INSTANTIATE_TEST_SUITE_P( |
| 5193 | InputDispatcherTransferFunctionTests, TransferTouchFixture, |
| 5194 | ::testing::Values( |
| 5195 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, sp<IBinder> /*ignored*/, |
| 5196 | sp<IBinder> destChannelToken) { |
| 5197 | return dispatcher->transferTouchOnDisplay(destChannelToken, |
| 5198 | ADISPLAY_ID_DEFAULT); |
| 5199 | }, |
| 5200 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, sp<IBinder> from, |
| 5201 | sp<IBinder> to) { |
| 5202 | return dispatcher->transferTouchGesture(from, to, |
| 5203 | /*isDragAndDrop=*/false); |
| 5204 | })); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5205 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5206 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5207 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5208 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5209 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5210 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5211 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5212 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5213 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5214 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5215 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5216 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5217 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5218 | |
| 5219 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5220 | mDispatcher->onWindowInfosChanged( |
| 5221 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5222 | |
| 5223 | PointF pointInFirst = {300, 200}; |
| 5224 | PointF pointInSecond = {300, 600}; |
| 5225 | |
| 5226 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5227 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5228 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5229 | {pointInFirst})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5230 | // Only the first window should get the down event |
| 5231 | firstWindow->consumeMotionDown(); |
| 5232 | secondWindow->assertNoEvents(); |
| 5233 | |
| 5234 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5235 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5236 | ADISPLAY_ID_DEFAULT, |
| 5237 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5238 | // The first window gets a move and the second a down |
| 5239 | firstWindow->consumeMotionMove(); |
| 5240 | secondWindow->consumeMotionDown(); |
| 5241 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5242 | // Transfer touch to the second window |
| 5243 | mDispatcher->transferTouchGesture(firstWindow->getToken(), secondWindow->getToken()); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5244 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 5245 | firstWindow->consumeMotionCancel(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5246 | secondWindow->consumeMotionPointerDown(1, ADISPLAY_ID_DEFAULT, |
| 5247 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5248 | |
| 5249 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5250 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5251 | ADISPLAY_ID_DEFAULT, |
| 5252 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5253 | // The first window gets nothing and the second gets pointer up |
| 5254 | firstWindow->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5255 | secondWindow->consumeMotionPointerUp(1, ADISPLAY_ID_DEFAULT, |
| 5256 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5257 | |
| 5258 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5259 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5260 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5261 | // The first window gets nothing and the second gets up |
| 5262 | firstWindow->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5263 | secondWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5264 | } |
| 5265 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5266 | // Same as TransferTouch_TwoPointersSplitTouch, but using 'transferTouchOnDisplay' api. |
| 5267 | // Unlike 'transferTouchGesture', calling 'transferTouchOnDisplay' when there are two windows |
| 5268 | // receiving touch is not supported, so the touch should continue on those windows and the |
| 5269 | // transferred-to window should get nothing. |
| 5270 | TEST_F(InputDispatcherTest, TransferTouchOnDisplay_TwoPointersSplitTouch) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5271 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5272 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5273 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5274 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5275 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5276 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5277 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5278 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5279 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5280 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5281 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5282 | |
| 5283 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5284 | mDispatcher->onWindowInfosChanged( |
| 5285 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5286 | |
| 5287 | PointF pointInFirst = {300, 200}; |
| 5288 | PointF pointInSecond = {300, 600}; |
| 5289 | |
| 5290 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5291 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5292 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5293 | {pointInFirst})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5294 | // Only the first window should get the down event |
| 5295 | firstWindow->consumeMotionDown(); |
| 5296 | secondWindow->assertNoEvents(); |
| 5297 | |
| 5298 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5299 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5300 | ADISPLAY_ID_DEFAULT, |
| 5301 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5302 | // The first window gets a move and the second a down |
| 5303 | firstWindow->consumeMotionMove(); |
| 5304 | secondWindow->consumeMotionDown(); |
| 5305 | |
| 5306 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5307 | const bool transferred = |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5308 | mDispatcher->transferTouchOnDisplay(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
| 5309 | // The 'transferTouchOnDisplay' call should not succeed, because there are 2 touched windows |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5310 | ASSERT_FALSE(transferred); |
| 5311 | firstWindow->assertNoEvents(); |
| 5312 | secondWindow->assertNoEvents(); |
| 5313 | |
| 5314 | // The rest of the dispatch should proceed as normal |
| 5315 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5316 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5317 | ADISPLAY_ID_DEFAULT, |
| 5318 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5319 | // The first window gets MOVE and the second gets pointer up |
| 5320 | firstWindow->consumeMotionMove(); |
| 5321 | secondWindow->consumeMotionUp(); |
| 5322 | |
| 5323 | // Send up event to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5324 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5325 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5326 | // The first window gets nothing and the second gets up |
| 5327 | firstWindow->consumeMotionUp(); |
| 5328 | secondWindow->assertNoEvents(); |
| 5329 | } |
| 5330 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5331 | // 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] | 5332 | // 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] | 5333 | // the windows info of second display before default display. |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5334 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5335 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5336 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5337 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5338 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5339 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5340 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5341 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5342 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5343 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5344 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5345 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5346 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5347 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5348 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5349 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5350 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5351 | |
| 5352 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5353 | mDispatcher->onWindowInfosChanged( |
| 5354 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 5355 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 5356 | *secondWindowInPrimary->getInfo()}, |
| 5357 | {}, |
| 5358 | 0, |
| 5359 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5360 | |
| 5361 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5362 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5363 | {50, 50})) |
| 5364 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5365 | |
| 5366 | // Window should receive motion event. |
| 5367 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5368 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5369 | // Transfer touch |
| 5370 | ASSERT_TRUE(mDispatcher->transferTouchGesture(firstWindowInPrimary->getToken(), |
| 5371 | secondWindowInPrimary->getToken())); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5372 | // The first window gets cancel. |
| 5373 | firstWindowInPrimary->consumeMotionCancel(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5374 | secondWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 5375 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5376 | |
| 5377 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5378 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5379 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 5380 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5381 | firstWindowInPrimary->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5382 | secondWindowInPrimary->consumeMotionMove(ADISPLAY_ID_DEFAULT, |
| 5383 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5384 | |
| 5385 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5386 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5387 | {150, 50})) |
| 5388 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5389 | firstWindowInPrimary->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5390 | secondWindowInPrimary->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5391 | } |
| 5392 | |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5393 | // Same as TransferTouch_CloneSurface, but this touch on the secondary display and use |
| 5394 | // 'transferTouchOnDisplay' api. |
| 5395 | TEST_F(InputDispatcherTest, TransferTouchOnDisplay_CloneSurface) { |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5396 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5397 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5398 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5399 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5400 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5401 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5402 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5403 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5404 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5405 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5406 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5407 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5408 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5409 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5410 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5411 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5412 | |
| 5413 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5414 | mDispatcher->onWindowInfosChanged( |
| 5415 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 5416 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 5417 | *secondWindowInPrimary->getInfo()}, |
| 5418 | {}, |
| 5419 | 0, |
| 5420 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5421 | |
| 5422 | // Touch on second display. |
| 5423 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5424 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 5425 | {50, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5426 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5427 | |
| 5428 | // Window should receive motion event. |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 5429 | firstWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5430 | |
| 5431 | // Transfer touch focus |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 5432 | ASSERT_TRUE(mDispatcher->transferTouchOnDisplay(secondWindowInSecondary->getToken(), |
| 5433 | SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5434 | |
| 5435 | // The first window gets cancel. |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 5436 | firstWindowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5437 | secondWindowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, |
| 5438 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5439 | |
| 5440 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5441 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5442 | SECOND_DISPLAY_ID, {150, 50})) |
| 5443 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 5444 | firstWindowInSecondary->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5445 | secondWindowInSecondary->consumeMotionMove(SECOND_DISPLAY_ID, |
| 5446 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5447 | |
| 5448 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5449 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5450 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 5451 | firstWindowInSecondary->assertNoEvents(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 5452 | secondWindowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5453 | } |
| 5454 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5455 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5456 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5457 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5458 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5459 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5460 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5461 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5462 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5463 | |
| 5464 | window->consumeFocusEvent(true); |
| 5465 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5466 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5467 | |
| 5468 | // Window should receive key down event. |
| 5469 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5470 | |
| 5471 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5472 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5473 | mFakePolicy->assertUserActivityPoked(); |
| 5474 | } |
| 5475 | |
| 5476 | TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) { |
| 5477 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5478 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5479 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5480 | |
| 5481 | window->setDisableUserActivity(true); |
| 5482 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5483 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5484 | setFocusedWindow(window); |
| 5485 | |
| 5486 | window->consumeFocusEvent(true); |
| 5487 | |
| 5488 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5489 | |
| 5490 | // Window should receive key down event. |
| 5491 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5492 | |
| 5493 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5494 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5495 | mFakePolicy->assertUserActivityNotPoked(); |
| 5496 | } |
| 5497 | |
| 5498 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) { |
| 5499 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5500 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5501 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5502 | |
| 5503 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5504 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5505 | setFocusedWindow(window); |
| 5506 | |
| 5507 | window->consumeFocusEvent(true); |
| 5508 | |
| 5509 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5510 | mDispatcher->waitForIdle(); |
| 5511 | |
| 5512 | // System key is not passed down |
| 5513 | window->assertNoEvents(); |
| 5514 | |
| 5515 | // Should have poked user activity |
| 5516 | mFakePolicy->assertUserActivityPoked(); |
| 5517 | } |
| 5518 | |
| 5519 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) { |
| 5520 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5521 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5522 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5523 | |
| 5524 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5525 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5526 | setFocusedWindow(window); |
| 5527 | |
| 5528 | window->consumeFocusEvent(true); |
| 5529 | |
| 5530 | mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5531 | mDispatcher->waitForIdle(); |
| 5532 | |
| 5533 | // System key is not passed down |
| 5534 | window->assertNoEvents(); |
| 5535 | |
| 5536 | // Should have poked user activity |
| 5537 | mFakePolicy->assertUserActivityPoked(); |
| 5538 | } |
| 5539 | |
| 5540 | TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) { |
| 5541 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5542 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5543 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5544 | |
| 5545 | window->setDisableUserActivity(true); |
| 5546 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5547 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5548 | setFocusedWindow(window); |
| 5549 | |
| 5550 | window->consumeFocusEvent(true); |
| 5551 | |
| 5552 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5553 | mDispatcher->waitForIdle(); |
| 5554 | |
| 5555 | // System key is not passed down |
| 5556 | window->assertNoEvents(); |
| 5557 | |
| 5558 | // Should have poked user activity |
| 5559 | mFakePolicy->assertUserActivityPoked(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5560 | } |
| 5561 | |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5562 | TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) { |
| 5563 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5564 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5565 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5566 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5567 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5568 | |
| 5569 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5570 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5571 | ADISPLAY_ID_DEFAULT, {100, 100})) |
| 5572 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5573 | |
| 5574 | window->consumeMotionEvent( |
| 5575 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 5576 | |
| 5577 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5578 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5579 | mFakePolicy->assertUserActivityPoked(); |
| 5580 | } |
| 5581 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5582 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5583 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5584 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5585 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5586 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5587 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5588 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5589 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5590 | mDispatcher->waitForIdle(); |
| 5591 | |
| 5592 | window->assertNoEvents(); |
| 5593 | } |
| 5594 | |
| 5595 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 5596 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5597 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5598 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5599 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5600 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5601 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5602 | |
| 5603 | // Send key |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5604 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5605 | // Send motion |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5606 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5607 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5608 | |
| 5609 | // Window should receive only the motion event |
| 5610 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5611 | window->assertNoEvents(); // Key event or focus event will not be received |
| 5612 | } |
| 5613 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5614 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 5615 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5616 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5617 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5618 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5619 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5620 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5621 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5622 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5623 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5624 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5625 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5626 | |
| 5627 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5628 | mDispatcher->onWindowInfosChanged( |
| 5629 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5630 | |
| 5631 | PointF pointInFirst = {300, 200}; |
| 5632 | PointF pointInSecond = {300, 600}; |
| 5633 | |
| 5634 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5635 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5636 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5637 | {pointInFirst})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5638 | // Only the first window should get the down event |
| 5639 | firstWindow->consumeMotionDown(); |
| 5640 | secondWindow->assertNoEvents(); |
| 5641 | |
| 5642 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5643 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5644 | ADISPLAY_ID_DEFAULT, |
| 5645 | {pointInFirst, pointInSecond})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5646 | // The first window gets a move and the second a down |
| 5647 | firstWindow->consumeMotionMove(); |
| 5648 | secondWindow->consumeMotionDown(); |
| 5649 | |
| 5650 | // Send pointer cancel to the second window |
| 5651 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 5652 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5653 | {pointInFirst, pointInSecond}); |
| 5654 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5655 | mDispatcher->notifyMotion(pointerUpMotionArgs); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5656 | // The first window gets move and the second gets cancel. |
| 5657 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 5658 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 5659 | |
| 5660 | // Send up event. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5661 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5662 | ADISPLAY_ID_DEFAULT)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5663 | // The first window gets up and the second gets nothing. |
| 5664 | firstWindow->consumeMotionUp(); |
| 5665 | secondWindow->assertNoEvents(); |
| 5666 | } |
| 5667 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 5668 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 5669 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5670 | |
| 5671 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5672 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5673 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 5674 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 5675 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 5676 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 5677 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5678 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 5679 | window->assertNoEvents(); |
| 5680 | mDispatcher->waitForIdle(); |
| 5681 | } |
| 5682 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5683 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 5684 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5685 | /** |
| 5686 | * Two entities that receive touch: A window, and a global monitor. |
| 5687 | * The touch goes to the window, and then the window disappears. |
| 5688 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 5689 | * for the monitor, as well. |
| 5690 | * 1. foregroundWindow |
| 5691 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 5692 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5693 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5694 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5695 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5696 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5697 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5698 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5699 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5700 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5701 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5702 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5703 | {100, 200})) |
| 5704 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5705 | |
| 5706 | // Both the foreground window and the global monitor should receive the touch down |
| 5707 | window->consumeMotionDown(); |
| 5708 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5709 | |
| 5710 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5711 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5712 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 5713 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5714 | |
| 5715 | window->consumeMotionMove(); |
| 5716 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5717 | |
| 5718 | // Now the foreground window goes away |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5719 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5720 | window->consumeMotionCancel(); |
| 5721 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 5722 | |
| 5723 | // If more events come in, there will be no more foreground window to send them to. This will |
| 5724 | // cause a cancel for the monitor, as well. |
| 5725 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5726 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 5727 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 5728 | << "Injection should fail because the window was removed"; |
| 5729 | window->assertNoEvents(); |
| 5730 | // Global monitor now gets the cancel |
| 5731 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 5732 | } |
| 5733 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5734 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5735 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5736 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5737 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5738 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5739 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5740 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5741 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5742 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5743 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5744 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5745 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5746 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5747 | } |
| 5748 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5749 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5750 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5751 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5752 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5753 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5754 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5755 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5756 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5757 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5758 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5759 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 5760 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5761 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5762 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5763 | // Pilfer pointers from the monitor. |
| 5764 | // This should not do anything and the window should continue to receive events. |
| 5765 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5766 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5767 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5768 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5769 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 5770 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5771 | |
| 5772 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 5773 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 5774 | } |
| 5775 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5776 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5777 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5778 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5779 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5780 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5781 | window->setWindowOffset(20, 40); |
| 5782 | window->setWindowTransform(0, 1, -1, 0); |
| 5783 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5784 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5785 | |
| 5786 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5787 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5788 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5789 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5790 | std::unique_ptr<MotionEvent> event = monitor.consumeMotion(); |
| 5791 | ASSERT_NE(nullptr, event); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 5792 | // Even though window has transform, gesture monitor must not. |
| 5793 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 5794 | } |
| 5795 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5796 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5797 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5798 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5799 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5800 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5801 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 5802 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 5803 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 5804 | } |
| 5805 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 5806 | /** |
| 5807 | * Two displays |
| 5808 | * The first monitor has a foreground window, a monitor |
| 5809 | * The second window has only one monitor. |
| 5810 | * We first inject a Down event into the first display, this injection should succeed and both |
| 5811 | * the foreground window and monitor should receive a down event, then inject a Down event into |
| 5812 | * the second display as well, this injection should fail, at this point, the first display |
| 5813 | * window and monitor should not receive a cancel or any other event. |
| 5814 | * Continue to inject Move and UP events to the first display, the events should be received |
| 5815 | * normally by the foreground window and monitor. |
| 5816 | */ |
| 5817 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCanceledWhenAnotherEmptyDisplayReceiveEvents) { |
| 5818 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5819 | sp<FakeWindowHandle> window = |
| 5820 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 5821 | |
| 5822 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5823 | FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 5824 | |
| 5825 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 5826 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5827 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5828 | {100, 200})) |
| 5829 | << "The down event injected into the first display should succeed"; |
| 5830 | |
| 5831 | window->consumeMotionDown(); |
| 5832 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 5833 | |
| 5834 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 5835 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 5836 | {100, 200})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 5837 | << "The down event injected into the second display should fail since there's no " |
| 5838 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 5839 | |
| 5840 | // Continue to inject event to first display. |
| 5841 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5842 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5843 | ADISPLAY_ID_DEFAULT, {110, 220})) |
| 5844 | << "The move event injected into the first display should succeed"; |
| 5845 | |
| 5846 | window->consumeMotionMove(); |
| 5847 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 5848 | |
| 5849 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5850 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5851 | {110, 220})) |
| 5852 | << "The up event injected into the first display should succeed"; |
| 5853 | |
| 5854 | window->consumeMotionUp(); |
| 5855 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 5856 | |
| 5857 | window->assertNoEvents(); |
| 5858 | monitor.assertNoEvents(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 5859 | secondMonitor.assertNoEvents(); |
| 5860 | } |
| 5861 | |
| 5862 | /** |
| 5863 | * Two displays |
| 5864 | * There is a monitor and foreground window on each display. |
| 5865 | * First, we inject down events into each of the two displays, at this point, the foreground windows |
| 5866 | * and monitors on both displays should receive down events. |
| 5867 | * At this point, the foreground window of the second display goes away, the gone window should |
| 5868 | * receive the cancel event, and the other windows and monitors should not receive any events. |
| 5869 | * Inject a move event into the second display. At this point, the injection should fail because |
| 5870 | * the second display no longer has a foreground window. At this point, the monitor on the second |
| 5871 | * display should receive a cancel event, and any windows or monitors on the first display should |
| 5872 | * not receive any events, and any subsequent injection of events into the second display should |
| 5873 | * also fail. |
| 5874 | * Continue to inject events into the first display, and the events should all be injected |
| 5875 | * successfully and received normally. |
| 5876 | */ |
| 5877 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCancelWhenAnotherDisplayMonitorTouchCanceled) { |
| 5878 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5879 | sp<FakeWindowHandle> window = |
| 5880 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 5881 | sp<FakeWindowHandle> secondWindow = |
| 5882 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondForeground", |
| 5883 | SECOND_DISPLAY_ID); |
| 5884 | |
| 5885 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5886 | FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 5887 | |
| 5888 | // There is a foreground window on both displays. |
| 5889 | mDispatcher->onWindowInfosChanged({{*window->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
| 5890 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5891 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5892 | {100, 200})) |
| 5893 | << "The down event injected into the first display should succeed"; |
| 5894 | |
| 5895 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5896 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 5897 | |
| 5898 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5899 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 5900 | {100, 200})) |
| 5901 | << "The down event injected into the second display should succeed"; |
| 5902 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 5903 | secondWindow->consumeMotionDown(SECOND_DISPLAY_ID); |
| 5904 | secondMonitor.consumeMotionDown(SECOND_DISPLAY_ID); |
| 5905 | |
| 5906 | // Now second window is gone away. |
| 5907 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 5908 | |
| 5909 | // The gone window should receive a cancel, and the monitor on the second display should not |
| 5910 | // receive any events. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 5911 | secondWindow->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 5912 | secondMonitor.assertNoEvents(); |
| 5913 | |
| 5914 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 5915 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5916 | SECOND_DISPLAY_ID, {110, 220})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 5917 | << "The move event injected into the second display should fail because there's no " |
| 5918 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 5919 | // Now the monitor on the second display should receive a cancel event. |
| 5920 | secondMonitor.consumeMotionCancel(SECOND_DISPLAY_ID); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 5921 | |
| 5922 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5923 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5924 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 5925 | << "The move event injected into the first display should succeed"; |
| 5926 | |
| 5927 | window->consumeMotionMove(); |
| 5928 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 5929 | |
| 5930 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 5931 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 5932 | {110, 220})) |
| 5933 | << "The up event injected into the second display should fail because there's no " |
| 5934 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 5935 | |
| 5936 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5937 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5938 | {110, 220})) |
| 5939 | << "The up event injected into the first display should succeed"; |
| 5940 | |
| 5941 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 5942 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 5943 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 5944 | window->assertNoEvents(); |
| 5945 | monitor.assertNoEvents(); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 5946 | secondWindow->assertNoEvents(); |
| 5947 | secondMonitor.assertNoEvents(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 5948 | } |
| 5949 | |
| 5950 | /** |
| 5951 | * One display with transform |
| 5952 | * There is a foreground window and a monitor on the display |
| 5953 | * Inject down event and move event sequentially, the foreground window and monitor can receive down |
| 5954 | * event and move event, then let the foreground window go away, the foreground window receives |
| 5955 | * cancel event, inject move event again, the monitor receives cancel event, all the events received |
| 5956 | * by the monitor should be with the same transform as the display |
| 5957 | */ |
| 5958 | TEST_F(InputDispatcherMonitorTest, MonitorTouchCancelEventWithDisplayTransform) { |
| 5959 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5960 | sp<FakeWindowHandle> window = |
| 5961 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 5962 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 5963 | |
| 5964 | ui::Transform transform; |
| 5965 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 5966 | |
| 5967 | gui::DisplayInfo displayInfo; |
| 5968 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 5969 | displayInfo.transform = transform; |
| 5970 | |
| 5971 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
| 5972 | |
| 5973 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5974 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5975 | {100, 200})) |
| 5976 | << "The down event injected should succeed"; |
| 5977 | |
| 5978 | window->consumeMotionDown(); |
| 5979 | std::unique_ptr<MotionEvent> downMotionEvent = monitor.consumeMotion(); |
| 5980 | EXPECT_EQ(transform, downMotionEvent->getTransform()); |
| 5981 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, downMotionEvent->getAction()); |
| 5982 | |
| 5983 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 5984 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 5985 | ADISPLAY_ID_DEFAULT, {110, 220})) |
| 5986 | << "The move event injected should succeed"; |
| 5987 | |
| 5988 | window->consumeMotionMove(); |
| 5989 | std::unique_ptr<MotionEvent> moveMotionEvent = monitor.consumeMotion(); |
| 5990 | EXPECT_EQ(transform, moveMotionEvent->getTransform()); |
| 5991 | EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, moveMotionEvent->getAction()); |
| 5992 | |
| 5993 | // Let foreground window gone |
| 5994 | mDispatcher->onWindowInfosChanged({{}, {displayInfo}, 0, 0}); |
| 5995 | |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 5996 | // Foreground window should receive a cancel event, but not the monitor. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 5997 | window->consumeMotionCancel(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 5998 | |
| 5999 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6000 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6001 | ADISPLAY_ID_DEFAULT, {110, 220})) |
| 6002 | << "The move event injected should failed"; |
| 6003 | // Now foreground should not receive any events, but monitor should receive a cancel event |
| 6004 | // with transform that same as display's display. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6005 | std::unique_ptr<MotionEvent> cancelMotionEvent = monitor.consumeMotion(); |
| 6006 | EXPECT_EQ(transform, cancelMotionEvent->getTransform()); |
| 6007 | EXPECT_EQ(ADISPLAY_ID_DEFAULT, cancelMotionEvent->getDisplayId()); |
| 6008 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, cancelMotionEvent->getAction()); |
| 6009 | |
| 6010 | // Other event inject to this display should fail. |
| 6011 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6012 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6013 | ADISPLAY_ID_DEFAULT, {110, 220})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6014 | << "The up event injected should fail because the touched window was removed"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6015 | window->assertNoEvents(); |
| 6016 | monitor.assertNoEvents(); |
| 6017 | } |
| 6018 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6019 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6020 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6021 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6022 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6023 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6024 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6025 | |
| 6026 | NotifyMotionArgs motionArgs = |
| 6027 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6028 | ADISPLAY_ID_DEFAULT); |
| 6029 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6030 | mDispatcher->notifyMotion(motionArgs); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6031 | // Window should receive motion down event. |
| 6032 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6033 | |
| 6034 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6035 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6036 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6037 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 6038 | motionArgs.pointerCoords[0].getX() - 10); |
| 6039 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6040 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 6041 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT, /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6042 | } |
| 6043 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6044 | /** |
| 6045 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 6046 | * the device default right away. In the test scenario, we check both the default value, |
| 6047 | * and the action of enabling / disabling. |
| 6048 | */ |
| 6049 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6050 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6051 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6052 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6053 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6054 | |
| 6055 | // Set focused application. |
| 6056 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6057 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6058 | |
| 6059 | SCOPED_TRACE("Check default value of touch mode"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6060 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6061 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6062 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6063 | |
| 6064 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6065 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6066 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6067 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6068 | |
| 6069 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6070 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6071 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6072 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6073 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6074 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6075 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6076 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6077 | |
| 6078 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6079 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6080 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6081 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6082 | |
| 6083 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6084 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6085 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6086 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6087 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6088 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6089 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6090 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6091 | |
| 6092 | window->assertNoEvents(); |
| 6093 | } |
| 6094 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6095 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6096 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6097 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6098 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6099 | |
| 6100 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6101 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6102 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6103 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6104 | setFocusedWindow(window); |
| 6105 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6106 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6107 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6108 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 6109 | mDispatcher->notifyKey(keyArgs); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6110 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6111 | std::unique_ptr<KeyEvent> event = window->consumeKey(); |
| 6112 | ASSERT_NE(event, nullptr); |
| 6113 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6114 | ASSERT_NE(verified, nullptr); |
| 6115 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 6116 | |
| 6117 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 6118 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 6119 | ASSERT_EQ(keyArgs.source, verified->source); |
| 6120 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 6121 | |
| 6122 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 6123 | |
| 6124 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6125 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6126 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6127 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 6128 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 6129 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 6130 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 6131 | } |
| 6132 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6133 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6134 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6135 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6136 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6137 | |
| 6138 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6139 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 6140 | ui::Transform transform; |
| 6141 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 6142 | |
| 6143 | gui::DisplayInfo displayInfo; |
| 6144 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 6145 | displayInfo.transform = transform; |
| 6146 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6147 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6148 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6149 | const NotifyMotionArgs motionArgs = |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6150 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6151 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6152 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6153 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6154 | std::unique_ptr<MotionEvent> event = window->consumeMotionEvent(); |
| 6155 | ASSERT_NE(nullptr, event); |
| 6156 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6157 | ASSERT_NE(verified, nullptr); |
| 6158 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 6159 | |
| 6160 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 6161 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 6162 | EXPECT_EQ(motionArgs.source, verified->source); |
| 6163 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 6164 | |
| 6165 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 6166 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 6167 | const vec2 rawXY = |
| 6168 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 6169 | motionArgs.pointerCoords[0].getXYValue()); |
| 6170 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 6171 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6172 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6173 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6174 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6175 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 6176 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 6177 | } |
| 6178 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 6179 | /** |
| 6180 | * Ensure that separate calls to sign the same data are generating the same key. |
| 6181 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 6182 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 6183 | * tests. |
| 6184 | */ |
| 6185 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 6186 | KeyEvent event = getTestKeyEvent(); |
| 6187 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 6188 | |
| 6189 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 6190 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 6191 | ASSERT_EQ(hmac1, hmac2); |
| 6192 | } |
| 6193 | |
| 6194 | /** |
| 6195 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 6196 | */ |
| 6197 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 6198 | KeyEvent event = getTestKeyEvent(); |
| 6199 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 6200 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 6201 | |
| 6202 | verifiedEvent.deviceId += 1; |
| 6203 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6204 | |
| 6205 | verifiedEvent.source += 1; |
| 6206 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6207 | |
| 6208 | verifiedEvent.eventTimeNanos += 1; |
| 6209 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6210 | |
| 6211 | verifiedEvent.displayId += 1; |
| 6212 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6213 | |
| 6214 | verifiedEvent.action += 1; |
| 6215 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6216 | |
| 6217 | verifiedEvent.downTimeNanos += 1; |
| 6218 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6219 | |
| 6220 | verifiedEvent.flags += 1; |
| 6221 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6222 | |
| 6223 | verifiedEvent.keyCode += 1; |
| 6224 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6225 | |
| 6226 | verifiedEvent.scanCode += 1; |
| 6227 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6228 | |
| 6229 | verifiedEvent.metaState += 1; |
| 6230 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6231 | |
| 6232 | verifiedEvent.repeatCount += 1; |
| 6233 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6234 | } |
| 6235 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6236 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 6237 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6238 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6239 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6240 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6241 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6242 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6243 | |
| 6244 | // Top window is also focusable but is not granted focus. |
| 6245 | windowTop->setFocusable(true); |
| 6246 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6247 | mDispatcher->onWindowInfosChanged( |
| 6248 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6249 | setFocusedWindow(windowSecond); |
| 6250 | |
| 6251 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6252 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6253 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6254 | |
| 6255 | // Focused window should receive event. |
| 6256 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6257 | windowTop->assertNoEvents(); |
| 6258 | } |
| 6259 | |
| 6260 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 6261 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6262 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6263 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6264 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6265 | |
| 6266 | window->setFocusable(true); |
| 6267 | // Release channel for window is no longer valid. |
| 6268 | window->releaseChannel(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6269 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6270 | setFocusedWindow(window); |
| 6271 | |
| 6272 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6273 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6274 | |
| 6275 | // window channel is invalid, so it should not receive any input event. |
| 6276 | window->assertNoEvents(); |
| 6277 | } |
| 6278 | |
| 6279 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 6280 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6281 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6282 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6283 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6284 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6285 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6286 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6287 | setFocusedWindow(window); |
| 6288 | |
| 6289 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6290 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6291 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6292 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6293 | window->assertNoEvents(); |
| 6294 | } |
| 6295 | |
| 6296 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 6297 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6298 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6299 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6300 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6301 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6302 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6303 | |
| 6304 | windowTop->setFocusable(true); |
| 6305 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6306 | mDispatcher->onWindowInfosChanged( |
| 6307 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6308 | setFocusedWindow(windowTop); |
| 6309 | windowTop->consumeFocusEvent(true); |
| 6310 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6311 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6312 | mDispatcher->onWindowInfosChanged( |
| 6313 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6314 | windowSecond->consumeFocusEvent(true); |
| 6315 | windowTop->consumeFocusEvent(false); |
| 6316 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6317 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6318 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6319 | |
| 6320 | // Focused window should receive event. |
| 6321 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6322 | } |
| 6323 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6324 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6325 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6326 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6327 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6328 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6329 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6330 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6331 | |
| 6332 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6333 | windowSecond->setFocusable(false); |
| 6334 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6335 | mDispatcher->onWindowInfosChanged( |
| 6336 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6337 | setFocusedWindow(windowTop); |
| 6338 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6339 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6340 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6341 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6342 | |
| 6343 | // Event should be dropped. |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6344 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6345 | windowSecond->assertNoEvents(); |
| 6346 | } |
| 6347 | |
| 6348 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 6349 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6350 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6351 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6352 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6353 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 6354 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6355 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6356 | |
| 6357 | window->setFocusable(true); |
| 6358 | previousFocusedWindow->setFocusable(true); |
| 6359 | window->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6360 | mDispatcher->onWindowInfosChanged( |
| 6361 | {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6362 | setFocusedWindow(previousFocusedWindow); |
| 6363 | previousFocusedWindow->consumeFocusEvent(true); |
| 6364 | |
| 6365 | // Requesting focus on invisible window takes focus from currently focused window. |
| 6366 | setFocusedWindow(window); |
| 6367 | previousFocusedWindow->consumeFocusEvent(false); |
| 6368 | |
| 6369 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6370 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6371 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 6372 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6373 | |
| 6374 | // Window does not get focus event or key down. |
| 6375 | window->assertNoEvents(); |
| 6376 | |
| 6377 | // Window becomes visible. |
| 6378 | window->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6379 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6380 | |
| 6381 | // Window receives focus event. |
| 6382 | window->consumeFocusEvent(true); |
| 6383 | // Focused window receives key down. |
| 6384 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6385 | } |
| 6386 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6387 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 6388 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6389 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6390 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6391 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6392 | |
| 6393 | // window is granted focus. |
| 6394 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6395 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6396 | setFocusedWindow(window); |
| 6397 | window->consumeFocusEvent(true); |
| 6398 | |
| 6399 | // When a display is removed window loses focus. |
| 6400 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 6401 | window->consumeFocusEvent(false); |
| 6402 | } |
| 6403 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6404 | /** |
| 6405 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 6406 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 6407 | * of the 'slipperyEnterWindow'. |
| 6408 | * |
| 6409 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 6410 | * a way so that the touched location is no longer covered by the top window. |
| 6411 | * |
| 6412 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 6413 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 6414 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 6415 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 6416 | * with ACTION_DOWN). |
| 6417 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 6418 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 6419 | * |
| 6420 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 6421 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 6422 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 6423 | * |
| 6424 | * In this test, we ensure that the event received by the bottom window has |
| 6425 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 6426 | */ |
| 6427 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6428 | constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6429 | constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1}; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6430 | |
| 6431 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6432 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6433 | |
| 6434 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6435 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6436 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6437 | // Make sure this one overlaps the bottom window |
| 6438 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 6439 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 6440 | // one. Windows with the same owner are not considered to be occluding each other. |
| 6441 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 6442 | |
| 6443 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6444 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6445 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6446 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6447 | mDispatcher->onWindowInfosChanged( |
| 6448 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6449 | |
| 6450 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6451 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 6452 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6453 | {{50, 50}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6454 | slipperyExitWindow->consumeMotionDown(); |
| 6455 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6456 | mDispatcher->onWindowInfosChanged( |
| 6457 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6458 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6459 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
| 6460 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6461 | {{51, 51}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6462 | |
| 6463 | slipperyExitWindow->consumeMotionCancel(); |
| 6464 | |
| 6465 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 6466 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 6467 | } |
| 6468 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 6469 | /** |
| 6470 | * Two windows, one on the left and another on the right. The left window is slippery. The right |
| 6471 | * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the |
| 6472 | * touch moves from the left window into the right window, the gesture should continue to go to the |
| 6473 | * left window. Touch shouldn't slip because the right window can't receive touches. This test |
| 6474 | * reproduces a crash. |
| 6475 | */ |
| 6476 | TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { |
| 6477 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6478 | |
| 6479 | sp<FakeWindowHandle> leftSlipperyWindow = |
| 6480 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 6481 | leftSlipperyWindow->setSlippery(true); |
| 6482 | leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6483 | |
| 6484 | sp<FakeWindowHandle> rightDropTouchesWindow = |
| 6485 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 6486 | rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100)); |
| 6487 | rightDropTouchesWindow->setDropInput(true); |
| 6488 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6489 | mDispatcher->onWindowInfosChanged( |
| 6490 | {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 6491 | |
| 6492 | // Start touch in the left window |
| 6493 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6494 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6495 | .build()); |
| 6496 | leftSlipperyWindow->consumeMotionDown(); |
| 6497 | |
| 6498 | // And move it into the right window |
| 6499 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6500 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 6501 | .build()); |
| 6502 | |
| 6503 | // Since the right window isn't eligible to receive input, touch does not slip. |
| 6504 | // The left window continues to receive the gesture. |
| 6505 | leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 6506 | rightDropTouchesWindow->assertNoEvents(); |
| 6507 | } |
| 6508 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 6509 | /** |
| 6510 | * A single window is on screen first. Touch is injected into that window. Next, a second window |
| 6511 | * appears. Since the first window is slippery, touch will move from the first window to the second. |
| 6512 | */ |
| 6513 | TEST_F(InputDispatcherTest, InjectedTouchSlips) { |
| 6514 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6515 | sp<FakeWindowHandle> originalWindow = |
| 6516 | sp<FakeWindowHandle>::make(application, mDispatcher, "Original", ADISPLAY_ID_DEFAULT); |
| 6517 | originalWindow->setFrame(Rect(0, 0, 200, 200)); |
| 6518 | originalWindow->setSlippery(true); |
| 6519 | |
| 6520 | sp<FakeWindowHandle> appearingWindow = |
| 6521 | sp<FakeWindowHandle>::make(application, mDispatcher, "Appearing", ADISPLAY_ID_DEFAULT); |
| 6522 | appearingWindow->setFrame(Rect(0, 0, 200, 200)); |
| 6523 | |
| 6524 | mDispatcher->onWindowInfosChanged({{*originalWindow->getInfo()}, {}, 0, 0}); |
| 6525 | |
| 6526 | // Touch down on the original window |
| 6527 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6528 | injectMotionEvent(*mDispatcher, |
| 6529 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6530 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 6531 | .build())); |
| 6532 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 6533 | |
| 6534 | // Now, a new window appears. This could be, for example, a notification shade that appears |
| 6535 | // after user starts to drag down on the launcher window. |
| 6536 | mDispatcher->onWindowInfosChanged( |
| 6537 | {{*appearingWindow->getInfo(), *originalWindow->getInfo()}, {}, 0, 0}); |
| 6538 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6539 | injectMotionEvent(*mDispatcher, |
| 6540 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6541 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(110)) |
| 6542 | .build())); |
| 6543 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 6544 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 6545 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6546 | injectMotionEvent(*mDispatcher, |
| 6547 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6548 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 6549 | .build())); |
| 6550 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 6551 | |
| 6552 | originalWindow->assertNoEvents(); |
| 6553 | appearingWindow->assertNoEvents(); |
| 6554 | } |
| 6555 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6556 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6557 | using Uid = gui::Uid; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6558 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6559 | |
| 6560 | sp<FakeWindowHandle> leftWindow = |
| 6561 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 6562 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6563 | leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6564 | |
| 6565 | sp<FakeWindowHandle> rightSpy = |
| 6566 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT); |
| 6567 | rightSpy->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6568 | rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6569 | rightSpy->setSpy(true); |
| 6570 | rightSpy->setTrustedOverlay(true); |
| 6571 | |
| 6572 | sp<FakeWindowHandle> rightWindow = |
| 6573 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 6574 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6575 | rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6576 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6577 | mDispatcher->onWindowInfosChanged( |
| 6578 | {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6579 | |
| 6580 | // Touch in the left window |
| 6581 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6582 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6583 | .build()); |
| 6584 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown()); |
| 6585 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6586 | ASSERT_NO_FATAL_FAILURE( |
| 6587 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6588 | |
| 6589 | // Touch another finger over the right windows |
| 6590 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6591 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6592 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6593 | .build()); |
| 6594 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown()); |
| 6595 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown()); |
| 6596 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove()); |
| 6597 | mDispatcher->waitForIdle(); |
| 6598 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6599 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, |
| 6600 | {Uid{101}, Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6601 | |
| 6602 | // Release finger over left window. The UP actions are not treated as device interaction. |
| 6603 | // The windows that did not receive the UP pointer will receive MOVE events, but since this |
| 6604 | // is part of the UP action, we do not treat this as device interaction. |
| 6605 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6606 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6607 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6608 | .build()); |
| 6609 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp()); |
| 6610 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 6611 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 6612 | mDispatcher->waitForIdle(); |
| 6613 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 6614 | |
| 6615 | // Move remaining finger |
| 6616 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6617 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6618 | .build()); |
| 6619 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 6620 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 6621 | mDispatcher->waitForIdle(); |
| 6622 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6623 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6624 | |
| 6625 | // Release all fingers |
| 6626 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6627 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6628 | .build()); |
| 6629 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp()); |
| 6630 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp()); |
| 6631 | mDispatcher->waitForIdle(); |
| 6632 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 6633 | } |
| 6634 | |
| 6635 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) { |
| 6636 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6637 | |
| 6638 | sp<FakeWindowHandle> window = |
| 6639 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 6640 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6641 | window->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6642 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6643 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6644 | setFocusedWindow(window); |
| 6645 | ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true)); |
| 6646 | |
| 6647 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build()); |
| 6648 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT)); |
| 6649 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6650 | ASSERT_NO_FATAL_FAILURE( |
| 6651 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6652 | |
| 6653 | // The UP actions are not treated as device interaction. |
| 6654 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build()); |
| 6655 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT)); |
| 6656 | mDispatcher->waitForIdle(); |
| 6657 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 6658 | } |
| 6659 | |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 6660 | TEST_F(InputDispatcherTest, HoverEnterExitSynthesisUsesNewEventId) { |
| 6661 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6662 | |
| 6663 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 6664 | ADISPLAY_ID_DEFAULT); |
| 6665 | left->setFrame(Rect(0, 0, 100, 100)); |
| 6666 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6667 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 6668 | right->setFrame(Rect(100, 0, 200, 100)); |
| 6669 | sp<FakeWindowHandle> spy = |
| 6670 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT); |
| 6671 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 6672 | spy->setTrustedOverlay(true); |
| 6673 | spy->setSpy(true); |
| 6674 | |
| 6675 | mDispatcher->onWindowInfosChanged( |
| 6676 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 6677 | |
| 6678 | // Send hover move to the left window, and ensure hover enter is synthesized with a new eventId. |
| 6679 | NotifyMotionArgs notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 6680 | ADISPLAY_ID_DEFAULT, {PointF{50, 50}}); |
| 6681 | mDispatcher->notifyMotion(notifyArgs); |
| 6682 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6683 | std::unique_ptr<MotionEvent> leftEnter = left->consumeMotionEvent( |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 6684 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), Not(WithEventId(notifyArgs.id)), |
| 6685 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6686 | ASSERT_NE(nullptr, leftEnter); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 6687 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 6688 | Not(WithEventId(notifyArgs.id)), |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6689 | Not(WithEventId(leftEnter->getId())), |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 6690 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 6691 | |
| 6692 | // Send move to the right window, and ensure hover exit and enter are synthesized with new ids. |
| 6693 | notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT, |
| 6694 | {PointF{150, 50}}); |
| 6695 | mDispatcher->notifyMotion(notifyArgs); |
| 6696 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6697 | std::unique_ptr<MotionEvent> leftExit = left->consumeMotionEvent( |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 6698 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), Not(WithEventId(notifyArgs.id)), |
| 6699 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6700 | ASSERT_NE(nullptr, leftExit); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 6701 | right->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 6702 | Not(WithEventId(notifyArgs.id)), |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6703 | Not(WithEventId(leftExit->getId())), |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 6704 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 6705 | |
| 6706 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithEventId(notifyArgs.id))); |
| 6707 | } |
| 6708 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 6709 | class InputDispatcherFallbackKeyTest : public InputDispatcherTest { |
| 6710 | protected: |
| 6711 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 6712 | sp<FakeWindowHandle> mWindow; |
| 6713 | |
| 6714 | virtual void SetUp() override { |
| 6715 | InputDispatcherTest::SetUp(); |
| 6716 | |
| 6717 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 6718 | |
| 6719 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 6720 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6721 | |
| 6722 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 6723 | setFocusedWindow(mWindow); |
| 6724 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeFocusEvent(/*hasFocus=*/true)); |
| 6725 | } |
| 6726 | |
| 6727 | void setFallback(int32_t keycode) { |
| 6728 | mFakePolicy->setUnhandledKeyHandler([keycode](const KeyEvent& event) { |
| 6729 | return KeyEventBuilder(event).keyCode(keycode).build(); |
| 6730 | }); |
| 6731 | } |
| 6732 | |
| 6733 | void consumeKey(bool handled, const ::testing::Matcher<KeyEvent>& matcher) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6734 | std::unique_ptr<KeyEvent> event = mWindow->consumeKey(handled); |
| 6735 | ASSERT_NE(nullptr, event); |
| 6736 | ASSERT_THAT(*event, matcher); |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 6737 | } |
| 6738 | }; |
| 6739 | |
| 6740 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotNotifiedForHandledKey) { |
| 6741 | mDispatcher->notifyKey( |
| 6742 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6743 | consumeKey(/*handled=*/true, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 6744 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6745 | } |
| 6746 | |
| 6747 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotifiedForUnhandledKey) { |
| 6748 | mDispatcher->notifyKey( |
| 6749 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6750 | consumeKey(/*handled=*/false, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 6751 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6752 | } |
| 6753 | |
| 6754 | TEST_F(InputDispatcherFallbackKeyTest, NoFallbackRequestedByPolicy) { |
| 6755 | mDispatcher->notifyKey( |
| 6756 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6757 | |
| 6758 | // Do not handle this key event. |
| 6759 | consumeKey(/*handled=*/false, |
| 6760 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6761 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6762 | |
| 6763 | // Since the policy did not request any fallback to be generated, ensure there are no events. |
| 6764 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6765 | } |
| 6766 | |
| 6767 | TEST_F(InputDispatcherFallbackKeyTest, FallbackDispatchForUnhandledKey) { |
| 6768 | setFallback(AKEYCODE_B); |
| 6769 | mDispatcher->notifyKey( |
| 6770 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6771 | |
| 6772 | // Do not handle this key event. |
| 6773 | consumeKey(/*handled=*/false, |
| 6774 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6775 | |
| 6776 | // Since the key was not handled, ensure the fallback event was dispatched instead. |
| 6777 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6778 | consumeKey(/*handled=*/true, |
| 6779 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 6780 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6781 | |
| 6782 | // Release the original key, and ensure the fallback key is also released. |
| 6783 | mDispatcher->notifyKey( |
| 6784 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6785 | consumeKey(/*handled=*/false, |
| 6786 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6787 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6788 | consumeKey(/*handled=*/true, |
| 6789 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 6790 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6791 | |
| 6792 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6793 | mWindow->assertNoEvents(); |
| 6794 | } |
| 6795 | |
| 6796 | TEST_F(InputDispatcherFallbackKeyTest, AppHandlesPreviouslyUnhandledKey) { |
| 6797 | setFallback(AKEYCODE_B); |
| 6798 | mDispatcher->notifyKey( |
| 6799 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6800 | |
| 6801 | // Do not handle this key event, but handle the fallback. |
| 6802 | consumeKey(/*handled=*/false, |
| 6803 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6804 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6805 | consumeKey(/*handled=*/true, |
| 6806 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 6807 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6808 | |
| 6809 | // Release the original key, and ensure the fallback key is also released. |
| 6810 | mDispatcher->notifyKey( |
| 6811 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6812 | // But this time, the app handles the original key. |
| 6813 | consumeKey(/*handled=*/true, |
| 6814 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6815 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6816 | // Ensure the fallback key is canceled. |
| 6817 | consumeKey(/*handled=*/true, |
| 6818 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 6819 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 6820 | |
| 6821 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6822 | mWindow->assertNoEvents(); |
| 6823 | } |
| 6824 | |
| 6825 | TEST_F(InputDispatcherFallbackKeyTest, AppDoesNotHandleFallback) { |
| 6826 | setFallback(AKEYCODE_B); |
| 6827 | mDispatcher->notifyKey( |
| 6828 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6829 | |
| 6830 | // Do not handle this key event. |
| 6831 | consumeKey(/*handled=*/false, |
| 6832 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6833 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6834 | // App does not handle the fallback either, so ensure another fallback is not generated. |
| 6835 | setFallback(AKEYCODE_C); |
| 6836 | consumeKey(/*handled=*/false, |
| 6837 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 6838 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6839 | |
| 6840 | // Release the original key, and ensure the fallback key is also released. |
| 6841 | setFallback(AKEYCODE_B); |
| 6842 | mDispatcher->notifyKey( |
| 6843 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6844 | consumeKey(/*handled=*/false, |
| 6845 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6846 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6847 | consumeKey(/*handled=*/false, |
| 6848 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 6849 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6850 | |
| 6851 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6852 | mWindow->assertNoEvents(); |
| 6853 | } |
| 6854 | |
| 6855 | TEST_F(InputDispatcherFallbackKeyTest, InconsistentPolicyCancelsFallback) { |
| 6856 | setFallback(AKEYCODE_B); |
| 6857 | mDispatcher->notifyKey( |
| 6858 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6859 | |
| 6860 | // Do not handle this key event, so fallback is generated. |
| 6861 | consumeKey(/*handled=*/false, |
| 6862 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6863 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6864 | consumeKey(/*handled=*/true, |
| 6865 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 6866 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6867 | |
| 6868 | // Release the original key, but assume the policy is misbehaving and it |
| 6869 | // generates an inconsistent fallback to the one from the DOWN event. |
| 6870 | setFallback(AKEYCODE_C); |
| 6871 | mDispatcher->notifyKey( |
| 6872 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6873 | consumeKey(/*handled=*/false, |
| 6874 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6875 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6876 | // Ensure the fallback key reported before as DOWN is canceled due to the inconsistency. |
| 6877 | consumeKey(/*handled=*/true, |
| 6878 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 6879 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 6880 | |
| 6881 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6882 | mWindow->assertNoEvents(); |
| 6883 | } |
| 6884 | |
| 6885 | TEST_F(InputDispatcherFallbackKeyTest, CanceledKeyCancelsFallback) { |
| 6886 | setFallback(AKEYCODE_B); |
| 6887 | mDispatcher->notifyKey( |
| 6888 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6889 | |
| 6890 | // Do not handle this key event, so fallback is generated. |
| 6891 | consumeKey(/*handled=*/false, |
| 6892 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6893 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6894 | consumeKey(/*handled=*/true, |
| 6895 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 6896 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6897 | |
| 6898 | // The original key is canceled. |
| 6899 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
| 6900 | .keyCode(AKEYCODE_A) |
| 6901 | .addFlag(AKEY_EVENT_FLAG_CANCELED) |
| 6902 | .build()); |
| 6903 | consumeKey(/*handled=*/false, |
| 6904 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), |
| 6905 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 6906 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6907 | // Ensure the fallback key is also canceled due to the original key being canceled. |
| 6908 | consumeKey(/*handled=*/true, |
| 6909 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 6910 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 6911 | |
| 6912 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 6913 | mWindow->assertNoEvents(); |
| 6914 | } |
| 6915 | |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6916 | TEST_F(InputDispatcherFallbackKeyTest, InputChannelRemovedDuringPolicyCall) { |
Prabir Pradhan | b13da8f | 2024-01-09 23:10:13 +0000 | [diff] [blame] | 6917 | setFallback(AKEYCODE_B); |
| 6918 | mDispatcher->notifyKey( |
| 6919 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6920 | |
| 6921 | // Do not handle this key event. |
| 6922 | consumeKey(/*handled=*/false, |
| 6923 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6924 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6925 | consumeKey(/*handled=*/true, |
| 6926 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 6927 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6928 | |
| 6929 | mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) { |
| 6930 | // When the unhandled key is reported to the policy next, remove the input channel. |
| 6931 | mDispatcher->removeInputChannel(mWindow->getToken()); |
| 6932 | return KeyEventBuilder(event).keyCode(AKEYCODE_B).build(); |
| 6933 | }); |
| 6934 | // Release the original key, and let the app now handle the previously unhandled key. |
| 6935 | // This should result in the previously generated fallback key to be cancelled. |
| 6936 | // Since the policy was notified of the unhandled DOWN event earlier, it will also be notified |
| 6937 | // of the UP event for consistency. The Dispatcher calls into the policy from its own thread |
| 6938 | // without holding the lock, because it need to synchronously fetch the fallback key. While in |
| 6939 | // the policy call, we will now remove the input channel. Once the policy call returns, the |
| 6940 | // Dispatcher will no longer have a channel to send cancellation events to. Ensure this does |
| 6941 | // not cause any crashes. |
| 6942 | mDispatcher->notifyKey( |
| 6943 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6944 | consumeKey(/*handled=*/true, |
| 6945 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6946 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6947 | } |
| 6948 | |
Prabir Pradhan | fa2c69f | 2024-02-01 20:31:34 +0000 | [diff] [blame] | 6949 | TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedDuringPolicyCall) { |
| 6950 | setFallback(AKEYCODE_B); |
| 6951 | mDispatcher->notifyKey( |
| 6952 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6953 | |
| 6954 | // Do not handle this key event. |
| 6955 | consumeKey(/*handled=*/false, |
| 6956 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6957 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6958 | consumeKey(/*handled=*/true, |
| 6959 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 6960 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6961 | |
| 6962 | mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) { |
| 6963 | // When the unhandled key is reported to the policy next, remove the window. |
| 6964 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 6965 | return KeyEventBuilder(event).keyCode(AKEYCODE_B).build(); |
| 6966 | }); |
| 6967 | // Release the original key, which the app will not handle. When this unhandled key is reported |
| 6968 | // to the policy, the window will be removed. |
| 6969 | mDispatcher->notifyKey( |
| 6970 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6971 | consumeKey(/*handled=*/false, |
| 6972 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6973 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6974 | |
| 6975 | // Since the window was removed, it loses focus, and the channel state will be reset. |
| 6976 | consumeKey(/*handled=*/true, |
| 6977 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 6978 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 6979 | mWindow->consumeFocusEvent(false); |
| 6980 | mWindow->assertNoEvents(); |
| 6981 | } |
| 6982 | |
| 6983 | TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedWhileAwaitingFinishedSignal) { |
| 6984 | setFallback(AKEYCODE_B); |
| 6985 | mDispatcher->notifyKey( |
| 6986 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 6987 | |
| 6988 | // Do not handle this key event. |
| 6989 | consumeKey(/*handled=*/false, |
| 6990 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 6991 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 6992 | const auto [seq, event] = mWindow->receiveEvent(); |
| 6993 | ASSERT_TRUE(seq.has_value() && event != nullptr) << "Failed to receive fallback event"; |
| 6994 | ASSERT_EQ(event->getType(), InputEventType::KEY); |
| 6995 | ASSERT_THAT(static_cast<const KeyEvent&>(*event), |
| 6996 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 6997 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 6998 | |
| 6999 | // Remove the window now, which should generate a cancellations and make the window lose focus. |
| 7000 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 7001 | consumeKey(/*handled=*/true, |
| 7002 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), |
| 7003 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 7004 | consumeKey(/*handled=*/true, |
| 7005 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7006 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 7007 | mWindow->consumeFocusEvent(false); |
| 7008 | |
| 7009 | // Finish the event by reporting it as handled. |
| 7010 | mWindow->finishEvent(*seq); |
| 7011 | mWindow->assertNoEvents(); |
| 7012 | } |
| 7013 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7014 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 7015 | protected: |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 7016 | static constexpr std::chrono::nanoseconds KEY_REPEAT_TIMEOUT = 40ms; |
| 7017 | static constexpr std::chrono::nanoseconds KEY_REPEAT_DELAY = 40ms; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7018 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7019 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7020 | sp<FakeWindowHandle> mWindow; |
| 7021 | |
| 7022 | virtual void SetUp() override { |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 7023 | InputDispatcherTest::SetUp(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7024 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 7025 | mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7026 | setUpWindow(); |
| 7027 | } |
| 7028 | |
| 7029 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7030 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7031 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7032 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7033 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7034 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7035 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7036 | mWindow->consumeFocusEvent(true); |
| 7037 | } |
| 7038 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7039 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7040 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7041 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7042 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7043 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7044 | |
| 7045 | // Window should receive key down event. |
| 7046 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7047 | } |
| 7048 | |
| 7049 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 7050 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7051 | mWindow->consumeKeyEvent( |
| 7052 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount))); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7053 | } |
| 7054 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7055 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7056 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7057 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7058 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7059 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7060 | |
| 7061 | // Window should receive key down event. |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7062 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7063 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7064 | } |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 7065 | |
| 7066 | void injectKeyRepeat(int32_t repeatCount) { |
| 7067 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7068 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, repeatCount, ADISPLAY_ID_DEFAULT)) |
| 7069 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 7070 | } |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7071 | }; |
| 7072 | |
| 7073 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7074 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7075 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 7076 | expectKeyRepeatOnce(repeatCount); |
| 7077 | } |
| 7078 | } |
| 7079 | |
| 7080 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7081 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7082 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 7083 | expectKeyRepeatOnce(repeatCount); |
| 7084 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7085 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7086 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7087 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 7088 | expectKeyRepeatOnce(repeatCount); |
| 7089 | } |
| 7090 | } |
| 7091 | |
| 7092 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7093 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 7094 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 7095 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7096 | mWindow->assertNoEvents(); |
| 7097 | } |
| 7098 | |
| 7099 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7100 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 7101 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 7102 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 7103 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7104 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7105 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7106 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7107 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 7108 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7109 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7110 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7111 | mWindow->assertNoEvents(); |
| 7112 | } |
| 7113 | |
| 7114 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7115 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 7116 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 7117 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 7118 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7119 | // 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] | 7120 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7121 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7122 | mWindow->assertNoEvents(); |
| 7123 | } |
| 7124 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 7125 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 7126 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7127 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7128 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 7129 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 7130 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 7131 | mWindow->assertNoEvents(); |
| 7132 | } |
| 7133 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7134 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 7135 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7136 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7137 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7138 | std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey(); |
| 7139 | ASSERT_NE(nullptr, repeatEvent); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7140 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7141 | IdGenerator::getSource(repeatEvent->getId())); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7142 | } |
| 7143 | } |
| 7144 | |
| 7145 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 7146 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7147 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7148 | |
| 7149 | std::unordered_set<int32_t> idSet; |
| 7150 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7151 | std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey(); |
| 7152 | ASSERT_NE(nullptr, repeatEvent); |
| 7153 | int32_t id = repeatEvent->getId(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7154 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 7155 | idSet.insert(id); |
| 7156 | } |
| 7157 | } |
| 7158 | |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 7159 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_CorrectRepeatCountWhenInjectKeyRepeat) { |
| 7160 | injectKeyRepeat(0); |
| 7161 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7162 | for (int32_t repeatCount = 1; repeatCount <= 2; ++repeatCount) { |
| 7163 | expectKeyRepeatOnce(repeatCount); |
| 7164 | } |
| 7165 | injectKeyRepeat(1); |
| 7166 | // Expect repeatCount to be 3 instead of 1 |
| 7167 | expectKeyRepeatOnce(3); |
| 7168 | } |
| 7169 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7170 | /* Test InputDispatcher for MultiDisplay */ |
| 7171 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 7172 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7173 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7174 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7175 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7176 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7177 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7178 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7179 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7180 | // Set focus window for primary display, but focused display would be second one. |
| 7181 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7182 | windowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7183 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
| 7184 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7185 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7186 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7187 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7188 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7189 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7190 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7191 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7192 | // Set focus display to second one. |
| 7193 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 7194 | // Set focus window for second display. |
| 7195 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7196 | windowInSecondary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7197 | mDispatcher->onWindowInfosChanged( |
| 7198 | {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7199 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7200 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7201 | } |
| 7202 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7203 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7204 | InputDispatcherTest::TearDown(); |
| 7205 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7206 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7207 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7208 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7209 | windowInSecondary.clear(); |
| 7210 | } |
| 7211 | |
| 7212 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7213 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7214 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7215 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7216 | sp<FakeWindowHandle> windowInSecondary; |
| 7217 | }; |
| 7218 | |
| 7219 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 7220 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7221 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7222 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7223 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7224 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7225 | windowInSecondary->assertNoEvents(); |
| 7226 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7227 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7228 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7229 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7230 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7231 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7232 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7233 | } |
| 7234 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7235 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 7236 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 7237 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7238 | injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7239 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7240 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 7241 | windowInSecondary->assertNoEvents(); |
| 7242 | |
| 7243 | // Test inject a key down without display id specified. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7244 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7245 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7246 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7247 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7248 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7249 | // Remove all windows in secondary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7250 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7251 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7252 | // Old focus should receive a cancel event. |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7253 | windowInSecondary->consumeKeyUp(ADISPLAY_ID_NONE, AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7254 | |
| 7255 | // Test inject a key down, should timeout because of no target window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7256 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7257 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7258 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7259 | windowInSecondary->assertNoEvents(); |
| 7260 | } |
| 7261 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7262 | // Test per-display input monitors for motion event. |
| 7263 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7264 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7265 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7266 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7267 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7268 | |
| 7269 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7270 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7271 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7272 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7273 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7274 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7275 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7276 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7277 | |
| 7278 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7279 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7280 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7281 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7282 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7283 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7284 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7285 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7286 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 7287 | // Lift up the touch from the second display |
| 7288 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7289 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 7290 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7291 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 7292 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 7293 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7294 | // Test inject a non-pointer motion event. |
| 7295 | // If specific a display, it will dispatch to the focused window of particular display, |
| 7296 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7297 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7298 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7299 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7300 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7301 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7302 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7303 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7304 | } |
| 7305 | |
| 7306 | // Test per-display input monitors for key event. |
| 7307 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7308 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7309 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7310 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7311 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7312 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7313 | |
| 7314 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7315 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7316 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7317 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7318 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7319 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7320 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7321 | } |
| 7322 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7323 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 7324 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7325 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7326 | secondWindowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7327 | mDispatcher->onWindowInfosChanged( |
| 7328 | {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(), |
| 7329 | *windowInSecondary->getInfo()}, |
| 7330 | {}, |
| 7331 | 0, |
| 7332 | 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7333 | setFocusedWindow(secondWindowInPrimary); |
| 7334 | windowInPrimary->consumeFocusEvent(false); |
| 7335 | secondWindowInPrimary->consumeFocusEvent(true); |
| 7336 | |
| 7337 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7338 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7339 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7340 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7341 | windowInPrimary->assertNoEvents(); |
| 7342 | windowInSecondary->assertNoEvents(); |
| 7343 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7344 | } |
| 7345 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7346 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 7347 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7348 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7349 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7350 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7351 | |
| 7352 | // Test touch down on primary display. |
| 7353 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7354 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7355 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7356 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7357 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7358 | |
| 7359 | // Test touch down on second display. |
| 7360 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7361 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7362 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7363 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 7364 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 7365 | |
| 7366 | // Trigger cancel touch. |
| 7367 | mDispatcher->cancelCurrentTouch(); |
| 7368 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 7369 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 7370 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 7371 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 7372 | |
| 7373 | // Test inject a move motion event, no window/monitor should receive the event. |
| 7374 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7375 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7376 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 7377 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 7378 | windowInPrimary->assertNoEvents(); |
| 7379 | monitorInPrimary.assertNoEvents(); |
| 7380 | |
| 7381 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7382 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7383 | SECOND_DISPLAY_ID, {110, 200})) |
| 7384 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 7385 | windowInSecondary->assertNoEvents(); |
| 7386 | monitorInSecondary.assertNoEvents(); |
| 7387 | } |
| 7388 | |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 7389 | /** |
| 7390 | * Send a key to the primary display and to the secondary display. |
| 7391 | * Then cause the key on the primary display to be canceled by sending in a stale key. |
| 7392 | * Ensure that the key on the primary display is canceled, and that the key on the secondary display |
| 7393 | * does not get canceled. |
| 7394 | */ |
| 7395 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture) { |
| 7396 | // Send a key down on primary display |
| 7397 | mDispatcher->notifyKey( |
| 7398 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 7399 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7400 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 7401 | .build()); |
| 7402 | windowInPrimary->consumeKeyEvent( |
| 7403 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 7404 | windowInSecondary->assertNoEvents(); |
| 7405 | |
| 7406 | // Send a key down on second display |
| 7407 | mDispatcher->notifyKey( |
| 7408 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 7409 | .displayId(SECOND_DISPLAY_ID) |
| 7410 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 7411 | .build()); |
| 7412 | windowInSecondary->consumeKeyEvent( |
| 7413 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID))); |
| 7414 | windowInPrimary->assertNoEvents(); |
| 7415 | |
| 7416 | // Send a valid key up event on primary display that will be dropped because it is stale |
| 7417 | NotifyKeyArgs staleKeyUp = |
| 7418 | KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
| 7419 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7420 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 7421 | .build(); |
| 7422 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms; |
| 7423 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
| 7424 | std::this_thread::sleep_for(STALE_EVENT_TIMEOUT); |
| 7425 | mDispatcher->notifyKey(staleKeyUp); |
| 7426 | |
| 7427 | // Only the key gesture corresponding to the dropped event should receive the cancel event. |
| 7428 | // Therefore, windowInPrimary should get the cancel event and windowInSecondary should not |
| 7429 | // receive any events. |
| 7430 | windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), |
| 7431 | WithDisplayId(ADISPLAY_ID_DEFAULT), |
| 7432 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 7433 | windowInSecondary->assertNoEvents(); |
| 7434 | } |
| 7435 | |
| 7436 | /** |
| 7437 | * Similar to 'WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture' but for motion events. |
| 7438 | */ |
| 7439 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropMotionEvent_OnlyCancelCorrespondingGesture) { |
| 7440 | // Send touch down on primary display. |
| 7441 | mDispatcher->notifyMotion( |
| 7442 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7443 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 7444 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7445 | .build()); |
| 7446 | windowInPrimary->consumeMotionEvent( |
| 7447 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 7448 | windowInSecondary->assertNoEvents(); |
| 7449 | |
| 7450 | // Send touch down on second display. |
| 7451 | mDispatcher->notifyMotion( |
| 7452 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7453 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 7454 | .displayId(SECOND_DISPLAY_ID) |
| 7455 | .build()); |
| 7456 | windowInPrimary->assertNoEvents(); |
| 7457 | windowInSecondary->consumeMotionEvent( |
| 7458 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID))); |
| 7459 | |
| 7460 | // inject a valid MotionEvent on primary display that will be stale when it arrives. |
| 7461 | NotifyMotionArgs staleMotionUp = |
| 7462 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7463 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7464 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 7465 | .build(); |
| 7466 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms; |
| 7467 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
| 7468 | std::this_thread::sleep_for(STALE_EVENT_TIMEOUT); |
| 7469 | mDispatcher->notifyMotion(staleMotionUp); |
| 7470 | |
| 7471 | // For stale motion events, we let the gesture to complete. This behaviour is different from key |
| 7472 | // events, where we would cancel the current keys instead. |
| 7473 | windowInPrimary->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 7474 | windowInSecondary->assertNoEvents(); |
| 7475 | } |
| 7476 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7477 | class InputFilterTest : public InputDispatcherTest { |
| 7478 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7479 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 7480 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7481 | NotifyMotionArgs motionArgs; |
| 7482 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7483 | motionArgs = |
| 7484 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7485 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7486 | motionArgs = |
| 7487 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7488 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7489 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7490 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 7491 | const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue()); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7492 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7493 | } else { |
| 7494 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 7495 | } |
| 7496 | } |
| 7497 | |
| 7498 | void testNotifyKey(bool expectToBeFiltered) { |
| 7499 | NotifyKeyArgs keyArgs; |
| 7500 | |
| 7501 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7502 | mDispatcher->notifyKey(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7503 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7504 | mDispatcher->notifyKey(keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7505 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7506 | |
| 7507 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 7508 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7509 | } else { |
| 7510 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 7511 | } |
| 7512 | } |
| 7513 | }; |
| 7514 | |
| 7515 | // Test InputFilter for MotionEvent |
| 7516 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 7517 | // Since the InputFilter is disabled by default, check if touch events aren't filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7518 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false); |
| 7519 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7520 | |
| 7521 | // Enable InputFilter |
| 7522 | mDispatcher->setInputFilterEnabled(true); |
| 7523 | // Test touch on both primary and second display, and check if both events are filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7524 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true); |
| 7525 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7526 | |
| 7527 | // Disable InputFilter |
| 7528 | mDispatcher->setInputFilterEnabled(false); |
| 7529 | // Test touch on both primary and second display, and check if both events aren't filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7530 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false); |
| 7531 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7532 | } |
| 7533 | |
| 7534 | // Test InputFilter for KeyEvent |
| 7535 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 7536 | // 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] | 7537 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7538 | |
| 7539 | // Enable InputFilter |
| 7540 | mDispatcher->setInputFilterEnabled(true); |
| 7541 | // Send a key event, and check if it is filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7542 | testNotifyKey(/*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7543 | |
| 7544 | // Disable InputFilter |
| 7545 | mDispatcher->setInputFilterEnabled(false); |
| 7546 | // Send a key event, and check if it isn't filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7547 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7548 | } |
| 7549 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7550 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 7551 | // logical display coordinate space. |
| 7552 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 7553 | ui::Transform firstDisplayTransform; |
| 7554 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 7555 | ui::Transform secondDisplayTransform; |
| 7556 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 7557 | |
| 7558 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 7559 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 7560 | displayInfos[0].transform = firstDisplayTransform; |
| 7561 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 7562 | displayInfos[1].transform = secondDisplayTransform; |
| 7563 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 7564 | mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0}); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7565 | |
| 7566 | // Enable InputFilter |
| 7567 | mDispatcher->setInputFilterEnabled(true); |
| 7568 | |
| 7569 | // Ensure the correct transforms are used for the displays. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7570 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true, firstDisplayTransform); |
| 7571 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7572 | } |
| 7573 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7574 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 7575 | protected: |
| 7576 | virtual void SetUp() override { |
| 7577 | InputDispatcherTest::SetUp(); |
| 7578 | |
| 7579 | /** |
| 7580 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 7581 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 7582 | * on. |
| 7583 | */ |
| 7584 | mDispatcher->setInputFilterEnabled(true); |
| 7585 | |
| 7586 | std::shared_ptr<InputApplicationHandle> application = |
| 7587 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7588 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 7589 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7590 | |
| 7591 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7592 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7593 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7594 | setFocusedWindow(mWindow); |
| 7595 | mWindow->consumeFocusEvent(true); |
| 7596 | } |
| 7597 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7598 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 7599 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7600 | KeyEvent event; |
| 7601 | |
| 7602 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 7603 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 7604 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7605 | KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7606 | const int32_t additionalPolicyFlags = |
| 7607 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 7608 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7609 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 7610 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7611 | policyFlags | additionalPolicyFlags)); |
| 7612 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7613 | mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags))); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7614 | } |
| 7615 | |
| 7616 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 7617 | int32_t flags) { |
| 7618 | MotionEvent event; |
| 7619 | PointerProperties pointerProperties[1]; |
| 7620 | PointerCoords pointerCoords[1]; |
| 7621 | pointerProperties[0].clear(); |
| 7622 | pointerProperties[0].id = 0; |
| 7623 | pointerCoords[0].clear(); |
| 7624 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 7625 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 7626 | |
| 7627 | ui::Transform identityTransform; |
| 7628 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 7629 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 7630 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 7631 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 7632 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 7633 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 7634 | eventTime, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7635 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7636 | |
| 7637 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 7638 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7639 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 7640 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7641 | policyFlags | additionalPolicyFlags)); |
| 7642 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7643 | mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId))); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7644 | } |
| 7645 | |
| 7646 | private: |
| 7647 | sp<FakeWindowHandle> mWindow; |
| 7648 | }; |
| 7649 | |
| 7650 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7651 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 7652 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 7653 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7654 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 7655 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7656 | } |
| 7657 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7658 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7659 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7660 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7661 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 7662 | } |
| 7663 | |
| 7664 | TEST_F(InputFilterInjectionPolicyTest, |
| 7665 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 7666 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7667 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7668 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7669 | } |
| 7670 | |
| 7671 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7672 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 7673 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7674 | } |
| 7675 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 7676 | class InputDispatcherUserActivityPokeTests : public InputDispatcherTest { |
| 7677 | protected: |
| 7678 | virtual void SetUp() override { |
| 7679 | InputDispatcherTest::SetUp(); |
| 7680 | |
| 7681 | std::shared_ptr<FakeApplicationHandle> application = |
| 7682 | std::make_shared<FakeApplicationHandle>(); |
| 7683 | application->setDispatchingTimeout(100ms); |
| 7684 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
| 7685 | ADISPLAY_ID_DEFAULT); |
Yeabkal Wubshit | 222d83d | 2024-01-24 18:00:09 +0000 | [diff] [blame] | 7686 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 7687 | mWindow->setDispatchingTimeout(100ms); |
| 7688 | mWindow->setFocusable(true); |
| 7689 | |
| 7690 | // Set focused application. |
| 7691 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7692 | |
| 7693 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 7694 | setFocusedWindow(mWindow); |
| 7695 | mWindow->consumeFocusEvent(true); |
| 7696 | } |
| 7697 | |
| 7698 | void notifyAndConsumeMotion(int32_t action, uint32_t source, int32_t displayId, |
| 7699 | nsecs_t eventTime) { |
| 7700 | mDispatcher->notifyMotion(MotionArgsBuilder(action, source) |
| 7701 | .displayId(displayId) |
| 7702 | .eventTime(eventTime) |
| 7703 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7704 | .build()); |
| 7705 | mWindow->consumeMotionEvent(WithMotionAction(action)); |
| 7706 | } |
| 7707 | |
| 7708 | private: |
| 7709 | sp<FakeWindowHandle> mWindow; |
| 7710 | }; |
| 7711 | |
| 7712 | TEST_F_WITH_FLAGS( |
| 7713 | InputDispatcherUserActivityPokeTests, MinPokeTimeObserved, |
| 7714 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 7715 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 7716 | mDispatcher->setMinTimeBetweenUserActivityPokes(50ms); |
| 7717 | |
| 7718 | // First event of type TOUCH. Should poke. |
| 7719 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7720 | milliseconds_to_nanoseconds(50)); |
| 7721 | mFakePolicy->assertUserActivityPoked( |
| 7722 | {{milliseconds_to_nanoseconds(50), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 7723 | |
| 7724 | // 80ns > 50ns has passed since previous TOUCH event. Should poke. |
| 7725 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7726 | milliseconds_to_nanoseconds(130)); |
| 7727 | mFakePolicy->assertUserActivityPoked( |
| 7728 | {{milliseconds_to_nanoseconds(130), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 7729 | |
| 7730 | // First event of type OTHER. Should poke (despite being within 50ns of previous TOUCH event). |
| 7731 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT, |
| 7732 | milliseconds_to_nanoseconds(135)); |
| 7733 | mFakePolicy->assertUserActivityPoked( |
| 7734 | {{milliseconds_to_nanoseconds(135), USER_ACTIVITY_EVENT_OTHER, ADISPLAY_ID_DEFAULT}}); |
| 7735 | |
| 7736 | // Within 50ns of previous TOUCH event. Should NOT poke. |
| 7737 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7738 | milliseconds_to_nanoseconds(140)); |
| 7739 | mFakePolicy->assertUserActivityNotPoked(); |
| 7740 | |
| 7741 | // Within 50ns of previous OTHER event. Should NOT poke. |
| 7742 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT, |
| 7743 | milliseconds_to_nanoseconds(150)); |
| 7744 | mFakePolicy->assertUserActivityNotPoked(); |
| 7745 | |
| 7746 | // Within 50ns of previous TOUCH event (which was at time 130). Should NOT poke. |
| 7747 | // Note that STYLUS is mapped to TOUCH user activity, since it's a pointer-type source. |
| 7748 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT, |
| 7749 | milliseconds_to_nanoseconds(160)); |
| 7750 | mFakePolicy->assertUserActivityNotPoked(); |
| 7751 | |
| 7752 | // 65ns > 50ns has passed since previous OTHER event. Should poke. |
| 7753 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT, |
| 7754 | milliseconds_to_nanoseconds(200)); |
| 7755 | mFakePolicy->assertUserActivityPoked( |
| 7756 | {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_OTHER, ADISPLAY_ID_DEFAULT}}); |
| 7757 | |
| 7758 | // 170ns > 50ns has passed since previous TOUCH event. Should poke. |
| 7759 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT, |
| 7760 | milliseconds_to_nanoseconds(300)); |
| 7761 | mFakePolicy->assertUserActivityPoked( |
| 7762 | {{milliseconds_to_nanoseconds(300), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 7763 | |
| 7764 | // Assert that there's no more user activity poke event. |
| 7765 | mFakePolicy->assertUserActivityNotPoked(); |
| 7766 | } |
| 7767 | |
| 7768 | TEST_F_WITH_FLAGS( |
| 7769 | InputDispatcherUserActivityPokeTests, DefaultMinPokeTimeOf100MsUsed, |
| 7770 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 7771 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 7772 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7773 | milliseconds_to_nanoseconds(200)); |
| 7774 | mFakePolicy->assertUserActivityPoked( |
| 7775 | {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 7776 | |
| 7777 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7778 | milliseconds_to_nanoseconds(280)); |
| 7779 | mFakePolicy->assertUserActivityNotPoked(); |
| 7780 | |
| 7781 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7782 | milliseconds_to_nanoseconds(340)); |
| 7783 | mFakePolicy->assertUserActivityPoked( |
| 7784 | {{milliseconds_to_nanoseconds(340), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 7785 | } |
| 7786 | |
| 7787 | TEST_F_WITH_FLAGS( |
| 7788 | InputDispatcherUserActivityPokeTests, ZeroMinPokeTimeDisablesRateLimiting, |
| 7789 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 7790 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 7791 | mDispatcher->setMinTimeBetweenUserActivityPokes(0ms); |
| 7792 | |
| 7793 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20); |
| 7794 | mFakePolicy->assertUserActivityPoked(); |
| 7795 | |
| 7796 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 30); |
| 7797 | mFakePolicy->assertUserActivityPoked(); |
| 7798 | } |
| 7799 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7800 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7801 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7802 | InputDispatcherTest::SetUp(); |
| 7803 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7804 | std::shared_ptr<FakeApplicationHandle> application = |
| 7805 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7806 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7807 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7808 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7809 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7810 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7811 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7812 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7813 | |
| 7814 | // Set focused application. |
| 7815 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7816 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7817 | |
| 7818 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7819 | mDispatcher->onWindowInfosChanged( |
| 7820 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7821 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7822 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7823 | } |
| 7824 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7825 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7826 | InputDispatcherTest::TearDown(); |
| 7827 | |
| 7828 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7829 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7830 | } |
| 7831 | |
| 7832 | protected: |
| 7833 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7834 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 7835 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7836 | }; |
| 7837 | |
| 7838 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 7839 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 7840 | // the onPointerDownOutsideFocus callback. |
| 7841 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7842 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7843 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 7844 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7845 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 7846 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7847 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7848 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7849 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 7850 | } |
| 7851 | |
| 7852 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 7853 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 7854 | // onPointerDownOutsideFocus callback. |
| 7855 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7856 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7857 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, |
| 7858 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7859 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 7860 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7861 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7862 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7863 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7864 | } |
| 7865 | |
| 7866 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 7867 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 7868 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 7869 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7870 | injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7871 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 7872 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7873 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7874 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7875 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7876 | } |
| 7877 | |
| 7878 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 7879 | // DOWN on the window that already has focus. Ensure no window received the |
| 7880 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7881 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7882 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7883 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 7884 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7885 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 7886 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7887 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7888 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7889 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 7890 | } |
| 7891 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 7892 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 7893 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 7894 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 7895 | const MotionEvent event = |
| 7896 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 7897 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 7898 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 7899 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 7900 | .build(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7901 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 7902 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7903 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 7904 | |
| 7905 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 7906 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 7907 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 7908 | mUnfocusedWindow->assertNoEvents(); |
| 7909 | } |
| 7910 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7911 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 7912 | // windows that point to the same client token. |
| 7913 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 7914 | virtual void SetUp() override { |
| 7915 | InputDispatcherTest::SetUp(); |
| 7916 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7917 | std::shared_ptr<FakeApplicationHandle> application = |
| 7918 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7919 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 7920 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7921 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 7922 | |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 7923 | mWindow2 = mWindow1->clone(ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7924 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 7925 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7926 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7927 | } |
| 7928 | |
| 7929 | protected: |
| 7930 | sp<FakeWindowHandle> mWindow1; |
| 7931 | sp<FakeWindowHandle> mWindow2; |
| 7932 | |
| 7933 | // 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] | 7934 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 7935 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 7936 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7937 | } |
| 7938 | |
| 7939 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 7940 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7941 | const std::string name = window->getName(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7942 | std::unique_ptr<MotionEvent> motionEvent = |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7943 | window->consumeMotionEvent(WithMotionAction(expectedAction)); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7944 | ASSERT_NE(nullptr, motionEvent); |
| 7945 | ASSERT_EQ(points.size(), motionEvent->getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7946 | |
| 7947 | for (size_t i = 0; i < points.size(); i++) { |
| 7948 | float expectedX = points[i].x; |
| 7949 | float expectedY = points[i].y; |
| 7950 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7951 | EXPECT_EQ(expectedX, motionEvent->getX(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7952 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7953 | << ", got " << motionEvent->getX(i); |
| 7954 | EXPECT_EQ(expectedY, motionEvent->getY(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7955 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7956 | << ", got " << motionEvent->getY(i); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7957 | } |
| 7958 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7959 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 7960 | void touchAndAssertPositions(sp<FakeWindowHandle> touchedWindow, int32_t action, |
| 7961 | const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7962 | std::vector<PointF> expectedPoints) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7963 | mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 7964 | ADISPLAY_ID_DEFAULT, touchedPoints)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7965 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 7966 | consumeMotionEvent(touchedWindow, action, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7967 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7968 | }; |
| 7969 | |
| 7970 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 7971 | // Touch Window 1 |
| 7972 | PointF touchedPoint = {10, 10}; |
| 7973 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 7974 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7975 | |
| 7976 | // Release touch on Window 1 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 7977 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7978 | |
| 7979 | // Touch Window 2 |
| 7980 | touchedPoint = {150, 150}; |
| 7981 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 7982 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7983 | } |
| 7984 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 7985 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 7986 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7987 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7988 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7989 | |
| 7990 | // Touch Window 1 |
| 7991 | PointF touchedPoint = {10, 10}; |
| 7992 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 7993 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7994 | // Release touch on Window 1 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 7995 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 7996 | |
| 7997 | // Touch Window 2 |
| 7998 | touchedPoint = {150, 150}; |
| 7999 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8000 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 8001 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8002 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8003 | // Update the transform so rotation is set |
| 8004 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8005 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8006 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8007 | touchAndAssertPositions(mWindow2, AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8008 | } |
| 8009 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8010 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8011 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8012 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8013 | |
| 8014 | // Touch Window 1 |
| 8015 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 8016 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8017 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8018 | |
| 8019 | // Touch Window 2 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8020 | // Since this is part of the same touch gesture that has already been dispatched to Window 1, |
| 8021 | // the touch stream from Window 2 will be merged with the stream in Window 1. The merged stream |
| 8022 | // will continue to be dispatched through Window 1. |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8023 | touchedPoints.push_back(PointF{150, 150}); |
| 8024 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8025 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8026 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8027 | // Release Window 2 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8028 | touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8029 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8030 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8031 | // Update the transform so rotation is set for Window 2 |
| 8032 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8033 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8034 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8035 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8036 | } |
| 8037 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8038 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8039 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8040 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8041 | |
| 8042 | // Touch Window 1 |
| 8043 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 8044 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8045 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8046 | |
| 8047 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8048 | touchedPoints.push_back(PointF{150, 150}); |
| 8049 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8050 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8051 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8052 | |
| 8053 | // Move both windows |
| 8054 | touchedPoints = {{20, 20}, {175, 175}}; |
| 8055 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 8056 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 8057 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8058 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8059 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8060 | // Release Window 2 |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8061 | touchAndAssertPositions(mWindow1, POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8062 | expectedPoints.pop_back(); |
| 8063 | |
| 8064 | // Touch Window 2 |
| 8065 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8066 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8067 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8068 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8069 | |
| 8070 | // Move both windows |
| 8071 | touchedPoints = {{20, 20}, {175, 175}}; |
| 8072 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 8073 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 8074 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8075 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8076 | } |
| 8077 | |
| 8078 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 8079 | mWindow1->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8080 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8081 | |
| 8082 | // Touch Window 1 |
| 8083 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 8084 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8085 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8086 | |
| 8087 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8088 | touchedPoints.push_back(PointF{150, 150}); |
| 8089 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8090 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8091 | touchAndAssertPositions(mWindow1, POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8092 | |
| 8093 | // Move both windows |
| 8094 | touchedPoints = {{20, 20}, {175, 175}}; |
| 8095 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 8096 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 8097 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8098 | touchAndAssertPositions(mWindow1, AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8099 | } |
| 8100 | |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 8101 | /** |
| 8102 | * When one of the windows is slippery, the touch should not slip into the other window with the |
| 8103 | * same input channel. |
| 8104 | */ |
| 8105 | TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) { |
| 8106 | mWindow1->setSlippery(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8107 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 8108 | |
| 8109 | // Touch down in window 1 |
| 8110 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8111 | ADISPLAY_ID_DEFAULT, {{50, 50}})); |
| 8112 | consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}}); |
| 8113 | |
| 8114 | // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip. |
| 8115 | // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN |
| 8116 | // getting generated. |
| 8117 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8118 | ADISPLAY_ID_DEFAULT, {{150, 150}})); |
| 8119 | |
| 8120 | consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}}); |
| 8121 | } |
| 8122 | |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8123 | /** |
| 8124 | * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and |
| 8125 | * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window |
| 8126 | * that the pointer is hovering over may have a different transform. |
| 8127 | */ |
| 8128 | TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) { |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8129 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8130 | |
| 8131 | // Start hover in window 1 |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 8132 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN) |
| 8133 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8134 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8135 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 8136 | {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8137 | // Move hover to window 2. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 8138 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8139 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 8140 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8141 | consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}}); |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 8142 | consumeMotionEvent(mWindow2, ACTION_HOVER_ENTER, |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8143 | {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})}); |
| 8144 | } |
| 8145 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8146 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 8147 | virtual void SetUp() override { |
| 8148 | InputDispatcherTest::SetUp(); |
| 8149 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8150 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8151 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8152 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 8153 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8154 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8155 | mWindow->setDispatchingTimeout(100ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8156 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8157 | |
| 8158 | // Set focused application. |
| 8159 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 8160 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8161 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8162 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8163 | mWindow->consumeFocusEvent(true); |
| 8164 | } |
| 8165 | |
| 8166 | virtual void TearDown() override { |
| 8167 | InputDispatcherTest::TearDown(); |
| 8168 | mWindow.clear(); |
| 8169 | } |
| 8170 | |
| 8171 | protected: |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8172 | static constexpr std::chrono::duration SPY_TIMEOUT = 200ms; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8173 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8174 | sp<FakeWindowHandle> mWindow; |
| 8175 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 8176 | |
| 8177 | void tapOnWindow() { |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 8178 | const auto touchingPointer = PointerBuilder(/*id=*/0, ToolType::FINGER) |
| 8179 | .x(WINDOW_LOCATION.x) |
| 8180 | .y(WINDOW_LOCATION.y); |
| 8181 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8182 | .pointer(touchingPointer) |
| 8183 | .build()); |
| 8184 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8185 | .pointer(touchingPointer) |
| 8186 | .build()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8187 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8188 | |
| 8189 | sp<FakeWindowHandle> addSpyWindow() { |
| 8190 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8191 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8192 | spy->setTrustedOverlay(true); |
| 8193 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8194 | spy->setSpy(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8195 | spy->setDispatchingTimeout(SPY_TIMEOUT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8196 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8197 | return spy; |
| 8198 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8199 | }; |
| 8200 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8201 | // Send a tap and respond, which should not cause an ANR. |
| 8202 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 8203 | tapOnWindow(); |
| 8204 | mWindow->consumeMotionDown(); |
| 8205 | mWindow->consumeMotionUp(); |
| 8206 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8207 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8208 | } |
| 8209 | |
| 8210 | // Send a regular key and respond, which should not cause an ANR. |
| 8211 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8212 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8213 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8214 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8215 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8216 | } |
| 8217 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 8218 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 8219 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8220 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 8221 | mWindow->consumeFocusEvent(false); |
| 8222 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8223 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8224 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 8225 | InputEventInjectionSync::NONE, CONSUME_TIMEOUT_EVENT_EXPECTED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8226 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8227 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 8228 | // Key will not go to window because we have no focused window. |
| 8229 | // The 'no focused window' ANR timer should start instead. |
| 8230 | |
| 8231 | // Now, the focused application goes away. |
| 8232 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 8233 | // The key should get dropped and there should be no ANR. |
| 8234 | |
| 8235 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8236 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8237 | } |
| 8238 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8239 | // 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] | 8240 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 8241 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8242 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8243 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8244 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8245 | WINDOW_LOCATION)); |
| 8246 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8247 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8248 | ASSERT_TRUE(sequenceNum); |
| 8249 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8250 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8251 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8252 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8253 | mWindow->consumeMotionEvent( |
| 8254 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8255 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8256 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8257 | } |
| 8258 | |
| 8259 | // Send a key to the app and have the app not respond right away. |
| 8260 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 8261 | // Inject a key, and don't respond - expect that ANR is called. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8262 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8263 | const auto [sequenceNum, _] = mWindow->receiveEvent(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8264 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8265 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8266 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8267 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8268 | } |
| 8269 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8270 | // We have a focused application, but no focused window |
| 8271 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8272 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8273 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8274 | mWindow->consumeFocusEvent(false); |
| 8275 | |
| 8276 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8277 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8278 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8279 | WINDOW_LOCATION)); |
| 8280 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 8281 | mDispatcher->waitForIdle(); |
| 8282 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8283 | |
| 8284 | // Once a focused event arrives, we get an ANR for this application |
| 8285 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 8286 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8287 | const InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8288 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8289 | InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8290 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8291 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 8292 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8293 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8294 | } |
| 8295 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8296 | /** |
| 8297 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 8298 | * there will not be an ANR. |
| 8299 | */ |
| 8300 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 8301 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8302 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8303 | mWindow->consumeFocusEvent(false); |
| 8304 | |
| 8305 | KeyEvent event; |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 8306 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 8307 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8308 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 8309 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 8310 | |
| 8311 | // Define a valid key down event that is stale (too old). |
| 8312 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8313 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /*flags=*/0, AKEYCODE_A, KEY_A, |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 8314 | AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8315 | |
Hu Guo | fe3c8f1 | 2023-09-22 17:20:15 +0800 | [diff] [blame] | 8316 | const int32_t policyFlags = |
| 8317 | 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] | 8318 | |
| 8319 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8320 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8321 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 8322 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 8323 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 8324 | << "Injection should fail because the event is stale"; |
| 8325 | |
| 8326 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8327 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8328 | mWindow->assertNoEvents(); |
| 8329 | } |
| 8330 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8331 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8332 | // Make sure that we don't notify policy twice about the same ANR. |
| 8333 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 8334 | const std::chrono::duration appTimeout = 400ms; |
| 8335 | mApplication->setDispatchingTimeout(appTimeout); |
| 8336 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 8337 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8338 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8339 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8340 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8341 | |
| 8342 | // Once a focused event arrives, we get an ANR for this application |
| 8343 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 8344 | // injection times out (instead of failing). |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 8345 | const std::chrono::duration eventInjectionTimeout = 100ms; |
| 8346 | ASSERT_LT(eventInjectionTimeout, appTimeout); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8347 | const InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8348 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 8349 | InputEventInjectionSync::WAIT_FOR_RESULT, eventInjectionTimeout, |
| 8350 | /*allowKeyRepeat=*/false); |
| 8351 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result) |
| 8352 | << "result=" << ftl::enum_string(result); |
| 8353 | // We already waited for 'eventInjectionTimeout`, because the countdown started when the event |
| 8354 | // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait. |
| 8355 | std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout; |
| 8356 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8357 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 8358 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8359 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 8360 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8361 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8362 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 8363 | // '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] | 8364 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8365 | } |
| 8366 | |
| 8367 | // We have a focused application, but no focused window |
| 8368 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8369 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8370 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8371 | mWindow->consumeFocusEvent(false); |
| 8372 | |
| 8373 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8374 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8375 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 8376 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 8377 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8378 | |
| 8379 | // Future focused events get dropped right away |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8380 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8381 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8382 | mWindow->assertNoEvents(); |
| 8383 | } |
| 8384 | |
| 8385 | /** |
| 8386 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 8387 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 8388 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 8389 | * the ANR mechanism should still work. |
| 8390 | * |
| 8391 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 8392 | * DOWN event, while not responding on the second one. |
| 8393 | */ |
| 8394 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 8395 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8396 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8397 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 8398 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 8399 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8400 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8401 | |
| 8402 | // Now send ACTION_UP, with identical timestamp |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8403 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8404 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 8405 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 8406 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8407 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8408 | |
| 8409 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 8410 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8411 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8412 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8413 | } |
| 8414 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8415 | // A spy window can receive an ANR |
| 8416 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 8417 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 8418 | |
| 8419 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8420 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8421 | WINDOW_LOCATION)); |
| 8422 | mWindow->consumeMotionDown(); |
| 8423 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8424 | const auto [sequenceNum, _] = spy->receiveEvent(); // ACTION_DOWN |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8425 | ASSERT_TRUE(sequenceNum); |
| 8426 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8427 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8428 | |
| 8429 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8430 | spy->consumeMotionEvent( |
| 8431 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8432 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8433 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8434 | } |
| 8435 | |
| 8436 | // 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] | 8437 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8438 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 8439 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8440 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8441 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8442 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8443 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8444 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8445 | |
| 8446 | // Stuck on the ACTION_UP |
| 8447 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8448 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8449 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8450 | // 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] | 8451 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8452 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8453 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8454 | |
| 8455 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 8456 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8457 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8458 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8459 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8460 | } |
| 8461 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8462 | // 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] | 8463 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8464 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 8465 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8466 | |
| 8467 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8468 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8469 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8470 | |
| 8471 | mWindow->consumeMotionDown(); |
| 8472 | // Stuck on the ACTION_UP |
| 8473 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8474 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8475 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8476 | // 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] | 8477 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8478 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8479 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8480 | |
| 8481 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 8482 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8483 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8484 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8485 | spy->assertNoEvents(); |
| 8486 | } |
| 8487 | |
| 8488 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8489 | mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8490 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 8491 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8492 | |
| 8493 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8494 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8495 | WINDOW_LOCATION)); |
| 8496 | |
| 8497 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8498 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 8499 | ASSERT_TRUE(consumeSeq); |
| 8500 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8501 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(), |
| 8502 | MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8503 | |
| 8504 | monitor.finishEvent(*consumeSeq); |
| 8505 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 8506 | |
| 8507 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8508 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8509 | } |
| 8510 | |
| 8511 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 8512 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 8513 | // get an ANR again. |
| 8514 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 8515 | // 2. consume all pending events (= queue becomes healthy again) |
| 8516 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 8517 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 8518 | tapOnWindow(); |
| 8519 | |
| 8520 | mWindow->consumeMotionDown(); |
| 8521 | // Block on ACTION_UP |
| 8522 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8523 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8524 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 8525 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8526 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8527 | mWindow->assertNoEvents(); |
| 8528 | |
| 8529 | tapOnWindow(); |
| 8530 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8531 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8532 | mWindow->consumeMotionUp(); |
| 8533 | |
| 8534 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8535 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8536 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8537 | mWindow->assertNoEvents(); |
| 8538 | } |
| 8539 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8540 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 8541 | // it. |
| 8542 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8543 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8544 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8545 | WINDOW_LOCATION)); |
| 8546 | |
| 8547 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8548 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8549 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8550 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 8551 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8552 | // 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] | 8553 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8554 | mWindow->consumeMotionEvent( |
| 8555 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8556 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8557 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8558 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8559 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8560 | } |
| 8561 | |
| 8562 | /** |
| 8563 | * 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] | 8564 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8565 | */ |
| 8566 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 8567 | // The timeouts in this test are established by relying on the fact that the "key waiting for |
| 8568 | // events timeout" is equal to 500ms. |
| 8569 | ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8570 | 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] | 8571 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8572 | |
| 8573 | tapOnWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8574 | const auto& [downSequenceNum, downEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8575 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8576 | const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8577 | ASSERT_TRUE(upSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8578 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 8579 | // Don't finish the events yet, and send a key |
| 8580 | mDispatcher->notifyKey( |
| 8581 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 8582 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 8583 | .build()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8584 | // Key will not be sent to the window, yet, because the window is still processing events |
| 8585 | // 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] | 8586 | // 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] | 8587 | mWindow->assertNoEvents(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8588 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 8589 | std::this_thread::sleep_for(400ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8590 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 8591 | // to the focused window, even though we have not yet finished the motion event |
| 8592 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8593 | mWindow->finishEvent(*downSequenceNum); |
| 8594 | mWindow->finishEvent(*upSequenceNum); |
| 8595 | } |
| 8596 | |
| 8597 | /** |
| 8598 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 8599 | * not go to the focused window until the motion is processed. |
| 8600 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 8601 | * focused window right away. |
| 8602 | */ |
| 8603 | TEST_F(InputDispatcherSingleWindowAnr, |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 8604 | PendingKey_IsDeliveredWhileMotionIsProcessingAndNewTouchComesIn) { |
| 8605 | // The timeouts in this test are established by relying on the fact that the "key waiting for |
| 8606 | // events timeout" is equal to 500ms. |
| 8607 | ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8608 | 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] | 8609 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8610 | |
| 8611 | tapOnWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8612 | const auto& [downSequenceNum, _] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8613 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8614 | const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8615 | ASSERT_TRUE(upSequenceNum); |
| 8616 | // Don't finish the events yet, and send a key |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 8617 | mDispatcher->notifyKey( |
| 8618 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 8619 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 8620 | .build()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8621 | // 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] | 8622 | mWindow->assertNoEvents(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8623 | |
| 8624 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 8625 | tapOnWindow(); |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame] | 8626 | // Now that we tapped, we should receive the key immediately. |
| 8627 | // Since there's still room for slowness, we use 200ms, which is much less than |
| 8628 | // the "key waiting for events' timeout of 500ms minus the already waited 100ms duration. |
| 8629 | std::unique_ptr<InputEvent> keyEvent = mWindow->consume(200ms); |
| 8630 | ASSERT_NE(nullptr, keyEvent); |
| 8631 | ASSERT_EQ(InputEventType::KEY, keyEvent->getType()); |
| 8632 | ASSERT_THAT(static_cast<KeyEvent&>(*keyEvent), WithKeyAction(AKEY_EVENT_ACTION_DOWN)); |
| 8633 | // it doesn't matter that we haven't ack'd the other events yet. We can finish events in any |
| 8634 | // order. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8635 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 8636 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 8637 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 8638 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8639 | mWindow->assertNoEvents(); |
| 8640 | } |
| 8641 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 8642 | /** |
| 8643 | * Send an event to the app and have the app not respond right away. |
| 8644 | * When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 8645 | * So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 8646 | * At some point, the window becomes responsive again. |
| 8647 | * Ensure that subsequent events get dropped, and the next gesture is delivered. |
| 8648 | */ |
| 8649 | TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) { |
| 8650 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8651 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 8652 | .build()); |
| 8653 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8654 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 8655 | ASSERT_TRUE(sequenceNum); |
| 8656 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 8657 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 8658 | |
| 8659 | mWindow->finishEvent(*sequenceNum); |
| 8660 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 8661 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8662 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
| 8663 | |
| 8664 | // Now that the window is responsive, let's continue the gesture. |
| 8665 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8666 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8667 | .build()); |
| 8668 | |
| 8669 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8670 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8671 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 8672 | .build()); |
| 8673 | |
| 8674 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8675 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8676 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 8677 | .build()); |
| 8678 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8679 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8680 | .build()); |
| 8681 | // We already canceled this pointer, so the window shouldn't get any new events. |
| 8682 | mWindow->assertNoEvents(); |
| 8683 | |
| 8684 | // Start another one. |
| 8685 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8686 | .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15)) |
| 8687 | .build()); |
| 8688 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 8689 | } |
| 8690 | |
Prabir Pradhan | fc36472 | 2024-02-08 17:51:20 +0000 | [diff] [blame] | 8691 | // Send an event to the app and have the app not respond right away. Then remove the app window. |
| 8692 | // When the window is removed, the dispatcher will cancel the events for that window. |
| 8693 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 8694 | TEST_F(InputDispatcherSingleWindowAnr, AnrAfterWindowRemoval) { |
| 8695 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8696 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8697 | {WINDOW_LOCATION})); |
| 8698 | |
| 8699 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
| 8700 | ASSERT_TRUE(sequenceNum); |
| 8701 | |
| 8702 | // Remove the window, but the input channel should remain alive. |
| 8703 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 8704 | |
| 8705 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 8706 | // Since the window was removed, Dispatcher does not know the PID associated with the window |
| 8707 | // anymore, so the policy is notified without the PID. |
| 8708 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow->getToken(), |
| 8709 | /*pid=*/std::nullopt); |
| 8710 | |
| 8711 | mWindow->finishEvent(*sequenceNum); |
| 8712 | // The cancellation was generated when the window was removed, along with the focus event. |
| 8713 | mWindow->consumeMotionEvent( |
| 8714 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 8715 | mWindow->consumeFocusEvent(false); |
| 8716 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8717 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt); |
| 8718 | } |
| 8719 | |
| 8720 | // Send an event to the app and have the app not respond right away. Wait for the policy to be |
| 8721 | // notified of the unresponsive window, then remove the app window. |
| 8722 | TEST_F(InputDispatcherSingleWindowAnr, AnrFollowedByWindowRemoval) { |
| 8723 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 8724 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8725 | {WINDOW_LOCATION})); |
| 8726 | |
| 8727 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
| 8728 | ASSERT_TRUE(sequenceNum); |
| 8729 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 8730 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 8731 | |
| 8732 | // Remove the window, but the input channel should remain alive. |
| 8733 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
| 8734 | |
| 8735 | mWindow->finishEvent(*sequenceNum); |
| 8736 | // The cancellation was generated during the ANR, and the window lost focus when it was removed. |
| 8737 | mWindow->consumeMotionEvent( |
| 8738 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 8739 | mWindow->consumeFocusEvent(false); |
| 8740 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8741 | // Since the window was removed, Dispatcher does not know the PID associated with the window |
| 8742 | // becoming responsive, so the policy is notified without the PID. |
| 8743 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), /*pid=*/std::nullopt); |
| 8744 | } |
| 8745 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8746 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 8747 | virtual void SetUp() override { |
| 8748 | InputDispatcherTest::SetUp(); |
| 8749 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8750 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8751 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8752 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 8753 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8754 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8755 | // 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] | 8756 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8757 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8758 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 8759 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8760 | mFocusedWindow->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8761 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8762 | |
| 8763 | // Set focused application. |
| 8764 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8765 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8766 | |
| 8767 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8768 | mDispatcher->onWindowInfosChanged( |
| 8769 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8770 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8771 | mFocusedWindow->consumeFocusEvent(true); |
| 8772 | } |
| 8773 | |
| 8774 | virtual void TearDown() override { |
| 8775 | InputDispatcherTest::TearDown(); |
| 8776 | |
| 8777 | mUnfocusedWindow.clear(); |
| 8778 | mFocusedWindow.clear(); |
| 8779 | } |
| 8780 | |
| 8781 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8782 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8783 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 8784 | sp<FakeWindowHandle> mFocusedWindow; |
| 8785 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 8786 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 8787 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 8788 | |
| 8789 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 8790 | |
| 8791 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 8792 | |
| 8793 | private: |
| 8794 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8795 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8796 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8797 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8798 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8799 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8800 | location)); |
| 8801 | } |
| 8802 | }; |
| 8803 | |
| 8804 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 8805 | // should be ANR'd first. |
| 8806 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8807 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 8808 | injectMotionEvent(*mDispatcher, |
| 8809 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8810 | AINPUT_SOURCE_TOUCHSCREEN) |
| 8811 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 8812 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 8813 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 8814 | .build())); |
| 8815 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 8816 | injectMotionEvent(*mDispatcher, |
| 8817 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 8818 | AINPUT_SOURCE_TOUCHSCREEN) |
| 8819 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 8820 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 8821 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 8822 | .build())); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8823 | mFocusedWindow->consumeMotionDown(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 8824 | mFocusedWindow->consumeMotionUp(); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 8825 | mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8826 | // We consumed all events, so no ANR |
| 8827 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8828 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8829 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8830 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 8831 | injectMotionEvent(*mDispatcher, |
| 8832 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 8833 | AINPUT_SOURCE_TOUCHSCREEN) |
| 8834 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 8835 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 8836 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 8837 | .build())); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8838 | const auto [unfocusedSequenceNum, _] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8839 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8840 | |
| 8841 | const std::chrono::duration timeout = |
| 8842 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8843 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 8844 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8845 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8846 | mFocusedWindow->consumeMotionDown(); |
| 8847 | // This cancel is generated because the connection was unresponsive |
| 8848 | mFocusedWindow->consumeMotionCancel(); |
| 8849 | mFocusedWindow->assertNoEvents(); |
| 8850 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8851 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8852 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 8853 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8854 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8855 | } |
| 8856 | |
| 8857 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 8858 | // it doesn't matter which order they should have ANR. |
| 8859 | // But we should receive ANR for both. |
| 8860 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 8861 | // Set the timeout for unfocused window to match the focused window |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8862 | mUnfocusedWindow->setDispatchingTimeout( |
| 8863 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8864 | mDispatcher->onWindowInfosChanged( |
| 8865 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8866 | |
| 8867 | tapOnFocusedWindow(); |
| 8868 | // 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] | 8869 | // 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] | 8870 | std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken( |
| 8871 | mFocusedWindow->getDispatchingTimeout( |
| 8872 | DISPATCHING_TIMEOUT)), |
| 8873 | mFakePolicy->getUnresponsiveWindowToken(0ms)}; |
| 8874 | |
| 8875 | ASSERT_THAT(anrConnectionTokens, |
| 8876 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 8877 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8878 | |
| 8879 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8880 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8881 | |
| 8882 | mFocusedWindow->consumeMotionDown(); |
| 8883 | mFocusedWindow->consumeMotionUp(); |
| 8884 | mUnfocusedWindow->consumeMotionOutside(); |
| 8885 | |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8886 | std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(), |
| 8887 | mFakePolicy->getResponsiveWindowToken()}; |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8888 | |
| 8889 | // Both applications should be marked as responsive, in any order |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8890 | ASSERT_THAT(responsiveTokens, |
| 8891 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 8892 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8893 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8894 | } |
| 8895 | |
| 8896 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 8897 | // We should also log an error to account for the dropped event (not tested here). |
| 8898 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 8899 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 8900 | tapOnFocusedWindow(); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 8901 | mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8902 | // Receive the events, but don't respond |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8903 | const auto [downEventSequenceNum, downEvent] = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8904 | ASSERT_TRUE(downEventSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8905 | const auto [upEventSequenceNum, upEvent] = mFocusedWindow->receiveEvent(); // ACTION_UP |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8906 | ASSERT_TRUE(upEventSequenceNum); |
| 8907 | const std::chrono::duration timeout = |
| 8908 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8909 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8910 | |
| 8911 | // Tap once again |
| 8912 | // 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] | 8913 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8914 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8915 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8916 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8917 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8918 | FOCUSED_WINDOW_LOCATION)); |
| 8919 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 8920 | // valid touch target |
| 8921 | mUnfocusedWindow->assertNoEvents(); |
| 8922 | |
| 8923 | // Consume the first tap |
| 8924 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 8925 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 8926 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8927 | // The second tap did not go to the focused window |
| 8928 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8929 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8930 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 8931 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8932 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8933 | } |
| 8934 | |
| 8935 | // If you tap outside of all windows, there will not be ANR |
| 8936 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8937 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8938 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8939 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 8940 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8941 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8942 | } |
| 8943 | |
| 8944 | // Since the focused window is paused, tapping on it should not produce any events |
| 8945 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 8946 | mFocusedWindow->setPaused(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8947 | mDispatcher->onWindowInfosChanged( |
| 8948 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8949 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8950 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8951 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8952 | FOCUSED_WINDOW_LOCATION)); |
| 8953 | |
| 8954 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 8955 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8956 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 8957 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8958 | |
| 8959 | mFocusedWindow->assertNoEvents(); |
| 8960 | mUnfocusedWindow->assertNoEvents(); |
| 8961 | } |
| 8962 | |
| 8963 | /** |
| 8964 | * 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] | 8965 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8966 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 8967 | * |
| 8968 | * Warning!!! |
| 8969 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 8970 | * and the injection timeout that we specify when injecting the key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8971 | * We must have the injection timeout (100ms) be smaller than |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8972 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 8973 | * |
| 8974 | * If that value changes, this test should also change. |
| 8975 | */ |
| 8976 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 8977 | // Set a long ANR timeout to prevent it from triggering |
| 8978 | mFocusedWindow->setDispatchingTimeout(2s); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8979 | mDispatcher->onWindowInfosChanged( |
| 8980 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8981 | |
| 8982 | tapOnUnfocusedWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8983 | const auto [downSequenceNum, downEvent] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8984 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8985 | const auto [upSequenceNum, upEvent] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8986 | ASSERT_TRUE(upSequenceNum); |
| 8987 | // Don't finish the events yet, and send a key |
| 8988 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 8989 | // window even if motions are still being processed. |
| 8990 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8991 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8992 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 8993 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8994 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8995 | // 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] | 8996 | // and the key remains pending, waiting for the touch events to be processed. |
| 8997 | // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED |
| 8998 | // under the hood. |
| 8999 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 9000 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9001 | |
| 9002 | // 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] | 9003 | mFocusedWindow->setFocusable(false); |
| 9004 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9005 | mDispatcher->onWindowInfosChanged( |
| 9006 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9007 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9008 | |
| 9009 | // Focus events should precede the key events |
| 9010 | mUnfocusedWindow->consumeFocusEvent(true); |
| 9011 | mFocusedWindow->consumeFocusEvent(false); |
| 9012 | |
| 9013 | // Finish the tap events, which should unblock dispatcher |
| 9014 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 9015 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 9016 | |
| 9017 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 9018 | // can finally go to the newly focused "mUnfocusedWindow". |
| 9019 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 9020 | mFocusedWindow->assertNoEvents(); |
| 9021 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9022 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9023 | } |
| 9024 | |
| 9025 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 9026 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 9027 | // The other window should not be affected by that. |
| 9028 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 9029 | // Touch Window 1 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9030 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9031 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9032 | {FOCUSED_WINDOW_LOCATION})); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 9033 | mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9034 | |
| 9035 | // Touch Window 2 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9036 | mDispatcher->notifyMotion( |
| 9037 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9038 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9039 | |
| 9040 | const std::chrono::duration timeout = |
| 9041 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9042 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9043 | |
| 9044 | mUnfocusedWindow->consumeMotionDown(); |
| 9045 | mFocusedWindow->consumeMotionDown(); |
| 9046 | // Focused window may or may not receive ACTION_MOVE |
| 9047 | // But it should definitely receive ACTION_CANCEL due to the ANR |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9048 | const auto [moveOrCancelSequenceNum, event] = mFocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9049 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 9050 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 9051 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 9052 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9053 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 9054 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 9055 | mFocusedWindow->consumeMotionCancel(); |
| 9056 | } else { |
| 9057 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 9058 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9059 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9060 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 9061 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9062 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9063 | mUnfocusedWindow->assertNoEvents(); |
| 9064 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9065 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9066 | } |
| 9067 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9068 | /** |
| 9069 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 9070 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 9071 | * |
| 9072 | * If the user touches another application during this time, the key should be dropped. |
| 9073 | * Next, if a new focused window comes in, without toggling the focused application, |
| 9074 | * then no ANR should occur. |
| 9075 | * |
| 9076 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 9077 | * but in some cases the policy may not update the focused application. |
| 9078 | */ |
| 9079 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 9080 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 9081 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | c033dfb | 2023-10-03 10:45:16 -0700 | [diff] [blame] | 9082 | focusedApplication->setDispatchingTimeout(300ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9083 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 9084 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 9085 | mFocusedWindow->setFocusable(false); |
| 9086 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9087 | mDispatcher->onWindowInfosChanged( |
| 9088 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9089 | mFocusedWindow->consumeFocusEvent(false); |
| 9090 | |
| 9091 | // Send a key. The ANR timer should start because there is no focused window. |
| 9092 | // 'focusedApplication' will get blamed if this timer completes. |
| 9093 | // 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] | 9094 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9095 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 9096 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9097 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9098 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9099 | |
| 9100 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 9101 | // then the injected touches won't cause the focused event to get dropped. |
| 9102 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 9103 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 9104 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 9105 | // For this test, it means that the key would get delivered to the window once it becomes |
| 9106 | // focused. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9107 | std::this_thread::sleep_for(100ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9108 | |
| 9109 | // Touch unfocused window. This should force the pending key to get dropped. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9110 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9111 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9112 | {UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9113 | |
| 9114 | // We do not consume the motion right away, because that would require dispatcher to first |
| 9115 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 9116 | // Set the focused window first. |
| 9117 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9118 | mDispatcher->onWindowInfosChanged( |
| 9119 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9120 | setFocusedWindow(mFocusedWindow); |
| 9121 | mFocusedWindow->consumeFocusEvent(true); |
| 9122 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 9123 | // to another application. This could be a bug / behaviour in the policy. |
| 9124 | |
| 9125 | mUnfocusedWindow->consumeMotionDown(); |
| 9126 | |
| 9127 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9128 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 9129 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 9130 | } |
| 9131 | |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 9132 | /** |
| 9133 | * If we are pruning input queue, we should never drop pointer events. Otherwise, we risk having |
| 9134 | * an inconsistent event stream inside the dispatcher. In this test, we make sure that the |
| 9135 | * dispatcher doesn't prune pointer events incorrectly. |
| 9136 | * |
| 9137 | * This test reproduces a crash in InputDispatcher. |
| 9138 | * To reproduce the crash, we need to simulate the conditions for "pruning input queue" to occur. |
| 9139 | * |
| 9140 | * Keep the currently focused application (mApplication), and have no focused window. |
| 9141 | * We set up two additional windows: |
| 9142 | * 1) The navigation bar window. This simulates the system "NavigationBar", which is used in the |
| 9143 | * 3-button navigation mode. This window injects a BACK button when it's touched. 2) The application |
| 9144 | * window. This window is not focusable, but is touchable. |
| 9145 | * |
| 9146 | * We first touch the navigation bar, which causes it to inject a key. Since there's no focused |
| 9147 | * window, the dispatcher doesn't process this key, and all other events inside dispatcher are now |
| 9148 | * blocked. The dispatcher is waiting for 'mApplication' to add a focused window. |
| 9149 | * |
| 9150 | * Now, we touch "Another window". This window is owned by a different application than |
| 9151 | * 'mApplication'. This causes the dispatcher to stop waiting for 'mApplication' to add a focused |
| 9152 | * window. Now, the "pruning input queue" behaviour should kick in, and the dispatcher should start |
| 9153 | * dropping the events from its queue. Ensure that no crash occurs. |
| 9154 | * |
| 9155 | * In this test, we are setting long timeouts to prevent ANRs and events dropped due to being stale. |
| 9156 | * This does not affect the test running time. |
| 9157 | */ |
| 9158 | TEST_F(InputDispatcherMultiWindowAnr, PruningInputQueueShouldNotDropPointerEvents) { |
| 9159 | std::shared_ptr<FakeApplicationHandle> systemUiApplication = |
| 9160 | std::make_shared<FakeApplicationHandle>(); |
| 9161 | systemUiApplication->setDispatchingTimeout(3000ms); |
| 9162 | mFakePolicy->setStaleEventTimeout(3000ms); |
| 9163 | sp<FakeWindowHandle> navigationBar = |
| 9164 | sp<FakeWindowHandle>::make(systemUiApplication, mDispatcher, "NavigationBar", |
| 9165 | ADISPLAY_ID_DEFAULT); |
| 9166 | navigationBar->setFocusable(false); |
| 9167 | navigationBar->setWatchOutsideTouch(true); |
| 9168 | navigationBar->setFrame(Rect(0, 0, 100, 100)); |
| 9169 | |
| 9170 | mApplication->setDispatchingTimeout(3000ms); |
| 9171 | // 'mApplication' is already focused, but we call it again here to make it explicit. |
| 9172 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 9173 | |
| 9174 | std::shared_ptr<FakeApplicationHandle> anotherApplication = |
| 9175 | std::make_shared<FakeApplicationHandle>(); |
| 9176 | sp<FakeWindowHandle> appWindow = |
| 9177 | sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Another window", |
| 9178 | ADISPLAY_ID_DEFAULT); |
| 9179 | appWindow->setFocusable(false); |
| 9180 | appWindow->setFrame(Rect(100, 100, 200, 200)); |
| 9181 | |
| 9182 | mDispatcher->onWindowInfosChanged( |
| 9183 | {{*navigationBar->getInfo(), *appWindow->getInfo()}, {}, 0, 0}); |
| 9184 | // 'mFocusedWindow' is no longer in the dispatcher window list, and therefore loses focus |
| 9185 | mFocusedWindow->consumeFocusEvent(false); |
| 9186 | |
| 9187 | // Touch down the navigation bar. It consumes the touch and injects a key into the dispatcher |
| 9188 | // in response. |
| 9189 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9190 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 9191 | .build()); |
| 9192 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 9193 | |
| 9194 | // Key will not be sent anywhere because we have no focused window. It will remain pending. |
| 9195 | // Pretend we are injecting KEYCODE_BACK, but it doesn't actually matter what key it is. |
| 9196 | InputEventInjectionResult result = |
| 9197 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 9198 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms, |
| 9199 | /*allowKeyRepeat=*/false); |
| 9200 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
| 9201 | |
| 9202 | // Finish the gesture - lift up finger and inject ACTION_UP key event |
| 9203 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 9204 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 9205 | .build()); |
| 9206 | result = injectKey(*mDispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 9207 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms, |
| 9208 | /*allowKeyRepeat=*/false); |
| 9209 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
| 9210 | // The key that was injected is blocking the dispatcher, so the navigation bar shouldn't be |
| 9211 | // getting any events yet. |
| 9212 | navigationBar->assertNoEvents(); |
| 9213 | |
| 9214 | // Now touch "Another window". This touch is going to a different application than the one we |
| 9215 | // are waiting for (which is 'mApplication'). |
| 9216 | // This should cause the dispatcher to drop the pending focus-dispatched events (like the key |
| 9217 | // trying to be injected) and to continue processing the rest of the events in the original |
| 9218 | // order. |
| 9219 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9220 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 9221 | .build()); |
| 9222 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 9223 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_OUTSIDE)); |
| 9224 | appWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 9225 | |
| 9226 | appWindow->assertNoEvents(); |
| 9227 | navigationBar->assertNoEvents(); |
| 9228 | } |
| 9229 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9230 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 9231 | // that has feature NO_INPUT_CHANNEL. |
| 9232 | // Layout: |
| 9233 | // Top (closest to user) |
| 9234 | // mNoInputWindow (above all windows) |
| 9235 | // mBottomWindow |
| 9236 | // Bottom (furthest from user) |
| 9237 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 9238 | virtual void SetUp() override { |
| 9239 | InputDispatcherTest::SetUp(); |
| 9240 | |
| 9241 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9242 | mNoInputWindow = |
| 9243 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 9244 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 9245 | /*createInputChannel=*/false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9246 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9247 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 9248 | // It's perfectly valid for this window to not have an associated input channel |
| 9249 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9250 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 9251 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9252 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 9253 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9254 | mDispatcher->onWindowInfosChanged( |
| 9255 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9256 | } |
| 9257 | |
| 9258 | protected: |
| 9259 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 9260 | sp<FakeWindowHandle> mNoInputWindow; |
| 9261 | sp<FakeWindowHandle> mBottomWindow; |
| 9262 | }; |
| 9263 | |
| 9264 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 9265 | PointF touchedPoint = {10, 10}; |
| 9266 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9267 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9268 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9269 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9270 | |
| 9271 | mNoInputWindow->assertNoEvents(); |
| 9272 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 9273 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 9274 | // and therefore should prevent mBottomWindow from receiving touches |
| 9275 | mBottomWindow->assertNoEvents(); |
| 9276 | } |
| 9277 | |
| 9278 | /** |
| 9279 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 9280 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 9281 | */ |
| 9282 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 9283 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9284 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 9285 | "Window with input channel and NO_INPUT_CHANNEL", |
| 9286 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9287 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9288 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9289 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9290 | mDispatcher->onWindowInfosChanged( |
| 9291 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9292 | |
| 9293 | PointF touchedPoint = {10, 10}; |
| 9294 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9295 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9296 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9297 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9298 | |
| 9299 | mNoInputWindow->assertNoEvents(); |
| 9300 | mBottomWindow->assertNoEvents(); |
| 9301 | } |
| 9302 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9303 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 9304 | protected: |
| 9305 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 9306 | sp<FakeWindowHandle> mWindow; |
| 9307 | sp<FakeWindowHandle> mMirror; |
| 9308 | |
| 9309 | virtual void SetUp() override { |
| 9310 | InputDispatcherTest::SetUp(); |
| 9311 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9312 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 9313 | mMirror = mWindow->clone(ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9314 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 9315 | mWindow->setFocusable(true); |
| 9316 | mMirror->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9317 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9318 | } |
| 9319 | }; |
| 9320 | |
| 9321 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 9322 | // Request focus on a mirrored window |
| 9323 | setFocusedWindow(mMirror); |
| 9324 | |
| 9325 | // window gets focused |
| 9326 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9327 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9328 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9329 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9330 | } |
| 9331 | |
| 9332 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 9333 | // focusable. |
| 9334 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 9335 | setFocusedWindow(mMirror); |
| 9336 | |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9337 | // window gets focused because it is above the mirror |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9338 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9339 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9340 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9341 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9342 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9343 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9344 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9345 | |
| 9346 | mMirror->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9347 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9348 | |
| 9349 | // window loses focus since one of the windows associated with the token in not focusable |
| 9350 | mWindow->consumeFocusEvent(false); |
| 9351 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9352 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9353 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9354 | mWindow->assertNoEvents(); |
| 9355 | } |
| 9356 | |
| 9357 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 9358 | // invisible. |
| 9359 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 9360 | setFocusedWindow(mMirror); |
| 9361 | |
| 9362 | // window gets focused |
| 9363 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9364 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9365 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9366 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9367 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9368 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9369 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9370 | |
| 9371 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9372 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9373 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9374 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9375 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9376 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9377 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9378 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9379 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9380 | |
| 9381 | mWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9382 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9383 | |
| 9384 | // window loses focus only after all windows associated with the token become invisible. |
| 9385 | mWindow->consumeFocusEvent(false); |
| 9386 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9387 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9388 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9389 | mWindow->assertNoEvents(); |
| 9390 | } |
| 9391 | |
| 9392 | // A focused & mirrored window remains focused until both windows are removed. |
| 9393 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 9394 | setFocusedWindow(mMirror); |
| 9395 | |
| 9396 | // window gets focused |
| 9397 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9398 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9399 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9400 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9401 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9402 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9403 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9404 | |
| 9405 | // single window is removed but the window token remains focused |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9406 | mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9407 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9408 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9409 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9410 | mMirror->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9411 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9412 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | bb3f1c0 | 2024-01-04 20:17:14 +0000 | [diff] [blame] | 9413 | mMirror->consumeKeyUp(ADISPLAY_ID_NONE); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9414 | |
| 9415 | // Both windows are removed |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9416 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9417 | mWindow->consumeFocusEvent(false); |
| 9418 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9419 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9420 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9421 | mWindow->assertNoEvents(); |
| 9422 | } |
| 9423 | |
| 9424 | // Focus request can be pending until one window becomes visible. |
| 9425 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 9426 | // Request focus on an invisible mirror. |
| 9427 | mWindow->setVisible(false); |
| 9428 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9429 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9430 | setFocusedWindow(mMirror); |
| 9431 | |
| 9432 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9433 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9434 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 9435 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9436 | |
| 9437 | mMirror->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9438 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9439 | |
| 9440 | // window gets focused |
| 9441 | mWindow->consumeFocusEvent(true); |
| 9442 | // window gets the pending key event |
| 9443 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 9444 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9445 | |
| 9446 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 9447 | protected: |
| 9448 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 9449 | sp<FakeWindowHandle> mWindow; |
| 9450 | sp<FakeWindowHandle> mSecondWindow; |
| 9451 | |
| 9452 | void SetUp() override { |
| 9453 | InputDispatcherTest::SetUp(); |
| 9454 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9455 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9456 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9457 | mSecondWindow = |
| 9458 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9459 | mSecondWindow->setFocusable(true); |
| 9460 | |
| 9461 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9462 | mDispatcher->onWindowInfosChanged( |
| 9463 | {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9464 | |
| 9465 | setFocusedWindow(mWindow); |
| 9466 | mWindow->consumeFocusEvent(true); |
| 9467 | } |
| 9468 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9469 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9470 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9471 | } |
| 9472 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9473 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 9474 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9475 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 9476 | auto request = mFakePolicy->assertSetPointerCaptureCalled(window, enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9477 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9478 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9479 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9480 | } |
| 9481 | }; |
| 9482 | |
| 9483 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 9484 | // Ensure that capture cannot be obtained for unfocused windows. |
| 9485 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 9486 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 9487 | mSecondWindow->assertNoEvents(); |
| 9488 | |
| 9489 | // Ensure that capture can be enabled from the focus window. |
| 9490 | requestAndVerifyPointerCapture(mWindow, true); |
| 9491 | |
| 9492 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 9493 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 9494 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 9495 | |
| 9496 | // Ensure that capture can be disabled from the window with capture. |
| 9497 | requestAndVerifyPointerCapture(mWindow, false); |
| 9498 | } |
| 9499 | |
| 9500 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9501 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9502 | |
| 9503 | setFocusedWindow(mSecondWindow); |
| 9504 | |
| 9505 | // Ensure that the capture disabled event was sent first. |
| 9506 | mWindow->consumeCaptureEvent(false); |
| 9507 | mWindow->consumeFocusEvent(false); |
| 9508 | mSecondWindow->consumeFocusEvent(true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 9509 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9510 | |
| 9511 | // 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] | 9512 | notifyPointerCaptureChanged({}); |
| 9513 | notifyPointerCaptureChanged(request); |
| 9514 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9515 | mWindow->assertNoEvents(); |
| 9516 | mSecondWindow->assertNoEvents(); |
| 9517 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 9518 | } |
| 9519 | |
| 9520 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9521 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9522 | |
| 9523 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9524 | notifyPointerCaptureChanged({}); |
| 9525 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9526 | |
| 9527 | // Ensure that Pointer Capture is disabled. |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 9528 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9529 | mWindow->consumeCaptureEvent(false); |
| 9530 | mWindow->assertNoEvents(); |
| 9531 | } |
| 9532 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9533 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 9534 | requestAndVerifyPointerCapture(mWindow, true); |
| 9535 | |
| 9536 | // The first window loses focus. |
| 9537 | setFocusedWindow(mSecondWindow); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 9538 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9539 | mWindow->consumeCaptureEvent(false); |
| 9540 | |
| 9541 | // Request Pointer Capture from the second window before the notification from InputReader |
| 9542 | // arrives. |
| 9543 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 9544 | auto request = mFakePolicy->assertSetPointerCaptureCalled(mSecondWindow, true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9545 | |
| 9546 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9547 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9548 | |
| 9549 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9550 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9551 | |
| 9552 | mSecondWindow->consumeFocusEvent(true); |
| 9553 | mSecondWindow->consumeCaptureEvent(true); |
| 9554 | } |
| 9555 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9556 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 9557 | // App repeatedly enables and disables capture. |
| 9558 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 9559 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9560 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 9561 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9562 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 9563 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9564 | |
| 9565 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 9566 | // first request is now stale, this should do nothing. |
| 9567 | notifyPointerCaptureChanged(firstRequest); |
| 9568 | mWindow->assertNoEvents(); |
| 9569 | |
| 9570 | // InputReader notifies that the second request was enabled. |
| 9571 | notifyPointerCaptureChanged(secondRequest); |
| 9572 | mWindow->consumeCaptureEvent(true); |
| 9573 | } |
| 9574 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 9575 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 9576 | requestAndVerifyPointerCapture(mWindow, true); |
| 9577 | |
| 9578 | // App toggles pointer capture off and on. |
| 9579 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 9580 | mFakePolicy->assertSetPointerCaptureCalled(mWindow, false); |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 9581 | |
| 9582 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 9583 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 9584 | |
| 9585 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 9586 | // preceding "disable" request. |
| 9587 | notifyPointerCaptureChanged(enableRequest); |
| 9588 | |
| 9589 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 9590 | // any notifications. |
| 9591 | mWindow->assertNoEvents(); |
| 9592 | } |
| 9593 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 9594 | /** |
| 9595 | * One window. Hover mouse in the window, and then start capture. Make sure that the relative |
| 9596 | * mouse movements don't affect the previous mouse hovering state. |
| 9597 | * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no |
| 9598 | * HOVER_MOVE events). |
| 9599 | */ |
| 9600 | TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) { |
| 9601 | // Mouse hover on the window |
| 9602 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 9603 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 9604 | .build()); |
| 9605 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 9606 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 9607 | .build()); |
| 9608 | |
| 9609 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER))); |
| 9610 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE))); |
| 9611 | |
| 9612 | // Start pointer capture |
| 9613 | requestAndVerifyPointerCapture(mWindow, true); |
| 9614 | |
| 9615 | // Send some relative mouse movements and receive them in the window. |
| 9616 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE) |
| 9617 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11)) |
| 9618 | .build()); |
| 9619 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11), |
| 9620 | WithSource(AINPUT_SOURCE_MOUSE_RELATIVE))); |
| 9621 | |
| 9622 | // Stop pointer capture |
| 9623 | requestAndVerifyPointerCapture(mWindow, false); |
| 9624 | |
| 9625 | // Continue hovering on the window |
| 9626 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 9627 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115)) |
| 9628 | .build()); |
| 9629 | mWindow->consumeMotionEvent( |
| 9630 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 9631 | |
| 9632 | mWindow->assertNoEvents(); |
| 9633 | } |
| 9634 | |
Hiroki Sato | 2504023 | 2024-02-22 17:21:22 +0900 | [diff] [blame] | 9635 | using InputDispatcherPointerCaptureDeathTest = InputDispatcherPointerCaptureTests; |
| 9636 | |
| 9637 | TEST_F(InputDispatcherPointerCaptureDeathTest, |
| 9638 | NotifyPointerCaptureChangedWithWrongTokenAbortsDispatcher) { |
| 9639 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
| 9640 | ScopedSilentDeath _silentDeath; |
| 9641 | |
| 9642 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 9643 | auto request = mFakePolicy->assertSetPointerCaptureCalled(mWindow, true); |
| 9644 | |
| 9645 | // Dispatch a pointer changed event with a wrong token. |
| 9646 | request.window = mSecondWindow->getToken(); |
| 9647 | ASSERT_DEATH( |
| 9648 | { |
| 9649 | notifyPointerCaptureChanged(request); |
| 9650 | mSecondWindow->consumeCaptureEvent(true); |
| 9651 | }, |
| 9652 | "Unexpected requested window for Pointer Capture."); |
| 9653 | } |
| 9654 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9655 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 9656 | protected: |
| 9657 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9658 | |
| 9659 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 9660 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 9661 | |
| 9662 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 9663 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 9664 | |
| 9665 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 9666 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 9667 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 9668 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 9669 | MAXIMUM_OBSCURING_OPACITY); |
| 9670 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9671 | static constexpr gui::Uid TOUCHED_APP_UID{10001}; |
| 9672 | static constexpr gui::Uid APP_B_UID{10002}; |
| 9673 | static constexpr gui::Uid APP_C_UID{10003}; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9674 | |
| 9675 | sp<FakeWindowHandle> mTouchWindow; |
| 9676 | |
| 9677 | virtual void SetUp() override { |
| 9678 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9679 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9680 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 9681 | } |
| 9682 | |
| 9683 | virtual void TearDown() override { |
| 9684 | InputDispatcherTest::TearDown(); |
| 9685 | mTouchWindow.clear(); |
| 9686 | } |
| 9687 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9688 | sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 9689 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9690 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9691 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9692 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9693 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9694 | return window; |
| 9695 | } |
| 9696 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9697 | sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9698 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 9699 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9700 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9701 | // Generate an arbitrary PID based on the UID |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9702 | 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] | 9703 | return window; |
| 9704 | } |
| 9705 | |
| 9706 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9707 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9708 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9709 | points)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9710 | } |
| 9711 | }; |
| 9712 | |
| 9713 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9714 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9715 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9716 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9717 | |
| 9718 | touch(); |
| 9719 | |
| 9720 | mTouchWindow->assertNoEvents(); |
| 9721 | } |
| 9722 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9723 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9724 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 9725 | const sp<FakeWindowHandle>& w = |
| 9726 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9727 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9728 | |
| 9729 | touch(); |
| 9730 | |
| 9731 | mTouchWindow->assertNoEvents(); |
| 9732 | } |
| 9733 | |
| 9734 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9735 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 9736 | const sp<FakeWindowHandle>& w = |
| 9737 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9738 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9739 | |
| 9740 | touch(); |
| 9741 | |
| 9742 | w->assertNoEvents(); |
| 9743 | } |
| 9744 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9745 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9746 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9747 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9748 | |
| 9749 | touch(); |
| 9750 | |
| 9751 | mTouchWindow->consumeAnyMotionDown(); |
| 9752 | } |
| 9753 | |
| 9754 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9755 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9756 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9757 | w->setFrame(Rect(0, 0, 50, 50)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9758 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9759 | |
| 9760 | touch({PointF{100, 100}}); |
| 9761 | |
| 9762 | mTouchWindow->consumeAnyMotionDown(); |
| 9763 | } |
| 9764 | |
| 9765 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9766 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9767 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9768 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9769 | |
| 9770 | touch(); |
| 9771 | |
| 9772 | mTouchWindow->consumeAnyMotionDown(); |
| 9773 | } |
| 9774 | |
| 9775 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 9776 | const sp<FakeWindowHandle>& w = |
| 9777 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9778 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9779 | |
| 9780 | touch(); |
| 9781 | |
| 9782 | mTouchWindow->consumeAnyMotionDown(); |
| 9783 | } |
| 9784 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9785 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 9786 | const sp<FakeWindowHandle>& w = |
| 9787 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9788 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9789 | |
| 9790 | touch(); |
| 9791 | |
| 9792 | w->assertNoEvents(); |
| 9793 | } |
| 9794 | |
| 9795 | /** |
| 9796 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 9797 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 9798 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 9799 | */ |
| 9800 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9801 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 9802 | const sp<FakeWindowHandle>& w = |
| 9803 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9804 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9805 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9806 | |
| 9807 | touch(); |
| 9808 | |
| 9809 | w->consumeMotionOutside(); |
| 9810 | } |
| 9811 | |
| 9812 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 9813 | const sp<FakeWindowHandle>& w = |
| 9814 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9815 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9816 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9817 | |
| 9818 | touch(); |
| 9819 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 9820 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9821 | } |
| 9822 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9823 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9824 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9825 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9826 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9827 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9828 | |
| 9829 | touch(); |
| 9830 | |
| 9831 | mTouchWindow->consumeAnyMotionDown(); |
| 9832 | } |
| 9833 | |
| 9834 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 9835 | const sp<FakeWindowHandle>& w = |
| 9836 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9837 | MAXIMUM_OBSCURING_OPACITY); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9838 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9839 | |
| 9840 | touch(); |
| 9841 | |
| 9842 | mTouchWindow->consumeAnyMotionDown(); |
| 9843 | } |
| 9844 | |
| 9845 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9846 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9847 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9848 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9849 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9850 | |
| 9851 | touch(); |
| 9852 | |
| 9853 | mTouchWindow->assertNoEvents(); |
| 9854 | } |
| 9855 | |
| 9856 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 9857 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 9858 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9859 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 9860 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9861 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9862 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 9863 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9864 | mDispatcher->onWindowInfosChanged( |
| 9865 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9866 | |
| 9867 | touch(); |
| 9868 | |
| 9869 | mTouchWindow->assertNoEvents(); |
| 9870 | } |
| 9871 | |
| 9872 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 9873 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 9874 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9875 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 9876 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9877 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9878 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 9879 | OPACITY_FAR_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9880 | mDispatcher->onWindowInfosChanged( |
| 9881 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9882 | |
| 9883 | touch(); |
| 9884 | |
| 9885 | mTouchWindow->consumeAnyMotionDown(); |
| 9886 | } |
| 9887 | |
| 9888 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9889 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 9890 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9891 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9892 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9893 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9894 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 9895 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9896 | mDispatcher->onWindowInfosChanged( |
| 9897 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9898 | |
| 9899 | touch(); |
| 9900 | |
| 9901 | mTouchWindow->consumeAnyMotionDown(); |
| 9902 | } |
| 9903 | |
| 9904 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 9905 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9906 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9907 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9908 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9909 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 9910 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9911 | mDispatcher->onWindowInfosChanged( |
| 9912 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9913 | |
| 9914 | touch(); |
| 9915 | |
| 9916 | mTouchWindow->assertNoEvents(); |
| 9917 | } |
| 9918 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9919 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9920 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 9921 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9922 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 9923 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9924 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9925 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9926 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9927 | mDispatcher->onWindowInfosChanged( |
| 9928 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9929 | |
| 9930 | touch(); |
| 9931 | |
| 9932 | mTouchWindow->assertNoEvents(); |
| 9933 | } |
| 9934 | |
| 9935 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9936 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 9937 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9938 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 9939 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9940 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9941 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 9942 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9943 | mDispatcher->onWindowInfosChanged( |
| 9944 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9945 | |
| 9946 | touch(); |
| 9947 | |
| 9948 | mTouchWindow->consumeAnyMotionDown(); |
| 9949 | } |
| 9950 | |
| 9951 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 9952 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9953 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 9954 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9955 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9956 | |
| 9957 | touch(); |
| 9958 | |
| 9959 | mTouchWindow->consumeAnyMotionDown(); |
| 9960 | } |
| 9961 | |
| 9962 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 9963 | const sp<FakeWindowHandle>& w = |
| 9964 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9965 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9966 | |
| 9967 | touch(); |
| 9968 | |
| 9969 | mTouchWindow->consumeAnyMotionDown(); |
| 9970 | } |
| 9971 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 9972 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9973 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 9974 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 9975 | const sp<FakeWindowHandle>& w = |
| 9976 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9977 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 9978 | |
| 9979 | touch(); |
| 9980 | |
| 9981 | mTouchWindow->assertNoEvents(); |
| 9982 | } |
| 9983 | |
| 9984 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 9985 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 9986 | const sp<FakeWindowHandle>& w = |
| 9987 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9988 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 9989 | |
| 9990 | touch(); |
| 9991 | |
| 9992 | mTouchWindow->consumeAnyMotionDown(); |
| 9993 | } |
| 9994 | |
| 9995 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9996 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 9997 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 9998 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9999 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10000 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10001 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10002 | |
| 10003 | touch(); |
| 10004 | |
| 10005 | mTouchWindow->consumeAnyMotionDown(); |
| 10006 | } |
| 10007 | |
| 10008 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10009 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 10010 | const sp<FakeWindowHandle>& w1 = |
| 10011 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 10012 | OPACITY_BELOW_THRESHOLD); |
| 10013 | const sp<FakeWindowHandle>& w2 = |
| 10014 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10015 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10016 | mDispatcher->onWindowInfosChanged( |
| 10017 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10018 | |
| 10019 | touch(); |
| 10020 | |
| 10021 | mTouchWindow->assertNoEvents(); |
| 10022 | } |
| 10023 | |
| 10024 | /** |
| 10025 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 10026 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 10027 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 10028 | */ |
| 10029 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10030 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 10031 | const sp<FakeWindowHandle>& wB = |
| 10032 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 10033 | OPACITY_BELOW_THRESHOLD); |
| 10034 | const sp<FakeWindowHandle>& wC = |
| 10035 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 10036 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10037 | mDispatcher->onWindowInfosChanged( |
| 10038 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10039 | |
| 10040 | touch(); |
| 10041 | |
| 10042 | mTouchWindow->assertNoEvents(); |
| 10043 | } |
| 10044 | |
| 10045 | /** |
| 10046 | * This test is testing that a window from a different UID but with same application token doesn't |
| 10047 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 10048 | */ |
| 10049 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10050 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 10051 | const sp<FakeWindowHandle>& w = |
| 10052 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 10053 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10054 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 10055 | |
| 10056 | touch(); |
| 10057 | |
| 10058 | mTouchWindow->consumeAnyMotionDown(); |
| 10059 | } |
| 10060 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10061 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 10062 | protected: |
| 10063 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 10064 | sp<FakeWindowHandle> mWindow; |
| 10065 | sp<FakeWindowHandle> mSecondWindow; |
| 10066 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10067 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10068 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 10069 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10070 | |
| 10071 | void SetUp() override { |
| 10072 | InputDispatcherTest::SetUp(); |
| 10073 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10074 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10075 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10076 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10077 | mSecondWindow = |
| 10078 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10079 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10080 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10081 | mSpyWindow = |
| 10082 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10083 | mSpyWindow->setSpy(true); |
| 10084 | mSpyWindow->setTrustedOverlay(true); |
| 10085 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 10086 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10087 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10088 | mDispatcher->onWindowInfosChanged( |
| 10089 | {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 10090 | {}, |
| 10091 | 0, |
| 10092 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10093 | } |
| 10094 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10095 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 10096 | switch (fromSource) { |
| 10097 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 10098 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10099 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10100 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10101 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10102 | break; |
| 10103 | case AINPUT_SOURCE_STYLUS: |
| 10104 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10105 | injectMotionEvent(*mDispatcher, |
| 10106 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10107 | AINPUT_SOURCE_STYLUS) |
| 10108 | .buttonState( |
| 10109 | AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 10110 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
| 10111 | .x(50) |
| 10112 | .y(50)) |
| 10113 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10114 | break; |
| 10115 | case AINPUT_SOURCE_MOUSE: |
| 10116 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10117 | injectMotionEvent(*mDispatcher, |
| 10118 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10119 | AINPUT_SOURCE_MOUSE) |
| 10120 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 10121 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 10122 | ToolType::MOUSE) |
| 10123 | .x(50) |
| 10124 | .y(50)) |
| 10125 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10126 | break; |
| 10127 | default: |
| 10128 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 10129 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10130 | |
| 10131 | // Window should receive motion event. |
| 10132 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10133 | // Spy window should also receive motion event |
| 10134 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10135 | } |
| 10136 | |
| 10137 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 10138 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 10139 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10140 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10141 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10142 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10143 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10144 | |
| 10145 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10146 | mDragWindow = |
| 10147 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10148 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10149 | mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), |
| 10150 | *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 10151 | {}, |
| 10152 | 0, |
| 10153 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10154 | |
| 10155 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10156 | bool transferred = |
Prabir Pradhan | 367f343 | 2024-02-13 23:05:58 +0000 | [diff] [blame] | 10157 | mDispatcher->transferTouchGesture(mWindow->getToken(), mDragWindow->getToken(), |
| 10158 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10159 | if (transferred) { |
| 10160 | mWindow->consumeMotionCancel(); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10161 | mDragWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10162 | } |
| 10163 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10164 | } |
| 10165 | }; |
| 10166 | |
| 10167 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10168 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10169 | |
| 10170 | // Move on window. |
| 10171 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10172 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10173 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10174 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10175 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10176 | mWindow->consumeDragEvent(false, 50, 50); |
| 10177 | mSecondWindow->assertNoEvents(); |
| 10178 | |
| 10179 | // Move to another window. |
| 10180 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10181 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10182 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10183 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10184 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10185 | mWindow->consumeDragEvent(true, 150, 50); |
| 10186 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10187 | |
| 10188 | // Move back to original window. |
| 10189 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10190 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10191 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10192 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10193 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10194 | mWindow->consumeDragEvent(false, 50, 50); |
| 10195 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 10196 | |
| 10197 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10198 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10199 | {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10200 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10201 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10202 | mWindow->assertNoEvents(); |
| 10203 | mSecondWindow->assertNoEvents(); |
| 10204 | } |
| 10205 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10206 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10207 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10208 | |
| 10209 | // No cancel event after drag start |
| 10210 | mSpyWindow->assertNoEvents(); |
| 10211 | |
| 10212 | const MotionEvent secondFingerDownEvent = |
| 10213 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10214 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10215 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10216 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10217 | .build(); |
| 10218 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10219 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10220 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10221 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10222 | |
| 10223 | // Receives cancel for first pointer after next pointer down |
| 10224 | mSpyWindow->consumeMotionCancel(); |
| 10225 | mSpyWindow->consumeMotionDown(); |
| 10226 | |
| 10227 | mSpyWindow->assertNoEvents(); |
| 10228 | } |
| 10229 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10230 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10231 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10232 | |
| 10233 | // Move on window. |
| 10234 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10235 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10236 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10237 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10238 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10239 | mWindow->consumeDragEvent(false, 50, 50); |
| 10240 | mSecondWindow->assertNoEvents(); |
| 10241 | |
| 10242 | // Move to another window. |
| 10243 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10244 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10245 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10246 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10247 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10248 | mWindow->consumeDragEvent(true, 150, 50); |
| 10249 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10250 | |
| 10251 | // drop to another window. |
| 10252 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10253 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10254 | {150, 50})) |
| 10255 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10256 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10257 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10258 | mWindow->assertNoEvents(); |
| 10259 | mSecondWindow->assertNoEvents(); |
| 10260 | } |
| 10261 | |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10262 | TEST_F(InputDispatcherDragTests, DragAndDropNotCancelledIfSomeOtherPointerIsPilfered) { |
| 10263 | startDrag(); |
| 10264 | |
| 10265 | // No cancel event after drag start |
| 10266 | mSpyWindow->assertNoEvents(); |
| 10267 | |
| 10268 | const MotionEvent secondFingerDownEvent = |
| 10269 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10270 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 10271 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10272 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 10273 | .build(); |
| 10274 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10275 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 10276 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10277 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10278 | |
| 10279 | // Receives cancel for first pointer after next pointer down |
| 10280 | mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 10281 | mSpyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithPointerIds({1}))); |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10282 | mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 10283 | |
| 10284 | mSpyWindow->assertNoEvents(); |
| 10285 | |
| 10286 | // Spy window calls pilfer pointers |
| 10287 | EXPECT_EQ(OK, mDispatcher->pilferPointers(mSpyWindow->getToken())); |
| 10288 | mDragWindow->assertNoEvents(); |
| 10289 | |
| 10290 | const MotionEvent firstFingerMoveEvent = |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 10291 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10292 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 10293 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(60).y(60)) |
| 10294 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 10295 | .build(); |
| 10296 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 10297 | injectMotionEvent(*mDispatcher, firstFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10298 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10299 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10300 | |
| 10301 | // Drag window should still receive the new event |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10302 | mDragWindow->consumeMotionEvent( |
| 10303 | AllOf(WithMotionAction(ACTION_MOVE), WithFlags(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE))); |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10304 | mDragWindow->assertNoEvents(); |
| 10305 | } |
| 10306 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10307 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10308 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10309 | |
| 10310 | // Move on window and keep button pressed. |
| 10311 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10312 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10313 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 10314 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10315 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10316 | .build())) |
| 10317 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10318 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10319 | mWindow->consumeDragEvent(false, 50, 50); |
| 10320 | mSecondWindow->assertNoEvents(); |
| 10321 | |
| 10322 | // Move to another window and release button, expect to drop item. |
| 10323 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10324 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10325 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 10326 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10327 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10328 | .build())) |
| 10329 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10330 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10331 | mWindow->assertNoEvents(); |
| 10332 | mSecondWindow->assertNoEvents(); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10333 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10334 | |
| 10335 | // nothing to the window. |
| 10336 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10337 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10338 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 10339 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10340 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10341 | .build())) |
| 10342 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10343 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10344 | mWindow->assertNoEvents(); |
| 10345 | mSecondWindow->assertNoEvents(); |
| 10346 | } |
| 10347 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10348 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10349 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10350 | |
| 10351 | // Set second window invisible. |
| 10352 | mSecondWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10353 | mDispatcher->onWindowInfosChanged( |
| 10354 | {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10355 | |
| 10356 | // Move on window. |
| 10357 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10358 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10359 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10360 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10361 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10362 | mWindow->consumeDragEvent(false, 50, 50); |
| 10363 | mSecondWindow->assertNoEvents(); |
| 10364 | |
| 10365 | // Move to another window. |
| 10366 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10367 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10368 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10369 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10370 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10371 | mWindow->consumeDragEvent(true, 150, 50); |
| 10372 | mSecondWindow->assertNoEvents(); |
| 10373 | |
| 10374 | // drop to another window. |
| 10375 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10376 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10377 | {150, 50})) |
| 10378 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10379 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10380 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10381 | mWindow->assertNoEvents(); |
| 10382 | mSecondWindow->assertNoEvents(); |
| 10383 | } |
| 10384 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10385 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10386 | // Ensure window could track pointerIds if it didn't support split touch. |
| 10387 | mWindow->setPreventSplitting(true); |
| 10388 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10389 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10390 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10391 | {50, 50})) |
| 10392 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10393 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10394 | |
| 10395 | const MotionEvent secondFingerDownEvent = |
| 10396 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10397 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10398 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10399 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10400 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10401 | .build(); |
| 10402 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10403 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10404 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10405 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10406 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10407 | |
| 10408 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10409 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10410 | } |
| 10411 | |
| 10412 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 10413 | // First down on second window. |
| 10414 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10415 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10416 | {150, 50})) |
| 10417 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10418 | |
| 10419 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10420 | |
| 10421 | // Second down on first window. |
| 10422 | const MotionEvent secondFingerDownEvent = |
| 10423 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10424 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10425 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10426 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10427 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10428 | .build(); |
| 10429 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10430 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10431 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10432 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10433 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9d5f9ce | 2024-01-24 00:03:41 +0000 | [diff] [blame] | 10434 | mSecondWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10435 | |
| 10436 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10437 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10438 | |
| 10439 | // Move on window. |
| 10440 | const MotionEvent secondFingerMoveEvent = |
| 10441 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 10442 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10443 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10444 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10445 | .build(); |
| 10446 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10447 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10448 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10449 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10450 | mWindow->consumeDragEvent(false, 50, 50); |
| 10451 | mSecondWindow->consumeMotionMove(); |
| 10452 | |
| 10453 | // Release the drag pointer should perform drop. |
| 10454 | const MotionEvent secondFingerUpEvent = |
| 10455 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 10456 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10457 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10458 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10459 | .build(); |
| 10460 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10461 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10462 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10463 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10464 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken()); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10465 | mWindow->assertNoEvents(); |
| 10466 | mSecondWindow->consumeMotionMove(); |
| 10467 | } |
| 10468 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10469 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10470 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10471 | |
| 10472 | // Update window of second display. |
| 10473 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10474 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10475 | mDispatcher->onWindowInfosChanged( |
| 10476 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 10477 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 10478 | {}, |
| 10479 | 0, |
| 10480 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10481 | |
| 10482 | // Let second display has a touch state. |
| 10483 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10484 | injectMotionEvent(*mDispatcher, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10485 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10486 | AINPUT_SOURCE_TOUCHSCREEN) |
| 10487 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10488 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10489 | .build())); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 10490 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10491 | // Update window again. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10492 | mDispatcher->onWindowInfosChanged( |
| 10493 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 10494 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 10495 | {}, |
| 10496 | 0, |
| 10497 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10498 | |
| 10499 | // Move on window. |
| 10500 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10501 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10502 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10503 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10504 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10505 | mWindow->consumeDragEvent(false, 50, 50); |
| 10506 | mSecondWindow->assertNoEvents(); |
| 10507 | |
| 10508 | // Move to another window. |
| 10509 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10510 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10511 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10512 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10513 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10514 | mWindow->consumeDragEvent(true, 150, 50); |
| 10515 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10516 | |
| 10517 | // drop to another window. |
| 10518 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10519 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10520 | {150, 50})) |
| 10521 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10522 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10523 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10524 | mWindow->assertNoEvents(); |
| 10525 | mSecondWindow->assertNoEvents(); |
| 10526 | } |
| 10527 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10528 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 10529 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 10530 | // Move on window. |
| 10531 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10532 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10533 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 10534 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10535 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10536 | .x(50) |
| 10537 | .y(50)) |
| 10538 | .build())) |
| 10539 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10540 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10541 | mWindow->consumeDragEvent(false, 50, 50); |
| 10542 | mSecondWindow->assertNoEvents(); |
| 10543 | |
| 10544 | // Move to another window. |
| 10545 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10546 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10547 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 10548 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10549 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10550 | .x(150) |
| 10551 | .y(50)) |
| 10552 | .build())) |
| 10553 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10554 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10555 | mWindow->consumeDragEvent(true, 150, 50); |
| 10556 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10557 | |
| 10558 | // drop to another window. |
| 10559 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10560 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10561 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 10562 | .buttonState(0) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10563 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10564 | .x(150) |
| 10565 | .y(50)) |
| 10566 | .build())) |
| 10567 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10568 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10569 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10570 | mWindow->assertNoEvents(); |
| 10571 | mSecondWindow->assertNoEvents(); |
| 10572 | } |
| 10573 | |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 10574 | /** |
| 10575 | * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag |
| 10576 | * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash. |
| 10577 | */ |
| 10578 | TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) { |
| 10579 | // Down on second window |
| 10580 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10581 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10582 | {150, 50})) |
| 10583 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10584 | |
| 10585 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown()); |
| 10586 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown()); |
| 10587 | |
| 10588 | // Down on first window |
| 10589 | const MotionEvent secondFingerDownEvent = |
| 10590 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10591 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10592 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10593 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 10594 | .build(); |
| 10595 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10596 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 10597 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10598 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10599 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 10600 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove()); |
| 10601 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1)); |
| 10602 | |
| 10603 | // Start drag on first window |
| 10604 | ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN)); |
| 10605 | |
| 10606 | // Trigger cancel |
| 10607 | mDispatcher->cancelCurrentTouch(); |
| 10608 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel()); |
Prabir Pradhan | 65455c7 | 2024-02-13 21:46:41 +0000 | [diff] [blame] | 10609 | ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, |
| 10610 | AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)); |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 10611 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel()); |
| 10612 | |
| 10613 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10614 | // The D&D finished with nullptr |
| 10615 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
| 10616 | |
| 10617 | // Remove drag window |
| 10618 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
| 10619 | |
| 10620 | // Inject a simple gesture, ensure dispatcher not crashed |
| 10621 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10622 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10623 | PointF{50, 50})) |
| 10624 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10625 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 10626 | |
| 10627 | const MotionEvent moveEvent = |
| 10628 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 10629 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10630 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10631 | .build(); |
| 10632 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10633 | injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT, |
| 10634 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10635 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10636 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove()); |
| 10637 | |
| 10638 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10639 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10640 | {50, 50})) |
| 10641 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10642 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp()); |
| 10643 | } |
| 10644 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 10645 | TEST_F(InputDispatcherDragTests, NoDragAndDropWithHoveringPointer) { |
| 10646 | // Start hovering over the window. |
| 10647 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10648 | injectMotionEvent(*mDispatcher, ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE, |
| 10649 | ADISPLAY_ID_DEFAULT, {50, 50})); |
| 10650 | |
| 10651 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 10652 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 10653 | |
| 10654 | ASSERT_FALSE(startDrag(/*sendDown=*/false)) |
| 10655 | << "Drag and drop should not work with a hovering pointer"; |
| 10656 | } |
| 10657 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10658 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 10659 | |
| 10660 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 10661 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10662 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 10663 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10664 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10665 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 10666 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10667 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10668 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10669 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10670 | |
| 10671 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10672 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10673 | window->assertNoEvents(); |
| 10674 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10675 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10676 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 10677 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 10678 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 10679 | mDispatcher->waitForIdle(); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10680 | window->assertNoEvents(); |
| 10681 | |
| 10682 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10683 | window->setDropInput(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10684 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10685 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10686 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10687 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 10688 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10689 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10690 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10691 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10692 | window->assertNoEvents(); |
| 10693 | } |
| 10694 | |
| 10695 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 10696 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 10697 | std::make_shared<FakeApplicationHandle>(); |
| 10698 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10699 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 10700 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10701 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10702 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10703 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10704 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10705 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 10706 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10707 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10708 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10709 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 10710 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10711 | mDispatcher->onWindowInfosChanged( |
| 10712 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10713 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10714 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10715 | |
| 10716 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10717 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10718 | window->assertNoEvents(); |
| 10719 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10720 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10721 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 10722 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 10723 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10724 | window->assertNoEvents(); |
| 10725 | |
| 10726 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10727 | window->setDropInputIfObscured(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10728 | mDispatcher->onWindowInfosChanged( |
| 10729 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10730 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10731 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10732 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 10733 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10734 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10735 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10736 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 10737 | window->assertNoEvents(); |
| 10738 | } |
| 10739 | |
| 10740 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 10741 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 10742 | std::make_shared<FakeApplicationHandle>(); |
| 10743 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10744 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 10745 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10746 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10747 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10748 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10749 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10750 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 10751 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10752 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10753 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10754 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 10755 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10756 | mDispatcher->onWindowInfosChanged( |
| 10757 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10758 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10759 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10760 | |
| 10761 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10762 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10763 | window->assertNoEvents(); |
| 10764 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10765 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10766 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 10767 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 10768 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10769 | window->assertNoEvents(); |
| 10770 | |
| 10771 | // 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] | 10772 | mDispatcher->onWindowInfosChanged( |
| 10773 | {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10774 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10775 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10776 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 10777 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10778 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10779 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10780 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10781 | window->assertNoEvents(); |
| 10782 | } |
| 10783 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10784 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 10785 | protected: |
| 10786 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10787 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10788 | sp<FakeWindowHandle> mWindow; |
| 10789 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10790 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10791 | |
| 10792 | void SetUp() override { |
| 10793 | InputDispatcherTest::SetUp(); |
| 10794 | |
| 10795 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10796 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10797 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10798 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10799 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10800 | mSecondWindow = |
| 10801 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10802 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10803 | mThirdWindow = |
| 10804 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 10805 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 10806 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10807 | |
| 10808 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10809 | mDispatcher->onWindowInfosChanged( |
| 10810 | {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()}, |
| 10811 | {}, |
| 10812 | 0, |
| 10813 | 0}); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10814 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10815 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10816 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10817 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 10818 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10819 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 10820 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 10821 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10822 | mThirdWindow->assertNoEvents(); |
| 10823 | } |
| 10824 | |
| 10825 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 10826 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10827 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10828 | SECOND_DISPLAY_ID)) { |
| 10829 | mWindow->assertNoEvents(); |
| 10830 | mSecondWindow->assertNoEvents(); |
| 10831 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 10832 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10833 | } |
| 10834 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10835 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10836 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 10837 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 10838 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10839 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 10840 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10841 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10842 | } |
| 10843 | }; |
| 10844 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10845 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 10846 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10847 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 10848 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10849 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10850 | } |
| 10851 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10852 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 10853 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10854 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 10855 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10856 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10857 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10858 | ownerUid, /*hasPermission=*/false, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 10859 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10860 | mWindow->assertNoEvents(); |
| 10861 | mSecondWindow->assertNoEvents(); |
| 10862 | } |
| 10863 | |
| 10864 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 10865 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10866 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 10867 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10868 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10869 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10870 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10871 | } |
| 10872 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10873 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 10874 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10875 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 10876 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10877 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10878 | mWindow->assertNoEvents(); |
| 10879 | mSecondWindow->assertNoEvents(); |
| 10880 | } |
| 10881 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10882 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 10883 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 10884 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 10885 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10886 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10887 | mWindow->assertNoEvents(); |
| 10888 | mSecondWindow->assertNoEvents(); |
| 10889 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 10890 | } |
| 10891 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 10892 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 10893 | // Interact with the window first. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10894 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10895 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 10896 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 10897 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 10898 | |
| 10899 | // Then remove focus. |
| 10900 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10901 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 10902 | |
| 10903 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 10904 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 10905 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 10906 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10907 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 10908 | } |
| 10909 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10910 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 10911 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10912 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10913 | std::shared_ptr<FakeApplicationHandle> application = |
| 10914 | std::make_shared<FakeApplicationHandle>(); |
| 10915 | std::string name = "Fake Spy "; |
| 10916 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10917 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 10918 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10919 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 10920 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10921 | return spy; |
| 10922 | } |
| 10923 | |
| 10924 | sp<FakeWindowHandle> createForeground() { |
| 10925 | std::shared_ptr<FakeApplicationHandle> application = |
| 10926 | std::make_shared<FakeApplicationHandle>(); |
| 10927 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10928 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 10929 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 10930 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10931 | return window; |
| 10932 | } |
| 10933 | |
| 10934 | private: |
| 10935 | int mSpyCount{0}; |
| 10936 | }; |
| 10937 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 10938 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10939 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 10940 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 10941 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 10942 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
Siarhei Vishniakou | 21f77bd | 2023-07-18 17:51:35 -0700 | [diff] [blame] | 10943 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 10944 | ScopedSilentDeath _silentDeath; |
| 10945 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10946 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 10947 | spy->setTrustedOverlay(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10948 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 10949 | ".* not a trusted overlay"); |
| 10950 | } |
| 10951 | |
| 10952 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10953 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 10954 | */ |
| 10955 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10956 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10957 | mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10958 | |
| 10959 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10960 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10961 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10962 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10963 | } |
| 10964 | |
| 10965 | /** |
| 10966 | * Verify the order in which different input windows receive events. The touched foreground window |
| 10967 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 10968 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 10969 | * receive events before ones belows it. |
| 10970 | * |
| 10971 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 10972 | * spy1, spy2, window, spy3. |
| 10973 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 10974 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 10975 | * window. |
| 10976 | */ |
| 10977 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 10978 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10979 | auto spy1 = createSpy(); |
| 10980 | auto spy2 = createSpy(); |
| 10981 | auto spy3 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10982 | mDispatcher->onWindowInfosChanged( |
| 10983 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10984 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 10985 | const size_t numChannels = channels.size(); |
| 10986 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 10987 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 10988 | if (!epollFd.ok()) { |
| 10989 | FAIL() << "Failed to create epoll fd"; |
| 10990 | } |
| 10991 | |
| 10992 | for (size_t i = 0; i < numChannels; i++) { |
| 10993 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 10994 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 10995 | FAIL() << "Failed to add fd to epoll"; |
| 10996 | } |
| 10997 | } |
| 10998 | |
| 10999 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11000 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11001 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11002 | |
| 11003 | std::vector<size_t> eventOrder; |
| 11004 | std::vector<struct epoll_event> events(numChannels); |
| 11005 | for (;;) { |
| 11006 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 11007 | (100ms).count()); |
| 11008 | if (nFds < 0) { |
| 11009 | FAIL() << "Failed to call epoll_wait"; |
| 11010 | } |
| 11011 | if (nFds == 0) { |
| 11012 | break; // epoll_wait timed out |
| 11013 | } |
| 11014 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 11015 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 11016 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11017 | channels[i]->consumeMotionDown(); |
| 11018 | } |
| 11019 | } |
| 11020 | |
| 11021 | // Verify the order in which the events were received. |
| 11022 | EXPECT_EQ(3u, eventOrder.size()); |
| 11023 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 11024 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 11025 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 11026 | } |
| 11027 | |
| 11028 | /** |
| 11029 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 11030 | */ |
| 11031 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 11032 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11033 | auto spy = createSpy(); |
| 11034 | spy->setTouchable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11035 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11036 | |
| 11037 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11038 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11039 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11040 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11041 | spy->assertNoEvents(); |
| 11042 | } |
| 11043 | |
| 11044 | /** |
| 11045 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 11046 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 11047 | * to the window. |
| 11048 | */ |
| 11049 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 11050 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11051 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11052 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11053 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11054 | |
| 11055 | // Inject an event outside the spy window's touchable region. |
| 11056 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11057 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11058 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11059 | window->consumeMotionDown(); |
| 11060 | spy->assertNoEvents(); |
| 11061 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11062 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11063 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11064 | window->consumeMotionUp(); |
| 11065 | spy->assertNoEvents(); |
| 11066 | |
| 11067 | // Inject an event inside the spy window's touchable region. |
| 11068 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11069 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11070 | {5, 10})) |
| 11071 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11072 | window->consumeMotionDown(); |
| 11073 | spy->consumeMotionDown(); |
| 11074 | } |
| 11075 | |
| 11076 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11077 | * 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] | 11078 | * 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] | 11079 | */ |
| 11080 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 11081 | auto window = createForeground(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11082 | window->setOwnerInfo(gui::Pid{12}, gui::Uid{34}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11083 | auto spy = createSpy(); |
| 11084 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11085 | spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11086 | spy->setFrame(Rect{0, 0, 20, 20}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11087 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11088 | |
| 11089 | // Inject an event outside the spy window's frame and touchable region. |
| 11090 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11091 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 11092 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11093 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11094 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 11095 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11096 | } |
| 11097 | |
| 11098 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11099 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 11100 | * pointers that are down within its bounds. |
| 11101 | */ |
| 11102 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 11103 | auto windowLeft = createForeground(); |
| 11104 | windowLeft->setFrame({0, 0, 100, 200}); |
| 11105 | auto windowRight = createForeground(); |
| 11106 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11107 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11108 | spy->setFrame({0, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11109 | mDispatcher->onWindowInfosChanged( |
| 11110 | {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11111 | |
| 11112 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11113 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11114 | {50, 50})) |
| 11115 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11116 | windowLeft->consumeMotionDown(); |
| 11117 | spy->consumeMotionDown(); |
| 11118 | |
| 11119 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 11120 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11121 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11122 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11123 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11124 | .build(); |
| 11125 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11126 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11127 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11128 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11129 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11130 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11131 | } |
| 11132 | |
| 11133 | /** |
| 11134 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 11135 | * the spy should receive the second pointer with ACTION_DOWN. |
| 11136 | */ |
| 11137 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 11138 | auto window = createForeground(); |
| 11139 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11140 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11141 | spyRight->setFrame({100, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11142 | mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11143 | |
| 11144 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11145 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11146 | {50, 50})) |
| 11147 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11148 | window->consumeMotionDown(); |
| 11149 | spyRight->assertNoEvents(); |
| 11150 | |
| 11151 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 11152 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11153 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11154 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11155 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11156 | .build(); |
| 11157 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11158 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11159 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11160 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11161 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11162 | spyRight->consumeMotionDown(); |
| 11163 | } |
| 11164 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11165 | /** |
| 11166 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 11167 | * windows should be allowed to control split touch. |
| 11168 | */ |
| 11169 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 11170 | // 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] | 11171 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11172 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 11173 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11174 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11175 | auto window = createForeground(); |
| 11176 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11177 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11178 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11179 | |
| 11180 | // First finger down, no window touched. |
| 11181 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11182 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11183 | {100, 200})) |
| 11184 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11185 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11186 | window->assertNoEvents(); |
| 11187 | |
| 11188 | // Second finger down on window, the window should receive touch down. |
| 11189 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 11190 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11191 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11192 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11193 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 11194 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11195 | .build(); |
| 11196 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11197 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11198 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11199 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11200 | |
| 11201 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11202 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11203 | } |
| 11204 | |
| 11205 | /** |
| 11206 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 11207 | * do not receive key events. |
| 11208 | */ |
| 11209 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11210 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11211 | spy->setFocusable(false); |
| 11212 | |
| 11213 | auto window = createForeground(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11214 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11215 | setFocusedWindow(window); |
| 11216 | window->consumeFocusEvent(true); |
| 11217 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11218 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11219 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 11220 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 11221 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11222 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11223 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 11224 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 11225 | |
| 11226 | spy->assertNoEvents(); |
| 11227 | } |
| 11228 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11229 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 11230 | |
| 11231 | /** |
| 11232 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 11233 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 11234 | */ |
| 11235 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 11236 | auto window = createForeground(); |
| 11237 | auto spy1 = createSpy(); |
| 11238 | auto spy2 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11239 | mDispatcher->onWindowInfosChanged( |
| 11240 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11241 | |
| 11242 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11243 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11244 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11245 | window->consumeMotionDown(); |
| 11246 | spy1->consumeMotionDown(); |
| 11247 | spy2->consumeMotionDown(); |
| 11248 | |
| 11249 | // Pilfer pointers from the second spy window. |
| 11250 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 11251 | spy2->assertNoEvents(); |
| 11252 | spy1->consumeMotionCancel(); |
| 11253 | window->consumeMotionCancel(); |
| 11254 | |
| 11255 | // The rest of the gesture should only be sent to the second spy window. |
| 11256 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11257 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11258 | ADISPLAY_ID_DEFAULT)) |
| 11259 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11260 | spy2->consumeMotionMove(); |
| 11261 | spy1->assertNoEvents(); |
| 11262 | window->assertNoEvents(); |
| 11263 | } |
| 11264 | |
| 11265 | /** |
| 11266 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 11267 | * in the middle of the gesture. |
| 11268 | */ |
| 11269 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 11270 | auto window = createForeground(); |
| 11271 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11272 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11273 | |
| 11274 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11275 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11276 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11277 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11278 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11279 | |
| 11280 | window->releaseChannel(); |
| 11281 | |
| 11282 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11283 | |
| 11284 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11285 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11286 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11287 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 11288 | } |
| 11289 | |
| 11290 | /** |
| 11291 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 11292 | * the spy, but not to any other windows. |
| 11293 | */ |
| 11294 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 11295 | auto spy = createSpy(); |
| 11296 | auto window = createForeground(); |
| 11297 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11298 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11299 | |
| 11300 | // First finger down on the window and the spy. |
| 11301 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11302 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11303 | {100, 200})) |
| 11304 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11305 | spy->consumeMotionDown(); |
| 11306 | window->consumeMotionDown(); |
| 11307 | |
| 11308 | // Spy window pilfers the pointers. |
| 11309 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11310 | window->consumeMotionCancel(); |
| 11311 | |
| 11312 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 11313 | const MotionEvent secondFingerDownEvent = |
| 11314 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11315 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11316 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11317 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 11318 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11319 | .build(); |
| 11320 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11321 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11322 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11323 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11324 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11325 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11326 | |
| 11327 | // Third finger goes down outside all windows, so injection should fail. |
| 11328 | const MotionEvent thirdFingerDownEvent = |
| 11329 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11330 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11331 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11332 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 11333 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 11334 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11335 | .build(); |
| 11336 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11337 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11338 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 11339 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11340 | |
| 11341 | spy->assertNoEvents(); |
| 11342 | window->assertNoEvents(); |
| 11343 | } |
| 11344 | |
| 11345 | /** |
| 11346 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 11347 | */ |
| 11348 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 11349 | auto spy = createSpy(); |
| 11350 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 11351 | auto window = createForeground(); |
| 11352 | window->setFrame(Rect(0, 0, 200, 200)); |
| 11353 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11354 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11355 | |
| 11356 | // First finger down on the window only |
| 11357 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11358 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11359 | {150, 150})) |
| 11360 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11361 | window->consumeMotionDown(); |
| 11362 | |
| 11363 | // Second finger down on the spy and window |
| 11364 | const MotionEvent secondFingerDownEvent = |
| 11365 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11366 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11367 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11368 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 11369 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11370 | .build(); |
| 11371 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11372 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11373 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11374 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11375 | spy->consumeMotionDown(); |
| 11376 | window->consumeMotionPointerDown(1); |
| 11377 | |
| 11378 | // Third finger down on the spy and window |
| 11379 | const MotionEvent thirdFingerDownEvent = |
| 11380 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11381 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11382 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11383 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 11384 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 11385 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11386 | .build(); |
| 11387 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11388 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11389 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11390 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11391 | spy->consumeMotionPointerDown(1); |
| 11392 | window->consumeMotionPointerDown(2); |
| 11393 | |
| 11394 | // Spy window pilfers the pointers. |
| 11395 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 11396 | window->consumeMotionPointerUp(/*idx=*/2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 11397 | window->consumeMotionPointerUp(/*idx=*/1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11398 | |
| 11399 | spy->assertNoEvents(); |
| 11400 | window->assertNoEvents(); |
| 11401 | } |
| 11402 | |
| 11403 | /** |
| 11404 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 11405 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 11406 | * window, then that window should receive ACTION_CANCEL. |
| 11407 | */ |
| 11408 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 11409 | auto spy = createSpy(); |
| 11410 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 11411 | auto window = createForeground(); |
| 11412 | window->setFrame(Rect(0, 0, 200, 200)); |
| 11413 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11414 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11415 | |
| 11416 | // First finger down on both spy and window |
| 11417 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11418 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11419 | {10, 10})) |
| 11420 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11421 | window->consumeMotionDown(); |
| 11422 | spy->consumeMotionDown(); |
| 11423 | |
| 11424 | // Second finger down on the spy and window |
| 11425 | const MotionEvent secondFingerDownEvent = |
| 11426 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11427 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11428 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11429 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 11430 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11431 | .build(); |
| 11432 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11433 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11434 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11435 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11436 | spy->consumeMotionPointerDown(1); |
| 11437 | window->consumeMotionPointerDown(1); |
| 11438 | |
| 11439 | // Spy window pilfers the pointers. |
| 11440 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11441 | window->consumeMotionCancel(); |
| 11442 | |
| 11443 | spy->assertNoEvents(); |
| 11444 | window->assertNoEvents(); |
| 11445 | } |
| 11446 | |
| 11447 | /** |
| 11448 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 11449 | * be sent to other windows |
| 11450 | */ |
| 11451 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 11452 | auto spy = createSpy(); |
| 11453 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 11454 | auto window = createForeground(); |
| 11455 | window->setFrame(Rect(0, 0, 200, 200)); |
| 11456 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11457 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11458 | |
| 11459 | // First finger down on both window and spy |
| 11460 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11461 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11462 | {10, 10})) |
| 11463 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11464 | window->consumeMotionDown(); |
| 11465 | spy->consumeMotionDown(); |
| 11466 | |
| 11467 | // Spy window pilfers the pointers. |
| 11468 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11469 | window->consumeMotionCancel(); |
| 11470 | |
| 11471 | // Second finger down on the window only |
| 11472 | const MotionEvent secondFingerDownEvent = |
| 11473 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11474 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11475 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11476 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 11477 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11478 | .build(); |
| 11479 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11480 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11481 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11482 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11483 | window->consumeMotionDown(); |
| 11484 | window->assertNoEvents(); |
| 11485 | |
| 11486 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 11487 | spy->consumeMotionMove(); |
| 11488 | spy->assertNoEvents(); |
| 11489 | } |
| 11490 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11491 | /** |
| 11492 | * A window on the left and a window on the right. Also, a spy window that's above all of the |
| 11493 | * windows, and spanning both left and right windows. |
| 11494 | * Send simultaneous motion streams from two different devices, one to the left window, and another |
| 11495 | * to the right window. |
| 11496 | * Pilfer from spy window. |
| 11497 | * Check that the pilfering only affects the pointers that are actually being received by the spy. |
| 11498 | */ |
| 11499 | TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) { |
| 11500 | sp<FakeWindowHandle> spy = createSpy(); |
| 11501 | spy->setFrame(Rect(0, 0, 200, 200)); |
| 11502 | sp<FakeWindowHandle> leftWindow = createForeground(); |
| 11503 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 11504 | |
| 11505 | sp<FakeWindowHandle> rightWindow = createForeground(); |
| 11506 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 11507 | |
| 11508 | constexpr int32_t stylusDeviceId = 1; |
| 11509 | constexpr int32_t touchDeviceId = 2; |
| 11510 | |
| 11511 | mDispatcher->onWindowInfosChanged( |
| 11512 | {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 11513 | |
| 11514 | // Stylus down on left window and spy |
| 11515 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 11516 | .deviceId(stylusDeviceId) |
| 11517 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 11518 | .build()); |
| 11519 | leftWindow->consumeMotionEvent( |
| 11520 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 11521 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 11522 | |
| 11523 | // Finger down on right window and spy - but spy already has stylus |
| 11524 | mDispatcher->notifyMotion( |
| 11525 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11526 | .deviceId(touchDeviceId) |
| 11527 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 11528 | .build()); |
| 11529 | rightWindow->consumeMotionEvent( |
| 11530 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11531 | spy->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11532 | |
| 11533 | // Act: pilfer from spy. Spy is currently receiving touch events. |
| 11534 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11535 | leftWindow->consumeMotionEvent( |
| 11536 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11537 | rightWindow->consumeMotionEvent( |
| 11538 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 11539 | |
| 11540 | // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus |
| 11541 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 11542 | .deviceId(stylusDeviceId) |
| 11543 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52)) |
| 11544 | .build()); |
| 11545 | mDispatcher->notifyMotion( |
| 11546 | MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 11547 | .deviceId(touchDeviceId) |
| 11548 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52)) |
| 11549 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11550 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11551 | |
| 11552 | spy->assertNoEvents(); |
| 11553 | leftWindow->assertNoEvents(); |
| 11554 | rightWindow->assertNoEvents(); |
| 11555 | } |
| 11556 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 11557 | TEST_F(InputDispatcherPilferPointersTest, NoPilferingWithHoveringPointers) { |
| 11558 | auto window = createForeground(); |
| 11559 | auto spy = createSpy(); |
| 11560 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 11561 | |
| 11562 | mDispatcher->notifyMotion( |
| 11563 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 11564 | .deviceId(1) |
| 11565 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(100).y(200)) |
| 11566 | .build()); |
| 11567 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 11568 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 11569 | |
| 11570 | // Pilfer pointers from the spy window should fail. |
| 11571 | EXPECT_NE(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11572 | spy->assertNoEvents(); |
| 11573 | window->assertNoEvents(); |
| 11574 | } |
| 11575 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11576 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 11577 | public: |
| 11578 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 11579 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 11580 | std::make_shared<FakeApplicationHandle>(); |
| 11581 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11582 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 11583 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11584 | overlay->setFocusable(false); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11585 | overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11586 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11587 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11588 | overlay->setTrustedOverlay(true); |
| 11589 | |
| 11590 | std::shared_ptr<FakeApplicationHandle> application = |
| 11591 | std::make_shared<FakeApplicationHandle>(); |
| 11592 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11593 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 11594 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11595 | window->setFocusable(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11596 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11597 | |
| 11598 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11599 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11600 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11601 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11602 | return {std::move(overlay), std::move(window)}; |
| 11603 | } |
| 11604 | |
| 11605 | void sendFingerEvent(int32_t action) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11606 | mDispatcher->notifyMotion( |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11607 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11608 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}})); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11609 | } |
| 11610 | |
| 11611 | void sendStylusEvent(int32_t action) { |
| 11612 | NotifyMotionArgs motionArgs = |
| 11613 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 11614 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11615 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11616 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11617 | } |
| 11618 | }; |
| 11619 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11620 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 11621 | |
| 11622 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
Siarhei Vishniakou | ad3b682 | 2023-06-22 14:17:35 -0700 | [diff] [blame] | 11623 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11624 | ScopedSilentDeath _silentDeath; |
| 11625 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11626 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 11627 | overlay->setTrustedOverlay(false); |
| 11628 | // 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] | 11629 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 11630 | {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11631 | ".* not a trusted overlay"); |
| 11632 | } |
| 11633 | |
| 11634 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 11635 | auto [overlay, window] = setupStylusOverlayScenario(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11636 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11637 | |
| 11638 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11639 | overlay->consumeMotionDown(); |
| 11640 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 11641 | overlay->consumeMotionUp(); |
| 11642 | |
| 11643 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11644 | window->consumeMotionDown(); |
| 11645 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 11646 | window->consumeMotionUp(); |
| 11647 | |
| 11648 | overlay->assertNoEvents(); |
| 11649 | window->assertNoEvents(); |
| 11650 | } |
| 11651 | |
| 11652 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 11653 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11654 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11655 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11656 | |
| 11657 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11658 | overlay->consumeMotionDown(); |
| 11659 | window->consumeMotionDown(); |
| 11660 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 11661 | overlay->consumeMotionUp(); |
| 11662 | window->consumeMotionUp(); |
| 11663 | |
| 11664 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11665 | window->consumeMotionDown(); |
| 11666 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 11667 | window->consumeMotionUp(); |
| 11668 | |
| 11669 | overlay->assertNoEvents(); |
| 11670 | window->assertNoEvents(); |
| 11671 | } |
| 11672 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 11673 | /** |
| 11674 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 11675 | * The scenario is as follows: |
| 11676 | * - The stylus interceptor overlay is configured as a spy window. |
| 11677 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 11678 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 11679 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 11680 | */ |
| 11681 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 11682 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 11683 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11684 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 11685 | |
| 11686 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11687 | overlay->consumeMotionDown(); |
| 11688 | window->consumeMotionDown(); |
| 11689 | |
| 11690 | // The interceptor pilfers the pointers. |
| 11691 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 11692 | window->consumeMotionCancel(); |
| 11693 | |
| 11694 | // The interceptor configures itself so that it is no longer a spy. |
| 11695 | overlay->setSpy(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11696 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 11697 | |
| 11698 | // It continues to receive the rest of the stylus gesture. |
| 11699 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 11700 | overlay->consumeMotionMove(); |
| 11701 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 11702 | overlay->consumeMotionUp(); |
| 11703 | |
| 11704 | window->assertNoEvents(); |
| 11705 | } |
| 11706 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11707 | struct User { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11708 | gui::Pid mPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11709 | gui::Uid mUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11710 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 11711 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 11712 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11713 | User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid) |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11714 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 11715 | |
| 11716 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11717 | return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11718 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 11719 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 11720 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 11721 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 11722 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 11723 | } |
| 11724 | |
| 11725 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11726 | return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11727 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11728 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11729 | mPolicyFlags); |
| 11730 | } |
| 11731 | |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11732 | sp<FakeWindowHandle> createWindow(const char* name) const { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11733 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 11734 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11735 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 11736 | name, ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11737 | window->setOwnerInfo(mPid, mUid); |
| 11738 | return window; |
| 11739 | } |
| 11740 | }; |
| 11741 | |
| 11742 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 11743 | |
| 11744 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11745 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11746 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11747 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11748 | |
| 11749 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11750 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11751 | window->consumeMotionDown(); |
| 11752 | |
| 11753 | setFocusedWindow(window); |
| 11754 | window->consumeFocusEvent(true); |
| 11755 | |
| 11756 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11757 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 11758 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 11759 | } |
| 11760 | |
| 11761 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11762 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11763 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11764 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11765 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11766 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11767 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 11768 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11769 | |
| 11770 | setFocusedWindow(window); |
| 11771 | window->consumeFocusEvent(true); |
| 11772 | |
| 11773 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 11774 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 11775 | window->assertNoEvents(); |
| 11776 | } |
| 11777 | |
| 11778 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11779 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11780 | auto window = owner.createWindow("Owned window"); |
| 11781 | auto spy = owner.createWindow("Owned spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11782 | spy->setSpy(true); |
| 11783 | spy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11784 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11785 | |
| 11786 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11787 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11788 | spy->consumeMotionDown(); |
| 11789 | window->consumeMotionDown(); |
| 11790 | } |
| 11791 | |
| 11792 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11793 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11794 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11795 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11796 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11797 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11798 | randosSpy->setSpy(true); |
| 11799 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11800 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11801 | |
| 11802 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 11803 | // not receive the event. |
| 11804 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11805 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11806 | randosSpy->assertNoEvents(); |
| 11807 | window->consumeMotionDown(); |
| 11808 | } |
| 11809 | |
| 11810 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11811 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11812 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11813 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11814 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11815 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11816 | randosSpy->setSpy(true); |
| 11817 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11818 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11819 | |
| 11820 | // A user that has injection permission can inject into any window. |
| 11821 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11822 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11823 | ADISPLAY_ID_DEFAULT)); |
| 11824 | randosSpy->consumeMotionDown(); |
| 11825 | window->consumeMotionDown(); |
| 11826 | |
| 11827 | setFocusedWindow(randosSpy); |
| 11828 | randosSpy->consumeFocusEvent(true); |
| 11829 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11830 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11831 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 11832 | window->assertNoEvents(); |
| 11833 | } |
| 11834 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 11835 | TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11836 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11837 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11838 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11839 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11840 | auto randosWindow = rando.createWindow("Rando's window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11841 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 11842 | randosWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11843 | mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11844 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 11845 | // 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] | 11846 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11847 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11848 | window->consumeMotionDown(); |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 11849 | randosWindow->assertNoEvents(); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11850 | } |
| 11851 | |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 11852 | using InputDispatcherPointerInWindowTest = InputDispatcherTest; |
| 11853 | |
| 11854 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWhenHovering) { |
| 11855 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 11856 | |
| 11857 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 11858 | ADISPLAY_ID_DEFAULT); |
| 11859 | left->setFrame(Rect(0, 0, 100, 100)); |
| 11860 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 11861 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 11862 | right->setFrame(Rect(100, 0, 200, 100)); |
| 11863 | sp<FakeWindowHandle> spy = |
| 11864 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT); |
| 11865 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 11866 | spy->setTrustedOverlay(true); |
| 11867 | spy->setSpy(true); |
| 11868 | |
| 11869 | mDispatcher->onWindowInfosChanged( |
| 11870 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 11871 | |
| 11872 | // Hover into the left window. |
| 11873 | mDispatcher->notifyMotion( |
| 11874 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 11875 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(50).y(50)) |
| 11876 | .build()); |
| 11877 | |
| 11878 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 11879 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 11880 | |
| 11881 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11882 | /*pointerId=*/0)); |
| 11883 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11884 | /*pointerId=*/0)); |
| 11885 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11886 | /*pointerId=*/0)); |
| 11887 | |
| 11888 | // Hover move to the right window. |
| 11889 | mDispatcher->notifyMotion( |
| 11890 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 11891 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 11892 | .build()); |
| 11893 | |
| 11894 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 11895 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 11896 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 11897 | |
| 11898 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11899 | /*pointerId=*/0)); |
| 11900 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11901 | /*pointerId=*/0)); |
| 11902 | ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11903 | /*pointerId=*/0)); |
| 11904 | |
| 11905 | // Stop hovering. |
| 11906 | mDispatcher->notifyMotion( |
| 11907 | MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 11908 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 11909 | .build()); |
| 11910 | |
| 11911 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 11912 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 11913 | |
| 11914 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11915 | /*pointerId=*/0)); |
| 11916 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11917 | /*pointerId=*/0)); |
| 11918 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11919 | /*pointerId=*/0)); |
| 11920 | } |
| 11921 | |
| 11922 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWithSplitTouch) { |
| 11923 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 11924 | |
| 11925 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 11926 | ADISPLAY_ID_DEFAULT); |
| 11927 | left->setFrame(Rect(0, 0, 100, 100)); |
| 11928 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 11929 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 11930 | right->setFrame(Rect(100, 0, 200, 100)); |
| 11931 | sp<FakeWindowHandle> spy = |
| 11932 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT); |
| 11933 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 11934 | spy->setTrustedOverlay(true); |
| 11935 | spy->setSpy(true); |
| 11936 | |
| 11937 | mDispatcher->onWindowInfosChanged( |
| 11938 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 11939 | |
| 11940 | // First pointer down on left window. |
| 11941 | mDispatcher->notifyMotion( |
| 11942 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11943 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11944 | .build()); |
| 11945 | |
| 11946 | left->consumeMotionDown(); |
| 11947 | spy->consumeMotionDown(); |
| 11948 | |
| 11949 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11950 | /*pointerId=*/0)); |
| 11951 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11952 | /*pointerId=*/0)); |
| 11953 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11954 | /*pointerId=*/0)); |
| 11955 | |
| 11956 | // Second pointer down on right window. |
| 11957 | mDispatcher->notifyMotion( |
| 11958 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11959 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11960 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 11961 | .build()); |
| 11962 | |
| 11963 | left->consumeMotionMove(); |
| 11964 | right->consumeMotionDown(); |
| 11965 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 11966 | |
| 11967 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11968 | /*pointerId=*/0)); |
| 11969 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11970 | /*pointerId=*/0)); |
| 11971 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11972 | /*pointerId=*/0)); |
| 11973 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11974 | /*pointerId=*/1)); |
| 11975 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11976 | /*pointerId=*/1)); |
| 11977 | ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11978 | /*pointerId=*/1)); |
| 11979 | |
| 11980 | // Second pointer up. |
| 11981 | mDispatcher->notifyMotion( |
| 11982 | MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 11983 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11984 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 11985 | .build()); |
| 11986 | |
| 11987 | left->consumeMotionMove(); |
| 11988 | right->consumeMotionUp(); |
| 11989 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 11990 | |
| 11991 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11992 | /*pointerId=*/0)); |
| 11993 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11994 | /*pointerId=*/0)); |
| 11995 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11996 | /*pointerId=*/0)); |
| 11997 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 11998 | /*pointerId=*/1)); |
| 11999 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12000 | /*pointerId=*/1)); |
| 12001 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12002 | /*pointerId=*/1)); |
| 12003 | |
| 12004 | // First pointer up. |
| 12005 | mDispatcher->notifyMotion( |
| 12006 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 12007 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12008 | .build()); |
| 12009 | |
| 12010 | left->consumeMotionUp(); |
| 12011 | spy->consumeMotionUp(); |
| 12012 | |
| 12013 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12014 | /*pointerId=*/0)); |
| 12015 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12016 | /*pointerId=*/0)); |
| 12017 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12018 | /*pointerId=*/0)); |
| 12019 | } |
| 12020 | |
| 12021 | TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse) { |
| 12022 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 12023 | |
| 12024 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 12025 | ADISPLAY_ID_DEFAULT); |
| 12026 | left->setFrame(Rect(0, 0, 100, 100)); |
| 12027 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 12028 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 12029 | right->setFrame(Rect(100, 0, 200, 100)); |
| 12030 | |
| 12031 | mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 12032 | |
| 12033 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12034 | /*pointerId=*/0)); |
| 12035 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12036 | /*pointerId=*/0)); |
| 12037 | |
| 12038 | // Hover move into the window. |
| 12039 | mDispatcher->notifyMotion( |
| 12040 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 12041 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50)) |
| 12042 | .rawXCursorPosition(50) |
| 12043 | .rawYCursorPosition(50) |
| 12044 | .deviceId(DEVICE_ID) |
| 12045 | .build()); |
| 12046 | |
| 12047 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12048 | |
| 12049 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12050 | /*pointerId=*/0)); |
| 12051 | |
| 12052 | // Move the mouse with another device. This cancels the hovering pointer from the first device. |
| 12053 | mDispatcher->notifyMotion( |
| 12054 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 12055 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50)) |
| 12056 | .rawXCursorPosition(51) |
| 12057 | .rawYCursorPosition(50) |
| 12058 | .deviceId(SECOND_DEVICE_ID) |
| 12059 | .build()); |
| 12060 | |
| 12061 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12062 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12063 | |
| 12064 | // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets |
| 12065 | // a HOVER_EXIT from the first device. |
| 12066 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12067 | /*pointerId=*/0)); |
| 12068 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, |
| 12069 | SECOND_DEVICE_ID, |
| 12070 | /*pointerId=*/0)); |
| 12071 | |
| 12072 | // Move the mouse outside the window. Document the current behavior, where the window does not |
| 12073 | // receive HOVER_EXIT even though the mouse left the window. |
| 12074 | mDispatcher->notifyMotion( |
| 12075 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 12076 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50)) |
| 12077 | .rawXCursorPosition(150) |
| 12078 | .rawYCursorPosition(50) |
| 12079 | .deviceId(SECOND_DEVICE_ID) |
| 12080 | .build()); |
| 12081 | |
| 12082 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12083 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12084 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12085 | /*pointerId=*/0)); |
| 12086 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, |
| 12087 | SECOND_DEVICE_ID, |
| 12088 | /*pointerId=*/0)); |
| 12089 | } |
| 12090 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 12091 | } // namespace android::inputdispatcher |