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" |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 18 | #include "../BlockingQueue.h" |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 19 | #include "FakeApplicationHandle.h" |
Prabir Pradhan | d3fb627 | 2023-12-15 01:21:47 +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 | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 35 | #include <input/Input.h> |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 36 | #include <input/PrintTools.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 37 | #include <linux/input.h> |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 38 | #include <sys/epoll.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 39 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 40 | #include <cinttypes> |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 41 | #include <compare> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 42 | #include <thread> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 43 | #include <unordered_set> |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 44 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 45 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 46 | using android::base::StringPrintf; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 47 | using android::gui::FocusRequest; |
| 48 | using android::gui::TouchOcclusionMode; |
| 49 | using android::gui::WindowInfo; |
| 50 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 51 | using android::os::InputEventInjectionResult; |
| 52 | using android::os::InputEventInjectionSync; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 53 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 54 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 55 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 56 | using namespace ftl::flag_operators; |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 57 | using testing::AllOf; |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 58 | using testing::Not; |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 59 | |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 60 | namespace { |
| 61 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 62 | // An arbitrary time value. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 63 | static constexpr nsecs_t ARBITRARY_TIME = 1234; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 64 | |
| 65 | // An arbitrary device id. |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 66 | static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 67 | static constexpr int32_t SECOND_DEVICE_ID = 2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 68 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 69 | // An arbitrary display id. |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 70 | static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
| 71 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 72 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 73 | // Ensure common actions are interchangeable between keys and motions for convenience. |
| 74 | static_assert(AMOTION_EVENT_ACTION_DOWN == AKEY_EVENT_ACTION_DOWN); |
| 75 | static_assert(AMOTION_EVENT_ACTION_UP == AKEY_EVENT_ACTION_UP); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 76 | static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN; |
| 77 | static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE; |
| 78 | static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP; |
| 79 | static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 80 | static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 81 | static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 82 | static constexpr int32_t ACTION_SCROLL = AMOTION_EVENT_ACTION_SCROLL; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 83 | static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 84 | static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 85 | /** |
| 86 | * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the |
| 87 | * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at |
| 88 | * index 0) is the new pointer going down. The same pointer could have been placed at a different |
| 89 | * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In |
| 90 | * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if |
| 91 | * pointer id=0 leaves but the pointer id=1 remains. |
| 92 | */ |
| 93 | static constexpr int32_t POINTER_0_DOWN = |
| 94 | AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 95 | static constexpr int32_t POINTER_1_DOWN = |
| 96 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 97 | static constexpr int32_t POINTER_2_DOWN = |
| 98 | AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 99 | static constexpr int32_t POINTER_3_DOWN = |
| 100 | AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 101 | static constexpr int32_t POINTER_0_UP = |
| 102 | AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 103 | static constexpr int32_t POINTER_1_UP = |
| 104 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 105 | static constexpr int32_t POINTER_2_UP = |
| 106 | AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 107 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 108 | // 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] | 109 | static constexpr gui::Pid WINDOW_PID{999}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 110 | static constexpr gui::Uid WINDOW_UID{1001}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 111 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 112 | // 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] | 113 | static constexpr gui::Pid SECONDARY_WINDOW_PID{1010}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 114 | static constexpr gui::Uid SECONDARY_WINDOW_UID{1012}; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 115 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 116 | // An arbitrary pid of the gesture monitor window |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 117 | static constexpr gui::Pid MONITOR_PID{2001}; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 118 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 119 | /** |
| 120 | * If we expect to receive the event, the timeout can be made very long. When the test are running |
| 121 | * correctly, we will actually never wait until the end of the timeout because the wait will end |
| 122 | * when the event comes in. Still, this value shouldn't be infinite. During development, a local |
| 123 | * change may cause the test to fail. This timeout should be short enough to not annoy so that the |
| 124 | * developer can see the failure quickly (on human scale). |
| 125 | */ |
| 126 | static constexpr std::chrono::duration CONSUME_TIMEOUT_EVENT_EXPECTED = 1000ms; |
| 127 | /** |
| 128 | * When no event is expected, we can have a very short timeout. A large value here would slow down |
| 129 | * the tests. In the unlikely event of system being too slow, the event may still be present but the |
| 130 | * timeout would complete before it is consumed. This would result in test flakiness. If this |
| 131 | * occurs, the flakiness rate would be high. Since the flakes are treated with high priority, this |
| 132 | * would get noticed and addressed quickly. |
| 133 | */ |
| 134 | static constexpr std::chrono::duration CONSUME_TIMEOUT_NO_EVENT_EXPECTED = 10ms; |
| 135 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 136 | static constexpr int expectedWallpaperFlags = |
| 137 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 138 | |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 139 | using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID; |
| 140 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 141 | /** |
| 142 | * Return a DOWN key event with KEYCODE_A. |
| 143 | */ |
| 144 | static KeyEvent getTestKeyEvent() { |
| 145 | KeyEvent event; |
| 146 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 147 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 148 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 149 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 150 | return event; |
| 151 | } |
| 152 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 153 | // --- FakeInputDispatcherPolicy --- |
| 154 | |
| 155 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 156 | struct AnrResult { |
| 157 | sp<IBinder> token{}; |
| 158 | gui::Pid pid{gui::Pid::INVALID}; |
| 159 | }; |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 160 | /* Stores data about a user-activity-poke event from the dispatcher. */ |
| 161 | struct UserActivityPokeEvent { |
| 162 | nsecs_t eventTime; |
| 163 | int32_t eventType; |
| 164 | int32_t displayId; |
| 165 | |
| 166 | bool operator==(const UserActivityPokeEvent& rhs) const = default; |
| 167 | |
| 168 | friend std::ostream& operator<<(std::ostream& os, const UserActivityPokeEvent& ev) { |
| 169 | os << "UserActivityPokeEvent[time=" << ev.eventTime << ", eventType=" << ev.eventType |
| 170 | << ", displayId=" << ev.displayId << "]"; |
| 171 | return os; |
| 172 | } |
| 173 | }; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 174 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 175 | public: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 176 | FakeInputDispatcherPolicy() = default; |
| 177 | virtual ~FakeInputDispatcherPolicy() = default; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 178 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 179 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 180 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 181 | ASSERT_EQ(event.getType(), InputEventType::KEY); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 182 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 183 | |
| 184 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 185 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 186 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 187 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 188 | } |
| 189 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 190 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 191 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 192 | ASSERT_EQ(event.getType(), InputEventType::MOTION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 193 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 194 | |
| 195 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 196 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 197 | EXPECT_EQ(motionEvent.getAction(), args.action); |
Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 198 | EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 199 | EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
| 200 | EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 201 | EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 202 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 203 | } |
| 204 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 205 | void assertFilterInputEventWasNotCalled() { |
| 206 | std::scoped_lock lock(mLock); |
| 207 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 208 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 209 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 210 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 211 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 212 | ASSERT_TRUE(mConfigurationChangedTime) |
| 213 | << "Timed out waiting for configuration changed call"; |
| 214 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 215 | mConfigurationChangedTime = std::nullopt; |
| 216 | } |
| 217 | |
| 218 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 219 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 220 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 221 | // We do not check id because it is not exposed to the policy |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 222 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 223 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 224 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 225 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 226 | mLastNotifySwitch = std::nullopt; |
| 227 | } |
| 228 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 229 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 230 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 231 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 232 | mOnPointerDownToken.clear(); |
| 233 | } |
| 234 | |
| 235 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 236 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 237 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 238 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 241 | // This function must be called soon after the expected ANR timer starts, |
| 242 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 243 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 244 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 245 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 246 | std::unique_lock lock(mLock); |
| 247 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 248 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 249 | ASSERT_NO_FATAL_FAILURE( |
| 250 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 251 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 254 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 255 | const sp<WindowInfoHandle>& window) { |
| 256 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 257 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 258 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 259 | } |
| 260 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 261 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 262 | const sp<IBinder>& expectedToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 263 | gui::Pid expectedPid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 264 | std::unique_lock lock(mLock); |
| 265 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 266 | AnrResult result; |
| 267 | ASSERT_NO_FATAL_FAILURE(result = |
| 268 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 269 | ASSERT_EQ(expectedToken, result.token); |
| 270 | ASSERT_EQ(expectedPid, result.pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 271 | } |
| 272 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 273 | /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 274 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 275 | std::unique_lock lock(mLock); |
| 276 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 277 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); |
| 278 | const auto& [token, _] = result; |
| 279 | return token; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 282 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 283 | gui::Pid expectedPid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 284 | std::unique_lock lock(mLock); |
| 285 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 286 | AnrResult result; |
| 287 | ASSERT_NO_FATAL_FAILURE( |
| 288 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 289 | ASSERT_EQ(expectedToken, result.token); |
| 290 | ASSERT_EQ(expectedPid, result.pid); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** Wrap call with ASSERT_NO_FATAL_FAILURE() to ensure the return value is valid. */ |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 294 | sp<IBinder> getResponsiveWindowToken() { |
| 295 | std::unique_lock lock(mLock); |
| 296 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 297 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 298 | const auto& [token, _] = result; |
| 299 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | void assertNotifyAnrWasNotCalled() { |
| 303 | std::scoped_lock lock(mLock); |
| 304 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 305 | ASSERT_TRUE(mAnrWindows.empty()); |
| 306 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 307 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 308 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 311 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 312 | std::unique_lock lock(mLock); |
| 313 | base::ScopedLockAssertion assumeLocked(mLock); |
| 314 | |
| 315 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 316 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 317 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 318 | enabled; |
| 319 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 320 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 321 | << ") to be called."; |
| 322 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 323 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 324 | auto request = *mPointerCaptureRequest; |
| 325 | mPointerCaptureRequest.reset(); |
| 326 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void assertSetPointerCaptureNotCalled() { |
| 330 | std::unique_lock lock(mLock); |
| 331 | base::ScopedLockAssertion assumeLocked(mLock); |
| 332 | |
| 333 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 334 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 335 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 336 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 337 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 338 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 341 | void assertDropTargetEquals(const InputDispatcherInterface& dispatcher, |
| 342 | const sp<IBinder>& targetToken) { |
| 343 | dispatcher.waitForIdle(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 344 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 345 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 346 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 347 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 348 | } |
| 349 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 350 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 351 | std::unique_lock lock(mLock); |
| 352 | base::ScopedLockAssertion assumeLocked(mLock); |
| 353 | std::optional<sp<IBinder>> receivedToken = |
| 354 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 355 | mNotifyInputChannelBroken); |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 356 | ASSERT_TRUE(receivedToken.has_value()) << "Did not receive the broken channel token"; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 357 | ASSERT_EQ(token, *receivedToken); |
| 358 | } |
| 359 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 360 | /** |
| 361 | * Set policy timeout. A value of zero means next key will not be intercepted. |
| 362 | */ |
| 363 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { |
| 364 | mInterceptKeyTimeout = timeout; |
| 365 | } |
| 366 | |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 367 | void setStaleEventTimeout(std::chrono::nanoseconds timeout) { mStaleEventTimeout = timeout; } |
| 368 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 369 | void assertUserActivityNotPoked() { |
| 370 | std::unique_lock lock(mLock); |
| 371 | base::ScopedLockAssertion assumeLocked(mLock); |
| 372 | |
| 373 | std::optional<UserActivityPokeEvent> pokeEvent = |
| 374 | getItemFromStorageLockedInterruptible(500ms, mUserActivityPokeEvents, lock, |
| 375 | mNotifyUserActivity); |
| 376 | |
| 377 | ASSERT_FALSE(pokeEvent) << "Expected user activity not to have been poked"; |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 380 | /** |
| 381 | * Asserts that a user activity poke has happened. The earliest recorded poke event will be |
| 382 | * cleared after this call. |
| 383 | * |
| 384 | * If an expected UserActivityPokeEvent is provided, asserts that the given event is the |
| 385 | * earliest recorded poke event. |
| 386 | */ |
| 387 | void assertUserActivityPoked(std::optional<UserActivityPokeEvent> expectedPokeEvent = {}) { |
| 388 | std::unique_lock lock(mLock); |
| 389 | base::ScopedLockAssertion assumeLocked(mLock); |
| 390 | |
| 391 | std::optional<UserActivityPokeEvent> pokeEvent = |
| 392 | getItemFromStorageLockedInterruptible(500ms, mUserActivityPokeEvents, lock, |
| 393 | mNotifyUserActivity); |
| 394 | ASSERT_TRUE(pokeEvent) << "Expected a user poke event"; |
| 395 | |
| 396 | if (expectedPokeEvent) { |
| 397 | ASSERT_EQ(expectedPokeEvent, *pokeEvent); |
| 398 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 401 | void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids) { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 402 | ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms)); |
| 403 | } |
| 404 | |
| 405 | void assertNotifyDeviceInteractionWasNotCalled() { |
| 406 | ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms)); |
| 407 | } |
| 408 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 409 | void setUnhandledKeyHandler(std::function<std::optional<KeyEvent>(const KeyEvent&)> handler) { |
| 410 | std::scoped_lock lock(mLock); |
| 411 | mUnhandledKeyHandler = handler; |
| 412 | } |
| 413 | |
| 414 | void assertUnhandledKeyReported(int32_t keycode) { |
| 415 | std::unique_lock lock(mLock); |
| 416 | base::ScopedLockAssertion assumeLocked(mLock); |
| 417 | std::optional<int32_t> unhandledKeycode = |
| 418 | getItemFromStorageLockedInterruptible(100ms, mReportedUnhandledKeycodes, lock, |
| 419 | mNotifyUnhandledKey); |
| 420 | ASSERT_TRUE(unhandledKeycode) << "Expected unhandled key to be reported"; |
| 421 | ASSERT_EQ(unhandledKeycode, keycode); |
| 422 | } |
| 423 | |
| 424 | void assertUnhandledKeyNotReported() { |
| 425 | std::unique_lock lock(mLock); |
| 426 | base::ScopedLockAssertion assumeLocked(mLock); |
| 427 | std::optional<int32_t> unhandledKeycode = |
| 428 | getItemFromStorageLockedInterruptible(10ms, mReportedUnhandledKeycodes, lock, |
| 429 | mNotifyUnhandledKey); |
| 430 | ASSERT_FALSE(unhandledKeycode) << "Expected unhandled key NOT to be reported"; |
| 431 | } |
| 432 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 433 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 434 | std::mutex mLock; |
| 435 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 436 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 437 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 438 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 439 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 440 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 441 | |
| 442 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 443 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 444 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 445 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 446 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 447 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 448 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 449 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 450 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 451 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 452 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 453 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 454 | |
| 455 | std::condition_variable mNotifyUserActivity; |
| 456 | std::queue<UserActivityPokeEvent> mUserActivityPokeEvents; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 457 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 458 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 459 | |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 460 | std::chrono::nanoseconds mStaleEventTimeout = 1000ms; |
| 461 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 462 | BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 463 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 464 | std::condition_variable mNotifyUnhandledKey; |
| 465 | std::queue<int32_t> mReportedUnhandledKeycodes GUARDED_BY(mLock); |
| 466 | std::function<std::optional<KeyEvent>(const KeyEvent&)> mUnhandledKeyHandler GUARDED_BY(mLock); |
| 467 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 468 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 469 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 470 | // first entry from the container and erase it. |
| 471 | template <class T> |
| 472 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 473 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 474 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 475 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 476 | // before checking if ANR was called. |
| 477 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 478 | // to provide it some time to act. 100ms seems reasonable. |
| 479 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 480 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 481 | std::optional<T> token = |
| 482 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 483 | if (!token.has_value()) { |
| 484 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 485 | return {}; |
| 486 | } |
| 487 | |
| 488 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 489 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 490 | // the dispatcher started counting before this function was called |
| 491 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 492 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 493 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 494 | << "ms, but waited " |
| 495 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 496 | << "ms instead"; |
| 497 | } |
| 498 | return *token; |
| 499 | } |
| 500 | |
| 501 | template <class T> |
| 502 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 503 | std::queue<T>& storage, |
| 504 | std::unique_lock<std::mutex>& lock, |
| 505 | std::condition_variable& condition) |
| 506 | REQUIRES(mLock) { |
| 507 | condition.wait_for(lock, timeout, |
| 508 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 509 | if (storage.empty()) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 510 | return std::nullopt; |
| 511 | } |
| 512 | T item = storage.front(); |
| 513 | storage.pop(); |
| 514 | return std::make_optional(item); |
| 515 | } |
| 516 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 517 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 518 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 519 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 522 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<gui::Pid> pid, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 523 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 524 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 525 | ASSERT_TRUE(pid.has_value()); |
| 526 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 527 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 528 | } |
| 529 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 530 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 531 | std::optional<gui::Pid> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 532 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 533 | ASSERT_TRUE(pid.has_value()); |
| 534 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 535 | mNotifyAnr.notify_all(); |
| 536 | } |
| 537 | |
| 538 | void notifyNoFocusedWindowAnr( |
| 539 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 540 | std::scoped_lock lock(mLock); |
| 541 | mAnrApplications.push(applicationHandle); |
| 542 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 543 | } |
| 544 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 545 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 546 | std::scoped_lock lock(mLock); |
| 547 | mBrokenInputChannels.push(connectionToken); |
| 548 | mNotifyInputChannelBroken.notify_all(); |
| 549 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 550 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 551 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 552 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 553 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 554 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 555 | const std::vector<float>& values) override {} |
| 556 | |
| 557 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 558 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 559 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 560 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 561 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 562 | bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 563 | std::scoped_lock lock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 564 | switch (inputEvent.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 565 | case InputEventType::KEY: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 566 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent); |
| 567 | mFilteredEvent = std::make_unique<KeyEvent>(keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 568 | break; |
| 569 | } |
| 570 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 571 | case InputEventType::MOTION: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 572 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent); |
| 573 | mFilteredEvent = std::make_unique<MotionEvent>(motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 574 | break; |
| 575 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 576 | default: { |
| 577 | ADD_FAILURE() << "Should only filter keys or motions"; |
| 578 | break; |
| 579 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 580 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 581 | return true; |
| 582 | } |
| 583 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 584 | void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override { |
| 585 | if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 586 | // Clear intercept state when we handled the event. |
| 587 | mInterceptKeyTimeout = 0ms; |
| 588 | } |
| 589 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 590 | |
Yeabkal Wubshit | 88a9041 | 2023-12-21 18:23:04 -0800 | [diff] [blame] | 591 | void interceptMotionBeforeQueueing(int32_t, uint32_t, int32_t, nsecs_t, uint32_t&) override {} |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 592 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 593 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 594 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 595 | // Clear intercept state so we could dispatch the event in next wake. |
| 596 | mInterceptKeyTimeout = 0ms; |
| 597 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 598 | } |
| 599 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 600 | std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent& event, |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 601 | uint32_t) override { |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 602 | std::scoped_lock lock(mLock); |
| 603 | mReportedUnhandledKeycodes.emplace(event.getKeyCode()); |
| 604 | mNotifyUnhandledKey.notify_all(); |
| 605 | return mUnhandledKeyHandler != nullptr ? mUnhandledKeyHandler(event) : std::nullopt; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 606 | } |
| 607 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 608 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 609 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 610 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 611 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 612 | * essentially a passthrough for notifySwitch. |
| 613 | */ |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 614 | mLastNotifySwitch = |
| 615 | NotifySwitchArgs(InputEvent::nextId(), when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 616 | } |
| 617 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 618 | void pokeUserActivity(nsecs_t eventTime, int32_t eventType, int32_t displayId) override { |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 619 | std::scoped_lock lock(mLock); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 620 | mNotifyUserActivity.notify_all(); |
| 621 | mUserActivityPokeEvents.push({eventTime, eventType, displayId}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 622 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 623 | |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 624 | bool isStaleEvent(nsecs_t currentTime, nsecs_t eventTime) override { |
| 625 | return std::chrono::nanoseconds(currentTime - eventTime) >= mStaleEventTimeout; |
| 626 | } |
| 627 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 628 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 629 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 630 | mOnPointerDownToken = newToken; |
| 631 | } |
| 632 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 633 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 634 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 635 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 636 | mPointerCaptureChangedCondition.notify_all(); |
| 637 | } |
| 638 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 639 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 640 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 641 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 642 | mDropTargetWindowToken = token; |
| 643 | } |
| 644 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 645 | void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 646 | const std::set<gui::Uid>& uids) override { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 647 | ASSERT_TRUE(mNotifiedInteractions.emplace(deviceId, uids)); |
| 648 | } |
| 649 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 650 | void assertFilterInputEventWasCalledInternal( |
| 651 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 652 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 653 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 654 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 655 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 656 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 657 | }; |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 658 | } // namespace |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 659 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 660 | // --- InputDispatcherTest --- |
| 661 | |
Prabir Pradhan | d3fb627 | 2023-12-15 01:21:47 +0000 | [diff] [blame] | 662 | // The trace is a global variable for now, to avoid having to pass it into all of the |
| 663 | // FakeWindowHandles created throughout the tests. |
| 664 | // TODO(b/210460522): Update the tests to avoid the need to have the trace be a global variable. |
| 665 | static std::shared_ptr<VerifyingTrace> gVerifyingTrace = std::make_shared<VerifyingTrace>(); |
| 666 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 667 | class InputDispatcherTest : public testing::Test { |
| 668 | protected: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 669 | std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 670 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 671 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 672 | void SetUp() override { |
Prabir Pradhan | d3fb627 | 2023-12-15 01:21:47 +0000 | [diff] [blame] | 673 | gVerifyingTrace->reset(); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 674 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
Prabir Pradhan | d3fb627 | 2023-12-15 01:21:47 +0000 | [diff] [blame] | 675 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, |
| 676 | std::make_unique<FakeInputTracingBackend>( |
| 677 | gVerifyingTrace)); |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 678 | |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 679 | mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 680 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 681 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 684 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 685 | ASSERT_EQ(OK, mDispatcher->stop()); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 686 | mFakePolicy.reset(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 687 | mDispatcher.reset(); |
Prabir Pradhan | d3fb627 | 2023-12-15 01:21:47 +0000 | [diff] [blame] | 688 | ASSERT_NO_FATAL_FAILURE(gVerifyingTrace->verifyExpectedEventsTraced()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 689 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 690 | |
| 691 | /** |
| 692 | * Used for debugging when writing the test |
| 693 | */ |
| 694 | void dumpDispatcherState() { |
| 695 | std::string dump; |
| 696 | mDispatcher->dump(dump); |
| 697 | std::stringstream ss(dump); |
| 698 | std::string to; |
| 699 | |
| 700 | while (std::getline(ss, to, '\n')) { |
| 701 | ALOGE("%s", to.c_str()); |
| 702 | } |
| 703 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 704 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 705 | void setFocusedWindow(const sp<WindowInfoHandle>& window) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 706 | FocusRequest request; |
| 707 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 708 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 709 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 710 | request.displayId = window->getInfo()->displayId; |
| 711 | mDispatcher->setFocusedWindow(request); |
| 712 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 713 | }; |
| 714 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 715 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 716 | KeyEvent event; |
| 717 | |
| 718 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 719 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 720 | INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 721 | /*action=*/-1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 722 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 723 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 724 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 725 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 726 | << "Should reject key events with undefined action."; |
| 727 | |
| 728 | // 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] | 729 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 730 | 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] | 731 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 732 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 733 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 734 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 735 | << "Should reject key events with ACTION_MULTIPLE."; |
| 736 | } |
| 737 | |
| 738 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 739 | MotionEvent event; |
| 740 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 741 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 742 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 743 | pointerProperties[i].clear(); |
| 744 | pointerProperties[i].id = i; |
| 745 | pointerCoords[i].clear(); |
| 746 | } |
| 747 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 748 | // Some constants commonly used below |
| 749 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 750 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 751 | constexpr int32_t metaState = AMETA_NONE; |
| 752 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 753 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 754 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 755 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 756 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 757 | /*action=*/-1, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 758 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 759 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 760 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 761 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 762 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 763 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 764 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 765 | << "Should reject motion events with undefined action."; |
| 766 | |
| 767 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 768 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 769 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 770 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 771 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 772 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 773 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 774 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 775 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 776 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 777 | << "Should reject motion events with pointer down index too large."; |
| 778 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 779 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 780 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 781 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 782 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 783 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 784 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 785 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 786 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 787 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 788 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 789 | << "Should reject motion events with pointer down index too small."; |
| 790 | |
| 791 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 792 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 793 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 794 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 795 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 796 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 797 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 798 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 799 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 800 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 801 | << "Should reject motion events with pointer up index too large."; |
| 802 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 803 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 804 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 805 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 806 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 807 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 808 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 809 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 810 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 811 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 812 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 813 | << "Should reject motion events with pointer up index too small."; |
| 814 | |
| 815 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 816 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 817 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 818 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 819 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 820 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 821 | /*pointerCount=*/0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 822 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 823 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 824 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 825 | << "Should reject motion events with 0 pointers."; |
| 826 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 827 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 828 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 829 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 830 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 831 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 832 | /*pointerCount=*/MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 833 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 834 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 835 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 836 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 837 | |
| 838 | // Rejects motion events with invalid pointer ids. |
| 839 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 840 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 841 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 842 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 843 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 844 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 845 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 846 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 847 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 848 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 849 | << "Should reject motion events with pointer ids less than 0."; |
| 850 | |
| 851 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 852 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 853 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 854 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 855 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 856 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 857 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 858 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 859 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 860 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 861 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 862 | |
| 863 | // Rejects motion events with duplicate pointer ids. |
| 864 | pointerProperties[0].id = 1; |
| 865 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 866 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 867 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 868 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 869 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 870 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 871 | /*pointerCount=*/2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 872 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 873 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 874 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 875 | << "Should reject motion events with duplicate pointer ids."; |
| 876 | } |
| 877 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 878 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 879 | |
| 880 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 881 | constexpr nsecs_t eventTime = 20; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 882 | mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 883 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 884 | |
| 885 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 886 | } |
| 887 | |
| 888 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 889 | NotifySwitchArgs args(InputEvent::nextId(), /*eventTime=*/20, /*policyFlags=*/0, |
| 890 | /*switchValues=*/1, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 891 | /*switchMask=*/2); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 892 | mDispatcher->notifySwitch(args); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 893 | |
| 894 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 895 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 896 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 897 | } |
| 898 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 899 | namespace { |
| 900 | |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 901 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 902 | // Default input dispatching timeout if there is no focused application or paused window |
| 903 | // from which to determine an appropriate dispatching timeout. |
| 904 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 905 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 906 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 907 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 908 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 909 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 910 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 911 | : mConsumer(std::move(clientChannel)), mName(name) {} |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 912 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 913 | std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = false) { |
| 914 | auto [consumeSeq, event] = receiveEvent(timeout); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 915 | if (!consumeSeq) { |
| 916 | return nullptr; |
| 917 | } |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 918 | finishEvent(*consumeSeq, handled); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 919 | return std::move(event); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | /** |
| 923 | * Receive an event without acknowledging it. |
| 924 | * Return the sequence number that could later be used to send finished signal. |
| 925 | */ |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 926 | std::pair<std::optional<uint32_t>, std::unique_ptr<InputEvent>> receiveEvent( |
| 927 | std::chrono::milliseconds timeout) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 928 | uint32_t consumeSeq; |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 929 | std::unique_ptr<InputEvent> event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 930 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 931 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 932 | status_t status = WOULD_BLOCK; |
| 933 | while (status == WOULD_BLOCK) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 934 | InputEvent* rawEventPtr = nullptr; |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 935 | status = mConsumer.consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 936 | &rawEventPtr); |
| 937 | event = std::unique_ptr<InputEvent>(rawEventPtr); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 938 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 939 | if (elapsed > timeout) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 940 | break; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | if (status == WOULD_BLOCK) { |
| 945 | // Just means there's no event available. |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 946 | return std::make_pair(std::nullopt, nullptr); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | if (status != OK) { |
| 950 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 951 | return std::make_pair(std::nullopt, nullptr); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 952 | } |
| 953 | if (event == nullptr) { |
| 954 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 955 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 956 | return std::make_pair(consumeSeq, std::move(event)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 957 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 958 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 959 | /** |
| 960 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 961 | */ |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 962 | void finishEvent(uint32_t consumeSeq, bool handled = true) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 963 | const status_t status = mConsumer.sendFinishedSignal(consumeSeq, handled); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 964 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 965 | } |
| 966 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 967 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 968 | const status_t status = mConsumer.sendTimeline(inputEventId, timeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 969 | ASSERT_EQ(OK, status); |
| 970 | } |
| 971 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 972 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 973 | std::optional<int32_t> expectedDisplayId, |
| 974 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 975 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 976 | |
| 977 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 978 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 979 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 980 | << mName.c_str() << " expected " << ftl::enum_string(expectedEventType) |
| 981 | << " event, got " << *event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 982 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 983 | if (expectedDisplayId.has_value()) { |
| 984 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 985 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 986 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 987 | switch (expectedEventType) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 988 | case InputEventType::KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 989 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 990 | ASSERT_THAT(keyEvent, WithKeyAction(expectedAction)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 991 | if (expectedFlags.has_value()) { |
| 992 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 993 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 994 | break; |
| 995 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 996 | case InputEventType::MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 997 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 998 | ASSERT_THAT(motionEvent, WithMotionAction(expectedAction)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 999 | if (expectedFlags.has_value()) { |
| 1000 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 1001 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 1002 | break; |
| 1003 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1004 | case InputEventType::FOCUS: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1005 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 1006 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1007 | case InputEventType::CAPTURE: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1008 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 1009 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1010 | case InputEventType::TOUCH_MODE: { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1011 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 1012 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1013 | case InputEventType::DRAG: { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1014 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 1015 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 1016 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1017 | } |
| 1018 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1019 | std::unique_ptr<MotionEvent> consumeMotion() { |
| 1020 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1021 | |
| 1022 | if (event == nullptr) { |
| 1023 | ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one."; |
| 1024 | return nullptr; |
| 1025 | } |
| 1026 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1027 | if (event->getType() != InputEventType::MOTION) { |
| 1028 | ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1029 | return nullptr; |
| 1030 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1031 | return std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event.release())); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1035 | std::unique_ptr<MotionEvent> motionEvent = consumeMotion(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1036 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; |
| 1037 | ASSERT_THAT(*motionEvent, matcher); |
| 1038 | } |
| 1039 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1040 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1041 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1042 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1043 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1044 | ASSERT_EQ(InputEventType::FOCUS, event->getType()) |
| 1045 | << "Instead of FocusEvent, got " << *event; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1046 | |
| 1047 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1048 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1049 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1050 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1051 | EXPECT_EQ(hasFocus, focusEvent.getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1052 | } |
| 1053 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1054 | void consumeCaptureEvent(bool hasCapture) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1055 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1056 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1057 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1058 | ASSERT_EQ(InputEventType::CAPTURE, event->getType()) |
| 1059 | << "Instead of CaptureEvent, got " << *event; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1060 | |
| 1061 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1062 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1063 | |
| 1064 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 1065 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 1066 | } |
| 1067 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1068 | void consumeDragEvent(bool isExiting, float x, float y) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1069 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1070 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1071 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1072 | ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1073 | |
| 1074 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1075 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1076 | |
| 1077 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 1078 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 1079 | EXPECT_EQ(x, dragEvent.getX()); |
| 1080 | EXPECT_EQ(y, dragEvent.getY()); |
| 1081 | } |
| 1082 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1083 | void consumeTouchModeEvent(bool inTouchMode) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1084 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1085 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1086 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1087 | ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType()) |
| 1088 | << "Instead of TouchModeEvent, got " << *event; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1089 | |
| 1090 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1091 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1092 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 1093 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 1094 | } |
| 1095 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1096 | void assertNoEvents() { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1097 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_NO_EVENT_EXPECTED); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1098 | if (event == nullptr) { |
| 1099 | return; |
| 1100 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1101 | if (event->getType() == InputEventType::KEY) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1102 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1103 | ADD_FAILURE() << "Received key event " << keyEvent; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1104 | } else if (event->getType() == InputEventType::MOTION) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1105 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1106 | ADD_FAILURE() << "Received motion event " << motionEvent; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1107 | } else if (event->getType() == InputEventType::FOCUS) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1108 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1109 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 1110 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1111 | } else if (event->getType() == InputEventType::CAPTURE) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1112 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 1113 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 1114 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1115 | } else if (event->getType() == InputEventType::TOUCH_MODE) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1116 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 1117 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 1118 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1119 | } |
| 1120 | FAIL() << mName.c_str() |
| 1121 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1124 | sp<IBinder> getToken() { return mConsumer.getChannel()->getConnectionToken(); } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1125 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 1126 | int getChannelFd() { return mConsumer.getChannel()->getFd(); } |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1127 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1128 | private: |
| 1129 | InputConsumer mConsumer; |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1130 | DynamicInputEventFactory mEventFactory; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1131 | |
| 1132 | std::string mName; |
| 1133 | }; |
| 1134 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1135 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1136 | public: |
| 1137 | static const int32_t WIDTH = 600; |
| 1138 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1139 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1140 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1141 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1142 | int32_t displayId, bool createInputChannel = true) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1143 | : mName(name) { |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1144 | sp<IBinder> token; |
| 1145 | if (createInputChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1146 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1147 | dispatcher->createInputChannel(name); |
| 1148 | token = (*channel)->getConnectionToken(); |
| 1149 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | inputApplicationHandle->updateInfo(); |
| 1153 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1154 | |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1155 | mInfo.token = token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1156 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1157 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1158 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1159 | mInfo.alpha = 1.0; |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 1160 | mInfo.frame = Rect(0, 0, WIDTH, HEIGHT); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1161 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1162 | mInfo.globalScaleFactor = 1.0; |
| 1163 | mInfo.touchableRegion.clear(); |
| 1164 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1165 | mInfo.ownerPid = WINDOW_PID; |
| 1166 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1167 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1168 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1169 | } |
| 1170 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1171 | sp<FakeWindowHandle> clone(int32_t displayId) { |
| 1172 | sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)"); |
| 1173 | handle->mInfo = mInfo; |
| 1174 | handle->mInfo.displayId = displayId; |
| 1175 | handle->mInfo.id = sId++; |
| 1176 | handle->mInputReceiver = mInputReceiver; |
| 1177 | return handle; |
| 1178 | } |
| 1179 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1180 | void setTouchable(bool touchable) { |
| 1181 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1182 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1183 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1184 | void setFocusable(bool focusable) { |
| 1185 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1186 | } |
| 1187 | |
| 1188 | void setVisible(bool visible) { |
| 1189 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1190 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1191 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1192 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1193 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1196 | void setPaused(bool paused) { |
| 1197 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1198 | } |
| 1199 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1200 | void setPreventSplitting(bool preventSplitting) { |
| 1201 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | void setSlippery(bool slippery) { |
| 1205 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1206 | } |
| 1207 | |
| 1208 | void setWatchOutsideTouch(bool watchOutside) { |
| 1209 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1210 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1211 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1212 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1213 | |
| 1214 | void setInterceptsStylus(bool interceptsStylus) { |
| 1215 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1216 | } |
| 1217 | |
| 1218 | void setDropInput(bool dropInput) { |
| 1219 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1220 | } |
| 1221 | |
| 1222 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1223 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1224 | } |
| 1225 | |
| 1226 | void setNoInputChannel(bool noInputChannel) { |
| 1227 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1228 | } |
| 1229 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1230 | void setDisableUserActivity(bool disableUserActivity) { |
| 1231 | mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity); |
| 1232 | } |
| 1233 | |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 1234 | void setGlobalStylusBlocksTouch(bool shouldGlobalStylusBlockTouch) { |
| 1235 | mInfo.setInputConfig(WindowInfo::InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH, |
| 1236 | shouldGlobalStylusBlockTouch); |
| 1237 | } |
| 1238 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1239 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1240 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1241 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1242 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1243 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1244 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1245 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 1246 | mInfo.frame = frame; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1247 | mInfo.touchableRegion.clear(); |
| 1248 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1249 | |
| 1250 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1251 | ui::Transform translate; |
| 1252 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1253 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1254 | } |
| 1255 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1256 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1257 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1258 | void setIsWallpaper(bool isWallpaper) { |
| 1259 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1260 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1261 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1262 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1263 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1264 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1265 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1266 | void setTrustedOverlay(bool trustedOverlay) { |
| 1267 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1268 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1269 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1270 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1271 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1272 | } |
| 1273 | |
| 1274 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1275 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1276 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1277 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1278 | std::unique_ptr<KeyEvent> consumeKey(bool handled = true) { |
| 1279 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED, handled); |
| 1280 | if (event == nullptr) { |
| 1281 | ADD_FAILURE() << "No event"; |
| 1282 | return nullptr; |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1283 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1284 | if (event->getType() != InputEventType::KEY) { |
| 1285 | ADD_FAILURE() << "Instead of key event, got " << event; |
| 1286 | return nullptr; |
| 1287 | } |
| 1288 | return std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event.release())); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | void consumeKeyEvent(const ::testing::Matcher<KeyEvent>& matcher) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1292 | std::unique_ptr<KeyEvent> keyEvent = consumeKey(); |
| 1293 | ASSERT_NE(nullptr, keyEvent); |
| 1294 | ASSERT_THAT(*keyEvent, matcher); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1297 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1298 | consumeKeyEvent(AllOf(WithKeyAction(ACTION_DOWN), WithDisplayId(expectedDisplayId), |
| 1299 | WithFlags(expectedFlags))); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1300 | } |
| 1301 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1302 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1303 | consumeKeyEvent(AllOf(WithKeyAction(ACTION_UP), WithDisplayId(expectedDisplayId), |
| 1304 | WithFlags(expectedFlags))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1307 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1308 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1309 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId), |
| 1310 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1314 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1315 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 1316 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1320 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1321 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1322 | } |
| 1323 | |
| 1324 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1325 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1326 | consumeMotionEvent( |
| 1327 | AllOf(WithMotionAction(ACTION_DOWN), |
| 1328 | testing::Conditional(expectedDisplayId.has_value(), |
| 1329 | WithDisplayId(*expectedDisplayId), testing::_), |
| 1330 | testing::Conditional(expectedFlags.has_value(), WithFlags(*expectedFlags), |
| 1331 | testing::_))); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1332 | } |
| 1333 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1334 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1335 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1336 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1337 | const int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1338 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1339 | consumeMotionEvent(AllOf(WithMotionAction(action), WithDisplayId(expectedDisplayId), |
| 1340 | WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1344 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1345 | const int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1346 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1347 | consumeMotionEvent(AllOf(WithMotionAction(action), WithDisplayId(expectedDisplayId), |
| 1348 | WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1352 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1353 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDisplayId(expectedDisplayId), |
| 1354 | WithFlags(expectedFlags))); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1357 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1358 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1359 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE), |
| 1360 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1361 | } |
| 1362 | |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1363 | void consumeMotionOutsideWithZeroedCoords() { |
| 1364 | consumeMotionEvent( |
| 1365 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE), WithRawCoords(0, 0))); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1366 | } |
| 1367 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1368 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1369 | ASSERT_NE(mInputReceiver, nullptr) |
| 1370 | << "Cannot consume events from a window with no receiver"; |
| 1371 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1372 | } |
| 1373 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1374 | void consumeCaptureEvent(bool hasCapture) { |
| 1375 | ASSERT_NE(mInputReceiver, nullptr) |
| 1376 | << "Cannot consume events from a window with no receiver"; |
| 1377 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1378 | } |
| 1379 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1380 | std::unique_ptr<MotionEvent> consumeMotionEvent( |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1381 | const ::testing::Matcher<MotionEvent>& matcher = testing::_) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1382 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
| 1383 | if (event == nullptr) { |
| 1384 | ADD_FAILURE() << "No event"; |
| 1385 | return nullptr; |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 1386 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1387 | if (event->getType() != InputEventType::MOTION) { |
| 1388 | ADD_FAILURE() << "Instead of motion event, got " << *event; |
| 1389 | return nullptr; |
| 1390 | } |
| 1391 | std::unique_ptr<MotionEvent> motionEvent = |
| 1392 | std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event.release())); |
| 1393 | EXPECT_THAT(*motionEvent, matcher); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1394 | return motionEvent; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1395 | } |
| 1396 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1397 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1398 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1399 | } |
| 1400 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1401 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1402 | ASSERT_NE(mInputReceiver, nullptr) |
| 1403 | << "Cannot consume events from a window with no receiver"; |
| 1404 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1405 | } |
| 1406 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1407 | std::pair<std::optional<uint32_t>, std::unique_ptr<InputEvent>> receiveEvent() { |
Prabir Pradhan | d3fb627 | 2023-12-15 01:21:47 +0000 | [diff] [blame] | 1408 | return receive(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | void finishEvent(uint32_t sequenceNum) { |
| 1412 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1413 | mInputReceiver->finishEvent(sequenceNum); |
| 1414 | } |
| 1415 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1416 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1417 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1418 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1419 | } |
| 1420 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1421 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1422 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1423 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1424 | return; // Can't receive events if the window does not have input channel |
| 1425 | } |
| 1426 | ASSERT_NE(nullptr, mInputReceiver) |
| 1427 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1428 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1429 | } |
| 1430 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1431 | sp<IBinder> getToken() { return mInfo.token; } |
| 1432 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1433 | const std::string& getName() { return mName; } |
| 1434 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 1435 | void setOwnerInfo(gui::Pid ownerPid, gui::Uid ownerUid) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1436 | mInfo.ownerPid = ownerPid; |
| 1437 | mInfo.ownerUid = ownerUid; |
| 1438 | } |
| 1439 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 1440 | gui::Pid getPid() const { return mInfo.ownerPid; } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1441 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1442 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1443 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1444 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1445 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1446 | private: |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1447 | FakeWindowHandle(std::string name) : mName(name){}; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1448 | const std::string mName; |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1449 | std::shared_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1450 | static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1451 | friend class sp<FakeWindowHandle>; |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1452 | |
Prabir Pradhan | d3fb627 | 2023-12-15 01:21:47 +0000 | [diff] [blame] | 1453 | // 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] | 1454 | std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = true) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1455 | if (mInputReceiver == nullptr) { |
| 1456 | LOG(FATAL) << "Cannot consume event from a window with no input event receiver"; |
| 1457 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1458 | std::unique_ptr<InputEvent> event = mInputReceiver->consume(timeout, handled); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1459 | if (event == nullptr) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1460 | ADD_FAILURE() << "Consume failed: no event"; |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1461 | } |
Prabir Pradhan | d3fb627 | 2023-12-15 01:21:47 +0000 | [diff] [blame] | 1462 | expectReceivedEventTraced(event); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1463 | return event; |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1464 | } |
Prabir Pradhan | d3fb627 | 2023-12-15 01:21:47 +0000 | [diff] [blame] | 1465 | |
| 1466 | // FakeWindowHandle uses this receive method to ensure received events are added to the trace. |
| 1467 | std::pair<std::optional<uint32_t /*seq*/>, std::unique_ptr<InputEvent>> receive() { |
| 1468 | if (mInputReceiver == nullptr) { |
| 1469 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1470 | return std::make_pair(std::nullopt, nullptr); |
| 1471 | } |
| 1472 | auto out = mInputReceiver->receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED); |
| 1473 | const auto& [_, event] = out; |
| 1474 | expectReceivedEventTraced(event); |
| 1475 | return std::move(out); |
| 1476 | } |
| 1477 | |
| 1478 | void expectReceivedEventTraced(const std::unique_ptr<InputEvent>& event) { |
| 1479 | if (!event) { |
| 1480 | return; |
| 1481 | } |
| 1482 | |
| 1483 | switch (event->getType()) { |
| 1484 | case InputEventType::KEY: { |
| 1485 | gVerifyingTrace->expectKeyDispatchTraced(static_cast<KeyEvent&>(*event)); |
| 1486 | break; |
| 1487 | } |
| 1488 | case InputEventType::MOTION: { |
| 1489 | gVerifyingTrace->expectMotionDispatchTraced(static_cast<MotionEvent&>(*event)); |
| 1490 | break; |
| 1491 | } |
| 1492 | default: |
| 1493 | break; |
| 1494 | } |
| 1495 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1496 | }; |
| 1497 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1498 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1499 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1500 | class FakeMonitorReceiver { |
| 1501 | public: |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1502 | FakeMonitorReceiver(InputDispatcher& dispatcher, const std::string name, int32_t displayId) |
| 1503 | : mInputReceiver(*dispatcher.createInputMonitor(displayId, name, MONITOR_PID), name) {} |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1504 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1505 | sp<IBinder> getToken() { return mInputReceiver.getToken(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1506 | |
| 1507 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1508 | mInputReceiver.consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1509 | expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | std::optional<int32_t> receiveEvent() { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1513 | const auto [sequenceNum, _] = mInputReceiver.receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED); |
| 1514 | return sequenceNum; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1517 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver.finishEvent(consumeSeq); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1518 | |
| 1519 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1520 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 1521 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1525 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 1526 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1530 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, |
| 1531 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1535 | mInputReceiver.consumeMotionEvent( |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1536 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 1537 | WithDisplayId(expectedDisplayId), |
| 1538 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
| 1539 | } |
| 1540 | |
| 1541 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 1542 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1543 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1544 | mInputReceiver.consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT, |
| 1545 | /*expectedFlags=*/0); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1549 | mInputReceiver.consumeMotionEvent(matcher); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1552 | std::unique_ptr<MotionEvent> consumeMotion() { return mInputReceiver.consumeMotion(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1553 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1554 | void assertNoEvents() { mInputReceiver.assertNoEvents(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1555 | |
| 1556 | private: |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1557 | FakeInputReceiver mInputReceiver; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1558 | }; |
| 1559 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1560 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1561 | InputDispatcher& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1562 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1563 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1564 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1565 | bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1566 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1567 | KeyEvent event; |
| 1568 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1569 | |
| 1570 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1571 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1572 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 1573 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1574 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1575 | if (!allowKeyRepeat) { |
| 1576 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1577 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1578 | // Inject event until dispatch out. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1579 | return dispatcher.injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1580 | } |
| 1581 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1582 | static void assertInjectedKeyTimesOut(InputDispatcher& dispatcher) { |
| 1583 | InputEventInjectionResult result = |
| 1584 | injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
| 1585 | InputEventInjectionSync::WAIT_FOR_RESULT, CONSUME_TIMEOUT_NO_EVENT_EXPECTED); |
| 1586 | if (result != InputEventInjectionResult::TIMED_OUT) { |
| 1587 | FAIL() << "Injection should have timed out, but got " << ftl::enum_string(result); |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | static InputEventInjectionResult injectKeyDown(InputDispatcher& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1592 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1593 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1594 | } |
| 1595 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1596 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1597 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1598 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1599 | static InputEventInjectionResult injectKeyDownNoRepeat(InputDispatcher& dispatcher, |
| 1600 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1601 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1602 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1603 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1604 | } |
| 1605 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1606 | static InputEventInjectionResult injectKeyUp(InputDispatcher& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1607 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1608 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1611 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1612 | InputDispatcher& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1613 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1614 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1615 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1616 | return dispatcher.injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1617 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1618 | } |
| 1619 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1620 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1621 | InputDispatcher& dispatcher, int32_t action, int32_t source, int32_t displayId, |
| 1622 | const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1623 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1624 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1625 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1626 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1627 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1628 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1629 | MotionEventBuilder motionBuilder = |
| 1630 | MotionEventBuilder(action, source) |
| 1631 | .displayId(displayId) |
| 1632 | .eventTime(eventTime) |
| 1633 | .rawXCursorPosition(cursorPosition.x) |
| 1634 | .rawYCursorPosition(cursorPosition.y) |
| 1635 | .pointer( |
| 1636 | PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y)); |
| 1637 | if (MotionEvent::getActionMasked(action) == ACTION_DOWN) { |
| 1638 | motionBuilder.downTime(eventTime); |
| 1639 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1640 | |
| 1641 | // Inject event until dispatch out. |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1642 | return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode, |
| 1643 | targetUid, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1644 | } |
| 1645 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1646 | static InputEventInjectionResult injectMotionDown(InputDispatcher& dispatcher, int32_t source, |
| 1647 | int32_t displayId, |
| 1648 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1649 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1650 | } |
| 1651 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1652 | static InputEventInjectionResult injectMotionUp(InputDispatcher& dispatcher, int32_t source, |
| 1653 | int32_t displayId, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1654 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1655 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1658 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1659 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1660 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1661 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 1662 | AINPUT_SOURCE_KEYBOARD, displayId, POLICY_FLAG_PASS_TO_USER, action, |
| 1663 | /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1664 | |
| 1665 | return args; |
| 1666 | } |
| 1667 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1668 | static NotifyKeyArgs generateSystemShortcutArgs(int32_t action, |
| 1669 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1670 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1671 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1672 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 1673 | AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_C, KEY_C, |
| 1674 | AMETA_META_ON, currentTime); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1675 | |
| 1676 | return args; |
| 1677 | } |
| 1678 | |
| 1679 | static NotifyKeyArgs generateAssistantKeyArgs(int32_t action, |
| 1680 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1681 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1682 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1683 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 1684 | AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_ASSIST, |
| 1685 | KEY_ASSISTANT, AMETA_NONE, currentTime); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1686 | |
| 1687 | return args; |
| 1688 | } |
| 1689 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1690 | [[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
| 1691 | int32_t displayId, |
| 1692 | const std::vector<PointF>& points) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1693 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1694 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1695 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1696 | } |
| 1697 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1698 | PointerProperties pointerProperties[pointerCount]; |
| 1699 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1700 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1701 | for (size_t i = 0; i < pointerCount; i++) { |
| 1702 | pointerProperties[i].clear(); |
| 1703 | pointerProperties[i].id = i; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1704 | pointerProperties[i].toolType = ToolType::FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1705 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1706 | pointerCoords[i].clear(); |
| 1707 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1708 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1709 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1710 | |
| 1711 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1712 | // Define a valid motion event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1713 | NotifyMotionArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, source, |
| 1714 | displayId, POLICY_FLAG_PASS_TO_USER, action, /*actionButton=*/0, |
| 1715 | /*flags=*/0, AMETA_NONE, /*buttonState=*/0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1716 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1717 | pointerCoords, /*xPrecision=*/0, /*yPrecision=*/0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1718 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1719 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /*videoFrames=*/{}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1720 | |
| 1721 | return args; |
| 1722 | } |
| 1723 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1724 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1725 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1726 | } |
| 1727 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1728 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1729 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1730 | } |
| 1731 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1732 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1733 | const PointerCaptureRequest& request) { |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1734 | return NotifyPointerCaptureChangedArgs(InputEvent::nextId(), systemTime(SYSTEM_TIME_MONOTONIC), |
| 1735 | request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1736 | } |
| 1737 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 1738 | } // namespace |
| 1739 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1740 | /** |
| 1741 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1742 | * broken channel. |
| 1743 | */ |
| 1744 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1745 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1746 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1747 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1748 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1749 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1750 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1751 | |
| 1752 | // Window closes its channel, but the window remains. |
| 1753 | window->destroyReceiver(); |
| 1754 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1755 | } |
| 1756 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1757 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1758 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1759 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1760 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1761 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1762 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1763 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1764 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1765 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1766 | |
| 1767 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1768 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1769 | } |
| 1770 | |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 1771 | using InputDispatcherDeathTest = InputDispatcherTest; |
| 1772 | |
| 1773 | /** |
| 1774 | * When 'onWindowInfosChanged' arguments contain a duplicate entry for the same window, dispatcher |
| 1775 | * should crash. |
| 1776 | */ |
| 1777 | TEST_F(InputDispatcherDeathTest, DuplicateWindowInfosAbortDispatcher) { |
| 1778 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
| 1779 | ScopedSilentDeath _silentDeath; |
| 1780 | |
| 1781 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1782 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1783 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1784 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 1785 | {{*window->getInfo(), *window->getInfo()}, {}, 0, 0}), |
| 1786 | "Incorrect WindowInfosUpdate provided"); |
| 1787 | } |
| 1788 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1789 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1790 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1791 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1792 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1793 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1794 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1795 | // Inject a MotionEvent to an unknown display. |
| 1796 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1797 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1798 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1799 | |
| 1800 | // Window should receive motion event. |
| 1801 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1802 | } |
| 1803 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1804 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1805 | * Calling onWindowInfosChanged once should not cause any issues. |
| 1806 | * 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] | 1807 | * called twice. |
| 1808 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1809 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1810 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1811 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1812 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1813 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1814 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1815 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1816 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1817 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1818 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1819 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1820 | |
| 1821 | // Window should receive motion event. |
| 1822 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1823 | } |
| 1824 | |
| 1825 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1826 | * Calling onWindowInfosChanged twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1827 | */ |
| 1828 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1829 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1830 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1831 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1832 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1833 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1834 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1835 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1836 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1837 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1838 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1839 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1840 | |
| 1841 | // Window should receive motion event. |
| 1842 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1843 | } |
| 1844 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1845 | // The foreground window should receive the first touch down event. |
| 1846 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1847 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1848 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1849 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1850 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1851 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1852 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1853 | mDispatcher->onWindowInfosChanged( |
| 1854 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1855 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1856 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1857 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1858 | |
| 1859 | // 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] | 1860 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1861 | windowSecond->assertNoEvents(); |
| 1862 | } |
| 1863 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1864 | /** |
| 1865 | * Two windows: A top window, and a wallpaper behind the window. |
| 1866 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1867 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1868 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1869 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1870 | */ |
| 1871 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1872 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1873 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1874 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1875 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1876 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1877 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1878 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1879 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1880 | mDispatcher->onWindowInfosChanged( |
| 1881 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1882 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1883 | injectMotionEvent(*mDispatcher, |
| 1884 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1885 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(200)) |
| 1886 | .build())) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1887 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1888 | |
| 1889 | // Both foreground window and its wallpaper should receive the touch down |
| 1890 | foregroundWindow->consumeMotionDown(); |
| 1891 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1892 | |
| 1893 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1894 | injectMotionEvent(*mDispatcher, |
| 1895 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1896 | .pointer(PointerBuilder(0, ToolType::FINGER).x(110).y(200)) |
| 1897 | .build())) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1898 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1899 | |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1900 | foregroundWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1901 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1902 | |
| 1903 | // Now the foreground window goes away, but the wallpaper stays |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1904 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1905 | foregroundWindow->consumeMotionCancel(); |
| 1906 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1907 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1908 | } |
| 1909 | |
| 1910 | /** |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1911 | * Two fingers down on the window, and lift off the first finger. |
| 1912 | * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event |
| 1913 | * contains a single pointer. |
| 1914 | */ |
| 1915 | TEST_F(InputDispatcherTest, CancelAfterPointer0Up) { |
| 1916 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1917 | sp<FakeWindowHandle> window = |
| 1918 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1919 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1920 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1921 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1922 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1923 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1924 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1925 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1926 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1927 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1928 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1929 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1930 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1931 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1932 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1933 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1934 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1935 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 1936 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1937 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 1938 | |
| 1939 | // Remove the window. The gesture should be canceled |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1940 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1941 | const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}}; |
| 1942 | window->consumeMotionEvent( |
| 1943 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers))); |
| 1944 | } |
| 1945 | |
| 1946 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1947 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1948 | * with the following differences: |
| 1949 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1950 | * clean up the connection. |
| 1951 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1952 | * Ensure that there's no crash in the dispatcher. |
| 1953 | */ |
| 1954 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1955 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1956 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1957 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1958 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1959 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1960 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1961 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1962 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1963 | mDispatcher->onWindowInfosChanged( |
| 1964 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1965 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1966 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1967 | {100, 200})) |
| 1968 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1969 | |
| 1970 | // Both foreground window and its wallpaper should receive the touch down |
| 1971 | foregroundWindow->consumeMotionDown(); |
| 1972 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1973 | |
| 1974 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1975 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1976 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1977 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1978 | |
| 1979 | foregroundWindow->consumeMotionMove(); |
| 1980 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1981 | |
| 1982 | // Wallpaper closes its channel, but the window remains. |
| 1983 | wallpaperWindow->destroyReceiver(); |
| 1984 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 1985 | |
| 1986 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 1987 | // is no longer valid. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1988 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1989 | foregroundWindow->consumeMotionCancel(); |
| 1990 | } |
| 1991 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1992 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 1993 | public ::testing::WithParamInterface<bool> {}; |
| 1994 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 1995 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1996 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1997 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1998 | * The top window gets a multitouch gesture. |
| 1999 | * Ensure that wallpaper gets the same gesture. |
| 2000 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2001 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2002 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2003 | sp<FakeWindowHandle> foregroundWindow = |
| 2004 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 2005 | foregroundWindow->setDupTouchToWallpaper(true); |
| 2006 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2007 | |
| 2008 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2009 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2010 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2011 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2012 | mDispatcher->onWindowInfosChanged( |
| 2013 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2014 | |
| 2015 | // Touch down on top window |
| 2016 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2017 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2018 | {100, 100})) |
| 2019 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2020 | |
| 2021 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2022 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2023 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2024 | |
| 2025 | // Second finger down on the top window |
| 2026 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2027 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2028 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2029 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2030 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2031 | .build(); |
| 2032 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2033 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2034 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2035 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2036 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2037 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
| 2038 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2039 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2040 | |
| 2041 | const MotionEvent secondFingerUpEvent = |
| 2042 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 2043 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2044 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2045 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2046 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2047 | .build(); |
| 2048 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2049 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2050 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2051 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2052 | foregroundWindow->consumeMotionPointerUp(0); |
| 2053 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2054 | |
| 2055 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2056 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 2057 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2058 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2059 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2060 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 2061 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 2062 | .x(100) |
| 2063 | .y(100)) |
| 2064 | .build(), |
| 2065 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2066 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2067 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2068 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | /** |
| 2072 | * Two windows: a window on the left and window on the right. |
| 2073 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 2074 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 2075 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 2076 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 2077 | * ACTION_POINTER_DOWN(1). |
| 2078 | */ |
| 2079 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 2080 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2081 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2082 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2083 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2084 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2085 | |
| 2086 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2087 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2088 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2089 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2090 | |
| 2091 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2092 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2093 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2094 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2095 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2096 | mDispatcher->onWindowInfosChanged( |
| 2097 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 2098 | {}, |
| 2099 | 0, |
| 2100 | 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2101 | |
| 2102 | // Touch down on left window |
| 2103 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2104 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2105 | {100, 100})) |
| 2106 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2107 | |
| 2108 | // Both foreground window and its wallpaper should receive the touch down |
| 2109 | leftWindow->consumeMotionDown(); |
| 2110 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2111 | |
| 2112 | // Second finger down on the right window |
| 2113 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2114 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2115 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2116 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2117 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2118 | .build(); |
| 2119 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2120 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2121 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2122 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2123 | |
| 2124 | leftWindow->consumeMotionMove(); |
| 2125 | // Since the touch is split, right window gets ACTION_DOWN |
| 2126 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2127 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2128 | expectedWallpaperFlags); |
| 2129 | |
| 2130 | // Now, leftWindow, which received the first finger, disappears. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2131 | mDispatcher->onWindowInfosChanged( |
| 2132 | {{*rightWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2133 | leftWindow->consumeMotionCancel(); |
| 2134 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 2135 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2136 | |
| 2137 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 2138 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 2139 | const MotionEvent secondFingerMoveEvent = |
| 2140 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2141 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2142 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2143 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2144 | .build(); |
| 2145 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2146 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2147 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 2148 | rightWindow->consumeMotionMove(); |
| 2149 | |
| 2150 | leftWindow->assertNoEvents(); |
| 2151 | rightWindow->assertNoEvents(); |
| 2152 | wallpaperWindow->assertNoEvents(); |
| 2153 | } |
| 2154 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2155 | /** |
| 2156 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 2157 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 2158 | * The right window should receive ACTION_DOWN. |
| 2159 | */ |
| 2160 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2161 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2162 | sp<FakeWindowHandle> leftWindow = |
| 2163 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2164 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2165 | leftWindow->setDupTouchToWallpaper(true); |
| 2166 | leftWindow->setSlippery(true); |
| 2167 | |
| 2168 | sp<FakeWindowHandle> rightWindow = |
| 2169 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2170 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2171 | |
| 2172 | sp<FakeWindowHandle> wallpaperWindow = |
| 2173 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2174 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2175 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2176 | mDispatcher->onWindowInfosChanged( |
| 2177 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 2178 | {}, |
| 2179 | 0, |
| 2180 | 0}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2181 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2182 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2183 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2184 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2185 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2186 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2187 | |
| 2188 | // Both foreground window and its wallpaper should receive the touch down |
| 2189 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2190 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2191 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2192 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2193 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2194 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2195 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2196 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2197 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2198 | leftWindow->consumeMotionCancel(); |
| 2199 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2200 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2203 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2204 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2205 | * interactive, it might stop sending this flag. |
| 2206 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 2207 | * to have a consistent input stream. |
| 2208 | * |
| 2209 | * Test procedure: |
| 2210 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 2211 | * DOWN (new gesture). |
| 2212 | * |
| 2213 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 2214 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 2215 | * |
| 2216 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 2217 | * regular window below) to emulate the actual situation where it happens on the device. |
| 2218 | */ |
| 2219 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 2220 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2221 | sp<FakeWindowHandle> spyWindow = |
| 2222 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2223 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2224 | spyWindow->setTrustedOverlay(true); |
| 2225 | spyWindow->setSpy(true); |
| 2226 | |
| 2227 | sp<FakeWindowHandle> window = |
| 2228 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2229 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2230 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2231 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2232 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2233 | |
| 2234 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2235 | mDispatcher->notifyMotion( |
| 2236 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2237 | .deviceId(touchDeviceId) |
| 2238 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2239 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2240 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2241 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2242 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2243 | .deviceId(touchDeviceId) |
| 2244 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2245 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2246 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2247 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2248 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2249 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2250 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2251 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2252 | |
| 2253 | // Cancel the current gesture. Send the cancel without the default policy flags. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2254 | mDispatcher->notifyMotion( |
| 2255 | MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 2256 | .deviceId(touchDeviceId) |
| 2257 | .policyFlags(0) |
| 2258 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2259 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2260 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2261 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2262 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2263 | |
| 2264 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 2265 | // 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] | 2266 | mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2267 | |
| 2268 | // Start new gesture |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2269 | mDispatcher->notifyMotion( |
| 2270 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2271 | .deviceId(touchDeviceId) |
| 2272 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2273 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2274 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2275 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2276 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2277 | |
| 2278 | // No more events |
| 2279 | spyWindow->assertNoEvents(); |
| 2280 | window->assertNoEvents(); |
| 2281 | } |
| 2282 | |
| 2283 | /** |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 2284 | * Same as the above 'TwoPointerCancelInconsistentPolicy' test, but for hovers. |
| 2285 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2286 | * interactive, it might stop sending this flag. |
| 2287 | * We've already ensured the consistency of the touch event in this case, and we should also ensure |
| 2288 | * the consistency of the hover event in this case. |
| 2289 | * |
| 2290 | * Test procedure: |
| 2291 | * HOVER_ENTER -> HOVER_MOVE -> (stop sending POLICY_FLAG_PASS_TO_USER) -> HOVER_EXIT |
| 2292 | * HOVER_ENTER -> HOVER_MOVE -> HOVER_EXIT |
| 2293 | * |
| 2294 | * We expect to receive two full streams of hover events. |
| 2295 | */ |
| 2296 | TEST_F(InputDispatcherTest, HoverEventInconsistentPolicy) { |
| 2297 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2298 | |
| 2299 | sp<FakeWindowHandle> window = |
| 2300 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2301 | window->setFrame(Rect(0, 0, 300, 300)); |
| 2302 | |
| 2303 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2304 | |
| 2305 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2306 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2307 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2308 | .build()); |
| 2309 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2310 | |
| 2311 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2312 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2313 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2314 | .build()); |
| 2315 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2316 | |
| 2317 | // Send hover exit without the default policy flags. |
| 2318 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2319 | .policyFlags(0) |
| 2320 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2321 | .build()); |
| 2322 | |
| 2323 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2324 | |
| 2325 | // Send a simple hover event stream, ensure dispatcher not crashed and window can receive |
| 2326 | // right event. |
| 2327 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2328 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2329 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(200).y(201)) |
| 2330 | .build()); |
| 2331 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2332 | |
| 2333 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2334 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2335 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 2336 | .build()); |
| 2337 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2338 | |
| 2339 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2340 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2341 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 2342 | .build()); |
| 2343 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2344 | } |
| 2345 | |
| 2346 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2347 | * Two windows: a window on the left and a window on the right. |
| 2348 | * Mouse is hovered from the right window into the left window. |
| 2349 | * Next, we tap on the left window, where the cursor was last seen. |
| 2350 | * The second tap is done onto the right window. |
| 2351 | * The mouse and tap are from two different devices. |
| 2352 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 2353 | * explicitly helps during debugging. |
| 2354 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2355 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 2356 | */ |
| 2357 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 2358 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2359 | sp<FakeWindowHandle> leftWindow = |
| 2360 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2361 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2362 | |
| 2363 | sp<FakeWindowHandle> rightWindow = |
| 2364 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2365 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2366 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2367 | mDispatcher->onWindowInfosChanged( |
| 2368 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2369 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 2370 | // stale. |
| 2371 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2372 | const int32_t mouseDeviceId = 6; |
| 2373 | const int32_t touchDeviceId = 4; |
| 2374 | // Move the cursor from right |
| 2375 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2376 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2377 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2378 | AINPUT_SOURCE_MOUSE) |
| 2379 | .deviceId(mouseDeviceId) |
| 2380 | .downTime(baseTime + 10) |
| 2381 | .eventTime(baseTime + 20) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2382 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2383 | .build())); |
| 2384 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2385 | |
| 2386 | // .. to the left window |
| 2387 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2388 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2389 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2390 | AINPUT_SOURCE_MOUSE) |
| 2391 | .deviceId(mouseDeviceId) |
| 2392 | .downTime(baseTime + 10) |
| 2393 | .eventTime(baseTime + 30) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2394 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2395 | .build())); |
| 2396 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2397 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2398 | // Now tap the left window |
| 2399 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2400 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2401 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2402 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2403 | .deviceId(touchDeviceId) |
| 2404 | .downTime(baseTime + 40) |
| 2405 | .eventTime(baseTime + 40) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2406 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2407 | .build())); |
| 2408 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2409 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2410 | |
| 2411 | // release tap |
| 2412 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2413 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2414 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2415 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2416 | .deviceId(touchDeviceId) |
| 2417 | .downTime(baseTime + 40) |
| 2418 | .eventTime(baseTime + 50) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2419 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2420 | .build())); |
| 2421 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2422 | |
| 2423 | // Tap the window on the right |
| 2424 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2425 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2426 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2427 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2428 | .deviceId(touchDeviceId) |
| 2429 | .downTime(baseTime + 60) |
| 2430 | .eventTime(baseTime + 60) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2431 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2432 | .build())); |
| 2433 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2434 | |
| 2435 | // release tap |
| 2436 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2437 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2438 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2439 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2440 | .deviceId(touchDeviceId) |
| 2441 | .downTime(baseTime + 60) |
| 2442 | .eventTime(baseTime + 70) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2443 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2444 | .build())); |
| 2445 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2446 | |
| 2447 | // No more events |
| 2448 | leftWindow->assertNoEvents(); |
| 2449 | rightWindow->assertNoEvents(); |
| 2450 | } |
| 2451 | |
| 2452 | /** |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2453 | * Start hovering in a window. While this hover is still active, make another window appear on top. |
| 2454 | * The top, obstructing window has no input channel, so it's not supposed to receive input. |
| 2455 | * While the top window is present, the hovering is stopped. |
| 2456 | * Later, hovering gets resumed again. |
| 2457 | * Ensure that new hover gesture is handled correctly. |
| 2458 | * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly |
| 2459 | * to the window that's currently being hovered over. |
| 2460 | */ |
| 2461 | TEST_F(InputDispatcherTest, HoverWhileWindowAppears) { |
| 2462 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2463 | sp<FakeWindowHandle> window = |
| 2464 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2465 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2466 | |
| 2467 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2468 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2469 | |
| 2470 | // Start hovering in the window |
| 2471 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2472 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2473 | .build()); |
| 2474 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2475 | |
| 2476 | // Now, an obscuring window appears! |
| 2477 | sp<FakeWindowHandle> obscuringWindow = |
| 2478 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2479 | ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 2480 | /*createInputChannel=*/false); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2481 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2482 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2483 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2484 | obscuringWindow->setNoInputChannel(true); |
| 2485 | obscuringWindow->setFocusable(false); |
| 2486 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2487 | mDispatcher->onWindowInfosChanged( |
| 2488 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2489 | |
| 2490 | // While this new obscuring window is present, the hovering is stopped |
| 2491 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2492 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2493 | .build()); |
| 2494 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2495 | |
| 2496 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2497 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2498 | |
| 2499 | // And a new hover gesture starts. |
| 2500 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2501 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2502 | .build()); |
| 2503 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2504 | } |
| 2505 | |
| 2506 | /** |
| 2507 | * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to |
| 2508 | * the obscuring window. |
| 2509 | */ |
| 2510 | TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) { |
| 2511 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2512 | sp<FakeWindowHandle> window = |
| 2513 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2514 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2515 | |
| 2516 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2517 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2518 | |
| 2519 | // Start hovering in the window |
| 2520 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2521 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2522 | .build()); |
| 2523 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2524 | |
| 2525 | // Now, an obscuring window appears! |
| 2526 | sp<FakeWindowHandle> obscuringWindow = |
| 2527 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2528 | ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 2529 | /*createInputChannel=*/false); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2530 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2531 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2532 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2533 | obscuringWindow->setNoInputChannel(true); |
| 2534 | obscuringWindow->setFocusable(false); |
| 2535 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2536 | mDispatcher->onWindowInfosChanged( |
| 2537 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2538 | |
| 2539 | // While this new obscuring window is present, the hovering continues. The event can't go to the |
| 2540 | // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window. |
| 2541 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2542 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2543 | .build()); |
| 2544 | obscuringWindow->assertNoEvents(); |
| 2545 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2546 | |
| 2547 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2548 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2549 | |
| 2550 | // Hovering continues in the same position. The hovering pointer re-enters the bottom window, |
| 2551 | // so it should generate a HOVER_ENTER |
| 2552 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2553 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2554 | .build()); |
| 2555 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2556 | |
| 2557 | // Now the MOVE should be getting dispatched normally |
| 2558 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2559 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2560 | .build()); |
| 2561 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2562 | } |
| 2563 | |
| 2564 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2565 | * Hover mouse over a window, and then send ACTION_SCROLL. Ensure both the hover and the scroll |
| 2566 | * events are delivered to the window. |
| 2567 | */ |
| 2568 | TEST_F(InputDispatcherTest, HoverMoveAndScroll) { |
| 2569 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2570 | sp<FakeWindowHandle> window = |
| 2571 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2572 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2573 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2574 | |
| 2575 | // Start hovering in the window |
| 2576 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2577 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 2578 | .build()); |
| 2579 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2580 | |
| 2581 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 2582 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2583 | .build()); |
| 2584 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2585 | |
| 2586 | // Scroll with the mouse |
| 2587 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_SCROLL, AINPUT_SOURCE_MOUSE) |
| 2588 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2589 | .build()); |
| 2590 | window->consumeMotionEvent(WithMotionAction(ACTION_SCROLL)); |
| 2591 | } |
| 2592 | |
| 2593 | using InputDispatcherMultiDeviceTest = InputDispatcherTest; |
| 2594 | |
| 2595 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2596 | * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that |
| 2597 | * touch is dropped, because stylus should be preferred over touch. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2598 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2599 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2600 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2601 | sp<FakeWindowHandle> window = |
| 2602 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2603 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2604 | |
| 2605 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2606 | |
| 2607 | constexpr int32_t touchDeviceId = 4; |
| 2608 | constexpr int32_t stylusDeviceId = 2; |
| 2609 | |
| 2610 | // Stylus down |
| 2611 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2612 | .deviceId(stylusDeviceId) |
| 2613 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2614 | .build()); |
| 2615 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2616 | |
| 2617 | // Touch down |
| 2618 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2619 | .deviceId(touchDeviceId) |
| 2620 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2621 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2622 | |
| 2623 | // Touch move |
| 2624 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2625 | .deviceId(touchDeviceId) |
| 2626 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2627 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2628 | // Touch is ignored because stylus is already down |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2629 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2630 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2631 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2632 | .deviceId(stylusDeviceId) |
| 2633 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2634 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2635 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2636 | WithCoords(101, 111))); |
| 2637 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2638 | window->assertNoEvents(); |
| 2639 | } |
| 2640 | |
| 2641 | /** |
| 2642 | * 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] | 2643 | * 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] | 2644 | * Similar test as above, but with added SPY window. |
| 2645 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2646 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2647 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2648 | sp<FakeWindowHandle> window = |
| 2649 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2650 | sp<FakeWindowHandle> spyWindow = |
| 2651 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2652 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2653 | spyWindow->setTrustedOverlay(true); |
| 2654 | spyWindow->setSpy(true); |
| 2655 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2656 | |
| 2657 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 2658 | |
| 2659 | constexpr int32_t touchDeviceId = 4; |
| 2660 | constexpr int32_t stylusDeviceId = 2; |
| 2661 | |
| 2662 | // Stylus down |
| 2663 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2664 | .deviceId(stylusDeviceId) |
| 2665 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2666 | .build()); |
| 2667 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2668 | spyWindow->consumeMotionEvent( |
| 2669 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2670 | |
| 2671 | // Touch down |
| 2672 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2673 | .deviceId(touchDeviceId) |
| 2674 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2675 | .build()); |
| 2676 | |
| 2677 | // Touch move |
| 2678 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2679 | .deviceId(touchDeviceId) |
| 2680 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2681 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2682 | |
| 2683 | // Touch is ignored because stylus is already down |
| 2684 | |
| 2685 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2686 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2687 | .deviceId(stylusDeviceId) |
| 2688 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2689 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2690 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2691 | WithCoords(101, 111))); |
| 2692 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2693 | WithCoords(101, 111))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2694 | |
| 2695 | window->assertNoEvents(); |
| 2696 | spyWindow->assertNoEvents(); |
| 2697 | } |
| 2698 | |
| 2699 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2700 | * 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] | 2701 | * touch is dropped, because stylus hover takes precedence. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2702 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2703 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2704 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2705 | sp<FakeWindowHandle> window = |
| 2706 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2707 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2708 | |
| 2709 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2710 | |
| 2711 | constexpr int32_t touchDeviceId = 4; |
| 2712 | constexpr int32_t stylusDeviceId = 2; |
| 2713 | |
| 2714 | // Stylus down on the window |
| 2715 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2716 | .deviceId(stylusDeviceId) |
| 2717 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2718 | .build()); |
| 2719 | window->consumeMotionEvent( |
| 2720 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2721 | |
| 2722 | // Touch down on window |
| 2723 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2724 | .deviceId(touchDeviceId) |
| 2725 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2726 | .build()); |
| 2727 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2728 | .deviceId(touchDeviceId) |
| 2729 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2730 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2731 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2732 | // Touch is ignored because stylus is hovering |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2733 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2734 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2735 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2736 | .deviceId(stylusDeviceId) |
| 2737 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2738 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2739 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2740 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2741 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2742 | // and subsequent touches continue to be ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2743 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2744 | .deviceId(touchDeviceId) |
| 2745 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2746 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2747 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2748 | } |
| 2749 | |
| 2750 | /** |
| 2751 | * 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] | 2752 | * Ensure that touch is canceled, because stylus hover should take precedence. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2753 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2754 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusHover) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2755 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2756 | sp<FakeWindowHandle> window = |
| 2757 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2758 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2759 | |
| 2760 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2761 | |
| 2762 | constexpr int32_t touchDeviceId = 4; |
| 2763 | constexpr int32_t stylusDeviceId = 2; |
| 2764 | |
| 2765 | // Touch down on window |
| 2766 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2767 | .deviceId(touchDeviceId) |
| 2768 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2769 | .build()); |
| 2770 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2771 | .deviceId(touchDeviceId) |
| 2772 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2773 | .build()); |
| 2774 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2775 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2776 | |
| 2777 | // Stylus hover on the window |
| 2778 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2779 | .deviceId(stylusDeviceId) |
| 2780 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2781 | .build()); |
| 2782 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2783 | .deviceId(stylusDeviceId) |
| 2784 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2785 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2786 | // Stylus hover movement causes touch to be canceled |
| 2787 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 2788 | WithCoords(141, 146))); |
| 2789 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 2790 | WithDeviceId(stylusDeviceId), WithCoords(100, 110))); |
| 2791 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2792 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2793 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2794 | // Subsequent touch movements are ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2795 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2796 | .deviceId(touchDeviceId) |
| 2797 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2798 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2799 | |
| 2800 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | /** |
| 2804 | * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that |
| 2805 | * the latest stylus takes over. That is, old stylus should be canceled and the new stylus should |
| 2806 | * become active. |
| 2807 | */ |
| 2808 | TEST_F(InputDispatcherMultiDeviceTest, LatestStylusWins) { |
| 2809 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2810 | sp<FakeWindowHandle> window = |
| 2811 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2812 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2813 | |
| 2814 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2815 | |
| 2816 | constexpr int32_t stylusDeviceId1 = 3; |
| 2817 | constexpr int32_t stylusDeviceId2 = 5; |
| 2818 | |
| 2819 | // Touch down on window |
| 2820 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2821 | .deviceId(stylusDeviceId1) |
| 2822 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100)) |
| 2823 | .build()); |
| 2824 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2825 | .deviceId(stylusDeviceId1) |
| 2826 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2827 | .build()); |
| 2828 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1))); |
| 2829 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2830 | |
| 2831 | // Second stylus down |
| 2832 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2833 | .deviceId(stylusDeviceId2) |
| 2834 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10)) |
| 2835 | .build()); |
| 2836 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2837 | .deviceId(stylusDeviceId2) |
| 2838 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11)) |
| 2839 | .build()); |
| 2840 | |
| 2841 | // First stylus is canceled, second one takes over. |
| 2842 | window->consumeMotionEvent( |
| 2843 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId1))); |
| 2844 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2))); |
| 2845 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2))); |
| 2846 | |
| 2847 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2848 | .deviceId(stylusDeviceId1) |
| 2849 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2850 | .build()); |
| 2851 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2852 | window->assertNoEvents(); |
| 2853 | } |
| 2854 | |
| 2855 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2856 | * One window. Touch down on the window. Then, stylus down on the window from another device. |
| 2857 | * Ensure that is canceled, because stylus down should be preferred over touch. |
| 2858 | */ |
| 2859 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusDown) { |
| 2860 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2861 | sp<FakeWindowHandle> window = |
| 2862 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2863 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2864 | |
| 2865 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2866 | |
| 2867 | constexpr int32_t touchDeviceId = 4; |
| 2868 | constexpr int32_t stylusDeviceId = 2; |
| 2869 | |
| 2870 | // Touch down on window |
| 2871 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2872 | .deviceId(touchDeviceId) |
| 2873 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2874 | .build()); |
| 2875 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2876 | .deviceId(touchDeviceId) |
| 2877 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2878 | .build()); |
| 2879 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2880 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2881 | |
| 2882 | // Stylus down on the window |
| 2883 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2884 | .deviceId(stylusDeviceId) |
| 2885 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2886 | .build()); |
| 2887 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 2888 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2889 | |
| 2890 | // Subsequent stylus movements are delivered correctly |
| 2891 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2892 | .deviceId(stylusDeviceId) |
| 2893 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2894 | .build()); |
| 2895 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2896 | WithCoords(101, 111))); |
| 2897 | } |
| 2898 | |
| 2899 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2900 | * Two windows: a window on the left and a window on the right. |
| 2901 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2902 | * down. Then, on the left window, also place second touch pointer down. |
| 2903 | * This test tries to reproduce a crash. |
| 2904 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2905 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2906 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2907 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2908 | sp<FakeWindowHandle> leftWindow = |
| 2909 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2910 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2911 | |
| 2912 | sp<FakeWindowHandle> rightWindow = |
| 2913 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2914 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2915 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2916 | mDispatcher->onWindowInfosChanged( |
| 2917 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2918 | |
| 2919 | const int32_t touchDeviceId = 4; |
| 2920 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2921 | |
| 2922 | // Start hovering over the left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2923 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2924 | .deviceId(mouseDeviceId) |
| 2925 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2926 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2927 | leftWindow->consumeMotionEvent( |
| 2928 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2929 | |
| 2930 | // Mouse down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2931 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2932 | .deviceId(mouseDeviceId) |
| 2933 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2934 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2935 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2936 | |
| 2937 | leftWindow->consumeMotionEvent( |
| 2938 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2939 | leftWindow->consumeMotionEvent( |
| 2940 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2941 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2942 | mDispatcher->notifyMotion( |
| 2943 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2944 | .deviceId(mouseDeviceId) |
| 2945 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2946 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2947 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2948 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2949 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2950 | |
| 2951 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2952 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2953 | .deviceId(touchDeviceId) |
| 2954 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2955 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2956 | leftWindow->assertNoEvents(); |
| 2957 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2958 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2959 | |
| 2960 | // Second touch pointer down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2961 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2962 | .deviceId(touchDeviceId) |
| 2963 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2964 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2965 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2966 | // Since this is now a new splittable pointer going down on the left window, and it's coming |
| 2967 | // from a different device, the current gesture in the left window (pointer down) should first |
| 2968 | // be canceled. |
| 2969 | leftWindow->consumeMotionEvent( |
| 2970 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2971 | leftWindow->consumeMotionEvent( |
| 2972 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2973 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2974 | // current implementation. |
| 2975 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2976 | rightWindow->consumeMotionEvent( |
| 2977 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2978 | |
| 2979 | leftWindow->assertNoEvents(); |
| 2980 | rightWindow->assertNoEvents(); |
| 2981 | } |
| 2982 | |
| 2983 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2984 | * Two windows: a window on the left and a window on the right. |
| 2985 | * Mouse is hovered on the left window and stylus is hovered on the right window. |
| 2986 | */ |
| 2987 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHover) { |
| 2988 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2989 | sp<FakeWindowHandle> leftWindow = |
| 2990 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2991 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2992 | |
| 2993 | sp<FakeWindowHandle> rightWindow = |
| 2994 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2995 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2996 | |
| 2997 | mDispatcher->onWindowInfosChanged( |
| 2998 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2999 | |
| 3000 | const int32_t stylusDeviceId = 3; |
| 3001 | const int32_t mouseDeviceId = 6; |
| 3002 | |
| 3003 | // Start hovering over the left window |
| 3004 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3005 | .deviceId(mouseDeviceId) |
| 3006 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 3007 | .build()); |
| 3008 | leftWindow->consumeMotionEvent( |
| 3009 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3010 | |
| 3011 | // Stylus hovered on right window |
| 3012 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3013 | .deviceId(stylusDeviceId) |
| 3014 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(300).y(100)) |
| 3015 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3016 | rightWindow->consumeMotionEvent( |
| 3017 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3018 | |
| 3019 | // Subsequent HOVER_MOVE events are dispatched correctly. |
| 3020 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 3021 | .deviceId(mouseDeviceId) |
| 3022 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 3023 | .build()); |
| 3024 | leftWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3025 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3026 | |
| 3027 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3028 | .deviceId(stylusDeviceId) |
| 3029 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(310).y(110)) |
| 3030 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3031 | rightWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3032 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3033 | |
| 3034 | leftWindow->assertNoEvents(); |
| 3035 | rightWindow->assertNoEvents(); |
| 3036 | } |
| 3037 | |
| 3038 | /** |
| 3039 | * Three windows: a window on the left and a window on the right. |
| 3040 | * And a spy window that's positioned above all of them. |
| 3041 | * Stylus down on the left window and remains down. Touch goes down on the right and remains down. |
| 3042 | * Check the stream that's received by the spy. |
| 3043 | */ |
| 3044 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy) { |
| 3045 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3046 | |
| 3047 | sp<FakeWindowHandle> spyWindow = |
| 3048 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3049 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 3050 | spyWindow->setTrustedOverlay(true); |
| 3051 | spyWindow->setSpy(true); |
| 3052 | |
| 3053 | sp<FakeWindowHandle> leftWindow = |
| 3054 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 3055 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3056 | |
| 3057 | sp<FakeWindowHandle> rightWindow = |
| 3058 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 3059 | |
| 3060 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3061 | |
| 3062 | mDispatcher->onWindowInfosChanged( |
| 3063 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3064 | |
| 3065 | const int32_t stylusDeviceId = 1; |
| 3066 | const int32_t touchDeviceId = 2; |
| 3067 | |
| 3068 | // Stylus down on the left window |
| 3069 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3070 | .deviceId(stylusDeviceId) |
| 3071 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 3072 | .build()); |
| 3073 | leftWindow->consumeMotionEvent( |
| 3074 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 3075 | spyWindow->consumeMotionEvent( |
| 3076 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 3077 | |
| 3078 | // Touch down on the right window |
| 3079 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3080 | .deviceId(touchDeviceId) |
| 3081 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3082 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3083 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3084 | rightWindow->consumeMotionEvent( |
| 3085 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3086 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3087 | // Spy window does not receive touch events, because stylus events take precedence, and it |
| 3088 | // already has an active stylus gesture. |
| 3089 | |
| 3090 | // 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] | 3091 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 3092 | .deviceId(stylusDeviceId) |
| 3093 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 3094 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3095 | leftWindow->consumeMotionEvent( |
| 3096 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 3097 | spyWindow->consumeMotionEvent( |
| 3098 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3099 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3100 | // Further MOVE events keep going to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3101 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3102 | .deviceId(touchDeviceId) |
| 3103 | .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110)) |
| 3104 | .build()); |
| 3105 | rightWindow->consumeMotionEvent( |
| 3106 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3107 | |
| 3108 | spyWindow->assertNoEvents(); |
| 3109 | leftWindow->assertNoEvents(); |
| 3110 | rightWindow->assertNoEvents(); |
| 3111 | } |
| 3112 | |
| 3113 | /** |
| 3114 | * Three windows: a window on the left, a window on the right, and a spy window positioned above |
| 3115 | * both. |
| 3116 | * Check hover in left window and touch down in the right window. |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3117 | * 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] | 3118 | * At the same time, left and right should be getting independent streams of hovering and touch, |
| 3119 | * respectively. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3120 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3121 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverBlocksTouchWithSpy) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3122 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3123 | |
| 3124 | sp<FakeWindowHandle> spyWindow = |
| 3125 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3126 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 3127 | spyWindow->setTrustedOverlay(true); |
| 3128 | spyWindow->setSpy(true); |
| 3129 | |
| 3130 | sp<FakeWindowHandle> leftWindow = |
| 3131 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 3132 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3133 | |
| 3134 | sp<FakeWindowHandle> rightWindow = |
| 3135 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 3136 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3137 | |
| 3138 | mDispatcher->onWindowInfosChanged( |
| 3139 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3140 | |
| 3141 | const int32_t stylusDeviceId = 1; |
| 3142 | const int32_t touchDeviceId = 2; |
| 3143 | |
| 3144 | // Stylus hover on the left window |
| 3145 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3146 | .deviceId(stylusDeviceId) |
| 3147 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 3148 | .build()); |
| 3149 | leftWindow->consumeMotionEvent( |
| 3150 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3151 | spyWindow->consumeMotionEvent( |
| 3152 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3153 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3154 | // Touch down on the right window. Spy doesn't receive this touch because it already has |
| 3155 | // stylus hovering there. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3156 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3157 | .deviceId(touchDeviceId) |
| 3158 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3159 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3160 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3161 | spyWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3162 | rightWindow->consumeMotionEvent( |
| 3163 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3164 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3165 | // 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] | 3166 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3167 | .deviceId(stylusDeviceId) |
| 3168 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 3169 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3170 | leftWindow->consumeMotionEvent( |
| 3171 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3172 | spyWindow->consumeMotionEvent( |
| 3173 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3174 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3175 | // Touch movements continue. They should be delivered to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3176 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3177 | .deviceId(touchDeviceId) |
| 3178 | .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101)) |
| 3179 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3180 | rightWindow->consumeMotionEvent( |
| 3181 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3182 | |
| 3183 | spyWindow->assertNoEvents(); |
| 3184 | leftWindow->assertNoEvents(); |
| 3185 | rightWindow->assertNoEvents(); |
| 3186 | } |
| 3187 | |
| 3188 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3189 | * On a single window, use two different devices: mouse and touch. |
| 3190 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 3191 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 3192 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 3193 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 3194 | * represent a new gesture. |
| 3195 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3196 | TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3197 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3198 | sp<FakeWindowHandle> window = |
| 3199 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3200 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3201 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3202 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3203 | |
| 3204 | const int32_t touchDeviceId = 4; |
| 3205 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3206 | |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3207 | // First touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3208 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3209 | .deviceId(touchDeviceId) |
| 3210 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3211 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3212 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3213 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3214 | .deviceId(touchDeviceId) |
| 3215 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3216 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3217 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3218 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3219 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3220 | .deviceId(touchDeviceId) |
| 3221 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3222 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3223 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3224 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3225 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3226 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 3227 | |
| 3228 | // Mouse down. The touch should be canceled |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3229 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3230 | .deviceId(mouseDeviceId) |
| 3231 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3232 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3233 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3234 | |
| 3235 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 3236 | WithPointerCount(1u))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3237 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3238 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3239 | mDispatcher->notifyMotion( |
| 3240 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3241 | .deviceId(mouseDeviceId) |
| 3242 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3243 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3244 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3245 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3246 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3247 | |
| 3248 | // Second touch pointer down. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3249 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3250 | .deviceId(touchDeviceId) |
| 3251 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3252 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3253 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3254 | // Since we already canceled this touch gesture, it will be ignored until a completely new |
| 3255 | // gesture is started. This is easier to implement than trying to keep track of the new pointer |
| 3256 | // and generating an ACTION_DOWN instead of ACTION_POINTER_DOWN. |
| 3257 | // However, mouse movements should continue to work. |
| 3258 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3259 | .deviceId(mouseDeviceId) |
| 3260 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3261 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110)) |
| 3262 | .build()); |
| 3263 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 3264 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3265 | window->assertNoEvents(); |
| 3266 | } |
| 3267 | |
| 3268 | /** |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3269 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels |
| 3270 | * the injected event. |
| 3271 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3272 | TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent) { |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3273 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3274 | sp<FakeWindowHandle> window = |
| 3275 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3276 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3277 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3278 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3279 | |
| 3280 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3281 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 3282 | // completion. |
| 3283 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3284 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3285 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3286 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3287 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3288 | .build())); |
| 3289 | window->consumeMotionEvent( |
| 3290 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3291 | |
| 3292 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer |
| 3293 | // should be canceled and the new gesture should take over. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3294 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3295 | .deviceId(touchDeviceId) |
| 3296 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3297 | .build()); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3298 | |
| 3299 | window->consumeMotionEvent( |
| 3300 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3301 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3302 | } |
| 3303 | |
| 3304 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3305 | * This test is similar to the test above, but the sequence of injected events is different. |
| 3306 | * |
| 3307 | * Two windows: a window on the left and a window on the right. |
| 3308 | * Mouse is hovered over the left window. |
| 3309 | * Next, we tap on the left window, where the cursor was last seen. |
| 3310 | * |
| 3311 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 3312 | * the left window. |
| 3313 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 3314 | * window (first), and then another on the left window (second). |
| 3315 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 3316 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 3317 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3318 | TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch) { |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3319 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3320 | sp<FakeWindowHandle> leftWindow = |
| 3321 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 3322 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3323 | |
| 3324 | sp<FakeWindowHandle> rightWindow = |
| 3325 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 3326 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3327 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3328 | mDispatcher->onWindowInfosChanged( |
| 3329 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3330 | |
| 3331 | const int32_t mouseDeviceId = 6; |
| 3332 | const int32_t touchDeviceId = 4; |
| 3333 | // Hover over the left window. Keep the cursor there. |
| 3334 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3335 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3336 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3337 | AINPUT_SOURCE_MOUSE) |
| 3338 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3339 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3340 | .build())); |
| 3341 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3342 | |
| 3343 | // Tap on left window |
| 3344 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3345 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3346 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3347 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3348 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3349 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3350 | .build())); |
| 3351 | |
| 3352 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3353 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3354 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3355 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3356 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3357 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3358 | .build())); |
| 3359 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3360 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3361 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 3362 | |
| 3363 | // First finger down on right window |
| 3364 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3365 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3366 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3367 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3368 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3369 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3370 | .build())); |
| 3371 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3372 | |
| 3373 | // Second finger down on the left window |
| 3374 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3375 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3376 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3377 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3378 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3379 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3380 | .build())); |
| 3381 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3382 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 3383 | |
| 3384 | // No more events |
| 3385 | leftWindow->assertNoEvents(); |
| 3386 | rightWindow->assertNoEvents(); |
| 3387 | } |
| 3388 | |
| 3389 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3390 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 3391 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 3392 | * touch is gone, stylus hovering should start working again. |
| 3393 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3394 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverIgnoresTouchTap) { |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3395 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3396 | sp<FakeWindowHandle> window = |
| 3397 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3398 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3399 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3400 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3401 | |
| 3402 | const int32_t stylusDeviceId = 5; |
| 3403 | const int32_t touchDeviceId = 4; |
| 3404 | // Start hovering with stylus |
| 3405 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3406 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3407 | MotionEventBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3408 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3409 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3410 | .build())); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3411 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3412 | |
| 3413 | // Finger down on the window |
| 3414 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3415 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3416 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3417 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3418 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3419 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3420 | // The touch device should be ignored! |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3421 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3422 | // Continue hovering with stylus. |
| 3423 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3424 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3425 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3426 | AINPUT_SOURCE_STYLUS) |
| 3427 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3428 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3429 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3430 | // Hovers continue to work |
| 3431 | window->consumeMotionEvent( |
| 3432 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3433 | |
| 3434 | // Lift up the finger |
| 3435 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3436 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3437 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3438 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3439 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3440 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3441 | .build())); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3442 | |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3443 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3444 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3445 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3446 | AINPUT_SOURCE_STYLUS) |
| 3447 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3448 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3449 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3450 | window->consumeMotionEvent( |
| 3451 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3452 | window->assertNoEvents(); |
| 3453 | } |
| 3454 | |
| 3455 | /** |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3456 | * If stylus is down anywhere on the screen, then touches should not be delivered to windows that |
| 3457 | * have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 3458 | * |
| 3459 | * Two windows: one on the left and one on the right. |
| 3460 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 3461 | * Stylus down on the left window, and then touch down on the right window. |
| 3462 | * Check that the right window doesn't get touches while the stylus is down on the left window. |
| 3463 | */ |
| 3464 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusDownBlocksTouch) { |
| 3465 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3466 | sp<FakeWindowHandle> leftWindow = |
| 3467 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
| 3468 | ADISPLAY_ID_DEFAULT); |
| 3469 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3470 | |
| 3471 | sp<FakeWindowHandle> sbtRightWindow = |
| 3472 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3473 | "Stylus blocks touch (right) window", ADISPLAY_ID_DEFAULT); |
| 3474 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3475 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 3476 | |
| 3477 | mDispatcher->onWindowInfosChanged( |
| 3478 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 3479 | |
| 3480 | const int32_t stylusDeviceId = 5; |
| 3481 | const int32_t touchDeviceId = 4; |
| 3482 | |
| 3483 | // Stylus down in the left window |
| 3484 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3485 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 3486 | .deviceId(stylusDeviceId) |
| 3487 | .build()); |
| 3488 | leftWindow->consumeMotionEvent( |
| 3489 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 3490 | |
| 3491 | // Finger tap on the right window |
| 3492 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3493 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3494 | .deviceId(touchDeviceId) |
| 3495 | .build()); |
| 3496 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3497 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3498 | .deviceId(touchDeviceId) |
| 3499 | .build()); |
| 3500 | |
| 3501 | // The touch should be blocked, because stylus is down somewhere else on screen! |
| 3502 | sbtRightWindow->assertNoEvents(); |
| 3503 | |
| 3504 | // Continue stylus motion, and ensure it's not impacted. |
| 3505 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 3506 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3507 | .deviceId(stylusDeviceId) |
| 3508 | .build()); |
| 3509 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3510 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3511 | .deviceId(stylusDeviceId) |
| 3512 | .build()); |
| 3513 | leftWindow->consumeMotionEvent( |
| 3514 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 3515 | leftWindow->consumeMotionEvent( |
| 3516 | AllOf(WithMotionAction(ACTION_UP), WithDeviceId(stylusDeviceId))); |
| 3517 | |
| 3518 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3519 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3520 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3521 | .deviceId(touchDeviceId) |
| 3522 | .build()); |
| 3523 | sbtRightWindow->consumeMotionEvent( |
| 3524 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3525 | } |
| 3526 | |
| 3527 | /** |
| 3528 | * If stylus is hovering anywhere on the screen, then touches should not be delivered to windows |
| 3529 | * that have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 3530 | * |
| 3531 | * Two windows: one on the left and one on the right. |
| 3532 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 3533 | * Stylus hover on the left window, and then touch down on the right window. |
| 3534 | * Check that the right window doesn't get touches while the stylus is hovering on the left window. |
| 3535 | */ |
| 3536 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusHoverBlocksTouch) { |
| 3537 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3538 | sp<FakeWindowHandle> leftWindow = |
| 3539 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
| 3540 | ADISPLAY_ID_DEFAULT); |
| 3541 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3542 | |
| 3543 | sp<FakeWindowHandle> sbtRightWindow = |
| 3544 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3545 | "Stylus blocks touch (right) window", ADISPLAY_ID_DEFAULT); |
| 3546 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3547 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 3548 | |
| 3549 | mDispatcher->onWindowInfosChanged( |
| 3550 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 3551 | |
| 3552 | const int32_t stylusDeviceId = 5; |
| 3553 | const int32_t touchDeviceId = 4; |
| 3554 | |
| 3555 | // Stylus hover in the left window |
| 3556 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3557 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 3558 | .deviceId(stylusDeviceId) |
| 3559 | .build()); |
| 3560 | leftWindow->consumeMotionEvent( |
| 3561 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3562 | |
| 3563 | // Finger tap on the right window |
| 3564 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3565 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3566 | .deviceId(touchDeviceId) |
| 3567 | .build()); |
| 3568 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3569 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3570 | .deviceId(touchDeviceId) |
| 3571 | .build()); |
| 3572 | |
| 3573 | // The touch should be blocked, because stylus is hovering somewhere else on screen! |
| 3574 | sbtRightWindow->assertNoEvents(); |
| 3575 | |
| 3576 | // Continue stylus motion, and ensure it's not impacted. |
| 3577 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3578 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3579 | .deviceId(stylusDeviceId) |
| 3580 | .build()); |
| 3581 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3582 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3583 | .deviceId(stylusDeviceId) |
| 3584 | .build()); |
| 3585 | leftWindow->consumeMotionEvent( |
| 3586 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3587 | leftWindow->consumeMotionEvent( |
| 3588 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId))); |
| 3589 | |
| 3590 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3591 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3592 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3593 | .deviceId(touchDeviceId) |
| 3594 | .build()); |
| 3595 | sbtRightWindow->consumeMotionEvent( |
| 3596 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3597 | } |
| 3598 | |
| 3599 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3600 | * A spy window above a window with no input channel. |
| 3601 | * Start hovering with a stylus device, and then tap with it. |
| 3602 | * Ensure spy window receives the entire sequence. |
| 3603 | */ |
| 3604 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 3605 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3606 | sp<FakeWindowHandle> spyWindow = |
| 3607 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3608 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3609 | spyWindow->setTrustedOverlay(true); |
| 3610 | spyWindow->setSpy(true); |
| 3611 | sp<FakeWindowHandle> window = |
| 3612 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3613 | window->setNoInputChannel(true); |
| 3614 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3615 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3616 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3617 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3618 | // Start hovering with stylus |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3619 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3620 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3621 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3622 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3623 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3624 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3625 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3626 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3627 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3628 | |
| 3629 | // Stylus touches down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3630 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3631 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3632 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3633 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3634 | |
| 3635 | // Stylus goes up |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3636 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3637 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3638 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3639 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 3640 | |
| 3641 | // Again hover |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3642 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3643 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3644 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3645 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3646 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3647 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3648 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3649 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3650 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3651 | |
| 3652 | // No more events |
| 3653 | spyWindow->assertNoEvents(); |
| 3654 | window->assertNoEvents(); |
| 3655 | } |
| 3656 | |
| 3657 | /** |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 3658 | * A stale stylus HOVER_EXIT event is injected. Since it's a stale event, it should generally be |
| 3659 | * rejected. But since we already have an ongoing gesture, this event should be processed. |
| 3660 | * This prevents inconsistent events being handled inside the dispatcher. |
| 3661 | */ |
| 3662 | TEST_F(InputDispatcherTest, StaleStylusHoverGestureIsComplete) { |
| 3663 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3664 | |
| 3665 | sp<FakeWindowHandle> window = |
| 3666 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3667 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3668 | |
| 3669 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3670 | |
| 3671 | // Start hovering with stylus |
| 3672 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3673 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3674 | .build()); |
| 3675 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3676 | |
| 3677 | NotifyMotionArgs hoverExit = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3678 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3679 | .build(); |
| 3680 | // Make this 'hoverExit' event stale |
| 3681 | mFakePolicy->setStaleEventTimeout(100ms); |
| 3682 | std::this_thread::sleep_for(100ms); |
| 3683 | |
| 3684 | // It shouldn't be dropped by the dispatcher, even though it's stale. |
| 3685 | mDispatcher->notifyMotion(hoverExit); |
| 3686 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3687 | |
| 3688 | // Stylus starts hovering again! There should be no crash. |
| 3689 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3690 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(51)) |
| 3691 | .build()); |
| 3692 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3693 | } |
| 3694 | |
| 3695 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3696 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 3697 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 3698 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 3699 | * While the mouse is down, new move events from the touch device should be ignored. |
| 3700 | */ |
| 3701 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 3702 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3703 | sp<FakeWindowHandle> spyWindow = |
| 3704 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3705 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3706 | spyWindow->setTrustedOverlay(true); |
| 3707 | spyWindow->setSpy(true); |
| 3708 | sp<FakeWindowHandle> window = |
| 3709 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3710 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3711 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3712 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3713 | |
| 3714 | const int32_t mouseDeviceId = 7; |
| 3715 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3716 | |
| 3717 | // Hover a bit with mouse first |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3718 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3719 | .deviceId(mouseDeviceId) |
| 3720 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3721 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3722 | spyWindow->consumeMotionEvent( |
| 3723 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3724 | window->consumeMotionEvent( |
| 3725 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3726 | |
| 3727 | // Start touching |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3728 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3729 | .deviceId(touchDeviceId) |
| 3730 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3731 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3732 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3733 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3734 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3735 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3736 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3737 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3738 | .deviceId(touchDeviceId) |
| 3739 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 3740 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3741 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3742 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3743 | |
| 3744 | // Pilfer the stream |
| 3745 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3746 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 3747 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3748 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3749 | .deviceId(touchDeviceId) |
| 3750 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 3751 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3752 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3753 | |
| 3754 | // Mouse down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3755 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3756 | .deviceId(mouseDeviceId) |
| 3757 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3758 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3759 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3760 | |
| 3761 | spyWindow->consumeMotionEvent( |
| 3762 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 3763 | spyWindow->consumeMotionEvent( |
| 3764 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3765 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3766 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3767 | mDispatcher->notifyMotion( |
| 3768 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3769 | .deviceId(mouseDeviceId) |
| 3770 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3771 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3772 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3773 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3774 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3775 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3776 | |
| 3777 | // Mouse move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3778 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3779 | .deviceId(mouseDeviceId) |
| 3780 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3781 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 3782 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3783 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3784 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3785 | |
| 3786 | // Touch move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3787 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3788 | .deviceId(touchDeviceId) |
| 3789 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 3790 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3791 | |
| 3792 | // No more events |
| 3793 | spyWindow->assertNoEvents(); |
| 3794 | window->assertNoEvents(); |
| 3795 | } |
| 3796 | |
| 3797 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3798 | * On the display, have a single window, and also an area where there's no window. |
| 3799 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 3800 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 3801 | */ |
| 3802 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 3803 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3804 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3805 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3806 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3807 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3808 | |
| 3809 | // Touch down on the empty space |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3810 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3811 | |
| 3812 | mDispatcher->waitForIdle(); |
| 3813 | window->assertNoEvents(); |
| 3814 | |
| 3815 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3816 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3817 | mDispatcher->waitForIdle(); |
| 3818 | window->consumeMotionDown(); |
| 3819 | } |
| 3820 | |
| 3821 | /** |
| 3822 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 3823 | * non-touchable window. |
| 3824 | */ |
| 3825 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 3826 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3827 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3828 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3829 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3830 | window1->setTouchable(false); |
| 3831 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3832 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3833 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3834 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3835 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3836 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3837 | // Touch down on the non-touchable window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3838 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3839 | |
| 3840 | mDispatcher->waitForIdle(); |
| 3841 | window1->assertNoEvents(); |
| 3842 | window2->assertNoEvents(); |
| 3843 | |
| 3844 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3845 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3846 | mDispatcher->waitForIdle(); |
| 3847 | window2->consumeMotionDown(); |
| 3848 | } |
| 3849 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3850 | /** |
| 3851 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 3852 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 3853 | */ |
| 3854 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 3855 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3856 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3857 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3858 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3859 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3860 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3861 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3862 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3863 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3864 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3865 | // Touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3866 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3867 | |
| 3868 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3869 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 3870 | std::unique_ptr<MotionEvent> motionEvent1 = window1->consumeMotionEvent(); |
| 3871 | ASSERT_NE(nullptr, motionEvent1); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3872 | window2->assertNoEvents(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 3873 | nsecs_t downTimeForWindow1 = motionEvent1->getDownTime(); |
| 3874 | ASSERT_EQ(motionEvent1->getDownTime(), motionEvent1->getEventTime()); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3875 | |
| 3876 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3877 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3878 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 3879 | std::unique_ptr<MotionEvent> motionEvent2 = window2->consumeMotionEvent(); |
| 3880 | ASSERT_NE(nullptr, motionEvent2); |
| 3881 | nsecs_t downTimeForWindow2 = motionEvent2->getDownTime(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3882 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 3883 | ASSERT_EQ(motionEvent2->getDownTime(), motionEvent2->getEventTime()); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3884 | |
| 3885 | // Now move the pointer on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3886 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3887 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3888 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3889 | |
| 3890 | // Now add new touch down on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3891 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3892 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3893 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3894 | |
| 3895 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 3896 | window1->consumeMotionMove(); |
| 3897 | window1->assertNoEvents(); |
| 3898 | |
| 3899 | // Now move the pointer on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3900 | mDispatcher->notifyMotion( |
| 3901 | generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3902 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3903 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3904 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3905 | mDispatcher->notifyMotion( |
| 3906 | generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3907 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3908 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3911 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3912 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3913 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3914 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3915 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3916 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3917 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3918 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3919 | |
| 3920 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3921 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3922 | mDispatcher->onWindowInfosChanged( |
| 3923 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3924 | |
| 3925 | // 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] | 3926 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3927 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3928 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3929 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3930 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3931 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3932 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3933 | |
| 3934 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3935 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3936 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3937 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3938 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3939 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3940 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3941 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3942 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3943 | |
| 3944 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3945 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3946 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3947 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3948 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3949 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3950 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3951 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3952 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3953 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3954 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3955 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3956 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3957 | AINPUT_SOURCE_MOUSE) |
| 3958 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3959 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3960 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3961 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3962 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3963 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3964 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3965 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3966 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3967 | AINPUT_SOURCE_MOUSE) |
| 3968 | .buttonState(0) |
| 3969 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3970 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3971 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3972 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3973 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3974 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3975 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3976 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3977 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3978 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3979 | .build())); |
| 3980 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3981 | |
| 3982 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3983 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3984 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3985 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3986 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3987 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3988 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3989 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3990 | |
| 3991 | // No more events |
| 3992 | windowLeft->assertNoEvents(); |
| 3993 | windowRight->assertNoEvents(); |
| 3994 | } |
| 3995 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3996 | /** |
| 3997 | * Put two fingers down (and don't release them) and click the mouse button. |
| 3998 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 3999 | * currently active gesture should be canceled, and the new one should proceed. |
| 4000 | */ |
| 4001 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 4002 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4003 | sp<FakeWindowHandle> window = |
| 4004 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4005 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4006 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4007 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4008 | |
| 4009 | const int32_t touchDeviceId = 4; |
| 4010 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4011 | |
| 4012 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4013 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4014 | .deviceId(touchDeviceId) |
| 4015 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4016 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4017 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4018 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4019 | .deviceId(touchDeviceId) |
| 4020 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4021 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 4022 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4023 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4024 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4025 | |
| 4026 | // Inject a series of mouse events for a mouse click |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4027 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4028 | .deviceId(mouseDeviceId) |
| 4029 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4030 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4031 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4032 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 4033 | WithPointerCount(2u))); |
| 4034 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 4035 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4036 | mDispatcher->notifyMotion( |
| 4037 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 4038 | .deviceId(mouseDeviceId) |
| 4039 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4040 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4041 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4042 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4043 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4044 | |
| 4045 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 4046 | // already canceled gesture, it should be ignored. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4047 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 4048 | .deviceId(touchDeviceId) |
| 4049 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 4050 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 4051 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4052 | window->assertNoEvents(); |
| 4053 | } |
| 4054 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4055 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 4056 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4057 | |
| 4058 | sp<FakeWindowHandle> spyWindow = |
| 4059 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 4060 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4061 | spyWindow->setTrustedOverlay(true); |
| 4062 | spyWindow->setSpy(true); |
| 4063 | sp<FakeWindowHandle> window = |
| 4064 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4065 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4066 | |
| 4067 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4068 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4069 | |
| 4070 | // Send mouse cursor to the window |
| 4071 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4072 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4073 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4074 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4075 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4076 | .build())); |
| 4077 | |
| 4078 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4079 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4080 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4081 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4082 | |
| 4083 | window->assertNoEvents(); |
| 4084 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4085 | } |
| 4086 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4087 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 4088 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4089 | |
| 4090 | sp<FakeWindowHandle> spyWindow = |
| 4091 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 4092 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4093 | spyWindow->setTrustedOverlay(true); |
| 4094 | spyWindow->setSpy(true); |
| 4095 | sp<FakeWindowHandle> window = |
| 4096 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4097 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4098 | |
| 4099 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4100 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4101 | |
| 4102 | // Send mouse cursor to the window |
| 4103 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4104 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4105 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4106 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4107 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4108 | .build())); |
| 4109 | |
| 4110 | // Move mouse cursor |
| 4111 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4112 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4113 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4114 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4115 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4116 | .build())); |
| 4117 | |
| 4118 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4119 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4120 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4121 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4122 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4123 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4124 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4125 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4126 | // Touch down on the window |
| 4127 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4128 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4129 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 4130 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4131 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4132 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4133 | .build())); |
| 4134 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4135 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4136 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4137 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4138 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4139 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4140 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4141 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4142 | |
| 4143 | // pilfer the motion, retaining the gesture on the spy window. |
| 4144 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 4145 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 4146 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4147 | |
| 4148 | // Touch UP on the window |
| 4149 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4150 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4151 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 4152 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4153 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4154 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4155 | .build())); |
| 4156 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4157 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4158 | |
| 4159 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 4160 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 4161 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 4162 | |
| 4163 | // One more tap - DOWN |
| 4164 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4165 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4166 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 4167 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4168 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4169 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4170 | .build())); |
| 4171 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4172 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4173 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4174 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4175 | |
| 4176 | // Touch UP on the window |
| 4177 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4178 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4179 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 4180 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4181 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4182 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4183 | .build())); |
| 4184 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4185 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4186 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4187 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4188 | |
| 4189 | window->assertNoEvents(); |
| 4190 | spyWindow->assertNoEvents(); |
| 4191 | } |
| 4192 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4193 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 4194 | // directly in this test. |
| 4195 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4196 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4197 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4198 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4199 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4200 | |
| 4201 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4202 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4203 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4204 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4205 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4206 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4207 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4208 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4209 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4210 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4211 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4212 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4213 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4214 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4215 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4216 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4217 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4218 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4219 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4220 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4221 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4222 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4223 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4224 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 4225 | AINPUT_SOURCE_MOUSE) |
| 4226 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4227 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4228 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4229 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4230 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4231 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4232 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4233 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4234 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 4235 | AINPUT_SOURCE_MOUSE) |
| 4236 | .buttonState(0) |
| 4237 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4238 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4239 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4240 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4241 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4242 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4243 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4244 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4245 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4246 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4247 | .build())); |
| 4248 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 4249 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 4250 | // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the |
| 4251 | // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail. |
| 4252 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4253 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4254 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 4255 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4256 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4257 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4258 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4259 | } |
| 4260 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4261 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4262 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 4263 | * is generated. |
| 4264 | */ |
| 4265 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 4266 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4267 | sp<FakeWindowHandle> window = |
| 4268 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4269 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 4270 | |
| 4271 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4272 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4273 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4274 | |
| 4275 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4276 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4277 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4278 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4279 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4280 | .build())); |
| 4281 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4282 | |
| 4283 | // Remove the window, but keep the channel. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4284 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4285 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 4286 | } |
| 4287 | |
| 4288 | /** |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4289 | * Test that invalid HOVER events sent by accessibility do not cause a fatal crash. |
| 4290 | */ |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 4291 | TEST_F_WITH_FLAGS(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash, |
| 4292 | REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(com::android::input::flags, |
| 4293 | a11y_crash_on_inconsistent_event_stream))) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4294 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4295 | sp<FakeWindowHandle> window = |
| 4296 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4297 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 4298 | |
| 4299 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4300 | |
| 4301 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 4302 | |
| 4303 | MotionEventBuilder hoverEnterBuilder = |
| 4304 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4305 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4306 | .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 4307 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4308 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 4309 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4310 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 4311 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4312 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4313 | } |
| 4314 | |
| 4315 | /** |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4316 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. |
| 4317 | */ |
| 4318 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { |
| 4319 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4320 | sp<FakeWindowHandle> window = |
| 4321 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4322 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4323 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4324 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4325 | |
| 4326 | const int32_t mouseDeviceId = 7; |
| 4327 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4328 | |
| 4329 | // Start hovering with the mouse |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4330 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4331 | .deviceId(mouseDeviceId) |
| 4332 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 4333 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4334 | window->consumeMotionEvent( |
| 4335 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 4336 | |
| 4337 | // Touch goes down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4338 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4339 | .deviceId(touchDeviceId) |
| 4340 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4341 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4342 | |
| 4343 | window->consumeMotionEvent( |
| 4344 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 4345 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 4346 | } |
| 4347 | |
| 4348 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4349 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4350 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 4351 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4352 | */ |
| 4353 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 4354 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4355 | sp<FakeWindowHandle> window = |
| 4356 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4357 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4358 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4359 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4360 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4361 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 4362 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4363 | ASSERT_NO_FATAL_FAILURE( |
| 4364 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4365 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4366 | |
| 4367 | // Tap on the window |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4368 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4369 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4370 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4371 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4372 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4373 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 4374 | |
| 4375 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4376 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4377 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4378 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4379 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4380 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4381 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4382 | ASSERT_NO_FATAL_FAILURE( |
| 4383 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4384 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4385 | } |
| 4386 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4387 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 4388 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4389 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 4390 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 4391 | ADISPLAY_ID_DEFAULT); |
| 4392 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 4393 | sp<FakeWindowHandle> windowSecondDisplay = |
| 4394 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 4395 | SECOND_DISPLAY_ID); |
| 4396 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 4397 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4398 | mDispatcher->onWindowInfosChanged( |
| 4399 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4400 | |
| 4401 | // Set cursor position in window in default display and check that hover enter and move |
| 4402 | // events are generated. |
| 4403 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4404 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4405 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4406 | AINPUT_SOURCE_MOUSE) |
| 4407 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4408 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4409 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4410 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4411 | |
| 4412 | // Remove all windows in secondary display and check that no event happens on window in |
| 4413 | // primary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4414 | mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0}); |
| 4415 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4416 | windowDefaultDisplay->assertNoEvents(); |
| 4417 | |
| 4418 | // Move cursor position in window in default display and check that only hover move |
| 4419 | // event is generated and not hover enter event. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4420 | mDispatcher->onWindowInfosChanged( |
| 4421 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4422 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4423 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4424 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4425 | AINPUT_SOURCE_MOUSE) |
| 4426 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4427 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4428 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4429 | windowDefaultDisplay->consumeMotionEvent( |
| 4430 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4431 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4432 | windowDefaultDisplay->assertNoEvents(); |
| 4433 | } |
| 4434 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4435 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4436 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4437 | |
| 4438 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4439 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4440 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4441 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4442 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4443 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4444 | |
| 4445 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4446 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4447 | mDispatcher->onWindowInfosChanged( |
| 4448 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4449 | |
| 4450 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 4451 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4452 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4453 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4454 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4455 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4456 | windowRight->assertNoEvents(); |
| 4457 | } |
| 4458 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4459 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4460 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4461 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4462 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4463 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4464 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4465 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4466 | setFocusedWindow(window); |
| 4467 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4468 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4469 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4470 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4471 | |
| 4472 | // Window should receive key down event. |
| 4473 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4474 | |
| 4475 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 4476 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4477 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 4478 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT, AKEY_EVENT_FLAG_CANCELED); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4479 | } |
| 4480 | |
| 4481 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4482 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4483 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4484 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4485 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4486 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4487 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4488 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4489 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4490 | |
| 4491 | // Window should receive motion down event. |
| 4492 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4493 | |
| 4494 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 4495 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4496 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 4497 | window->consumeMotionEvent( |
| 4498 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4499 | } |
| 4500 | |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 4501 | TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) { |
| 4502 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4503 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4504 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4505 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4506 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 4507 | |
| 4508 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 4509 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10)) |
| 4510 | .build()); |
| 4511 | |
| 4512 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 4513 | |
| 4514 | // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT |
| 4515 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
| 4516 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4517 | |
| 4518 | // After the device has been reset, a new hovering stream can be sent to the window |
| 4519 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 4520 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15)) |
| 4521 | .build()); |
| 4522 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 4523 | } |
| 4524 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4525 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 4526 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4527 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4528 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4529 | window->setFocusable(true); |
| 4530 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4531 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4532 | setFocusedWindow(window); |
| 4533 | |
| 4534 | window->consumeFocusEvent(true); |
| 4535 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4536 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4537 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 4538 | const nsecs_t injectTime = keyArgs.eventTime; |
| 4539 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4540 | mDispatcher->notifyKey(keyArgs); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4541 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 4542 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4543 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 4544 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 4545 | } |
| 4546 | |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 4547 | /** |
| 4548 | * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set. |
| 4549 | */ |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4550 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 4551 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4552 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4553 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4554 | window->setFocusable(true); |
| 4555 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4556 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4557 | setFocusedWindow(window); |
| 4558 | |
| 4559 | window->consumeFocusEvent(true); |
| 4560 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4561 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4562 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 4563 | |
| 4564 | // Set a value that's significantly larger than the default consumption timeout. If the |
| 4565 | // implementation is correct, the actual value doesn't matter; it won't slow down the test. |
| 4566 | mFakePolicy->setInterceptKeyTimeout(600ms); |
| 4567 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
| 4568 | // Window should receive key event immediately when same key up. |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4569 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 4570 | } |
| 4571 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4572 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4573 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 4574 | * WATCH_OUTSIDE_TOUCH. |
| 4575 | * Both windows are owned by the same UID. |
| 4576 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 4577 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 4578 | */ |
| 4579 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 4580 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4581 | sp<FakeWindowHandle> window = |
| 4582 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4583 | window->setFrame(Rect{0, 0, 100, 100}); |
| 4584 | |
| 4585 | sp<FakeWindowHandle> outsideWindow = |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4586 | sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window", |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4587 | ADISPLAY_ID_DEFAULT); |
| 4588 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 4589 | outsideWindow->setWatchOutsideTouch(true); |
| 4590 | // 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] | 4591 | mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4592 | |
| 4593 | // Tap on first window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4594 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4595 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4596 | {PointF{50, 50}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4597 | window->consumeMotionDown(); |
| 4598 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 4599 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 4600 | outsideWindow->consumeMotionEvent( |
| 4601 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 4602 | |
| 4603 | // Ensure outsideWindow doesn't get any more events for the gesture. |
| 4604 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4605 | ADISPLAY_ID_DEFAULT, {PointF{51, 51}})); |
| 4606 | window->consumeMotionMove(); |
| 4607 | outsideWindow->assertNoEvents(); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4608 | } |
| 4609 | |
| 4610 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4611 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 4612 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 4613 | * ACTION_OUTSIDE event is sent per gesture. |
| 4614 | */ |
| 4615 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 4616 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 4617 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4618 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4619 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4620 | window->setWatchOutsideTouch(true); |
| 4621 | window->setFrame(Rect{0, 0, 100, 100}); |
| 4622 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4623 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4624 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4625 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 4626 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4627 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 4628 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4629 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4630 | mDispatcher->onWindowInfosChanged( |
| 4631 | {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4632 | |
| 4633 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4634 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4635 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4636 | {PointF{-10, -10}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4637 | window->assertNoEvents(); |
| 4638 | secondWindow->assertNoEvents(); |
| 4639 | |
| 4640 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 4641 | // Now, `window` should get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4642 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4643 | ADISPLAY_ID_DEFAULT, |
| 4644 | {PointF{-10, -10}, PointF{105, 105}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4645 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 4646 | window->consumeMotionEvent( |
| 4647 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4648 | secondWindow->consumeMotionDown(); |
| 4649 | thirdWindow->assertNoEvents(); |
| 4650 | |
| 4651 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 4652 | // 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] | 4653 | mDispatcher->notifyMotion( |
| 4654 | generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4655 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4656 | window->assertNoEvents(); |
| 4657 | secondWindow->consumeMotionMove(); |
| 4658 | thirdWindow->consumeMotionDown(); |
| 4659 | } |
| 4660 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4661 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 4662 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4663 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4664 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4665 | window->setFocusable(true); |
| 4666 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4667 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4668 | setFocusedWindow(window); |
| 4669 | |
| 4670 | window->consumeFocusEvent(true); |
| 4671 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4672 | const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 4673 | const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 4674 | mDispatcher->notifyKey(keyDown); |
| 4675 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4676 | |
| 4677 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4678 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 4679 | |
| 4680 | // 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] | 4681 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4682 | |
| 4683 | window->consumeFocusEvent(false); |
| 4684 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4685 | mDispatcher->notifyKey(keyDown); |
| 4686 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4687 | window->assertNoEvents(); |
| 4688 | } |
| 4689 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4690 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 4691 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4692 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4693 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4694 | // Ensure window is non-split and have some transform. |
| 4695 | window->setPreventSplitting(true); |
| 4696 | window->setWindowOffset(20, 40); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4697 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4698 | |
| 4699 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4700 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4701 | {50, 50})) |
| 4702 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4703 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4704 | |
| 4705 | const MotionEvent secondFingerDownEvent = |
| 4706 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4707 | .displayId(ADISPLAY_ID_DEFAULT) |
| 4708 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4709 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 4710 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4711 | .build(); |
| 4712 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4713 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4714 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 4715 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4716 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 4717 | std::unique_ptr<MotionEvent> event = window->consumeMotionEvent(); |
| 4718 | ASSERT_NE(nullptr, event); |
| 4719 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 4720 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 4721 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 4722 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 4723 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4724 | } |
| 4725 | |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 4726 | /** |
| 4727 | * Two windows: a splittable and a non-splittable. |
| 4728 | * The non-splittable window shouldn't receive any "incomplete" gestures. |
| 4729 | * Send the first pointer to the splittable window, and then touch the non-splittable window. |
| 4730 | * The second pointer should be dropped because the initial window is splittable, so it won't get |
| 4731 | * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any |
| 4732 | * "incomplete" gestures. |
| 4733 | */ |
| 4734 | TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) { |
| 4735 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4736 | sp<FakeWindowHandle> leftWindow = |
| 4737 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window", |
| 4738 | ADISPLAY_ID_DEFAULT); |
| 4739 | leftWindow->setPreventSplitting(false); |
| 4740 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4741 | sp<FakeWindowHandle> rightWindow = |
| 4742 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window", |
| 4743 | ADISPLAY_ID_DEFAULT); |
| 4744 | rightWindow->setPreventSplitting(true); |
| 4745 | rightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 4746 | mDispatcher->onWindowInfosChanged( |
| 4747 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 4748 | |
| 4749 | // Touch down on left, splittable window |
| 4750 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4751 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4752 | .build()); |
| 4753 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4754 | |
| 4755 | mDispatcher->notifyMotion( |
| 4756 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4757 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 4758 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
| 4759 | .build()); |
| 4760 | leftWindow->assertNoEvents(); |
| 4761 | rightWindow->assertNoEvents(); |
| 4762 | } |
| 4763 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4764 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) { |
| 4765 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4766 | sp<FakeWindowHandle> window = |
| 4767 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4768 | window->setFrame(Rect(0, 0, 400, 400)); |
| 4769 | sp<FakeWindowHandle> trustedOverlay = |
| 4770 | sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay", |
| 4771 | ADISPLAY_ID_DEFAULT); |
| 4772 | trustedOverlay->setSpy(true); |
| 4773 | trustedOverlay->setTrustedOverlay(true); |
| 4774 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4775 | mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4776 | |
| 4777 | // Start a three-finger touchpad swipe |
| 4778 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4779 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4780 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4781 | .build()); |
| 4782 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 4783 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4784 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4785 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4786 | .build()); |
| 4787 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 4788 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4789 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4790 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 4791 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4792 | .build()); |
| 4793 | |
| 4794 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4795 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4796 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN)); |
| 4797 | |
| 4798 | // Move the swipe a bit |
| 4799 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4800 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4801 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4802 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4803 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4804 | .build()); |
| 4805 | |
| 4806 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 4807 | |
| 4808 | // End the swipe |
| 4809 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 4810 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4811 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4812 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4813 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4814 | .build()); |
| 4815 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 4816 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4817 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4818 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4819 | .build()); |
| 4820 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4821 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4822 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4823 | .build()); |
| 4824 | |
| 4825 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP)); |
| 4826 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 4827 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 4828 | |
| 4829 | window->assertNoEvents(); |
| 4830 | } |
| 4831 | |
| 4832 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) { |
| 4833 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4834 | sp<FakeWindowHandle> window = |
| 4835 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4836 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4837 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4838 | |
| 4839 | // Start a three-finger touchpad swipe |
| 4840 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4841 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4842 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4843 | .build()); |
| 4844 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 4845 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4846 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4847 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4848 | .build()); |
| 4849 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 4850 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4851 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4852 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 4853 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4854 | .build()); |
| 4855 | |
| 4856 | // Move the swipe a bit |
| 4857 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4858 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4859 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4860 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4861 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4862 | .build()); |
| 4863 | |
| 4864 | // End the swipe |
| 4865 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 4866 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4867 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4868 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4869 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4870 | .build()); |
| 4871 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 4872 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4873 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4874 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4875 | .build()); |
| 4876 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4877 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4878 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4879 | .build()); |
| 4880 | |
| 4881 | window->assertNoEvents(); |
| 4882 | } |
| 4883 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4884 | /** |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4885 | * Send a two-pointer gesture to a single window. The window's orientation changes in response to |
| 4886 | * the first pointer. |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4887 | * 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] | 4888 | */ |
| 4889 | TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) { |
| 4890 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4891 | sp<FakeWindowHandle> window = |
| 4892 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4893 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4894 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4895 | |
| 4896 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4897 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4898 | .downTime(baseTime + 10) |
| 4899 | .eventTime(baseTime + 10) |
| 4900 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4901 | .build()); |
| 4902 | |
| 4903 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4904 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4905 | // Change the transform so that the orientation is now different from original. |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4906 | window->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4907 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4908 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4909 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4910 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4911 | .downTime(baseTime + 10) |
| 4912 | .eventTime(baseTime + 30) |
| 4913 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4914 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 4915 | .build()); |
| 4916 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4917 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4918 | |
| 4919 | // 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] | 4920 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4921 | .downTime(baseTime + 10) |
| 4922 | .eventTime(baseTime + 40) |
| 4923 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4924 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 4925 | .build()); |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4926 | |
| 4927 | window->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 4928 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4929 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4930 | .downTime(baseTime + 10) |
| 4931 | .eventTime(baseTime + 50) |
| 4932 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4933 | .build()); |
| 4934 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4935 | window->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 4936 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4937 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4938 | .downTime(baseTime + 60) |
| 4939 | .eventTime(baseTime + 60) |
| 4940 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40)) |
| 4941 | .build()); |
| 4942 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4943 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4944 | } |
| 4945 | |
| 4946 | /** |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 4947 | * When there are multiple screens, such as screen projection to TV or screen recording, if the |
| 4948 | * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and |
| 4949 | * its coordinates should be converted by the transform of the windows of target screen. |
| 4950 | */ |
| 4951 | TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) { |
| 4952 | // This case will create a window and a spy window on the default display and mirror |
| 4953 | // window on the second display. cancel event is sent through spy window pilferPointers |
| 4954 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4955 | |
| 4956 | sp<FakeWindowHandle> spyWindowDefaultDisplay = |
| 4957 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 4958 | spyWindowDefaultDisplay->setTrustedOverlay(true); |
| 4959 | spyWindowDefaultDisplay->setSpy(true); |
| 4960 | |
| 4961 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 4962 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 4963 | ADISPLAY_ID_DEFAULT); |
| 4964 | windowDefaultDisplay->setWindowTransform(1, 0, 0, 1); |
| 4965 | |
| 4966 | sp<FakeWindowHandle> windowSecondDisplay = windowDefaultDisplay->clone(SECOND_DISPLAY_ID); |
| 4967 | windowSecondDisplay->setWindowTransform(2, 0, 0, 2); |
| 4968 | |
| 4969 | // Add the windows to the dispatcher |
| 4970 | mDispatcher->onWindowInfosChanged( |
| 4971 | {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(), |
| 4972 | *windowSecondDisplay->getInfo()}, |
| 4973 | {}, |
| 4974 | 0, |
| 4975 | 0}); |
| 4976 | |
| 4977 | // Send down to ADISPLAY_ID_DEFAULT |
| 4978 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4979 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4980 | {100, 100})) |
| 4981 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4982 | |
| 4983 | spyWindowDefaultDisplay->consumeMotionDown(); |
| 4984 | windowDefaultDisplay->consumeMotionDown(); |
| 4985 | |
| 4986 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken())); |
| 4987 | |
| 4988 | // windowDefaultDisplay gets cancel |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 4989 | std::unique_ptr<MotionEvent> event = windowDefaultDisplay->consumeMotionEvent(); |
| 4990 | ASSERT_NE(nullptr, event); |
| 4991 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event->getAction()); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 4992 | |
| 4993 | // The cancel event is sent to windowDefaultDisplay of the ADISPLAY_ID_DEFAULT display, so the |
| 4994 | // coordinates of the cancel are converted by windowDefaultDisplay's transform, the x and y |
| 4995 | // coordinates are both 100, otherwise if the cancel event is sent to windowSecondDisplay of |
| 4996 | // SECOND_DISPLAY_ID, the x and y coordinates are 200 |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 4997 | EXPECT_EQ(100, event->getX(0)); |
| 4998 | EXPECT_EQ(100, event->getY(0)); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 4999 | } |
| 5000 | |
| 5001 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5002 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 5003 | * |
| 5004 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 5005 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 5006 | * space. |
| 5007 | */ |
| 5008 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 5009 | public: |
| 5010 | void SetUp() override { |
| 5011 | InputDispatcherTest::SetUp(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5012 | removeAllWindowsAndDisplays(); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5013 | } |
| 5014 | |
| 5015 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 5016 | gui::DisplayInfo info; |
| 5017 | info.displayId = displayId; |
| 5018 | info.transform = transform; |
| 5019 | mDisplayInfos.push_back(std::move(info)); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5020 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5021 | } |
| 5022 | |
| 5023 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 5024 | mWindowInfos.push_back(*windowHandle->getInfo()); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 5025 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5026 | } |
| 5027 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5028 | void removeAllWindowsAndDisplays() { |
| 5029 | mDisplayInfos.clear(); |
| 5030 | mWindowInfos.clear(); |
| 5031 | } |
| 5032 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5033 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 5034 | // on the display. |
| 5035 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 5036 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 5037 | // respectively. |
| 5038 | ui::Transform displayTransform; |
| 5039 | displayTransform.set(2, 0, 0, 4); |
| 5040 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 5041 | |
| 5042 | std::shared_ptr<FakeApplicationHandle> application = |
| 5043 | std::make_shared<FakeApplicationHandle>(); |
| 5044 | |
| 5045 | // Add two windows to the display. Their frames are represented in the display space. |
| 5046 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5047 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5048 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5049 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 5050 | addWindow(firstWindow); |
| 5051 | |
| 5052 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5053 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5054 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5055 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 5056 | addWindow(secondWindow); |
| 5057 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 5058 | } |
| 5059 | |
| 5060 | private: |
| 5061 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 5062 | std::vector<gui::WindowInfo> mWindowInfos; |
| 5063 | }; |
| 5064 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5065 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5066 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5067 | // 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] | 5068 | // selected so that if the hit test was performed with the point and the bounds being in |
| 5069 | // different coordinate spaces, the event would end up in the incorrect window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5070 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5071 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5072 | {PointF{75, 55}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5073 | |
| 5074 | firstWindow->consumeMotionDown(); |
| 5075 | secondWindow->assertNoEvents(); |
| 5076 | } |
| 5077 | |
| 5078 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 5079 | // the event should be treated as being in the logical display space. |
| 5080 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 5081 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5082 | // Send down to the first window. The point is represented in the logical display space. The |
| 5083 | // point is selected so that if the hit test was done in logical display space, then it would |
| 5084 | // end up in the incorrect window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5085 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5086 | PointF{75 * 2, 55 * 4}); |
| 5087 | |
| 5088 | firstWindow->consumeMotionDown(); |
| 5089 | secondWindow->assertNoEvents(); |
| 5090 | } |
| 5091 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5092 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 5093 | // event should be treated as being in the logical display space. |
| 5094 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 5095 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5096 | |
| 5097 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5098 | ui::Transform injectedEventTransform; |
| 5099 | injectedEventTransform.set(matrix); |
| 5100 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 5101 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 5102 | |
| 5103 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5104 | .displayId(ADISPLAY_ID_DEFAULT) |
| 5105 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 5106 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5107 | .x(untransformedPoint.x) |
| 5108 | .y(untransformedPoint.y)) |
| 5109 | .build(); |
| 5110 | event.transform(matrix); |
| 5111 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5112 | injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5113 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 5114 | |
| 5115 | firstWindow->consumeMotionDown(); |
| 5116 | secondWindow->assertNoEvents(); |
| 5117 | } |
| 5118 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5119 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 5120 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5121 | |
| 5122 | // Send down to the second window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5123 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5124 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5125 | {PointF{150, 220}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5126 | |
| 5127 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5128 | std::unique_ptr<MotionEvent> event = secondWindow->consumeMotionEvent(); |
| 5129 | ASSERT_NE(nullptr, event); |
| 5130 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5131 | |
| 5132 | // 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] | 5133 | EXPECT_EQ(300, event->getRawX(0)); |
| 5134 | EXPECT_EQ(880, event->getRawY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5135 | |
| 5136 | // Ensure that the x and y values are in the window's coordinate space. |
| 5137 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 5138 | // 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] | 5139 | EXPECT_EQ(100, event->getX(0)); |
| 5140 | EXPECT_EQ(80, event->getY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5141 | } |
| 5142 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5143 | TEST_F(InputDispatcherDisplayProjectionTest, CancelMotionWithCorrectCoordinates) { |
| 5144 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5145 | // The monitor will always receive events in the logical display's coordinate space, because |
| 5146 | // it does not have a window. |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5147 | FakeMonitorReceiver monitor{*mDispatcher, "Monitor", ADISPLAY_ID_DEFAULT}; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5148 | |
| 5149 | // Send down to the first window. |
| 5150 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5151 | ADISPLAY_ID_DEFAULT, {PointF{50, 100}})); |
| 5152 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5153 | monitor.consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5154 | |
| 5155 | // Second pointer goes down on second window. |
| 5156 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5157 | ADISPLAY_ID_DEFAULT, |
| 5158 | {PointF{50, 100}, PointF{150, 220}})); |
| 5159 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80))); |
| 5160 | const std::map<int32_t, PointF> expectedMonitorPointers{{0, PointF{100, 400}}, |
| 5161 | {1, PointF{300, 880}}}; |
| 5162 | monitor.consumeMotionEvent( |
| 5163 | AllOf(WithMotionAction(POINTER_1_DOWN), WithPointers(expectedMonitorPointers))); |
| 5164 | |
| 5165 | mDispatcher->cancelCurrentTouch(); |
| 5166 | |
| 5167 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 5168 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 80))); |
| 5169 | monitor.consumeMotionEvent( |
| 5170 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedMonitorPointers))); |
| 5171 | } |
| 5172 | |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 5173 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeDownWithCorrectCoordinates) { |
| 5174 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5175 | |
| 5176 | // Send down to the first window. |
| 5177 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5178 | ADISPLAY_ID_DEFAULT, {PointF{50, 100}})); |
| 5179 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5180 | |
| 5181 | // The pointer is transferred to the second window, and the second window receives it in the |
| 5182 | // correct coordinate space. |
| 5183 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 5184 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 5185 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(-100, -400))); |
| 5186 | } |
| 5187 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5188 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverEnterExitWithCorrectCoordinates) { |
| 5189 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5190 | |
| 5191 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 5192 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 5193 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5194 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5195 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5196 | |
| 5197 | // Touch down at the same location and ensure a hover exit is synthesized. |
| 5198 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
| 5199 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5200 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5201 | WithRawCoords(300, 880))); |
| 5202 | secondWindow->consumeMotionEvent( |
| 5203 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5204 | secondWindow->assertNoEvents(); |
| 5205 | firstWindow->assertNoEvents(); |
| 5206 | } |
| 5207 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5208 | // Same as above, but while the window is being mirrored. |
| 5209 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 5210 | SynthesizeHoverEnterExitWithCorrectCoordinatesWhenMirrored) { |
| 5211 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5212 | |
| 5213 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5214 | ui::Transform secondDisplayTransform; |
| 5215 | secondDisplayTransform.set(matrix); |
| 5216 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 5217 | |
| 5218 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 5219 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 5220 | addWindow(secondWindowClone); |
| 5221 | |
| 5222 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 5223 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 5224 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5225 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5226 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5227 | |
| 5228 | // Touch down at the same location and ensure a hover exit is synthesized for the correct |
| 5229 | // display. |
| 5230 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
| 5231 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5232 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5233 | WithRawCoords(300, 880))); |
| 5234 | secondWindow->consumeMotionEvent( |
| 5235 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5236 | secondWindow->assertNoEvents(); |
| 5237 | firstWindow->assertNoEvents(); |
| 5238 | } |
| 5239 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5240 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverCancelationWithCorrectCoordinates) { |
| 5241 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5242 | |
| 5243 | // Send hover enter to second window |
| 5244 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
| 5245 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5246 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5247 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5248 | |
| 5249 | mDispatcher->cancelCurrentTouch(); |
| 5250 | |
| 5251 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5252 | WithRawCoords(300, 880))); |
| 5253 | secondWindow->assertNoEvents(); |
| 5254 | firstWindow->assertNoEvents(); |
| 5255 | } |
| 5256 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5257 | // Same as above, but while the window is being mirrored. |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5258 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 5259 | SynthesizeHoverCancelationWithCorrectCoordinatesWhenMirrored) { |
| 5260 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5261 | |
| 5262 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5263 | ui::Transform secondDisplayTransform; |
| 5264 | secondDisplayTransform.set(matrix); |
| 5265 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 5266 | |
| 5267 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 5268 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 5269 | addWindow(secondWindowClone); |
| 5270 | |
| 5271 | // Send hover enter to second window |
| 5272 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
| 5273 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5274 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5275 | WithCoords(100, 80), WithRawCoords(300, 880), |
| 5276 | WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 5277 | |
| 5278 | mDispatcher->cancelCurrentTouch(); |
| 5279 | |
| 5280 | // Ensure the cancelation happens with the correct displayId and the correct coordinates. |
| 5281 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5282 | WithRawCoords(300, 880), |
| 5283 | WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 5284 | secondWindow->assertNoEvents(); |
| 5285 | firstWindow->assertNoEvents(); |
| 5286 | } |
| 5287 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5288 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ |
| 5289 | class InputDispatcherDisplayOrientationFixture |
| 5290 | : public InputDispatcherDisplayProjectionTest, |
| 5291 | public ::testing::WithParamInterface<ui::Rotation> {}; |
| 5292 | |
| 5293 | // This test verifies the touchable region of a window for all rotations of the display by tapping |
| 5294 | // in different locations on the display, specifically points close to the four corners of a |
| 5295 | // window. |
| 5296 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { |
| 5297 | constexpr static int32_t displayWidth = 400; |
| 5298 | constexpr static int32_t displayHeight = 800; |
| 5299 | |
| 5300 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5301 | |
| 5302 | const auto rotation = GetParam(); |
| 5303 | |
| 5304 | // Set up the display with the specified rotation. |
| 5305 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 5306 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 5307 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 5308 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 5309 | logicalDisplayWidth, logicalDisplayHeight); |
| 5310 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 5311 | |
| 5312 | // Create a window with its bounds determined in the logical display. |
| 5313 | const Rect frameInLogicalDisplay(100, 100, 200, 300); |
| 5314 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); |
| 5315 | sp<FakeWindowHandle> window = |
| 5316 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 5317 | window->setFrame(frameInDisplay, displayTransform); |
| 5318 | addWindow(window); |
| 5319 | |
| 5320 | // The following points in logical display space should be inside the window. |
| 5321 | static const std::array<vec2, 4> insidePoints{ |
| 5322 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 5323 | for (const auto pointInsideWindow : insidePoints) { |
| 5324 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); |
| 5325 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5326 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5327 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5328 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5329 | window->consumeMotionDown(); |
| 5330 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5331 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 5332 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5333 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5334 | window->consumeMotionUp(); |
| 5335 | } |
| 5336 | |
| 5337 | // The following points in logical display space should be outside the window. |
| 5338 | static const std::array<vec2, 5> outsidePoints{ |
| 5339 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 5340 | for (const auto pointOutsideWindow : outsidePoints) { |
| 5341 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); |
| 5342 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5343 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5344 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5345 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5346 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5347 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 5348 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5349 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5350 | } |
| 5351 | window->assertNoEvents(); |
| 5352 | } |
| 5353 | |
| 5354 | // Run the precision tests for all rotations. |
| 5355 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, |
| 5356 | InputDispatcherDisplayOrientationFixture, |
| 5357 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, |
| 5358 | ui::ROTATION_270), |
| 5359 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { |
| 5360 | return ftl::enum_string(testParamInfo.param); |
| 5361 | }); |
| 5362 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5363 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 5364 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5365 | |
| 5366 | class TransferTouchFixture : public InputDispatcherTest, |
| 5367 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 5368 | |
| 5369 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5370 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5371 | |
| 5372 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5373 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5374 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5375 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5376 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5377 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5378 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5379 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5380 | sp<FakeWindowHandle> wallpaper = |
| 5381 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 5382 | wallpaper->setIsWallpaper(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5383 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5384 | mDispatcher->onWindowInfosChanged( |
| 5385 | {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5386 | |
| 5387 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5388 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5389 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5390 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5391 | // Only the first window should get the down event |
| 5392 | firstWindow->consumeMotionDown(); |
| 5393 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5394 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5395 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5396 | // Transfer touch to the second window |
| 5397 | TransferFunction f = GetParam(); |
| 5398 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5399 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5400 | // The first window gets cancel and the second gets down |
| 5401 | firstWindow->consumeMotionCancel(); |
| 5402 | secondWindow->consumeMotionDown(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5403 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5404 | |
| 5405 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5406 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5407 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5408 | // The first window gets no events and the second gets up |
| 5409 | firstWindow->assertNoEvents(); |
| 5410 | secondWindow->consumeMotionUp(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5411 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5412 | } |
| 5413 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5414 | /** |
| 5415 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 5416 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 5417 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 5418 | * natural to the user. |
| 5419 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 5420 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 5421 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 5422 | * the first window to the second. |
| 5423 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 5424 | * the other API, as well. |
| 5425 | */ |
| 5426 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 5427 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5428 | |
| 5429 | // Create a couple of windows + a spy window |
| 5430 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5431 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5432 | spyWindow->setTrustedOverlay(true); |
| 5433 | spyWindow->setSpy(true); |
| 5434 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5435 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5436 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5437 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5438 | |
| 5439 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5440 | mDispatcher->onWindowInfosChanged( |
| 5441 | {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5442 | |
| 5443 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5444 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5445 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5446 | // Only the first window and spy should get the down event |
| 5447 | spyWindow->consumeMotionDown(); |
| 5448 | firstWindow->consumeMotionDown(); |
| 5449 | |
| 5450 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 5451 | // if f === 'transferTouch'. |
| 5452 | TransferFunction f = GetParam(); |
| 5453 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5454 | ASSERT_TRUE(success); |
| 5455 | // The first window gets cancel and the second gets down |
| 5456 | firstWindow->consumeMotionCancel(); |
| 5457 | secondWindow->consumeMotionDown(); |
| 5458 | |
| 5459 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5460 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5461 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5462 | // The first window gets no events and the second+spy get up |
| 5463 | firstWindow->assertNoEvents(); |
| 5464 | spyWindow->consumeMotionUp(); |
| 5465 | secondWindow->consumeMotionUp(); |
| 5466 | } |
| 5467 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5468 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5469 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5470 | |
| 5471 | PointF touchPoint = {10, 10}; |
| 5472 | |
| 5473 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5474 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5475 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5476 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5477 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5478 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5479 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5480 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5481 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5482 | |
| 5483 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5484 | mDispatcher->onWindowInfosChanged( |
| 5485 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5486 | |
| 5487 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5488 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5489 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5490 | {touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5491 | // Only the first window should get the down event |
| 5492 | firstWindow->consumeMotionDown(); |
| 5493 | secondWindow->assertNoEvents(); |
| 5494 | |
| 5495 | // Send pointer down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5496 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5497 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5498 | // Only the first window should get the pointer down event |
| 5499 | firstWindow->consumeMotionPointerDown(1); |
| 5500 | secondWindow->assertNoEvents(); |
| 5501 | |
| 5502 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5503 | TransferFunction f = GetParam(); |
| 5504 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5505 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5506 | // The first window gets cancel and the second gets down and pointer down |
| 5507 | firstWindow->consumeMotionCancel(); |
| 5508 | secondWindow->consumeMotionDown(); |
| 5509 | secondWindow->consumeMotionPointerDown(1); |
| 5510 | |
| 5511 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5512 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5513 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5514 | // The first window gets nothing and the second gets pointer up |
| 5515 | firstWindow->assertNoEvents(); |
| 5516 | secondWindow->consumeMotionPointerUp(1); |
| 5517 | |
| 5518 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5519 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5520 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5521 | // The first window gets nothing and the second gets up |
| 5522 | firstWindow->assertNoEvents(); |
| 5523 | secondWindow->consumeMotionUp(); |
| 5524 | } |
| 5525 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5526 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 5527 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5528 | |
| 5529 | // Create a couple of windows |
| 5530 | sp<FakeWindowHandle> firstWindow = |
| 5531 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5532 | ADISPLAY_ID_DEFAULT); |
| 5533 | firstWindow->setDupTouchToWallpaper(true); |
| 5534 | sp<FakeWindowHandle> secondWindow = |
| 5535 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5536 | ADISPLAY_ID_DEFAULT); |
| 5537 | secondWindow->setDupTouchToWallpaper(true); |
| 5538 | |
| 5539 | sp<FakeWindowHandle> wallpaper1 = |
| 5540 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 5541 | wallpaper1->setIsWallpaper(true); |
| 5542 | |
| 5543 | sp<FakeWindowHandle> wallpaper2 = |
| 5544 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 5545 | wallpaper2->setIsWallpaper(true); |
| 5546 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5547 | mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(), |
| 5548 | *secondWindow->getInfo(), *wallpaper2->getInfo()}, |
| 5549 | {}, |
| 5550 | 0, |
| 5551 | 0}); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5552 | |
| 5553 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5554 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5555 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5556 | |
| 5557 | // Only the first window should get the down event |
| 5558 | firstWindow->consumeMotionDown(); |
| 5559 | secondWindow->assertNoEvents(); |
| 5560 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5561 | wallpaper2->assertNoEvents(); |
| 5562 | |
| 5563 | // Transfer touch focus to the second window |
| 5564 | TransferFunction f = GetParam(); |
| 5565 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5566 | ASSERT_TRUE(success); |
| 5567 | |
| 5568 | // The first window gets cancel and the second gets down |
| 5569 | firstWindow->consumeMotionCancel(); |
| 5570 | secondWindow->consumeMotionDown(); |
| 5571 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5572 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5573 | |
| 5574 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5575 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5576 | ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5577 | // The first window gets no events and the second gets up |
| 5578 | firstWindow->assertNoEvents(); |
| 5579 | secondWindow->consumeMotionUp(); |
| 5580 | wallpaper1->assertNoEvents(); |
| 5581 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5582 | } |
| 5583 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5584 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 5585 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 5586 | // for the case where there are multiple pointers split across several windows. |
| 5587 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 5588 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5589 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 5590 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5591 | return dispatcher->transferTouch(destChannelToken, |
| 5592 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5593 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5594 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 5595 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5596 | return dispatcher->transferTouchFocus(from, to, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5597 | /*isDragAndDrop=*/false); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5598 | })); |
| 5599 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5600 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5601 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5602 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5603 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5604 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5605 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5606 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5607 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5608 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5609 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5610 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5611 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5612 | |
| 5613 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5614 | mDispatcher->onWindowInfosChanged( |
| 5615 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5616 | |
| 5617 | PointF pointInFirst = {300, 200}; |
| 5618 | PointF pointInSecond = {300, 600}; |
| 5619 | |
| 5620 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5621 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5622 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5623 | {pointInFirst})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5624 | // Only the first window should get the down event |
| 5625 | firstWindow->consumeMotionDown(); |
| 5626 | secondWindow->assertNoEvents(); |
| 5627 | |
| 5628 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5629 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5630 | ADISPLAY_ID_DEFAULT, |
| 5631 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5632 | // The first window gets a move and the second a down |
| 5633 | firstWindow->consumeMotionMove(); |
| 5634 | secondWindow->consumeMotionDown(); |
| 5635 | |
| 5636 | // Transfer touch focus to the second window |
| 5637 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 5638 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 5639 | firstWindow->consumeMotionCancel(); |
| 5640 | secondWindow->consumeMotionPointerDown(1); |
| 5641 | |
| 5642 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5643 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5644 | ADISPLAY_ID_DEFAULT, |
| 5645 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5646 | // The first window gets nothing and the second gets pointer up |
| 5647 | firstWindow->assertNoEvents(); |
| 5648 | secondWindow->consumeMotionPointerUp(1); |
| 5649 | |
| 5650 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5651 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5652 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5653 | // The first window gets nothing and the second gets up |
| 5654 | firstWindow->assertNoEvents(); |
| 5655 | secondWindow->consumeMotionUp(); |
| 5656 | } |
| 5657 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5658 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 5659 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 5660 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 5661 | // window should get nothing. |
| 5662 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 5663 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5664 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5665 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5666 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5667 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5668 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5669 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5670 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5671 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5672 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5673 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5674 | |
| 5675 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5676 | mDispatcher->onWindowInfosChanged( |
| 5677 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5678 | |
| 5679 | PointF pointInFirst = {300, 200}; |
| 5680 | PointF pointInSecond = {300, 600}; |
| 5681 | |
| 5682 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5683 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5684 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5685 | {pointInFirst})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5686 | // Only the first window should get the down event |
| 5687 | firstWindow->consumeMotionDown(); |
| 5688 | secondWindow->assertNoEvents(); |
| 5689 | |
| 5690 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5691 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5692 | ADISPLAY_ID_DEFAULT, |
| 5693 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5694 | // The first window gets a move and the second a down |
| 5695 | firstWindow->consumeMotionMove(); |
| 5696 | secondWindow->consumeMotionDown(); |
| 5697 | |
| 5698 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5699 | const bool transferred = |
| 5700 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5701 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 5702 | ASSERT_FALSE(transferred); |
| 5703 | firstWindow->assertNoEvents(); |
| 5704 | secondWindow->assertNoEvents(); |
| 5705 | |
| 5706 | // The rest of the dispatch should proceed as normal |
| 5707 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5708 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5709 | ADISPLAY_ID_DEFAULT, |
| 5710 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5711 | // The first window gets MOVE and the second gets pointer up |
| 5712 | firstWindow->consumeMotionMove(); |
| 5713 | secondWindow->consumeMotionUp(); |
| 5714 | |
| 5715 | // Send up event to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5716 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5717 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5718 | // The first window gets nothing and the second gets up |
| 5719 | firstWindow->consumeMotionUp(); |
| 5720 | secondWindow->assertNoEvents(); |
| 5721 | } |
| 5722 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5723 | // This case will create two windows and one mirrored window on the default display and mirror |
| 5724 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 5725 | // the windows info of second display before default display. |
| 5726 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 5727 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5728 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5729 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5730 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5731 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5732 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5733 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5734 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5735 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5736 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5737 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5738 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5739 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5740 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5741 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5742 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5743 | |
| 5744 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5745 | mDispatcher->onWindowInfosChanged( |
| 5746 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 5747 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 5748 | *secondWindowInPrimary->getInfo()}, |
| 5749 | {}, |
| 5750 | 0, |
| 5751 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5752 | |
| 5753 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5754 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5755 | {50, 50})) |
| 5756 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5757 | |
| 5758 | // Window should receive motion event. |
| 5759 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5760 | |
| 5761 | // Transfer touch focus |
| 5762 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 5763 | secondWindowInPrimary->getToken())); |
| 5764 | // The first window gets cancel. |
| 5765 | firstWindowInPrimary->consumeMotionCancel(); |
| 5766 | secondWindowInPrimary->consumeMotionDown(); |
| 5767 | |
| 5768 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5769 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5770 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 5771 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5772 | firstWindowInPrimary->assertNoEvents(); |
| 5773 | secondWindowInPrimary->consumeMotionMove(); |
| 5774 | |
| 5775 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5776 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5777 | {150, 50})) |
| 5778 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5779 | firstWindowInPrimary->assertNoEvents(); |
| 5780 | secondWindowInPrimary->consumeMotionUp(); |
| 5781 | } |
| 5782 | |
| 5783 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 5784 | // 'transferTouch' api. |
| 5785 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 5786 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5787 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5788 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5789 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5790 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5791 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5792 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5793 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5794 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5795 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5796 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5797 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5798 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5799 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5800 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5801 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5802 | |
| 5803 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5804 | mDispatcher->onWindowInfosChanged( |
| 5805 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 5806 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 5807 | *secondWindowInPrimary->getInfo()}, |
| 5808 | {}, |
| 5809 | 0, |
| 5810 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5811 | |
| 5812 | // Touch on second display. |
| 5813 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5814 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 5815 | {50, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5816 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5817 | |
| 5818 | // Window should receive motion event. |
| 5819 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 5820 | |
| 5821 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5822 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5823 | |
| 5824 | // The first window gets cancel. |
| 5825 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 5826 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 5827 | |
| 5828 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5829 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5830 | SECOND_DISPLAY_ID, {150, 50})) |
| 5831 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5832 | firstWindowInPrimary->assertNoEvents(); |
| 5833 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 5834 | |
| 5835 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5836 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5837 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5838 | firstWindowInPrimary->assertNoEvents(); |
| 5839 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 5840 | } |
| 5841 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5842 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5843 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5844 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5845 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5846 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5847 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5848 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5849 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5850 | |
| 5851 | window->consumeFocusEvent(true); |
| 5852 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5853 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5854 | |
| 5855 | // Window should receive key down event. |
| 5856 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5857 | |
| 5858 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5859 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5860 | mFakePolicy->assertUserActivityPoked(); |
| 5861 | } |
| 5862 | |
| 5863 | TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) { |
| 5864 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5865 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5866 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5867 | |
| 5868 | window->setDisableUserActivity(true); |
| 5869 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5870 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5871 | setFocusedWindow(window); |
| 5872 | |
| 5873 | window->consumeFocusEvent(true); |
| 5874 | |
| 5875 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5876 | |
| 5877 | // Window should receive key down event. |
| 5878 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5879 | |
| 5880 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5881 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5882 | mFakePolicy->assertUserActivityNotPoked(); |
| 5883 | } |
| 5884 | |
| 5885 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) { |
| 5886 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5887 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5888 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5889 | |
| 5890 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5891 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5892 | setFocusedWindow(window); |
| 5893 | |
| 5894 | window->consumeFocusEvent(true); |
| 5895 | |
| 5896 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5897 | mDispatcher->waitForIdle(); |
| 5898 | |
| 5899 | // System key is not passed down |
| 5900 | window->assertNoEvents(); |
| 5901 | |
| 5902 | // Should have poked user activity |
| 5903 | mFakePolicy->assertUserActivityPoked(); |
| 5904 | } |
| 5905 | |
| 5906 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) { |
| 5907 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5908 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5909 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5910 | |
| 5911 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5912 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5913 | setFocusedWindow(window); |
| 5914 | |
| 5915 | window->consumeFocusEvent(true); |
| 5916 | |
| 5917 | mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5918 | mDispatcher->waitForIdle(); |
| 5919 | |
| 5920 | // System key is not passed down |
| 5921 | window->assertNoEvents(); |
| 5922 | |
| 5923 | // Should have poked user activity |
| 5924 | mFakePolicy->assertUserActivityPoked(); |
| 5925 | } |
| 5926 | |
| 5927 | TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) { |
| 5928 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5929 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5930 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5931 | |
| 5932 | window->setDisableUserActivity(true); |
| 5933 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5934 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5935 | setFocusedWindow(window); |
| 5936 | |
| 5937 | window->consumeFocusEvent(true); |
| 5938 | |
| 5939 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5940 | mDispatcher->waitForIdle(); |
| 5941 | |
| 5942 | // System key is not passed down |
| 5943 | window->assertNoEvents(); |
| 5944 | |
| 5945 | // Should have poked user activity |
| 5946 | mFakePolicy->assertUserActivityPoked(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5947 | } |
| 5948 | |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5949 | TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) { |
| 5950 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5951 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5952 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5953 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5954 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5955 | |
| 5956 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5957 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5958 | ADISPLAY_ID_DEFAULT, {100, 100})) |
| 5959 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5960 | |
| 5961 | window->consumeMotionEvent( |
| 5962 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 5963 | |
| 5964 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5965 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5966 | mFakePolicy->assertUserActivityPoked(); |
| 5967 | } |
| 5968 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5969 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5970 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5971 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5972 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5973 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5974 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5975 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5976 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5977 | mDispatcher->waitForIdle(); |
| 5978 | |
| 5979 | window->assertNoEvents(); |
| 5980 | } |
| 5981 | |
| 5982 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 5983 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5984 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5985 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5986 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5987 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5988 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5989 | |
| 5990 | // Send key |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5991 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5992 | // Send motion |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5993 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5994 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5995 | |
| 5996 | // Window should receive only the motion event |
| 5997 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5998 | window->assertNoEvents(); // Key event or focus event will not be received |
| 5999 | } |
| 6000 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6001 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 6002 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6003 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6004 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6005 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 6006 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6007 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6008 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6009 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6010 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 6011 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6012 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6013 | |
| 6014 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6015 | mDispatcher->onWindowInfosChanged( |
| 6016 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6017 | |
| 6018 | PointF pointInFirst = {300, 200}; |
| 6019 | PointF pointInSecond = {300, 600}; |
| 6020 | |
| 6021 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6022 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 6023 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6024 | {pointInFirst})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6025 | // Only the first window should get the down event |
| 6026 | firstWindow->consumeMotionDown(); |
| 6027 | secondWindow->assertNoEvents(); |
| 6028 | |
| 6029 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6030 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6031 | ADISPLAY_ID_DEFAULT, |
| 6032 | {pointInFirst, pointInSecond})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6033 | // The first window gets a move and the second a down |
| 6034 | firstWindow->consumeMotionMove(); |
| 6035 | secondWindow->consumeMotionDown(); |
| 6036 | |
| 6037 | // Send pointer cancel to the second window |
| 6038 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6039 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6040 | {pointInFirst, pointInSecond}); |
| 6041 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6042 | mDispatcher->notifyMotion(pointerUpMotionArgs); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6043 | // The first window gets move and the second gets cancel. |
| 6044 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 6045 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 6046 | |
| 6047 | // Send up event. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6048 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6049 | ADISPLAY_ID_DEFAULT)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6050 | // The first window gets up and the second gets nothing. |
| 6051 | firstWindow->consumeMotionUp(); |
| 6052 | secondWindow->assertNoEvents(); |
| 6053 | } |
| 6054 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6055 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 6056 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6057 | |
| 6058 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6059 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6060 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6061 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 6062 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 6063 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 6064 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6065 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6066 | window->assertNoEvents(); |
| 6067 | mDispatcher->waitForIdle(); |
| 6068 | } |
| 6069 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6070 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 6071 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6072 | /** |
| 6073 | * Two entities that receive touch: A window, and a global monitor. |
| 6074 | * The touch goes to the window, and then the window disappears. |
| 6075 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 6076 | * for the monitor, as well. |
| 6077 | * 1. foregroundWindow |
| 6078 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 6079 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6080 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6081 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6082 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6083 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6084 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6085 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6086 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6087 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6088 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6089 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6090 | {100, 200})) |
| 6091 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6092 | |
| 6093 | // Both the foreground window and the global monitor should receive the touch down |
| 6094 | window->consumeMotionDown(); |
| 6095 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6096 | |
| 6097 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6098 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6099 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 6100 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6101 | |
| 6102 | window->consumeMotionMove(); |
| 6103 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6104 | |
| 6105 | // Now the foreground window goes away |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6106 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6107 | window->consumeMotionCancel(); |
| 6108 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 6109 | |
| 6110 | // If more events come in, there will be no more foreground window to send them to. This will |
| 6111 | // cause a cancel for the monitor, as well. |
| 6112 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6113 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6114 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 6115 | << "Injection should fail because the window was removed"; |
| 6116 | window->assertNoEvents(); |
| 6117 | // Global monitor now gets the cancel |
| 6118 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6119 | } |
| 6120 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6121 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6122 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6123 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6124 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6125 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6126 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6127 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6128 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6129 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6130 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6131 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6132 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6133 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6134 | } |
| 6135 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6136 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6137 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6138 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6139 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6140 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6141 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6142 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6143 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6144 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6145 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6146 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6147 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6148 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6149 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6150 | // Pilfer pointers from the monitor. |
| 6151 | // This should not do anything and the window should continue to receive events. |
| 6152 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6153 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6154 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6155 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6156 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6157 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6158 | |
| 6159 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6160 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6161 | } |
| 6162 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6163 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6164 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6165 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6166 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6167 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6168 | window->setWindowOffset(20, 40); |
| 6169 | window->setWindowTransform(0, 1, -1, 0); |
| 6170 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6171 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6172 | |
| 6173 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6174 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6175 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6176 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6177 | std::unique_ptr<MotionEvent> event = monitor.consumeMotion(); |
| 6178 | ASSERT_NE(nullptr, event); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6179 | // Even though window has transform, gesture monitor must not. |
| 6180 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 6181 | } |
| 6182 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6183 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6184 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6185 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6186 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6187 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6188 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6189 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 6190 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6191 | } |
| 6192 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6193 | /** |
| 6194 | * Two displays |
| 6195 | * The first monitor has a foreground window, a monitor |
| 6196 | * The second window has only one monitor. |
| 6197 | * We first inject a Down event into the first display, this injection should succeed and both |
| 6198 | * the foreground window and monitor should receive a down event, then inject a Down event into |
| 6199 | * the second display as well, this injection should fail, at this point, the first display |
| 6200 | * window and monitor should not receive a cancel or any other event. |
| 6201 | * Continue to inject Move and UP events to the first display, the events should be received |
| 6202 | * normally by the foreground window and monitor. |
| 6203 | */ |
| 6204 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCanceledWhenAnotherEmptyDisplayReceiveEvents) { |
| 6205 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6206 | sp<FakeWindowHandle> window = |
| 6207 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 6208 | |
| 6209 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6210 | FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 6211 | |
| 6212 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 6213 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6214 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6215 | {100, 200})) |
| 6216 | << "The down event injected into the first display should succeed"; |
| 6217 | |
| 6218 | window->consumeMotionDown(); |
| 6219 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6220 | |
| 6221 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6222 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 6223 | {100, 200})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6224 | << "The down event injected into the second display should fail since there's no " |
| 6225 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6226 | |
| 6227 | // Continue to inject event to first display. |
| 6228 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6229 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6230 | ADISPLAY_ID_DEFAULT, {110, 220})) |
| 6231 | << "The move event injected into the first display should succeed"; |
| 6232 | |
| 6233 | window->consumeMotionMove(); |
| 6234 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6235 | |
| 6236 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6237 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6238 | {110, 220})) |
| 6239 | << "The up event injected into the first display should succeed"; |
| 6240 | |
| 6241 | window->consumeMotionUp(); |
| 6242 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6243 | |
| 6244 | window->assertNoEvents(); |
| 6245 | monitor.assertNoEvents(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6246 | secondMonitor.assertNoEvents(); |
| 6247 | } |
| 6248 | |
| 6249 | /** |
| 6250 | * Two displays |
| 6251 | * There is a monitor and foreground window on each display. |
| 6252 | * First, we inject down events into each of the two displays, at this point, the foreground windows |
| 6253 | * and monitors on both displays should receive down events. |
| 6254 | * At this point, the foreground window of the second display goes away, the gone window should |
| 6255 | * receive the cancel event, and the other windows and monitors should not receive any events. |
| 6256 | * Inject a move event into the second display. At this point, the injection should fail because |
| 6257 | * the second display no longer has a foreground window. At this point, the monitor on the second |
| 6258 | * display should receive a cancel event, and any windows or monitors on the first display should |
| 6259 | * not receive any events, and any subsequent injection of events into the second display should |
| 6260 | * also fail. |
| 6261 | * Continue to inject events into the first display, and the events should all be injected |
| 6262 | * successfully and received normally. |
| 6263 | */ |
| 6264 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCancelWhenAnotherDisplayMonitorTouchCanceled) { |
| 6265 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6266 | sp<FakeWindowHandle> window = |
| 6267 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 6268 | sp<FakeWindowHandle> secondWindow = |
| 6269 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondForeground", |
| 6270 | SECOND_DISPLAY_ID); |
| 6271 | |
| 6272 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6273 | FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 6274 | |
| 6275 | // There is a foreground window on both displays. |
| 6276 | mDispatcher->onWindowInfosChanged({{*window->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
| 6277 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6278 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6279 | {100, 200})) |
| 6280 | << "The down event injected into the first display should succeed"; |
| 6281 | |
| 6282 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6283 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6284 | |
| 6285 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6286 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 6287 | {100, 200})) |
| 6288 | << "The down event injected into the second display should succeed"; |
| 6289 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6290 | secondWindow->consumeMotionDown(SECOND_DISPLAY_ID); |
| 6291 | secondMonitor.consumeMotionDown(SECOND_DISPLAY_ID); |
| 6292 | |
| 6293 | // Now second window is gone away. |
| 6294 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 6295 | |
| 6296 | // The gone window should receive a cancel, and the monitor on the second display should not |
| 6297 | // receive any events. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6298 | secondWindow->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 6299 | secondMonitor.assertNoEvents(); |
| 6300 | |
| 6301 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6302 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6303 | SECOND_DISPLAY_ID, {110, 220})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6304 | << "The move event injected into the second display should fail because there's no " |
| 6305 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6306 | // Now the monitor on the second display should receive a cancel event. |
| 6307 | secondMonitor.consumeMotionCancel(SECOND_DISPLAY_ID); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6308 | |
| 6309 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6310 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6311 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 6312 | << "The move event injected into the first display should succeed"; |
| 6313 | |
| 6314 | window->consumeMotionMove(); |
| 6315 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6316 | |
| 6317 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6318 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 6319 | {110, 220})) |
| 6320 | << "The up event injected into the second display should fail because there's no " |
| 6321 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6322 | |
| 6323 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6324 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6325 | {110, 220})) |
| 6326 | << "The up event injected into the first display should succeed"; |
| 6327 | |
| 6328 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6329 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6330 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6331 | window->assertNoEvents(); |
| 6332 | monitor.assertNoEvents(); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6333 | secondWindow->assertNoEvents(); |
| 6334 | secondMonitor.assertNoEvents(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6335 | } |
| 6336 | |
| 6337 | /** |
| 6338 | * One display with transform |
| 6339 | * There is a foreground window and a monitor on the display |
| 6340 | * Inject down event and move event sequentially, the foreground window and monitor can receive down |
| 6341 | * event and move event, then let the foreground window go away, the foreground window receives |
| 6342 | * cancel event, inject move event again, the monitor receives cancel event, all the events received |
| 6343 | * by the monitor should be with the same transform as the display |
| 6344 | */ |
| 6345 | TEST_F(InputDispatcherMonitorTest, MonitorTouchCancelEventWithDisplayTransform) { |
| 6346 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6347 | sp<FakeWindowHandle> window = |
| 6348 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 6349 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6350 | |
| 6351 | ui::Transform transform; |
| 6352 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 6353 | |
| 6354 | gui::DisplayInfo displayInfo; |
| 6355 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 6356 | displayInfo.transform = transform; |
| 6357 | |
| 6358 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
| 6359 | |
| 6360 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6361 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6362 | {100, 200})) |
| 6363 | << "The down event injected should succeed"; |
| 6364 | |
| 6365 | window->consumeMotionDown(); |
| 6366 | std::unique_ptr<MotionEvent> downMotionEvent = monitor.consumeMotion(); |
| 6367 | EXPECT_EQ(transform, downMotionEvent->getTransform()); |
| 6368 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, downMotionEvent->getAction()); |
| 6369 | |
| 6370 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6371 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6372 | ADISPLAY_ID_DEFAULT, {110, 220})) |
| 6373 | << "The move event injected should succeed"; |
| 6374 | |
| 6375 | window->consumeMotionMove(); |
| 6376 | std::unique_ptr<MotionEvent> moveMotionEvent = monitor.consumeMotion(); |
| 6377 | EXPECT_EQ(transform, moveMotionEvent->getTransform()); |
| 6378 | EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, moveMotionEvent->getAction()); |
| 6379 | |
| 6380 | // Let foreground window gone |
| 6381 | mDispatcher->onWindowInfosChanged({{}, {displayInfo}, 0, 0}); |
| 6382 | |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6383 | // Foreground window should receive a cancel event, but not the monitor. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6384 | window->consumeMotionCancel(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6385 | |
| 6386 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6387 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6388 | ADISPLAY_ID_DEFAULT, {110, 220})) |
| 6389 | << "The move event injected should failed"; |
| 6390 | // Now foreground should not receive any events, but monitor should receive a cancel event |
| 6391 | // with transform that same as display's display. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6392 | std::unique_ptr<MotionEvent> cancelMotionEvent = monitor.consumeMotion(); |
| 6393 | EXPECT_EQ(transform, cancelMotionEvent->getTransform()); |
| 6394 | EXPECT_EQ(ADISPLAY_ID_DEFAULT, cancelMotionEvent->getDisplayId()); |
| 6395 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, cancelMotionEvent->getAction()); |
| 6396 | |
| 6397 | // Other event inject to this display should fail. |
| 6398 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6399 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6400 | ADISPLAY_ID_DEFAULT, {110, 220})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6401 | << "The up event injected should fail because the touched window was removed"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6402 | window->assertNoEvents(); |
| 6403 | monitor.assertNoEvents(); |
| 6404 | } |
| 6405 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6406 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6407 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6408 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6409 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6410 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6411 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6412 | |
| 6413 | NotifyMotionArgs motionArgs = |
| 6414 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6415 | ADISPLAY_ID_DEFAULT); |
| 6416 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6417 | mDispatcher->notifyMotion(motionArgs); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6418 | // Window should receive motion down event. |
| 6419 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6420 | |
| 6421 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6422 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6423 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6424 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 6425 | motionArgs.pointerCoords[0].getX() - 10); |
| 6426 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6427 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 6428 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT, /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6429 | } |
| 6430 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6431 | /** |
| 6432 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 6433 | * the device default right away. In the test scenario, we check both the default value, |
| 6434 | * and the action of enabling / disabling. |
| 6435 | */ |
| 6436 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6437 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6438 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6439 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6440 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6441 | |
| 6442 | // Set focused application. |
| 6443 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6444 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6445 | |
| 6446 | SCOPED_TRACE("Check default value of touch mode"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6447 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6448 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6449 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6450 | |
| 6451 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6452 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6453 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6454 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6455 | |
| 6456 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6457 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6458 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6459 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6460 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6461 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6462 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6463 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6464 | |
| 6465 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6466 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6467 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6468 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6469 | |
| 6470 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6471 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6472 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6473 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6474 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6475 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6476 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6477 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6478 | |
| 6479 | window->assertNoEvents(); |
| 6480 | } |
| 6481 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6482 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6483 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6484 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6485 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6486 | |
| 6487 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6488 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6489 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6490 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6491 | setFocusedWindow(window); |
| 6492 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6493 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6494 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6495 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 6496 | mDispatcher->notifyKey(keyArgs); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6497 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6498 | std::unique_ptr<KeyEvent> event = window->consumeKey(); |
| 6499 | ASSERT_NE(event, nullptr); |
| 6500 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6501 | ASSERT_NE(verified, nullptr); |
| 6502 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 6503 | |
| 6504 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 6505 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 6506 | ASSERT_EQ(keyArgs.source, verified->source); |
| 6507 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 6508 | |
| 6509 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 6510 | |
| 6511 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6512 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6513 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6514 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 6515 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 6516 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 6517 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 6518 | } |
| 6519 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6520 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6521 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6522 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6523 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6524 | |
| 6525 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6526 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 6527 | ui::Transform transform; |
| 6528 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 6529 | |
| 6530 | gui::DisplayInfo displayInfo; |
| 6531 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 6532 | displayInfo.transform = transform; |
| 6533 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6534 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6535 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6536 | const NotifyMotionArgs motionArgs = |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6537 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6538 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6539 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6540 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6541 | std::unique_ptr<MotionEvent> event = window->consumeMotionEvent(); |
| 6542 | ASSERT_NE(nullptr, event); |
| 6543 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6544 | ASSERT_NE(verified, nullptr); |
| 6545 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 6546 | |
| 6547 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 6548 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 6549 | EXPECT_EQ(motionArgs.source, verified->source); |
| 6550 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 6551 | |
| 6552 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 6553 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 6554 | const vec2 rawXY = |
| 6555 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 6556 | motionArgs.pointerCoords[0].getXYValue()); |
| 6557 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 6558 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6559 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6560 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6561 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6562 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 6563 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 6564 | } |
| 6565 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 6566 | /** |
| 6567 | * Ensure that separate calls to sign the same data are generating the same key. |
| 6568 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 6569 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 6570 | * tests. |
| 6571 | */ |
| 6572 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 6573 | KeyEvent event = getTestKeyEvent(); |
| 6574 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 6575 | |
| 6576 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 6577 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 6578 | ASSERT_EQ(hmac1, hmac2); |
| 6579 | } |
| 6580 | |
| 6581 | /** |
| 6582 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 6583 | */ |
| 6584 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 6585 | KeyEvent event = getTestKeyEvent(); |
| 6586 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 6587 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 6588 | |
| 6589 | verifiedEvent.deviceId += 1; |
| 6590 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6591 | |
| 6592 | verifiedEvent.source += 1; |
| 6593 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6594 | |
| 6595 | verifiedEvent.eventTimeNanos += 1; |
| 6596 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6597 | |
| 6598 | verifiedEvent.displayId += 1; |
| 6599 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6600 | |
| 6601 | verifiedEvent.action += 1; |
| 6602 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6603 | |
| 6604 | verifiedEvent.downTimeNanos += 1; |
| 6605 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6606 | |
| 6607 | verifiedEvent.flags += 1; |
| 6608 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6609 | |
| 6610 | verifiedEvent.keyCode += 1; |
| 6611 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6612 | |
| 6613 | verifiedEvent.scanCode += 1; |
| 6614 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6615 | |
| 6616 | verifiedEvent.metaState += 1; |
| 6617 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6618 | |
| 6619 | verifiedEvent.repeatCount += 1; |
| 6620 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6621 | } |
| 6622 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6623 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 6624 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6625 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6626 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6627 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6628 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6629 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6630 | |
| 6631 | // Top window is also focusable but is not granted focus. |
| 6632 | windowTop->setFocusable(true); |
| 6633 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6634 | mDispatcher->onWindowInfosChanged( |
| 6635 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6636 | setFocusedWindow(windowSecond); |
| 6637 | |
| 6638 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6639 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6640 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6641 | |
| 6642 | // Focused window should receive event. |
| 6643 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6644 | windowTop->assertNoEvents(); |
| 6645 | } |
| 6646 | |
| 6647 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 6648 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6649 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6650 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6651 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6652 | |
| 6653 | window->setFocusable(true); |
| 6654 | // Release channel for window is no longer valid. |
| 6655 | window->releaseChannel(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6656 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6657 | setFocusedWindow(window); |
| 6658 | |
| 6659 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6660 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6661 | |
| 6662 | // window channel is invalid, so it should not receive any input event. |
| 6663 | window->assertNoEvents(); |
| 6664 | } |
| 6665 | |
| 6666 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 6667 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6668 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6669 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6670 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6671 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6672 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6673 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6674 | setFocusedWindow(window); |
| 6675 | |
| 6676 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6677 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6678 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6679 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6680 | window->assertNoEvents(); |
| 6681 | } |
| 6682 | |
| 6683 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 6684 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6685 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6686 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6687 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6688 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6689 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6690 | |
| 6691 | windowTop->setFocusable(true); |
| 6692 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6693 | mDispatcher->onWindowInfosChanged( |
| 6694 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6695 | setFocusedWindow(windowTop); |
| 6696 | windowTop->consumeFocusEvent(true); |
| 6697 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6698 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6699 | mDispatcher->onWindowInfosChanged( |
| 6700 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6701 | windowSecond->consumeFocusEvent(true); |
| 6702 | windowTop->consumeFocusEvent(false); |
| 6703 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6704 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6705 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6706 | |
| 6707 | // Focused window should receive event. |
| 6708 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6709 | } |
| 6710 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6711 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6712 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6713 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6714 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6715 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6716 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6717 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6718 | |
| 6719 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6720 | windowSecond->setFocusable(false); |
| 6721 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6722 | mDispatcher->onWindowInfosChanged( |
| 6723 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6724 | setFocusedWindow(windowTop); |
| 6725 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6726 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6727 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6728 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6729 | |
| 6730 | // Event should be dropped. |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6731 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6732 | windowSecond->assertNoEvents(); |
| 6733 | } |
| 6734 | |
| 6735 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 6736 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6737 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6738 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6739 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6740 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 6741 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6742 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6743 | |
| 6744 | window->setFocusable(true); |
| 6745 | previousFocusedWindow->setFocusable(true); |
| 6746 | window->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6747 | mDispatcher->onWindowInfosChanged( |
| 6748 | {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6749 | setFocusedWindow(previousFocusedWindow); |
| 6750 | previousFocusedWindow->consumeFocusEvent(true); |
| 6751 | |
| 6752 | // Requesting focus on invisible window takes focus from currently focused window. |
| 6753 | setFocusedWindow(window); |
| 6754 | previousFocusedWindow->consumeFocusEvent(false); |
| 6755 | |
| 6756 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6757 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6758 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 6759 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6760 | |
| 6761 | // Window does not get focus event or key down. |
| 6762 | window->assertNoEvents(); |
| 6763 | |
| 6764 | // Window becomes visible. |
| 6765 | window->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6766 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6767 | |
| 6768 | // Window receives focus event. |
| 6769 | window->consumeFocusEvent(true); |
| 6770 | // Focused window receives key down. |
| 6771 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6772 | } |
| 6773 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6774 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 6775 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6776 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6777 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6778 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6779 | |
| 6780 | // window is granted focus. |
| 6781 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6782 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6783 | setFocusedWindow(window); |
| 6784 | window->consumeFocusEvent(true); |
| 6785 | |
| 6786 | // When a display is removed window loses focus. |
| 6787 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 6788 | window->consumeFocusEvent(false); |
| 6789 | } |
| 6790 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6791 | /** |
| 6792 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 6793 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 6794 | * of the 'slipperyEnterWindow'. |
| 6795 | * |
| 6796 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 6797 | * a way so that the touched location is no longer covered by the top window. |
| 6798 | * |
| 6799 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 6800 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 6801 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 6802 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 6803 | * with ACTION_DOWN). |
| 6804 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 6805 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 6806 | * |
| 6807 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 6808 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 6809 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 6810 | * |
| 6811 | * In this test, we ensure that the event received by the bottom window has |
| 6812 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 6813 | */ |
| 6814 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6815 | constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6816 | constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1}; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6817 | |
| 6818 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6819 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6820 | |
| 6821 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6822 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6823 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6824 | // Make sure this one overlaps the bottom window |
| 6825 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 6826 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 6827 | // one. Windows with the same owner are not considered to be occluding each other. |
| 6828 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 6829 | |
| 6830 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6831 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6832 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6833 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6834 | mDispatcher->onWindowInfosChanged( |
| 6835 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6836 | |
| 6837 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6838 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 6839 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6840 | {{50, 50}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6841 | slipperyExitWindow->consumeMotionDown(); |
| 6842 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6843 | mDispatcher->onWindowInfosChanged( |
| 6844 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6845 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6846 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
| 6847 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6848 | {{51, 51}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6849 | |
| 6850 | slipperyExitWindow->consumeMotionCancel(); |
| 6851 | |
| 6852 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 6853 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 6854 | } |
| 6855 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 6856 | /** |
| 6857 | * Two windows, one on the left and another on the right. The left window is slippery. The right |
| 6858 | * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the |
| 6859 | * touch moves from the left window into the right window, the gesture should continue to go to the |
| 6860 | * left window. Touch shouldn't slip because the right window can't receive touches. This test |
| 6861 | * reproduces a crash. |
| 6862 | */ |
| 6863 | TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { |
| 6864 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6865 | |
| 6866 | sp<FakeWindowHandle> leftSlipperyWindow = |
| 6867 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 6868 | leftSlipperyWindow->setSlippery(true); |
| 6869 | leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6870 | |
| 6871 | sp<FakeWindowHandle> rightDropTouchesWindow = |
| 6872 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 6873 | rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100)); |
| 6874 | rightDropTouchesWindow->setDropInput(true); |
| 6875 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6876 | mDispatcher->onWindowInfosChanged( |
| 6877 | {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 6878 | |
| 6879 | // Start touch in the left window |
| 6880 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6881 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6882 | .build()); |
| 6883 | leftSlipperyWindow->consumeMotionDown(); |
| 6884 | |
| 6885 | // And move it into the right window |
| 6886 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6887 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 6888 | .build()); |
| 6889 | |
| 6890 | // Since the right window isn't eligible to receive input, touch does not slip. |
| 6891 | // The left window continues to receive the gesture. |
| 6892 | leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 6893 | rightDropTouchesWindow->assertNoEvents(); |
| 6894 | } |
| 6895 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 6896 | /** |
| 6897 | * A single window is on screen first. Touch is injected into that window. Next, a second window |
| 6898 | * appears. Since the first window is slippery, touch will move from the first window to the second. |
| 6899 | */ |
| 6900 | TEST_F(InputDispatcherTest, InjectedTouchSlips) { |
| 6901 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6902 | sp<FakeWindowHandle> originalWindow = |
| 6903 | sp<FakeWindowHandle>::make(application, mDispatcher, "Original", ADISPLAY_ID_DEFAULT); |
| 6904 | originalWindow->setFrame(Rect(0, 0, 200, 200)); |
| 6905 | originalWindow->setSlippery(true); |
| 6906 | |
| 6907 | sp<FakeWindowHandle> appearingWindow = |
| 6908 | sp<FakeWindowHandle>::make(application, mDispatcher, "Appearing", ADISPLAY_ID_DEFAULT); |
| 6909 | appearingWindow->setFrame(Rect(0, 0, 200, 200)); |
| 6910 | |
| 6911 | mDispatcher->onWindowInfosChanged({{*originalWindow->getInfo()}, {}, 0, 0}); |
| 6912 | |
| 6913 | // Touch down on the original window |
| 6914 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6915 | injectMotionEvent(*mDispatcher, |
| 6916 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6917 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 6918 | .build())); |
| 6919 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 6920 | |
| 6921 | // Now, a new window appears. This could be, for example, a notification shade that appears |
| 6922 | // after user starts to drag down on the launcher window. |
| 6923 | mDispatcher->onWindowInfosChanged( |
| 6924 | {{*appearingWindow->getInfo(), *originalWindow->getInfo()}, {}, 0, 0}); |
| 6925 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6926 | injectMotionEvent(*mDispatcher, |
| 6927 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6928 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(110)) |
| 6929 | .build())); |
| 6930 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 6931 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 6932 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6933 | injectMotionEvent(*mDispatcher, |
| 6934 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6935 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 6936 | .build())); |
| 6937 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 6938 | |
| 6939 | originalWindow->assertNoEvents(); |
| 6940 | appearingWindow->assertNoEvents(); |
| 6941 | } |
| 6942 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6943 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6944 | using Uid = gui::Uid; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6945 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6946 | |
| 6947 | sp<FakeWindowHandle> leftWindow = |
| 6948 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 6949 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6950 | leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6951 | |
| 6952 | sp<FakeWindowHandle> rightSpy = |
| 6953 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT); |
| 6954 | rightSpy->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6955 | rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6956 | rightSpy->setSpy(true); |
| 6957 | rightSpy->setTrustedOverlay(true); |
| 6958 | |
| 6959 | sp<FakeWindowHandle> rightWindow = |
| 6960 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 6961 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6962 | rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6963 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6964 | mDispatcher->onWindowInfosChanged( |
| 6965 | {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6966 | |
| 6967 | // Touch in the left window |
| 6968 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6969 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6970 | .build()); |
| 6971 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown()); |
| 6972 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6973 | ASSERT_NO_FATAL_FAILURE( |
| 6974 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6975 | |
| 6976 | // Touch another finger over the right windows |
| 6977 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6978 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6979 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6980 | .build()); |
| 6981 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown()); |
| 6982 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown()); |
| 6983 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove()); |
| 6984 | mDispatcher->waitForIdle(); |
| 6985 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6986 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, |
| 6987 | {Uid{101}, Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6988 | |
| 6989 | // Release finger over left window. The UP actions are not treated as device interaction. |
| 6990 | // The windows that did not receive the UP pointer will receive MOVE events, but since this |
| 6991 | // is part of the UP action, we do not treat this as device interaction. |
| 6992 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6993 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6994 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6995 | .build()); |
| 6996 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp()); |
| 6997 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 6998 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 6999 | mDispatcher->waitForIdle(); |
| 7000 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 7001 | |
| 7002 | // Move remaining finger |
| 7003 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 7004 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 7005 | .build()); |
| 7006 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 7007 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 7008 | mDispatcher->waitForIdle(); |
| 7009 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7010 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7011 | |
| 7012 | // Release all fingers |
| 7013 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7014 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 7015 | .build()); |
| 7016 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp()); |
| 7017 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp()); |
| 7018 | mDispatcher->waitForIdle(); |
| 7019 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 7020 | } |
| 7021 | |
| 7022 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) { |
| 7023 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7024 | |
| 7025 | sp<FakeWindowHandle> window = |
| 7026 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 7027 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 7028 | window->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7029 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7030 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7031 | setFocusedWindow(window); |
| 7032 | ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true)); |
| 7033 | |
| 7034 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build()); |
| 7035 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT)); |
| 7036 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7037 | ASSERT_NO_FATAL_FAILURE( |
| 7038 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7039 | |
| 7040 | // The UP actions are not treated as device interaction. |
| 7041 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build()); |
| 7042 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT)); |
| 7043 | mDispatcher->waitForIdle(); |
| 7044 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 7045 | } |
| 7046 | |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7047 | TEST_F(InputDispatcherTest, HoverEnterExitSynthesisUsesNewEventId) { |
| 7048 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7049 | |
| 7050 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 7051 | ADISPLAY_ID_DEFAULT); |
| 7052 | left->setFrame(Rect(0, 0, 100, 100)); |
| 7053 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7054 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 7055 | right->setFrame(Rect(100, 0, 200, 100)); |
| 7056 | sp<FakeWindowHandle> spy = |
| 7057 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT); |
| 7058 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 7059 | spy->setTrustedOverlay(true); |
| 7060 | spy->setSpy(true); |
| 7061 | |
| 7062 | mDispatcher->onWindowInfosChanged( |
| 7063 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 7064 | |
| 7065 | // Send hover move to the left window, and ensure hover enter is synthesized with a new eventId. |
| 7066 | NotifyMotionArgs notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 7067 | ADISPLAY_ID_DEFAULT, {PointF{50, 50}}); |
| 7068 | mDispatcher->notifyMotion(notifyArgs); |
| 7069 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7070 | std::unique_ptr<MotionEvent> leftEnter = left->consumeMotionEvent( |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7071 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), Not(WithEventId(notifyArgs.id)), |
| 7072 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7073 | ASSERT_NE(nullptr, leftEnter); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7074 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 7075 | Not(WithEventId(notifyArgs.id)), |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7076 | Not(WithEventId(leftEnter->getId())), |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7077 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 7078 | |
| 7079 | // Send move to the right window, and ensure hover exit and enter are synthesized with new ids. |
| 7080 | notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT, |
| 7081 | {PointF{150, 50}}); |
| 7082 | mDispatcher->notifyMotion(notifyArgs); |
| 7083 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7084 | std::unique_ptr<MotionEvent> leftExit = left->consumeMotionEvent( |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7085 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), Not(WithEventId(notifyArgs.id)), |
| 7086 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7087 | ASSERT_NE(nullptr, leftExit); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7088 | right->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 7089 | Not(WithEventId(notifyArgs.id)), |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7090 | Not(WithEventId(leftExit->getId())), |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7091 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 7092 | |
| 7093 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithEventId(notifyArgs.id))); |
| 7094 | } |
| 7095 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 7096 | class InputDispatcherFallbackKeyTest : public InputDispatcherTest { |
| 7097 | protected: |
| 7098 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7099 | sp<FakeWindowHandle> mWindow; |
| 7100 | |
| 7101 | virtual void SetUp() override { |
| 7102 | InputDispatcherTest::SetUp(); |
| 7103 | |
| 7104 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 7105 | |
| 7106 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 7107 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7108 | |
| 7109 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 7110 | setFocusedWindow(mWindow); |
| 7111 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeFocusEvent(/*hasFocus=*/true)); |
| 7112 | } |
| 7113 | |
| 7114 | void setFallback(int32_t keycode) { |
| 7115 | mFakePolicy->setUnhandledKeyHandler([keycode](const KeyEvent& event) { |
| 7116 | return KeyEventBuilder(event).keyCode(keycode).build(); |
| 7117 | }); |
| 7118 | } |
| 7119 | |
| 7120 | void consumeKey(bool handled, const ::testing::Matcher<KeyEvent>& matcher) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7121 | std::unique_ptr<KeyEvent> event = mWindow->consumeKey(handled); |
| 7122 | ASSERT_NE(nullptr, event); |
| 7123 | ASSERT_THAT(*event, matcher); |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 7124 | } |
| 7125 | }; |
| 7126 | |
| 7127 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotNotifiedForHandledKey) { |
| 7128 | mDispatcher->notifyKey( |
| 7129 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7130 | consumeKey(/*handled=*/true, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 7131 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7132 | } |
| 7133 | |
| 7134 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotifiedForUnhandledKey) { |
| 7135 | mDispatcher->notifyKey( |
| 7136 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7137 | consumeKey(/*handled=*/false, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 7138 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7139 | } |
| 7140 | |
| 7141 | TEST_F(InputDispatcherFallbackKeyTest, NoFallbackRequestedByPolicy) { |
| 7142 | mDispatcher->notifyKey( |
| 7143 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7144 | |
| 7145 | // Do not handle this key event. |
| 7146 | consumeKey(/*handled=*/false, |
| 7147 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7148 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7149 | |
| 7150 | // Since the policy did not request any fallback to be generated, ensure there are no events. |
| 7151 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7152 | } |
| 7153 | |
| 7154 | TEST_F(InputDispatcherFallbackKeyTest, FallbackDispatchForUnhandledKey) { |
| 7155 | setFallback(AKEYCODE_B); |
| 7156 | mDispatcher->notifyKey( |
| 7157 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7158 | |
| 7159 | // Do not handle this key event. |
| 7160 | consumeKey(/*handled=*/false, |
| 7161 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7162 | |
| 7163 | // Since the key was not handled, ensure the fallback event was dispatched instead. |
| 7164 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7165 | consumeKey(/*handled=*/true, |
| 7166 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7167 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7168 | |
| 7169 | // Release the original key, and ensure the fallback key is also released. |
| 7170 | mDispatcher->notifyKey( |
| 7171 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7172 | consumeKey(/*handled=*/false, |
| 7173 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7174 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7175 | consumeKey(/*handled=*/true, |
| 7176 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7177 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7178 | |
| 7179 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7180 | mWindow->assertNoEvents(); |
| 7181 | } |
| 7182 | |
| 7183 | TEST_F(InputDispatcherFallbackKeyTest, AppHandlesPreviouslyUnhandledKey) { |
| 7184 | setFallback(AKEYCODE_B); |
| 7185 | mDispatcher->notifyKey( |
| 7186 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7187 | |
| 7188 | // Do not handle this key event, but handle the fallback. |
| 7189 | consumeKey(/*handled=*/false, |
| 7190 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7191 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7192 | consumeKey(/*handled=*/true, |
| 7193 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7194 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7195 | |
| 7196 | // Release the original key, and ensure the fallback key is also released. |
| 7197 | mDispatcher->notifyKey( |
| 7198 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7199 | // But this time, the app handles the original key. |
| 7200 | consumeKey(/*handled=*/true, |
| 7201 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7202 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7203 | // Ensure the fallback key is canceled. |
| 7204 | consumeKey(/*handled=*/true, |
| 7205 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7206 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 7207 | |
| 7208 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7209 | mWindow->assertNoEvents(); |
| 7210 | } |
| 7211 | |
| 7212 | TEST_F(InputDispatcherFallbackKeyTest, AppDoesNotHandleFallback) { |
| 7213 | setFallback(AKEYCODE_B); |
| 7214 | mDispatcher->notifyKey( |
| 7215 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7216 | |
| 7217 | // Do not handle this key event. |
| 7218 | consumeKey(/*handled=*/false, |
| 7219 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7220 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7221 | // App does not handle the fallback either, so ensure another fallback is not generated. |
| 7222 | setFallback(AKEYCODE_C); |
| 7223 | consumeKey(/*handled=*/false, |
| 7224 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7225 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7226 | |
| 7227 | // Release the original key, and ensure the fallback key is also released. |
| 7228 | setFallback(AKEYCODE_B); |
| 7229 | mDispatcher->notifyKey( |
| 7230 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7231 | consumeKey(/*handled=*/false, |
| 7232 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7233 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7234 | consumeKey(/*handled=*/false, |
| 7235 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7236 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7237 | |
| 7238 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7239 | mWindow->assertNoEvents(); |
| 7240 | } |
| 7241 | |
| 7242 | TEST_F(InputDispatcherFallbackKeyTest, InconsistentPolicyCancelsFallback) { |
| 7243 | setFallback(AKEYCODE_B); |
| 7244 | mDispatcher->notifyKey( |
| 7245 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7246 | |
| 7247 | // Do not handle this key event, so fallback is generated. |
| 7248 | consumeKey(/*handled=*/false, |
| 7249 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7250 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7251 | consumeKey(/*handled=*/true, |
| 7252 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7253 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7254 | |
| 7255 | // Release the original key, but assume the policy is misbehaving and it |
| 7256 | // generates an inconsistent fallback to the one from the DOWN event. |
| 7257 | setFallback(AKEYCODE_C); |
| 7258 | mDispatcher->notifyKey( |
| 7259 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7260 | consumeKey(/*handled=*/false, |
| 7261 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7262 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7263 | // Ensure the fallback key reported before as DOWN is canceled due to the inconsistency. |
| 7264 | consumeKey(/*handled=*/true, |
| 7265 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7266 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 7267 | |
| 7268 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7269 | mWindow->assertNoEvents(); |
| 7270 | } |
| 7271 | |
| 7272 | TEST_F(InputDispatcherFallbackKeyTest, CanceledKeyCancelsFallback) { |
| 7273 | setFallback(AKEYCODE_B); |
| 7274 | mDispatcher->notifyKey( |
| 7275 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7276 | |
| 7277 | // Do not handle this key event, so fallback is generated. |
| 7278 | consumeKey(/*handled=*/false, |
| 7279 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7280 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7281 | consumeKey(/*handled=*/true, |
| 7282 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7283 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7284 | |
| 7285 | // The original key is canceled. |
| 7286 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
| 7287 | .keyCode(AKEYCODE_A) |
| 7288 | .addFlag(AKEY_EVENT_FLAG_CANCELED) |
| 7289 | .build()); |
| 7290 | consumeKey(/*handled=*/false, |
| 7291 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), |
| 7292 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 7293 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7294 | // Ensure the fallback key is also canceled due to the original key being canceled. |
| 7295 | consumeKey(/*handled=*/true, |
| 7296 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7297 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 7298 | |
| 7299 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7300 | mWindow->assertNoEvents(); |
| 7301 | } |
| 7302 | |
Prabir Pradhan | b13da8f | 2024-01-09 23:10:13 +0000 | [diff] [blame] | 7303 | TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedDuringPolicyCall) { |
| 7304 | setFallback(AKEYCODE_B); |
| 7305 | mDispatcher->notifyKey( |
| 7306 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7307 | |
| 7308 | // Do not handle this key event. |
| 7309 | consumeKey(/*handled=*/false, |
| 7310 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7311 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7312 | consumeKey(/*handled=*/true, |
| 7313 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7314 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7315 | |
| 7316 | mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) { |
| 7317 | // When the unhandled key is reported to the policy next, remove the input channel. |
| 7318 | mDispatcher->removeInputChannel(mWindow->getToken()); |
| 7319 | return KeyEventBuilder(event).keyCode(AKEYCODE_B).build(); |
| 7320 | }); |
| 7321 | // Release the original key, and let the app now handle the previously unhandled key. |
| 7322 | // This should result in the previously generated fallback key to be cancelled. |
| 7323 | // Since the policy was notified of the unhandled DOWN event earlier, it will also be notified |
| 7324 | // of the UP event for consistency. The Dispatcher calls into the policy from its own thread |
| 7325 | // without holding the lock, because it need to synchronously fetch the fallback key. While in |
| 7326 | // the policy call, we will now remove the input channel. Once the policy call returns, the |
| 7327 | // Dispatcher will no longer have a channel to send cancellation events to. Ensure this does |
| 7328 | // not cause any crashes. |
| 7329 | mDispatcher->notifyKey( |
| 7330 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7331 | consumeKey(/*handled=*/true, |
| 7332 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7333 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7334 | } |
| 7335 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7336 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 7337 | protected: |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 7338 | static constexpr std::chrono::nanoseconds KEY_REPEAT_TIMEOUT = 40ms; |
| 7339 | static constexpr std::chrono::nanoseconds KEY_REPEAT_DELAY = 40ms; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7340 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7341 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7342 | sp<FakeWindowHandle> mWindow; |
| 7343 | |
| 7344 | virtual void SetUp() override { |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 7345 | InputDispatcherTest::SetUp(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7346 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 7347 | mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7348 | setUpWindow(); |
| 7349 | } |
| 7350 | |
| 7351 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7352 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7353 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7354 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7355 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7356 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7357 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7358 | mWindow->consumeFocusEvent(true); |
| 7359 | } |
| 7360 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7361 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7362 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7363 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7364 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7365 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7366 | |
| 7367 | // Window should receive key down event. |
| 7368 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7369 | } |
| 7370 | |
| 7371 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 7372 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7373 | mWindow->consumeKeyEvent( |
| 7374 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount))); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7375 | } |
| 7376 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7377 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7378 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7379 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7380 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7381 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7382 | |
| 7383 | // Window should receive key down event. |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7384 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7385 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7386 | } |
| 7387 | }; |
| 7388 | |
| 7389 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7390 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7391 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 7392 | expectKeyRepeatOnce(repeatCount); |
| 7393 | } |
| 7394 | } |
| 7395 | |
| 7396 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7397 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7398 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 7399 | expectKeyRepeatOnce(repeatCount); |
| 7400 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7401 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7402 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7403 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 7404 | expectKeyRepeatOnce(repeatCount); |
| 7405 | } |
| 7406 | } |
| 7407 | |
| 7408 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7409 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 7410 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 7411 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7412 | mWindow->assertNoEvents(); |
| 7413 | } |
| 7414 | |
| 7415 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7416 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 7417 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 7418 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 7419 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7420 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7421 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7422 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7423 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 7424 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7425 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7426 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7427 | mWindow->assertNoEvents(); |
| 7428 | } |
| 7429 | |
| 7430 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7431 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 7432 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 7433 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 7434 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7435 | // 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] | 7436 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7437 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7438 | mWindow->assertNoEvents(); |
| 7439 | } |
| 7440 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 7441 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 7442 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7443 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7444 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 7445 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 7446 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 7447 | mWindow->assertNoEvents(); |
| 7448 | } |
| 7449 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7450 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 7451 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7452 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7453 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7454 | std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey(); |
| 7455 | ASSERT_NE(nullptr, repeatEvent); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7456 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7457 | IdGenerator::getSource(repeatEvent->getId())); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7458 | } |
| 7459 | } |
| 7460 | |
| 7461 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 7462 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7463 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7464 | |
| 7465 | std::unordered_set<int32_t> idSet; |
| 7466 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7467 | std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey(); |
| 7468 | ASSERT_NE(nullptr, repeatEvent); |
| 7469 | int32_t id = repeatEvent->getId(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7470 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 7471 | idSet.insert(id); |
| 7472 | } |
| 7473 | } |
| 7474 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7475 | /* Test InputDispatcher for MultiDisplay */ |
| 7476 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 7477 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7478 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7479 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7480 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7481 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7482 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7483 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7484 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7485 | // Set focus window for primary display, but focused display would be second one. |
| 7486 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7487 | windowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7488 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
| 7489 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7490 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7491 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7492 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7493 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7494 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7495 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7496 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7497 | // Set focus display to second one. |
| 7498 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 7499 | // Set focus window for second display. |
| 7500 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7501 | windowInSecondary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7502 | mDispatcher->onWindowInfosChanged( |
| 7503 | {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7504 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7505 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7506 | } |
| 7507 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7508 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7509 | InputDispatcherTest::TearDown(); |
| 7510 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7511 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7512 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7513 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7514 | windowInSecondary.clear(); |
| 7515 | } |
| 7516 | |
| 7517 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7518 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7519 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7520 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7521 | sp<FakeWindowHandle> windowInSecondary; |
| 7522 | }; |
| 7523 | |
| 7524 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 7525 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7526 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7527 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7528 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7529 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7530 | windowInSecondary->assertNoEvents(); |
| 7531 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7532 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7533 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7534 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7535 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7536 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7537 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7538 | } |
| 7539 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7540 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 7541 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 7542 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7543 | injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7544 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7545 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 7546 | windowInSecondary->assertNoEvents(); |
| 7547 | |
| 7548 | // Test inject a key down without display id specified. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7549 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7550 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7551 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7552 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7553 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7554 | // Remove all windows in secondary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7555 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7556 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7557 | // Old focus should receive a cancel event. |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7558 | windowInSecondary->consumeKeyUp(ADISPLAY_ID_NONE, AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7559 | |
| 7560 | // Test inject a key down, should timeout because of no target window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7561 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7562 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7563 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7564 | windowInSecondary->assertNoEvents(); |
| 7565 | } |
| 7566 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7567 | // Test per-display input monitors for motion event. |
| 7568 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7569 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7570 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7571 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7572 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7573 | |
| 7574 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7575 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7576 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7577 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7578 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7579 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7580 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7581 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7582 | |
| 7583 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7584 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7585 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7586 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7587 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7588 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7589 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7590 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7591 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 7592 | // Lift up the touch from the second display |
| 7593 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7594 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 7595 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7596 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 7597 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 7598 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7599 | // Test inject a non-pointer motion event. |
| 7600 | // If specific a display, it will dispatch to the focused window of particular display, |
| 7601 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7602 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7603 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7604 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7605 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7606 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7607 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7608 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7609 | } |
| 7610 | |
| 7611 | // Test per-display input monitors for key event. |
| 7612 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7613 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7614 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7615 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7616 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7617 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7618 | |
| 7619 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7620 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7621 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7622 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7623 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7624 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7625 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7626 | } |
| 7627 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7628 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 7629 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7630 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7631 | secondWindowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7632 | mDispatcher->onWindowInfosChanged( |
| 7633 | {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(), |
| 7634 | *windowInSecondary->getInfo()}, |
| 7635 | {}, |
| 7636 | 0, |
| 7637 | 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7638 | setFocusedWindow(secondWindowInPrimary); |
| 7639 | windowInPrimary->consumeFocusEvent(false); |
| 7640 | secondWindowInPrimary->consumeFocusEvent(true); |
| 7641 | |
| 7642 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7643 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7644 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7645 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7646 | windowInPrimary->assertNoEvents(); |
| 7647 | windowInSecondary->assertNoEvents(); |
| 7648 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7649 | } |
| 7650 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7651 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 7652 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7653 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7654 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7655 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7656 | |
| 7657 | // Test touch down on primary display. |
| 7658 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7659 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7660 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7661 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7662 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7663 | |
| 7664 | // Test touch down on second display. |
| 7665 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7666 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7667 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7668 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 7669 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 7670 | |
| 7671 | // Trigger cancel touch. |
| 7672 | mDispatcher->cancelCurrentTouch(); |
| 7673 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 7674 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 7675 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 7676 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 7677 | |
| 7678 | // Test inject a move motion event, no window/monitor should receive the event. |
| 7679 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7680 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7681 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 7682 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 7683 | windowInPrimary->assertNoEvents(); |
| 7684 | monitorInPrimary.assertNoEvents(); |
| 7685 | |
| 7686 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7687 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7688 | SECOND_DISPLAY_ID, {110, 200})) |
| 7689 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 7690 | windowInSecondary->assertNoEvents(); |
| 7691 | monitorInSecondary.assertNoEvents(); |
| 7692 | } |
| 7693 | |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 7694 | /** |
| 7695 | * Send a key to the primary display and to the secondary display. |
| 7696 | * Then cause the key on the primary display to be canceled by sending in a stale key. |
| 7697 | * Ensure that the key on the primary display is canceled, and that the key on the secondary display |
| 7698 | * does not get canceled. |
| 7699 | */ |
| 7700 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture) { |
| 7701 | // Send a key down on primary display |
| 7702 | mDispatcher->notifyKey( |
| 7703 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 7704 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7705 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 7706 | .build()); |
| 7707 | windowInPrimary->consumeKeyEvent( |
| 7708 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 7709 | windowInSecondary->assertNoEvents(); |
| 7710 | |
| 7711 | // Send a key down on second display |
| 7712 | mDispatcher->notifyKey( |
| 7713 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 7714 | .displayId(SECOND_DISPLAY_ID) |
| 7715 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 7716 | .build()); |
| 7717 | windowInSecondary->consumeKeyEvent( |
| 7718 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID))); |
| 7719 | windowInPrimary->assertNoEvents(); |
| 7720 | |
| 7721 | // Send a valid key up event on primary display that will be dropped because it is stale |
| 7722 | NotifyKeyArgs staleKeyUp = |
| 7723 | KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
| 7724 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7725 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 7726 | .build(); |
| 7727 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms; |
| 7728 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
| 7729 | std::this_thread::sleep_for(STALE_EVENT_TIMEOUT); |
| 7730 | mDispatcher->notifyKey(staleKeyUp); |
| 7731 | |
| 7732 | // Only the key gesture corresponding to the dropped event should receive the cancel event. |
| 7733 | // Therefore, windowInPrimary should get the cancel event and windowInSecondary should not |
| 7734 | // receive any events. |
| 7735 | windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), |
| 7736 | WithDisplayId(ADISPLAY_ID_DEFAULT), |
| 7737 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 7738 | windowInSecondary->assertNoEvents(); |
| 7739 | } |
| 7740 | |
| 7741 | /** |
| 7742 | * Similar to 'WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture' but for motion events. |
| 7743 | */ |
| 7744 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropMotionEvent_OnlyCancelCorrespondingGesture) { |
| 7745 | // Send touch down on primary display. |
| 7746 | mDispatcher->notifyMotion( |
| 7747 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7748 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 7749 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7750 | .build()); |
| 7751 | windowInPrimary->consumeMotionEvent( |
| 7752 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 7753 | windowInSecondary->assertNoEvents(); |
| 7754 | |
| 7755 | // Send touch down on second display. |
| 7756 | mDispatcher->notifyMotion( |
| 7757 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7758 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 7759 | .displayId(SECOND_DISPLAY_ID) |
| 7760 | .build()); |
| 7761 | windowInPrimary->assertNoEvents(); |
| 7762 | windowInSecondary->consumeMotionEvent( |
| 7763 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID))); |
| 7764 | |
| 7765 | // inject a valid MotionEvent on primary display that will be stale when it arrives. |
| 7766 | NotifyMotionArgs staleMotionUp = |
| 7767 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7768 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7769 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 7770 | .build(); |
| 7771 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms; |
| 7772 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
| 7773 | std::this_thread::sleep_for(STALE_EVENT_TIMEOUT); |
| 7774 | mDispatcher->notifyMotion(staleMotionUp); |
| 7775 | |
| 7776 | // For stale motion events, we let the gesture to complete. This behaviour is different from key |
| 7777 | // events, where we would cancel the current keys instead. |
| 7778 | windowInPrimary->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 7779 | windowInSecondary->assertNoEvents(); |
| 7780 | } |
| 7781 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7782 | class InputFilterTest : public InputDispatcherTest { |
| 7783 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7784 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 7785 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7786 | NotifyMotionArgs motionArgs; |
| 7787 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7788 | motionArgs = |
| 7789 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7790 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7791 | motionArgs = |
| 7792 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7793 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7794 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7795 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 7796 | const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue()); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7797 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7798 | } else { |
| 7799 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 7800 | } |
| 7801 | } |
| 7802 | |
| 7803 | void testNotifyKey(bool expectToBeFiltered) { |
| 7804 | NotifyKeyArgs keyArgs; |
| 7805 | |
| 7806 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7807 | mDispatcher->notifyKey(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7808 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7809 | mDispatcher->notifyKey(keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7810 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7811 | |
| 7812 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 7813 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7814 | } else { |
| 7815 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 7816 | } |
| 7817 | } |
| 7818 | }; |
| 7819 | |
| 7820 | // Test InputFilter for MotionEvent |
| 7821 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 7822 | // 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] | 7823 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false); |
| 7824 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7825 | |
| 7826 | // Enable InputFilter |
| 7827 | mDispatcher->setInputFilterEnabled(true); |
| 7828 | // 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] | 7829 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true); |
| 7830 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7831 | |
| 7832 | // Disable InputFilter |
| 7833 | mDispatcher->setInputFilterEnabled(false); |
| 7834 | // 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] | 7835 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false); |
| 7836 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7837 | } |
| 7838 | |
| 7839 | // Test InputFilter for KeyEvent |
| 7840 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 7841 | // 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] | 7842 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7843 | |
| 7844 | // Enable InputFilter |
| 7845 | mDispatcher->setInputFilterEnabled(true); |
| 7846 | // Send a key event, and check if it is filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7847 | testNotifyKey(/*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7848 | |
| 7849 | // Disable InputFilter |
| 7850 | mDispatcher->setInputFilterEnabled(false); |
| 7851 | // Send a key event, and check if it isn't filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7852 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7853 | } |
| 7854 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7855 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 7856 | // logical display coordinate space. |
| 7857 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 7858 | ui::Transform firstDisplayTransform; |
| 7859 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 7860 | ui::Transform secondDisplayTransform; |
| 7861 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 7862 | |
| 7863 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 7864 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 7865 | displayInfos[0].transform = firstDisplayTransform; |
| 7866 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 7867 | displayInfos[1].transform = secondDisplayTransform; |
| 7868 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 7869 | mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0}); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7870 | |
| 7871 | // Enable InputFilter |
| 7872 | mDispatcher->setInputFilterEnabled(true); |
| 7873 | |
| 7874 | // Ensure the correct transforms are used for the displays. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7875 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true, firstDisplayTransform); |
| 7876 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7877 | } |
| 7878 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7879 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 7880 | protected: |
| 7881 | virtual void SetUp() override { |
| 7882 | InputDispatcherTest::SetUp(); |
| 7883 | |
| 7884 | /** |
| 7885 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 7886 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 7887 | * on. |
| 7888 | */ |
| 7889 | mDispatcher->setInputFilterEnabled(true); |
| 7890 | |
| 7891 | std::shared_ptr<InputApplicationHandle> application = |
| 7892 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7893 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 7894 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7895 | |
| 7896 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7897 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7898 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7899 | setFocusedWindow(mWindow); |
| 7900 | mWindow->consumeFocusEvent(true); |
| 7901 | } |
| 7902 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7903 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 7904 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7905 | KeyEvent event; |
| 7906 | |
| 7907 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 7908 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 7909 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7910 | KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7911 | const int32_t additionalPolicyFlags = |
| 7912 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 7913 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7914 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 7915 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7916 | policyFlags | additionalPolicyFlags)); |
| 7917 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7918 | mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags))); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7919 | } |
| 7920 | |
| 7921 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 7922 | int32_t flags) { |
| 7923 | MotionEvent event; |
| 7924 | PointerProperties pointerProperties[1]; |
| 7925 | PointerCoords pointerCoords[1]; |
| 7926 | pointerProperties[0].clear(); |
| 7927 | pointerProperties[0].id = 0; |
| 7928 | pointerCoords[0].clear(); |
| 7929 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 7930 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 7931 | |
| 7932 | ui::Transform identityTransform; |
| 7933 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 7934 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 7935 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 7936 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 7937 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 7938 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 7939 | eventTime, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7940 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7941 | |
| 7942 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 7943 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7944 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 7945 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7946 | policyFlags | additionalPolicyFlags)); |
| 7947 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7948 | mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId))); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7949 | } |
| 7950 | |
| 7951 | private: |
| 7952 | sp<FakeWindowHandle> mWindow; |
| 7953 | }; |
| 7954 | |
| 7955 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7956 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 7957 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 7958 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7959 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 7960 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7961 | } |
| 7962 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7963 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7964 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7965 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7966 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 7967 | } |
| 7968 | |
| 7969 | TEST_F(InputFilterInjectionPolicyTest, |
| 7970 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 7971 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7972 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7973 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7974 | } |
| 7975 | |
| 7976 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7977 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 7978 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7979 | } |
| 7980 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 7981 | class InputDispatcherUserActivityPokeTests : public InputDispatcherTest { |
| 7982 | protected: |
| 7983 | virtual void SetUp() override { |
| 7984 | InputDispatcherTest::SetUp(); |
| 7985 | |
| 7986 | std::shared_ptr<FakeApplicationHandle> application = |
| 7987 | std::make_shared<FakeApplicationHandle>(); |
| 7988 | application->setDispatchingTimeout(100ms); |
| 7989 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
| 7990 | ADISPLAY_ID_DEFAULT); |
Yeabkal Wubshit | 222d83d | 2024-01-24 18:00:09 +0000 | [diff] [blame] | 7991 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 7992 | mWindow->setDispatchingTimeout(100ms); |
| 7993 | mWindow->setFocusable(true); |
| 7994 | |
| 7995 | // Set focused application. |
| 7996 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7997 | |
| 7998 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 7999 | setFocusedWindow(mWindow); |
| 8000 | mWindow->consumeFocusEvent(true); |
| 8001 | } |
| 8002 | |
| 8003 | void notifyAndConsumeMotion(int32_t action, uint32_t source, int32_t displayId, |
| 8004 | nsecs_t eventTime) { |
| 8005 | mDispatcher->notifyMotion(MotionArgsBuilder(action, source) |
| 8006 | .displayId(displayId) |
| 8007 | .eventTime(eventTime) |
| 8008 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8009 | .build()); |
| 8010 | mWindow->consumeMotionEvent(WithMotionAction(action)); |
| 8011 | } |
| 8012 | |
| 8013 | private: |
| 8014 | sp<FakeWindowHandle> mWindow; |
| 8015 | }; |
| 8016 | |
| 8017 | TEST_F_WITH_FLAGS( |
| 8018 | InputDispatcherUserActivityPokeTests, MinPokeTimeObserved, |
| 8019 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 8020 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 8021 | mDispatcher->setMinTimeBetweenUserActivityPokes(50ms); |
| 8022 | |
| 8023 | // First event of type TOUCH. Should poke. |
| 8024 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8025 | milliseconds_to_nanoseconds(50)); |
| 8026 | mFakePolicy->assertUserActivityPoked( |
| 8027 | {{milliseconds_to_nanoseconds(50), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 8028 | |
| 8029 | // 80ns > 50ns has passed since previous TOUCH event. Should poke. |
| 8030 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8031 | milliseconds_to_nanoseconds(130)); |
| 8032 | mFakePolicy->assertUserActivityPoked( |
| 8033 | {{milliseconds_to_nanoseconds(130), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 8034 | |
| 8035 | // First event of type OTHER. Should poke (despite being within 50ns of previous TOUCH event). |
| 8036 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT, |
| 8037 | milliseconds_to_nanoseconds(135)); |
| 8038 | mFakePolicy->assertUserActivityPoked( |
| 8039 | {{milliseconds_to_nanoseconds(135), USER_ACTIVITY_EVENT_OTHER, ADISPLAY_ID_DEFAULT}}); |
| 8040 | |
| 8041 | // Within 50ns of previous TOUCH event. Should NOT poke. |
| 8042 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8043 | milliseconds_to_nanoseconds(140)); |
| 8044 | mFakePolicy->assertUserActivityNotPoked(); |
| 8045 | |
| 8046 | // Within 50ns of previous OTHER event. Should NOT poke. |
| 8047 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT, |
| 8048 | milliseconds_to_nanoseconds(150)); |
| 8049 | mFakePolicy->assertUserActivityNotPoked(); |
| 8050 | |
| 8051 | // Within 50ns of previous TOUCH event (which was at time 130). Should NOT poke. |
| 8052 | // Note that STYLUS is mapped to TOUCH user activity, since it's a pointer-type source. |
| 8053 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT, |
| 8054 | milliseconds_to_nanoseconds(160)); |
| 8055 | mFakePolicy->assertUserActivityNotPoked(); |
| 8056 | |
| 8057 | // 65ns > 50ns has passed since previous OTHER event. Should poke. |
| 8058 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT, |
| 8059 | milliseconds_to_nanoseconds(200)); |
| 8060 | mFakePolicy->assertUserActivityPoked( |
| 8061 | {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_OTHER, ADISPLAY_ID_DEFAULT}}); |
| 8062 | |
| 8063 | // 170ns > 50ns has passed since previous TOUCH event. Should poke. |
| 8064 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT, |
| 8065 | milliseconds_to_nanoseconds(300)); |
| 8066 | mFakePolicy->assertUserActivityPoked( |
| 8067 | {{milliseconds_to_nanoseconds(300), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 8068 | |
| 8069 | // Assert that there's no more user activity poke event. |
| 8070 | mFakePolicy->assertUserActivityNotPoked(); |
| 8071 | } |
| 8072 | |
| 8073 | TEST_F_WITH_FLAGS( |
| 8074 | InputDispatcherUserActivityPokeTests, DefaultMinPokeTimeOf100MsUsed, |
| 8075 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 8076 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 8077 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8078 | milliseconds_to_nanoseconds(200)); |
| 8079 | mFakePolicy->assertUserActivityPoked( |
| 8080 | {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 8081 | |
| 8082 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8083 | milliseconds_to_nanoseconds(280)); |
| 8084 | mFakePolicy->assertUserActivityNotPoked(); |
| 8085 | |
| 8086 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8087 | milliseconds_to_nanoseconds(340)); |
| 8088 | mFakePolicy->assertUserActivityPoked( |
| 8089 | {{milliseconds_to_nanoseconds(340), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 8090 | } |
| 8091 | |
| 8092 | TEST_F_WITH_FLAGS( |
| 8093 | InputDispatcherUserActivityPokeTests, ZeroMinPokeTimeDisablesRateLimiting, |
| 8094 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 8095 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 8096 | mDispatcher->setMinTimeBetweenUserActivityPokes(0ms); |
| 8097 | |
| 8098 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20); |
| 8099 | mFakePolicy->assertUserActivityPoked(); |
| 8100 | |
| 8101 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 30); |
| 8102 | mFakePolicy->assertUserActivityPoked(); |
| 8103 | } |
| 8104 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8105 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 8106 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8107 | InputDispatcherTest::SetUp(); |
| 8108 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8109 | std::shared_ptr<FakeApplicationHandle> application = |
| 8110 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8111 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8112 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8113 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8114 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8115 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8116 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8117 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8118 | |
| 8119 | // Set focused application. |
| 8120 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8121 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8122 | |
| 8123 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8124 | mDispatcher->onWindowInfosChanged( |
| 8125 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8126 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8127 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8128 | } |
| 8129 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 8130 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8131 | InputDispatcherTest::TearDown(); |
| 8132 | |
| 8133 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8134 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8135 | } |
| 8136 | |
| 8137 | protected: |
| 8138 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8139 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 8140 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8141 | }; |
| 8142 | |
| 8143 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 8144 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 8145 | // the onPointerDownOutsideFocus callback. |
| 8146 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8147 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8148 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 8149 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8150 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 8151 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8152 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8153 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8154 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 8155 | } |
| 8156 | |
| 8157 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 8158 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 8159 | // onPointerDownOutsideFocus callback. |
| 8160 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8161 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8162 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, |
| 8163 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8164 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 8165 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8166 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8167 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8168 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8169 | } |
| 8170 | |
| 8171 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 8172 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 8173 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 8174 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8175 | injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8176 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 8177 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8178 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8179 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8180 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8181 | } |
| 8182 | |
| 8183 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 8184 | // DOWN on the window that already has focus. Ensure no window received the |
| 8185 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8186 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8187 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8188 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 8189 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8190 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 8191 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8192 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8193 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8194 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8195 | } |
| 8196 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 8197 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 8198 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 8199 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 8200 | const MotionEvent event = |
| 8201 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 8202 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8203 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 8204 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 8205 | .build(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8206 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 8207 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8208 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 8209 | |
| 8210 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8211 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 8212 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 8213 | mUnfocusedWindow->assertNoEvents(); |
| 8214 | } |
| 8215 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8216 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 8217 | // windows that point to the same client token. |
| 8218 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 8219 | virtual void SetUp() override { |
| 8220 | InputDispatcherTest::SetUp(); |
| 8221 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8222 | std::shared_ptr<FakeApplicationHandle> application = |
| 8223 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8224 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 8225 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8226 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 8227 | |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 8228 | mWindow2 = mWindow1->clone(ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8229 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 8230 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8231 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8232 | } |
| 8233 | |
| 8234 | protected: |
| 8235 | sp<FakeWindowHandle> mWindow1; |
| 8236 | sp<FakeWindowHandle> mWindow2; |
| 8237 | |
| 8238 | // 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] | 8239 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 8240 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 8241 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8242 | } |
| 8243 | |
| 8244 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 8245 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8246 | const std::string name = window->getName(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8247 | std::unique_ptr<MotionEvent> motionEvent = |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 8248 | window->consumeMotionEvent(WithMotionAction(expectedAction)); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8249 | ASSERT_NE(nullptr, motionEvent); |
| 8250 | ASSERT_EQ(points.size(), motionEvent->getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8251 | |
| 8252 | for (size_t i = 0; i < points.size(); i++) { |
| 8253 | float expectedX = points[i].x; |
| 8254 | float expectedY = points[i].y; |
| 8255 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8256 | EXPECT_EQ(expectedX, motionEvent->getX(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8257 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8258 | << ", got " << motionEvent->getX(i); |
| 8259 | EXPECT_EQ(expectedY, motionEvent->getY(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8260 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8261 | << ", got " << motionEvent->getY(i); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8262 | } |
| 8263 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8264 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 8265 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8266 | std::vector<PointF> expectedPoints) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8267 | mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 8268 | ADISPLAY_ID_DEFAULT, touchedPoints)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8269 | |
| 8270 | // Always consume from window1 since it's the window that has the InputReceiver |
| 8271 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 8272 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8273 | }; |
| 8274 | |
| 8275 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 8276 | // Touch Window 1 |
| 8277 | PointF touchedPoint = {10, 10}; |
| 8278 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8279 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8280 | |
| 8281 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8282 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8283 | |
| 8284 | // Touch Window 2 |
| 8285 | touchedPoint = {150, 150}; |
| 8286 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8287 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8288 | } |
| 8289 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8290 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 8291 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8292 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8293 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8294 | |
| 8295 | // Touch Window 1 |
| 8296 | PointF touchedPoint = {10, 10}; |
| 8297 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8298 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8299 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8300 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8301 | |
| 8302 | // Touch Window 2 |
| 8303 | touchedPoint = {150, 150}; |
| 8304 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8305 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 8306 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8307 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8308 | // Update the transform so rotation is set |
| 8309 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8310 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8311 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 8312 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8313 | } |
| 8314 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8315 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8316 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8317 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8318 | |
| 8319 | // Touch Window 1 |
| 8320 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 8321 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8322 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8323 | |
| 8324 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8325 | touchedPoints.push_back(PointF{150, 150}); |
| 8326 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8327 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8328 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8329 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8330 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8331 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8332 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8333 | // Update the transform so rotation is set for Window 2 |
| 8334 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8335 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8336 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8337 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8338 | } |
| 8339 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8340 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8341 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8342 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8343 | |
| 8344 | // Touch Window 1 |
| 8345 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 8346 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8347 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8348 | |
| 8349 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8350 | touchedPoints.push_back(PointF{150, 150}); |
| 8351 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8352 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8353 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8354 | |
| 8355 | // Move both windows |
| 8356 | touchedPoints = {{20, 20}, {175, 175}}; |
| 8357 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 8358 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 8359 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8360 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8361 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8362 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8363 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8364 | expectedPoints.pop_back(); |
| 8365 | |
| 8366 | // Touch Window 2 |
| 8367 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8368 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8369 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8370 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8371 | |
| 8372 | // Move both windows |
| 8373 | touchedPoints = {{20, 20}, {175, 175}}; |
| 8374 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 8375 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 8376 | |
| 8377 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8378 | } |
| 8379 | |
| 8380 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 8381 | mWindow1->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8382 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8383 | |
| 8384 | // Touch Window 1 |
| 8385 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 8386 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8387 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8388 | |
| 8389 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8390 | touchedPoints.push_back(PointF{150, 150}); |
| 8391 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8392 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8393 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8394 | |
| 8395 | // Move both windows |
| 8396 | touchedPoints = {{20, 20}, {175, 175}}; |
| 8397 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 8398 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 8399 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8400 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8401 | } |
| 8402 | |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 8403 | /** |
| 8404 | * When one of the windows is slippery, the touch should not slip into the other window with the |
| 8405 | * same input channel. |
| 8406 | */ |
| 8407 | TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) { |
| 8408 | mWindow1->setSlippery(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8409 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 8410 | |
| 8411 | // Touch down in window 1 |
| 8412 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8413 | ADISPLAY_ID_DEFAULT, {{50, 50}})); |
| 8414 | consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}}); |
| 8415 | |
| 8416 | // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip. |
| 8417 | // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN |
| 8418 | // getting generated. |
| 8419 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8420 | ADISPLAY_ID_DEFAULT, {{150, 150}})); |
| 8421 | |
| 8422 | consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}}); |
| 8423 | } |
| 8424 | |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8425 | /** |
| 8426 | * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and |
| 8427 | * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window |
| 8428 | * that the pointer is hovering over may have a different transform. |
| 8429 | */ |
| 8430 | TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) { |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8431 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8432 | |
| 8433 | // Start hover in window 1 |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 8434 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN) |
| 8435 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8436 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8437 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 8438 | {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8439 | // Move hover to window 2. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 8440 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8441 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 8442 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8443 | consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}}); |
| 8444 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 8445 | {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})}); |
| 8446 | } |
| 8447 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8448 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 8449 | virtual void SetUp() override { |
| 8450 | InputDispatcherTest::SetUp(); |
| 8451 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8452 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8453 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8454 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 8455 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8456 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8457 | mWindow->setDispatchingTimeout(100ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8458 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8459 | |
| 8460 | // Set focused application. |
| 8461 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 8462 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8463 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8464 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8465 | mWindow->consumeFocusEvent(true); |
| 8466 | } |
| 8467 | |
| 8468 | virtual void TearDown() override { |
| 8469 | InputDispatcherTest::TearDown(); |
| 8470 | mWindow.clear(); |
| 8471 | } |
| 8472 | |
| 8473 | protected: |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8474 | static constexpr std::chrono::duration SPY_TIMEOUT = 200ms; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8475 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8476 | sp<FakeWindowHandle> mWindow; |
| 8477 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 8478 | |
| 8479 | void tapOnWindow() { |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 8480 | const auto touchingPointer = PointerBuilder(/*id=*/0, ToolType::FINGER) |
| 8481 | .x(WINDOW_LOCATION.x) |
| 8482 | .y(WINDOW_LOCATION.y); |
| 8483 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8484 | .pointer(touchingPointer) |
| 8485 | .build()); |
| 8486 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8487 | .pointer(touchingPointer) |
| 8488 | .build()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8489 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8490 | |
| 8491 | sp<FakeWindowHandle> addSpyWindow() { |
| 8492 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8493 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8494 | spy->setTrustedOverlay(true); |
| 8495 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8496 | spy->setSpy(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8497 | spy->setDispatchingTimeout(SPY_TIMEOUT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8498 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8499 | return spy; |
| 8500 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8501 | }; |
| 8502 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8503 | // Send a tap and respond, which should not cause an ANR. |
| 8504 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 8505 | tapOnWindow(); |
| 8506 | mWindow->consumeMotionDown(); |
| 8507 | mWindow->consumeMotionUp(); |
| 8508 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8509 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8510 | } |
| 8511 | |
| 8512 | // Send a regular key and respond, which should not cause an ANR. |
| 8513 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8514 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8515 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8516 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8517 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8518 | } |
| 8519 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 8520 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 8521 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8522 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 8523 | mWindow->consumeFocusEvent(false); |
| 8524 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8525 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8526 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 8527 | InputEventInjectionSync::NONE, CONSUME_TIMEOUT_EVENT_EXPECTED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8528 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8529 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 8530 | // Key will not go to window because we have no focused window. |
| 8531 | // The 'no focused window' ANR timer should start instead. |
| 8532 | |
| 8533 | // Now, the focused application goes away. |
| 8534 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 8535 | // The key should get dropped and there should be no ANR. |
| 8536 | |
| 8537 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8538 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8539 | } |
| 8540 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8541 | // 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] | 8542 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 8543 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8544 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8545 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8546 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8547 | WINDOW_LOCATION)); |
| 8548 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8549 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8550 | ASSERT_TRUE(sequenceNum); |
| 8551 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8552 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8553 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8554 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8555 | mWindow->consumeMotionEvent( |
| 8556 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8557 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8558 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8559 | } |
| 8560 | |
| 8561 | // Send a key to the app and have the app not respond right away. |
| 8562 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 8563 | // Inject a key, and don't respond - expect that ANR is called. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8564 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8565 | const auto [sequenceNum, _] = mWindow->receiveEvent(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8566 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8567 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8568 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8569 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8570 | } |
| 8571 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8572 | // We have a focused application, but no focused window |
| 8573 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8574 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8575 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8576 | mWindow->consumeFocusEvent(false); |
| 8577 | |
| 8578 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8579 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8580 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8581 | WINDOW_LOCATION)); |
| 8582 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 8583 | mDispatcher->waitForIdle(); |
| 8584 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8585 | |
| 8586 | // Once a focused event arrives, we get an ANR for this application |
| 8587 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 8588 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8589 | const InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8590 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8591 | InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8592 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8593 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 8594 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8595 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8596 | } |
| 8597 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8598 | /** |
| 8599 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 8600 | * there will not be an ANR. |
| 8601 | */ |
| 8602 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 8603 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8604 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8605 | mWindow->consumeFocusEvent(false); |
| 8606 | |
| 8607 | KeyEvent event; |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 8608 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 8609 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8610 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 8611 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 8612 | |
| 8613 | // Define a valid key down event that is stale (too old). |
| 8614 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8615 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /*flags=*/0, AKEYCODE_A, KEY_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8616 | AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8617 | |
| 8618 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 8619 | |
| 8620 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8621 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8622 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 8623 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 8624 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 8625 | << "Injection should fail because the event is stale"; |
| 8626 | |
| 8627 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8628 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8629 | mWindow->assertNoEvents(); |
| 8630 | } |
| 8631 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8632 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8633 | // Make sure that we don't notify policy twice about the same ANR. |
| 8634 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 8635 | const std::chrono::duration appTimeout = 400ms; |
| 8636 | mApplication->setDispatchingTimeout(appTimeout); |
| 8637 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 8638 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8639 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8640 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8641 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8642 | |
| 8643 | // Once a focused event arrives, we get an ANR for this application |
| 8644 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 8645 | // injection times out (instead of failing). |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 8646 | const std::chrono::duration eventInjectionTimeout = 100ms; |
| 8647 | ASSERT_LT(eventInjectionTimeout, appTimeout); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8648 | const InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8649 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 8650 | InputEventInjectionSync::WAIT_FOR_RESULT, eventInjectionTimeout, |
| 8651 | /*allowKeyRepeat=*/false); |
| 8652 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result) |
| 8653 | << "result=" << ftl::enum_string(result); |
| 8654 | // We already waited for 'eventInjectionTimeout`, because the countdown started when the event |
| 8655 | // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait. |
| 8656 | std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout; |
| 8657 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8658 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 8659 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8660 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 8661 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8662 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8663 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 8664 | // '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] | 8665 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8666 | } |
| 8667 | |
| 8668 | // We have a focused application, but no focused window |
| 8669 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8670 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8671 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8672 | mWindow->consumeFocusEvent(false); |
| 8673 | |
| 8674 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8675 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8676 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 8677 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 8678 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8679 | |
| 8680 | // Future focused events get dropped right away |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8681 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8682 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8683 | mWindow->assertNoEvents(); |
| 8684 | } |
| 8685 | |
| 8686 | /** |
| 8687 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 8688 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 8689 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 8690 | * the ANR mechanism should still work. |
| 8691 | * |
| 8692 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 8693 | * DOWN event, while not responding on the second one. |
| 8694 | */ |
| 8695 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 8696 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8697 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8698 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 8699 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 8700 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8701 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8702 | |
| 8703 | // Now send ACTION_UP, with identical timestamp |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8704 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8705 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 8706 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 8707 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8708 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8709 | |
| 8710 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 8711 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8712 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8713 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8714 | } |
| 8715 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8716 | // A spy window can receive an ANR |
| 8717 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 8718 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 8719 | |
| 8720 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8721 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8722 | WINDOW_LOCATION)); |
| 8723 | mWindow->consumeMotionDown(); |
| 8724 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8725 | const auto [sequenceNum, _] = spy->receiveEvent(); // ACTION_DOWN |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8726 | ASSERT_TRUE(sequenceNum); |
| 8727 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8728 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8729 | |
| 8730 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8731 | spy->consumeMotionEvent( |
| 8732 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8733 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8734 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8735 | } |
| 8736 | |
| 8737 | // 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] | 8738 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8739 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 8740 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8741 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8742 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8743 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8744 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8745 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8746 | |
| 8747 | // Stuck on the ACTION_UP |
| 8748 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8749 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8750 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8751 | // 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] | 8752 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8753 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8754 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8755 | |
| 8756 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 8757 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8758 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8759 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8760 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8761 | } |
| 8762 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8763 | // 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] | 8764 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8765 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 8766 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8767 | |
| 8768 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8769 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8770 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8771 | |
| 8772 | mWindow->consumeMotionDown(); |
| 8773 | // Stuck on the ACTION_UP |
| 8774 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8775 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8776 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8777 | // 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] | 8778 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8779 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8780 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8781 | |
| 8782 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 8783 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8784 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8785 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8786 | spy->assertNoEvents(); |
| 8787 | } |
| 8788 | |
| 8789 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8790 | mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8791 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 8792 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8793 | |
| 8794 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8795 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8796 | WINDOW_LOCATION)); |
| 8797 | |
| 8798 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8799 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 8800 | ASSERT_TRUE(consumeSeq); |
| 8801 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8802 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(), |
| 8803 | MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8804 | |
| 8805 | monitor.finishEvent(*consumeSeq); |
| 8806 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 8807 | |
| 8808 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8809 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8810 | } |
| 8811 | |
| 8812 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 8813 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 8814 | // get an ANR again. |
| 8815 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 8816 | // 2. consume all pending events (= queue becomes healthy again) |
| 8817 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 8818 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 8819 | tapOnWindow(); |
| 8820 | |
| 8821 | mWindow->consumeMotionDown(); |
| 8822 | // Block on ACTION_UP |
| 8823 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8824 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8825 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 8826 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8827 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8828 | mWindow->assertNoEvents(); |
| 8829 | |
| 8830 | tapOnWindow(); |
| 8831 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8832 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8833 | mWindow->consumeMotionUp(); |
| 8834 | |
| 8835 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8836 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8837 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8838 | mWindow->assertNoEvents(); |
| 8839 | } |
| 8840 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8841 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 8842 | // it. |
| 8843 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8844 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8845 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8846 | WINDOW_LOCATION)); |
| 8847 | |
| 8848 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8849 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8850 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8851 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 8852 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8853 | // 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] | 8854 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8855 | mWindow->consumeMotionEvent( |
| 8856 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8857 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8858 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8859 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8860 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8861 | } |
| 8862 | |
| 8863 | /** |
| 8864 | * 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] | 8865 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8866 | * |
| 8867 | * Warning!!! |
| 8868 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 8869 | * and the injection timeout that we specify when injecting the key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8870 | * We must have the injection timeout (100ms) be smaller than |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8871 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 8872 | * |
| 8873 | * If that value changes, this test should also change. |
| 8874 | */ |
| 8875 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 8876 | 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] | 8877 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8878 | |
| 8879 | tapOnWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8880 | const auto& [downSequenceNum, downEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8881 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8882 | const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8883 | ASSERT_TRUE(upSequenceNum); |
| 8884 | // Don't finish the events yet, and send a key |
| 8885 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 8886 | // window even if motions are still being processed. But because the injection timeout is short, |
| 8887 | // we will receive INJECTION_TIMED_OUT as the result. |
| 8888 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8889 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8890 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 8891 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8892 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8893 | // Key will not be sent to the window, yet, because the window is still processing events |
| 8894 | // 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] | 8895 | // Make sure that `assertNoEvents` doesn't wait too long, because it could cause an ANR. |
| 8896 | // Rely here on the fact that it uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED under the hood. |
| 8897 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 8898 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8899 | |
| 8900 | std::this_thread::sleep_for(500ms); |
| 8901 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 8902 | // to the focused window, even though we have not yet finished the motion event |
| 8903 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8904 | mWindow->finishEvent(*downSequenceNum); |
| 8905 | mWindow->finishEvent(*upSequenceNum); |
| 8906 | } |
| 8907 | |
| 8908 | /** |
| 8909 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 8910 | * not go to the focused window until the motion is processed. |
| 8911 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 8912 | * focused window right away. |
| 8913 | */ |
| 8914 | TEST_F(InputDispatcherSingleWindowAnr, |
| 8915 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 8916 | 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] | 8917 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8918 | |
| 8919 | tapOnWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8920 | const auto& [downSequenceNum, _] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8921 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8922 | const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8923 | ASSERT_TRUE(upSequenceNum); |
| 8924 | // Don't finish the events yet, and send a key |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 8925 | mDispatcher->notifyKey( |
| 8926 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 8927 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 8928 | .build()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8929 | // At this point, key is still pending, and should not be sent to the application yet. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8930 | // Make sure the `assertNoEvents` check doesn't take too long. It uses |
| 8931 | // CONSUME_TIMEOUT_NO_EVENT_EXPECTED under the hood. |
| 8932 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 8933 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8934 | |
| 8935 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 8936 | tapOnWindow(); |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 8937 | mWindow->consumeKeyEvent(WithKeyAction(AKEY_EVENT_ACTION_DOWN)); // it doesn't matter that we |
| 8938 | // haven't ack'd the other events yet. We can finish events in any order. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8939 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 8940 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 8941 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 8942 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8943 | mWindow->assertNoEvents(); |
| 8944 | } |
| 8945 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 8946 | /** |
| 8947 | * Send an event to the app and have the app not respond right away. |
| 8948 | * When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 8949 | * So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 8950 | * At some point, the window becomes responsive again. |
| 8951 | * Ensure that subsequent events get dropped, and the next gesture is delivered. |
| 8952 | */ |
| 8953 | TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) { |
| 8954 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8955 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 8956 | .build()); |
| 8957 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8958 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 8959 | ASSERT_TRUE(sequenceNum); |
| 8960 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 8961 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 8962 | |
| 8963 | mWindow->finishEvent(*sequenceNum); |
| 8964 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 8965 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8966 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
| 8967 | |
| 8968 | // Now that the window is responsive, let's continue the gesture. |
| 8969 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8970 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8971 | .build()); |
| 8972 | |
| 8973 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8974 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8975 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 8976 | .build()); |
| 8977 | |
| 8978 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8979 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8980 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 8981 | .build()); |
| 8982 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8983 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8984 | .build()); |
| 8985 | // We already canceled this pointer, so the window shouldn't get any new events. |
| 8986 | mWindow->assertNoEvents(); |
| 8987 | |
| 8988 | // Start another one. |
| 8989 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8990 | .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15)) |
| 8991 | .build()); |
| 8992 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 8993 | } |
| 8994 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8995 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 8996 | virtual void SetUp() override { |
| 8997 | InputDispatcherTest::SetUp(); |
| 8998 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8999 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9000 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9001 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 9002 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9003 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9004 | // 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] | 9005 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9006 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9007 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 9008 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9009 | mFocusedWindow->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9010 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9011 | |
| 9012 | // Set focused application. |
| 9013 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 9014 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9015 | |
| 9016 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9017 | mDispatcher->onWindowInfosChanged( |
| 9018 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9019 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9020 | mFocusedWindow->consumeFocusEvent(true); |
| 9021 | } |
| 9022 | |
| 9023 | virtual void TearDown() override { |
| 9024 | InputDispatcherTest::TearDown(); |
| 9025 | |
| 9026 | mUnfocusedWindow.clear(); |
| 9027 | mFocusedWindow.clear(); |
| 9028 | } |
| 9029 | |
| 9030 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9031 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9032 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 9033 | sp<FakeWindowHandle> mFocusedWindow; |
| 9034 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 9035 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 9036 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 9037 | |
| 9038 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 9039 | |
| 9040 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 9041 | |
| 9042 | private: |
| 9043 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9044 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9045 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9046 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9047 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9048 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9049 | location)); |
| 9050 | } |
| 9051 | }; |
| 9052 | |
| 9053 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 9054 | // should be ANR'd first. |
| 9055 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9056 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 9057 | injectMotionEvent(*mDispatcher, |
| 9058 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 9059 | AINPUT_SOURCE_TOUCHSCREEN) |
| 9060 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 9061 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 9062 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 9063 | .build())); |
| 9064 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9065 | injectMotionEvent(*mDispatcher, |
| 9066 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 9067 | AINPUT_SOURCE_TOUCHSCREEN) |
| 9068 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 9069 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 9070 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 9071 | .build())); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9072 | mFocusedWindow->consumeMotionDown(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 9073 | mFocusedWindow->consumeMotionUp(); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 9074 | mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9075 | // We consumed all events, so no ANR |
| 9076 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9077 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9078 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9079 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 9080 | injectMotionEvent(*mDispatcher, |
| 9081 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 9082 | AINPUT_SOURCE_TOUCHSCREEN) |
| 9083 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 9084 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 9085 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 9086 | .build())); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9087 | const auto [unfocusedSequenceNum, _] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9088 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9089 | |
| 9090 | const std::chrono::duration timeout = |
| 9091 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9092 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 9093 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9094 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9095 | mFocusedWindow->consumeMotionDown(); |
| 9096 | // This cancel is generated because the connection was unresponsive |
| 9097 | mFocusedWindow->consumeMotionCancel(); |
| 9098 | mFocusedWindow->assertNoEvents(); |
| 9099 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9100 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9101 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 9102 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9103 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9104 | } |
| 9105 | |
| 9106 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 9107 | // it doesn't matter which order they should have ANR. |
| 9108 | // But we should receive ANR for both. |
| 9109 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 9110 | // Set the timeout for unfocused window to match the focused window |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9111 | mUnfocusedWindow->setDispatchingTimeout( |
| 9112 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9113 | mDispatcher->onWindowInfosChanged( |
| 9114 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9115 | |
| 9116 | tapOnFocusedWindow(); |
| 9117 | // 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] | 9118 | // 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] | 9119 | std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken( |
| 9120 | mFocusedWindow->getDispatchingTimeout( |
| 9121 | DISPATCHING_TIMEOUT)), |
| 9122 | mFakePolicy->getUnresponsiveWindowToken(0ms)}; |
| 9123 | |
| 9124 | ASSERT_THAT(anrConnectionTokens, |
| 9125 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 9126 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9127 | |
| 9128 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9129 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9130 | |
| 9131 | mFocusedWindow->consumeMotionDown(); |
| 9132 | mFocusedWindow->consumeMotionUp(); |
| 9133 | mUnfocusedWindow->consumeMotionOutside(); |
| 9134 | |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9135 | std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(), |
| 9136 | mFakePolicy->getResponsiveWindowToken()}; |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9137 | |
| 9138 | // Both applications should be marked as responsive, in any order |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9139 | ASSERT_THAT(responsiveTokens, |
| 9140 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 9141 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9142 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9143 | } |
| 9144 | |
| 9145 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 9146 | // We should also log an error to account for the dropped event (not tested here). |
| 9147 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 9148 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 9149 | tapOnFocusedWindow(); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 9150 | mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9151 | // Receive the events, but don't respond |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9152 | const auto [downEventSequenceNum, downEvent] = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9153 | ASSERT_TRUE(downEventSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9154 | const auto [upEventSequenceNum, upEvent] = mFocusedWindow->receiveEvent(); // ACTION_UP |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9155 | ASSERT_TRUE(upEventSequenceNum); |
| 9156 | const std::chrono::duration timeout = |
| 9157 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9158 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9159 | |
| 9160 | // Tap once again |
| 9161 | // 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] | 9162 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9163 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9164 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9165 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9166 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9167 | FOCUSED_WINDOW_LOCATION)); |
| 9168 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 9169 | // valid touch target |
| 9170 | mUnfocusedWindow->assertNoEvents(); |
| 9171 | |
| 9172 | // Consume the first tap |
| 9173 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 9174 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 9175 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9176 | // The second tap did not go to the focused window |
| 9177 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9178 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9179 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 9180 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9181 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9182 | } |
| 9183 | |
| 9184 | // If you tap outside of all windows, there will not be ANR |
| 9185 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9186 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9187 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9188 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 9189 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9190 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9191 | } |
| 9192 | |
| 9193 | // Since the focused window is paused, tapping on it should not produce any events |
| 9194 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 9195 | mFocusedWindow->setPaused(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9196 | mDispatcher->onWindowInfosChanged( |
| 9197 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9198 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9199 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9200 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9201 | FOCUSED_WINDOW_LOCATION)); |
| 9202 | |
| 9203 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 9204 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9205 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 9206 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9207 | |
| 9208 | mFocusedWindow->assertNoEvents(); |
| 9209 | mUnfocusedWindow->assertNoEvents(); |
| 9210 | } |
| 9211 | |
| 9212 | /** |
| 9213 | * 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] | 9214 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9215 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 9216 | * |
| 9217 | * Warning!!! |
| 9218 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 9219 | * and the injection timeout that we specify when injecting the key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9220 | * We must have the injection timeout (100ms) be smaller than |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9221 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 9222 | * |
| 9223 | * If that value changes, this test should also change. |
| 9224 | */ |
| 9225 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 9226 | // Set a long ANR timeout to prevent it from triggering |
| 9227 | mFocusedWindow->setDispatchingTimeout(2s); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9228 | mDispatcher->onWindowInfosChanged( |
| 9229 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9230 | |
| 9231 | tapOnUnfocusedWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9232 | const auto [downSequenceNum, downEvent] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9233 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9234 | const auto [upSequenceNum, upEvent] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9235 | ASSERT_TRUE(upSequenceNum); |
| 9236 | // Don't finish the events yet, and send a key |
| 9237 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 9238 | // window even if motions are still being processed. |
| 9239 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9240 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9241 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 9242 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9243 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9244 | // 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] | 9245 | // and the key remains pending, waiting for the touch events to be processed. |
| 9246 | // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED |
| 9247 | // under the hood. |
| 9248 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 9249 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9250 | |
| 9251 | // 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] | 9252 | mFocusedWindow->setFocusable(false); |
| 9253 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9254 | mDispatcher->onWindowInfosChanged( |
| 9255 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9256 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9257 | |
| 9258 | // Focus events should precede the key events |
| 9259 | mUnfocusedWindow->consumeFocusEvent(true); |
| 9260 | mFocusedWindow->consumeFocusEvent(false); |
| 9261 | |
| 9262 | // Finish the tap events, which should unblock dispatcher |
| 9263 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 9264 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 9265 | |
| 9266 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 9267 | // can finally go to the newly focused "mUnfocusedWindow". |
| 9268 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 9269 | mFocusedWindow->assertNoEvents(); |
| 9270 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9271 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9272 | } |
| 9273 | |
| 9274 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 9275 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 9276 | // The other window should not be affected by that. |
| 9277 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 9278 | // Touch Window 1 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9279 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9280 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9281 | {FOCUSED_WINDOW_LOCATION})); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 9282 | mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9283 | |
| 9284 | // Touch Window 2 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9285 | mDispatcher->notifyMotion( |
| 9286 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9287 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9288 | |
| 9289 | const std::chrono::duration timeout = |
| 9290 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9291 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9292 | |
| 9293 | mUnfocusedWindow->consumeMotionDown(); |
| 9294 | mFocusedWindow->consumeMotionDown(); |
| 9295 | // Focused window may or may not receive ACTION_MOVE |
| 9296 | // But it should definitely receive ACTION_CANCEL due to the ANR |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9297 | const auto [moveOrCancelSequenceNum, event] = mFocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9298 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 9299 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 9300 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 9301 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9302 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 9303 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 9304 | mFocusedWindow->consumeMotionCancel(); |
| 9305 | } else { |
| 9306 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 9307 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9308 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9309 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 9310 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9311 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9312 | mUnfocusedWindow->assertNoEvents(); |
| 9313 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9314 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9315 | } |
| 9316 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9317 | /** |
| 9318 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 9319 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 9320 | * |
| 9321 | * If the user touches another application during this time, the key should be dropped. |
| 9322 | * Next, if a new focused window comes in, without toggling the focused application, |
| 9323 | * then no ANR should occur. |
| 9324 | * |
| 9325 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 9326 | * but in some cases the policy may not update the focused application. |
| 9327 | */ |
| 9328 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 9329 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 9330 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | c033dfb | 2023-10-03 10:45:16 -0700 | [diff] [blame] | 9331 | focusedApplication->setDispatchingTimeout(300ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9332 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 9333 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 9334 | mFocusedWindow->setFocusable(false); |
| 9335 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9336 | mDispatcher->onWindowInfosChanged( |
| 9337 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9338 | mFocusedWindow->consumeFocusEvent(false); |
| 9339 | |
| 9340 | // Send a key. The ANR timer should start because there is no focused window. |
| 9341 | // 'focusedApplication' will get blamed if this timer completes. |
| 9342 | // 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] | 9343 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9344 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 9345 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9346 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9347 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9348 | |
| 9349 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 9350 | // then the injected touches won't cause the focused event to get dropped. |
| 9351 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 9352 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 9353 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 9354 | // For this test, it means that the key would get delivered to the window once it becomes |
| 9355 | // focused. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9356 | std::this_thread::sleep_for(100ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9357 | |
| 9358 | // Touch unfocused window. This should force the pending key to get dropped. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9359 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9360 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9361 | {UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9362 | |
| 9363 | // We do not consume the motion right away, because that would require dispatcher to first |
| 9364 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 9365 | // Set the focused window first. |
| 9366 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9367 | mDispatcher->onWindowInfosChanged( |
| 9368 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9369 | setFocusedWindow(mFocusedWindow); |
| 9370 | mFocusedWindow->consumeFocusEvent(true); |
| 9371 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 9372 | // to another application. This could be a bug / behaviour in the policy. |
| 9373 | |
| 9374 | mUnfocusedWindow->consumeMotionDown(); |
| 9375 | |
| 9376 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9377 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 9378 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 9379 | } |
| 9380 | |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 9381 | /** |
| 9382 | * If we are pruning input queue, we should never drop pointer events. Otherwise, we risk having |
| 9383 | * an inconsistent event stream inside the dispatcher. In this test, we make sure that the |
| 9384 | * dispatcher doesn't prune pointer events incorrectly. |
| 9385 | * |
| 9386 | * This test reproduces a crash in InputDispatcher. |
| 9387 | * To reproduce the crash, we need to simulate the conditions for "pruning input queue" to occur. |
| 9388 | * |
| 9389 | * Keep the currently focused application (mApplication), and have no focused window. |
| 9390 | * We set up two additional windows: |
| 9391 | * 1) The navigation bar window. This simulates the system "NavigationBar", which is used in the |
| 9392 | * 3-button navigation mode. This window injects a BACK button when it's touched. 2) The application |
| 9393 | * window. This window is not focusable, but is touchable. |
| 9394 | * |
| 9395 | * We first touch the navigation bar, which causes it to inject a key. Since there's no focused |
| 9396 | * window, the dispatcher doesn't process this key, and all other events inside dispatcher are now |
| 9397 | * blocked. The dispatcher is waiting for 'mApplication' to add a focused window. |
| 9398 | * |
| 9399 | * Now, we touch "Another window". This window is owned by a different application than |
| 9400 | * 'mApplication'. This causes the dispatcher to stop waiting for 'mApplication' to add a focused |
| 9401 | * window. Now, the "pruning input queue" behaviour should kick in, and the dispatcher should start |
| 9402 | * dropping the events from its queue. Ensure that no crash occurs. |
| 9403 | * |
| 9404 | * In this test, we are setting long timeouts to prevent ANRs and events dropped due to being stale. |
| 9405 | * This does not affect the test running time. |
| 9406 | */ |
| 9407 | TEST_F(InputDispatcherMultiWindowAnr, PruningInputQueueShouldNotDropPointerEvents) { |
| 9408 | std::shared_ptr<FakeApplicationHandle> systemUiApplication = |
| 9409 | std::make_shared<FakeApplicationHandle>(); |
| 9410 | systemUiApplication->setDispatchingTimeout(3000ms); |
| 9411 | mFakePolicy->setStaleEventTimeout(3000ms); |
| 9412 | sp<FakeWindowHandle> navigationBar = |
| 9413 | sp<FakeWindowHandle>::make(systemUiApplication, mDispatcher, "NavigationBar", |
| 9414 | ADISPLAY_ID_DEFAULT); |
| 9415 | navigationBar->setFocusable(false); |
| 9416 | navigationBar->setWatchOutsideTouch(true); |
| 9417 | navigationBar->setFrame(Rect(0, 0, 100, 100)); |
| 9418 | |
| 9419 | mApplication->setDispatchingTimeout(3000ms); |
| 9420 | // 'mApplication' is already focused, but we call it again here to make it explicit. |
| 9421 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 9422 | |
| 9423 | std::shared_ptr<FakeApplicationHandle> anotherApplication = |
| 9424 | std::make_shared<FakeApplicationHandle>(); |
| 9425 | sp<FakeWindowHandle> appWindow = |
| 9426 | sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Another window", |
| 9427 | ADISPLAY_ID_DEFAULT); |
| 9428 | appWindow->setFocusable(false); |
| 9429 | appWindow->setFrame(Rect(100, 100, 200, 200)); |
| 9430 | |
| 9431 | mDispatcher->onWindowInfosChanged( |
| 9432 | {{*navigationBar->getInfo(), *appWindow->getInfo()}, {}, 0, 0}); |
| 9433 | // 'mFocusedWindow' is no longer in the dispatcher window list, and therefore loses focus |
| 9434 | mFocusedWindow->consumeFocusEvent(false); |
| 9435 | |
| 9436 | // Touch down the navigation bar. It consumes the touch and injects a key into the dispatcher |
| 9437 | // in response. |
| 9438 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9439 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 9440 | .build()); |
| 9441 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 9442 | |
| 9443 | // Key will not be sent anywhere because we have no focused window. It will remain pending. |
| 9444 | // Pretend we are injecting KEYCODE_BACK, but it doesn't actually matter what key it is. |
| 9445 | InputEventInjectionResult result = |
| 9446 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 9447 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms, |
| 9448 | /*allowKeyRepeat=*/false); |
| 9449 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
| 9450 | |
| 9451 | // Finish the gesture - lift up finger and inject ACTION_UP key event |
| 9452 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 9453 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 9454 | .build()); |
| 9455 | result = injectKey(*mDispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 9456 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms, |
| 9457 | /*allowKeyRepeat=*/false); |
| 9458 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
| 9459 | // The key that was injected is blocking the dispatcher, so the navigation bar shouldn't be |
| 9460 | // getting any events yet. |
| 9461 | navigationBar->assertNoEvents(); |
| 9462 | |
| 9463 | // Now touch "Another window". This touch is going to a different application than the one we |
| 9464 | // are waiting for (which is 'mApplication'). |
| 9465 | // This should cause the dispatcher to drop the pending focus-dispatched events (like the key |
| 9466 | // trying to be injected) and to continue processing the rest of the events in the original |
| 9467 | // order. |
| 9468 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9469 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 9470 | .build()); |
| 9471 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 9472 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_OUTSIDE)); |
| 9473 | appWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 9474 | |
| 9475 | appWindow->assertNoEvents(); |
| 9476 | navigationBar->assertNoEvents(); |
| 9477 | } |
| 9478 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9479 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 9480 | // that has feature NO_INPUT_CHANNEL. |
| 9481 | // Layout: |
| 9482 | // Top (closest to user) |
| 9483 | // mNoInputWindow (above all windows) |
| 9484 | // mBottomWindow |
| 9485 | // Bottom (furthest from user) |
| 9486 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 9487 | virtual void SetUp() override { |
| 9488 | InputDispatcherTest::SetUp(); |
| 9489 | |
| 9490 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9491 | mNoInputWindow = |
| 9492 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 9493 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 9494 | /*createInputChannel=*/false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9495 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9496 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 9497 | // It's perfectly valid for this window to not have an associated input channel |
| 9498 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9499 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 9500 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9501 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 9502 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9503 | mDispatcher->onWindowInfosChanged( |
| 9504 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9505 | } |
| 9506 | |
| 9507 | protected: |
| 9508 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 9509 | sp<FakeWindowHandle> mNoInputWindow; |
| 9510 | sp<FakeWindowHandle> mBottomWindow; |
| 9511 | }; |
| 9512 | |
| 9513 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 9514 | PointF touchedPoint = {10, 10}; |
| 9515 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9516 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9517 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9518 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9519 | |
| 9520 | mNoInputWindow->assertNoEvents(); |
| 9521 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 9522 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 9523 | // and therefore should prevent mBottomWindow from receiving touches |
| 9524 | mBottomWindow->assertNoEvents(); |
| 9525 | } |
| 9526 | |
| 9527 | /** |
| 9528 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 9529 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 9530 | */ |
| 9531 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 9532 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9533 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 9534 | "Window with input channel and NO_INPUT_CHANNEL", |
| 9535 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9536 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9537 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9538 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9539 | mDispatcher->onWindowInfosChanged( |
| 9540 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9541 | |
| 9542 | PointF touchedPoint = {10, 10}; |
| 9543 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9544 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9545 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9546 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9547 | |
| 9548 | mNoInputWindow->assertNoEvents(); |
| 9549 | mBottomWindow->assertNoEvents(); |
| 9550 | } |
| 9551 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9552 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 9553 | protected: |
| 9554 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 9555 | sp<FakeWindowHandle> mWindow; |
| 9556 | sp<FakeWindowHandle> mMirror; |
| 9557 | |
| 9558 | virtual void SetUp() override { |
| 9559 | InputDispatcherTest::SetUp(); |
| 9560 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9561 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 9562 | mMirror = mWindow->clone(ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9563 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 9564 | mWindow->setFocusable(true); |
| 9565 | mMirror->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9566 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9567 | } |
| 9568 | }; |
| 9569 | |
| 9570 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 9571 | // Request focus on a mirrored window |
| 9572 | setFocusedWindow(mMirror); |
| 9573 | |
| 9574 | // window gets focused |
| 9575 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9576 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9577 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9578 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9579 | } |
| 9580 | |
| 9581 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 9582 | // focusable. |
| 9583 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 9584 | setFocusedWindow(mMirror); |
| 9585 | |
| 9586 | // window gets focused |
| 9587 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9588 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9589 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9590 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9591 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9592 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9593 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9594 | |
| 9595 | mMirror->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9596 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9597 | |
| 9598 | // window loses focus since one of the windows associated with the token in not focusable |
| 9599 | mWindow->consumeFocusEvent(false); |
| 9600 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9601 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9602 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9603 | mWindow->assertNoEvents(); |
| 9604 | } |
| 9605 | |
| 9606 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 9607 | // invisible. |
| 9608 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 9609 | setFocusedWindow(mMirror); |
| 9610 | |
| 9611 | // window gets focused |
| 9612 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9613 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9614 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9615 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9616 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9617 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9618 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9619 | |
| 9620 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9621 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9622 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9623 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9624 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9625 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9626 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9627 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9628 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9629 | |
| 9630 | mWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9631 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9632 | |
| 9633 | // window loses focus only after all windows associated with the token become invisible. |
| 9634 | mWindow->consumeFocusEvent(false); |
| 9635 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9636 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9637 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9638 | mWindow->assertNoEvents(); |
| 9639 | } |
| 9640 | |
| 9641 | // A focused & mirrored window remains focused until both windows are removed. |
| 9642 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 9643 | setFocusedWindow(mMirror); |
| 9644 | |
| 9645 | // window gets focused |
| 9646 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9647 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9648 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9649 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9650 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9651 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9652 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9653 | |
| 9654 | // single window is removed but the window token remains focused |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9655 | mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9656 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9657 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9658 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9659 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9660 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9661 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9662 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9663 | |
| 9664 | // Both windows are removed |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9665 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9666 | mWindow->consumeFocusEvent(false); |
| 9667 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9668 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9669 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9670 | mWindow->assertNoEvents(); |
| 9671 | } |
| 9672 | |
| 9673 | // Focus request can be pending until one window becomes visible. |
| 9674 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 9675 | // Request focus on an invisible mirror. |
| 9676 | mWindow->setVisible(false); |
| 9677 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9678 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9679 | setFocusedWindow(mMirror); |
| 9680 | |
| 9681 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9682 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9683 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 9684 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9685 | |
| 9686 | mMirror->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9687 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9688 | |
| 9689 | // window gets focused |
| 9690 | mWindow->consumeFocusEvent(true); |
| 9691 | // window gets the pending key event |
| 9692 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 9693 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9694 | |
| 9695 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 9696 | protected: |
| 9697 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 9698 | sp<FakeWindowHandle> mWindow; |
| 9699 | sp<FakeWindowHandle> mSecondWindow; |
| 9700 | |
| 9701 | void SetUp() override { |
| 9702 | InputDispatcherTest::SetUp(); |
| 9703 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9704 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9705 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9706 | mSecondWindow = |
| 9707 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9708 | mSecondWindow->setFocusable(true); |
| 9709 | |
| 9710 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9711 | mDispatcher->onWindowInfosChanged( |
| 9712 | {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9713 | |
| 9714 | setFocusedWindow(mWindow); |
| 9715 | mWindow->consumeFocusEvent(true); |
| 9716 | } |
| 9717 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9718 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9719 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9720 | } |
| 9721 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9722 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 9723 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9724 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9725 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 9726 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9727 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9728 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9729 | } |
| 9730 | }; |
| 9731 | |
| 9732 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 9733 | // Ensure that capture cannot be obtained for unfocused windows. |
| 9734 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 9735 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 9736 | mSecondWindow->assertNoEvents(); |
| 9737 | |
| 9738 | // Ensure that capture can be enabled from the focus window. |
| 9739 | requestAndVerifyPointerCapture(mWindow, true); |
| 9740 | |
| 9741 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 9742 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 9743 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 9744 | |
| 9745 | // Ensure that capture can be disabled from the window with capture. |
| 9746 | requestAndVerifyPointerCapture(mWindow, false); |
| 9747 | } |
| 9748 | |
| 9749 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9750 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9751 | |
| 9752 | setFocusedWindow(mSecondWindow); |
| 9753 | |
| 9754 | // Ensure that the capture disabled event was sent first. |
| 9755 | mWindow->consumeCaptureEvent(false); |
| 9756 | mWindow->consumeFocusEvent(false); |
| 9757 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9758 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9759 | |
| 9760 | // 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] | 9761 | notifyPointerCaptureChanged({}); |
| 9762 | notifyPointerCaptureChanged(request); |
| 9763 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9764 | mWindow->assertNoEvents(); |
| 9765 | mSecondWindow->assertNoEvents(); |
| 9766 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 9767 | } |
| 9768 | |
| 9769 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9770 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9771 | |
| 9772 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9773 | notifyPointerCaptureChanged({}); |
| 9774 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9775 | |
| 9776 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9777 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9778 | mWindow->consumeCaptureEvent(false); |
| 9779 | mWindow->assertNoEvents(); |
| 9780 | } |
| 9781 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9782 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 9783 | requestAndVerifyPointerCapture(mWindow, true); |
| 9784 | |
| 9785 | // The first window loses focus. |
| 9786 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9787 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9788 | mWindow->consumeCaptureEvent(false); |
| 9789 | |
| 9790 | // Request Pointer Capture from the second window before the notification from InputReader |
| 9791 | // arrives. |
| 9792 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9793 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9794 | |
| 9795 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9796 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9797 | |
| 9798 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9799 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9800 | |
| 9801 | mSecondWindow->consumeFocusEvent(true); |
| 9802 | mSecondWindow->consumeCaptureEvent(true); |
| 9803 | } |
| 9804 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9805 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 9806 | // App repeatedly enables and disables capture. |
| 9807 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 9808 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 9809 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 9810 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 9811 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 9812 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 9813 | |
| 9814 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 9815 | // first request is now stale, this should do nothing. |
| 9816 | notifyPointerCaptureChanged(firstRequest); |
| 9817 | mWindow->assertNoEvents(); |
| 9818 | |
| 9819 | // InputReader notifies that the second request was enabled. |
| 9820 | notifyPointerCaptureChanged(secondRequest); |
| 9821 | mWindow->consumeCaptureEvent(true); |
| 9822 | } |
| 9823 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 9824 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 9825 | requestAndVerifyPointerCapture(mWindow, true); |
| 9826 | |
| 9827 | // App toggles pointer capture off and on. |
| 9828 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 9829 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 9830 | |
| 9831 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 9832 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 9833 | |
| 9834 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 9835 | // preceding "disable" request. |
| 9836 | notifyPointerCaptureChanged(enableRequest); |
| 9837 | |
| 9838 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 9839 | // any notifications. |
| 9840 | mWindow->assertNoEvents(); |
| 9841 | } |
| 9842 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 9843 | /** |
| 9844 | * One window. Hover mouse in the window, and then start capture. Make sure that the relative |
| 9845 | * mouse movements don't affect the previous mouse hovering state. |
| 9846 | * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no |
| 9847 | * HOVER_MOVE events). |
| 9848 | */ |
| 9849 | TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) { |
| 9850 | // Mouse hover on the window |
| 9851 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 9852 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 9853 | .build()); |
| 9854 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 9855 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 9856 | .build()); |
| 9857 | |
| 9858 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER))); |
| 9859 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE))); |
| 9860 | |
| 9861 | // Start pointer capture |
| 9862 | requestAndVerifyPointerCapture(mWindow, true); |
| 9863 | |
| 9864 | // Send some relative mouse movements and receive them in the window. |
| 9865 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE) |
| 9866 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11)) |
| 9867 | .build()); |
| 9868 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11), |
| 9869 | WithSource(AINPUT_SOURCE_MOUSE_RELATIVE))); |
| 9870 | |
| 9871 | // Stop pointer capture |
| 9872 | requestAndVerifyPointerCapture(mWindow, false); |
| 9873 | |
| 9874 | // Continue hovering on the window |
| 9875 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 9876 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115)) |
| 9877 | .build()); |
| 9878 | mWindow->consumeMotionEvent( |
| 9879 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 9880 | |
| 9881 | mWindow->assertNoEvents(); |
| 9882 | } |
| 9883 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9884 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 9885 | protected: |
| 9886 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9887 | |
| 9888 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 9889 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 9890 | |
| 9891 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 9892 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 9893 | |
| 9894 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 9895 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 9896 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 9897 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 9898 | MAXIMUM_OBSCURING_OPACITY); |
| 9899 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9900 | static constexpr gui::Uid TOUCHED_APP_UID{10001}; |
| 9901 | static constexpr gui::Uid APP_B_UID{10002}; |
| 9902 | static constexpr gui::Uid APP_C_UID{10003}; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9903 | |
| 9904 | sp<FakeWindowHandle> mTouchWindow; |
| 9905 | |
| 9906 | virtual void SetUp() override { |
| 9907 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9908 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9909 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 9910 | } |
| 9911 | |
| 9912 | virtual void TearDown() override { |
| 9913 | InputDispatcherTest::TearDown(); |
| 9914 | mTouchWindow.clear(); |
| 9915 | } |
| 9916 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9917 | sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 9918 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9919 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9920 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9921 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9922 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9923 | return window; |
| 9924 | } |
| 9925 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9926 | sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9927 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 9928 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9929 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9930 | // Generate an arbitrary PID based on the UID |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9931 | 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] | 9932 | return window; |
| 9933 | } |
| 9934 | |
| 9935 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9936 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9937 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9938 | points)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9939 | } |
| 9940 | }; |
| 9941 | |
| 9942 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9943 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9944 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9945 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9946 | |
| 9947 | touch(); |
| 9948 | |
| 9949 | mTouchWindow->assertNoEvents(); |
| 9950 | } |
| 9951 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9952 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9953 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 9954 | const sp<FakeWindowHandle>& w = |
| 9955 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9956 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9957 | |
| 9958 | touch(); |
| 9959 | |
| 9960 | mTouchWindow->assertNoEvents(); |
| 9961 | } |
| 9962 | |
| 9963 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9964 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 9965 | const sp<FakeWindowHandle>& w = |
| 9966 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9967 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9968 | |
| 9969 | touch(); |
| 9970 | |
| 9971 | w->assertNoEvents(); |
| 9972 | } |
| 9973 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9974 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9975 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9976 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9977 | |
| 9978 | touch(); |
| 9979 | |
| 9980 | mTouchWindow->consumeAnyMotionDown(); |
| 9981 | } |
| 9982 | |
| 9983 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9984 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9985 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9986 | w->setFrame(Rect(0, 0, 50, 50)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9987 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9988 | |
| 9989 | touch({PointF{100, 100}}); |
| 9990 | |
| 9991 | mTouchWindow->consumeAnyMotionDown(); |
| 9992 | } |
| 9993 | |
| 9994 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9995 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9996 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9997 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9998 | |
| 9999 | touch(); |
| 10000 | |
| 10001 | mTouchWindow->consumeAnyMotionDown(); |
| 10002 | } |
| 10003 | |
| 10004 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 10005 | const sp<FakeWindowHandle>& w = |
| 10006 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10007 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10008 | |
| 10009 | touch(); |
| 10010 | |
| 10011 | mTouchWindow->consumeAnyMotionDown(); |
| 10012 | } |
| 10013 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 10014 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 10015 | const sp<FakeWindowHandle>& w = |
| 10016 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10017 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 10018 | |
| 10019 | touch(); |
| 10020 | |
| 10021 | w->assertNoEvents(); |
| 10022 | } |
| 10023 | |
| 10024 | /** |
| 10025 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 10026 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 10027 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 10028 | */ |
| 10029 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10030 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 10031 | const sp<FakeWindowHandle>& w = |
| 10032 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10033 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10034 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 10035 | |
| 10036 | touch(); |
| 10037 | |
| 10038 | w->consumeMotionOutside(); |
| 10039 | } |
| 10040 | |
| 10041 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 10042 | const sp<FakeWindowHandle>& w = |
| 10043 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10044 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10045 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 10046 | |
| 10047 | touch(); |
| 10048 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 10049 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 10050 | } |
| 10051 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10052 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10053 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10054 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10055 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10056 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10057 | |
| 10058 | touch(); |
| 10059 | |
| 10060 | mTouchWindow->consumeAnyMotionDown(); |
| 10061 | } |
| 10062 | |
| 10063 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 10064 | const sp<FakeWindowHandle>& w = |
| 10065 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10066 | MAXIMUM_OBSCURING_OPACITY); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10067 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10068 | |
| 10069 | touch(); |
| 10070 | |
| 10071 | mTouchWindow->consumeAnyMotionDown(); |
| 10072 | } |
| 10073 | |
| 10074 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10075 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10076 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10077 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10078 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10079 | |
| 10080 | touch(); |
| 10081 | |
| 10082 | mTouchWindow->assertNoEvents(); |
| 10083 | } |
| 10084 | |
| 10085 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 10086 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 10087 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10088 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 10089 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10090 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10091 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 10092 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10093 | mDispatcher->onWindowInfosChanged( |
| 10094 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10095 | |
| 10096 | touch(); |
| 10097 | |
| 10098 | mTouchWindow->assertNoEvents(); |
| 10099 | } |
| 10100 | |
| 10101 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 10102 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 10103 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10104 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 10105 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10106 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10107 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 10108 | OPACITY_FAR_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10109 | mDispatcher->onWindowInfosChanged( |
| 10110 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10111 | |
| 10112 | touch(); |
| 10113 | |
| 10114 | mTouchWindow->consumeAnyMotionDown(); |
| 10115 | } |
| 10116 | |
| 10117 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10118 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 10119 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10120 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10121 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10122 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10123 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 10124 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10125 | mDispatcher->onWindowInfosChanged( |
| 10126 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10127 | |
| 10128 | touch(); |
| 10129 | |
| 10130 | mTouchWindow->consumeAnyMotionDown(); |
| 10131 | } |
| 10132 | |
| 10133 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 10134 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10135 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10136 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10137 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10138 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 10139 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10140 | mDispatcher->onWindowInfosChanged( |
| 10141 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10142 | |
| 10143 | touch(); |
| 10144 | |
| 10145 | mTouchWindow->assertNoEvents(); |
| 10146 | } |
| 10147 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10148 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10149 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 10150 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10151 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 10152 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10153 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10154 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10155 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10156 | mDispatcher->onWindowInfosChanged( |
| 10157 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10158 | |
| 10159 | touch(); |
| 10160 | |
| 10161 | mTouchWindow->assertNoEvents(); |
| 10162 | } |
| 10163 | |
| 10164 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10165 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 10166 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10167 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 10168 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10169 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10170 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10171 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10172 | mDispatcher->onWindowInfosChanged( |
| 10173 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10174 | |
| 10175 | touch(); |
| 10176 | |
| 10177 | mTouchWindow->consumeAnyMotionDown(); |
| 10178 | } |
| 10179 | |
| 10180 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 10181 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10182 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 10183 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10184 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10185 | |
| 10186 | touch(); |
| 10187 | |
| 10188 | mTouchWindow->consumeAnyMotionDown(); |
| 10189 | } |
| 10190 | |
| 10191 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 10192 | const sp<FakeWindowHandle>& w = |
| 10193 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10194 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10195 | |
| 10196 | touch(); |
| 10197 | |
| 10198 | mTouchWindow->consumeAnyMotionDown(); |
| 10199 | } |
| 10200 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 10201 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10202 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 10203 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 10204 | const sp<FakeWindowHandle>& w = |
| 10205 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10206 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 10207 | |
| 10208 | touch(); |
| 10209 | |
| 10210 | mTouchWindow->assertNoEvents(); |
| 10211 | } |
| 10212 | |
| 10213 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 10214 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 10215 | const sp<FakeWindowHandle>& w = |
| 10216 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10217 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 10218 | |
| 10219 | touch(); |
| 10220 | |
| 10221 | mTouchWindow->consumeAnyMotionDown(); |
| 10222 | } |
| 10223 | |
| 10224 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10225 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 10226 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 10227 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10228 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10229 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10230 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10231 | |
| 10232 | touch(); |
| 10233 | |
| 10234 | mTouchWindow->consumeAnyMotionDown(); |
| 10235 | } |
| 10236 | |
| 10237 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10238 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 10239 | const sp<FakeWindowHandle>& w1 = |
| 10240 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 10241 | OPACITY_BELOW_THRESHOLD); |
| 10242 | const sp<FakeWindowHandle>& w2 = |
| 10243 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10244 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10245 | mDispatcher->onWindowInfosChanged( |
| 10246 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10247 | |
| 10248 | touch(); |
| 10249 | |
| 10250 | mTouchWindow->assertNoEvents(); |
| 10251 | } |
| 10252 | |
| 10253 | /** |
| 10254 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 10255 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 10256 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 10257 | */ |
| 10258 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10259 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 10260 | const sp<FakeWindowHandle>& wB = |
| 10261 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 10262 | OPACITY_BELOW_THRESHOLD); |
| 10263 | const sp<FakeWindowHandle>& wC = |
| 10264 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 10265 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10266 | mDispatcher->onWindowInfosChanged( |
| 10267 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10268 | |
| 10269 | touch(); |
| 10270 | |
| 10271 | mTouchWindow->assertNoEvents(); |
| 10272 | } |
| 10273 | |
| 10274 | /** |
| 10275 | * This test is testing that a window from a different UID but with same application token doesn't |
| 10276 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 10277 | */ |
| 10278 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10279 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 10280 | const sp<FakeWindowHandle>& w = |
| 10281 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 10282 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10283 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 10284 | |
| 10285 | touch(); |
| 10286 | |
| 10287 | mTouchWindow->consumeAnyMotionDown(); |
| 10288 | } |
| 10289 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10290 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 10291 | protected: |
| 10292 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 10293 | sp<FakeWindowHandle> mWindow; |
| 10294 | sp<FakeWindowHandle> mSecondWindow; |
| 10295 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10296 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10297 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 10298 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10299 | |
| 10300 | void SetUp() override { |
| 10301 | InputDispatcherTest::SetUp(); |
| 10302 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10303 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10304 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10305 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10306 | mSecondWindow = |
| 10307 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10308 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10309 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10310 | mSpyWindow = |
| 10311 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10312 | mSpyWindow->setSpy(true); |
| 10313 | mSpyWindow->setTrustedOverlay(true); |
| 10314 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 10315 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10316 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10317 | mDispatcher->onWindowInfosChanged( |
| 10318 | {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 10319 | {}, |
| 10320 | 0, |
| 10321 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10322 | } |
| 10323 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10324 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 10325 | switch (fromSource) { |
| 10326 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 10327 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10328 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10329 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10330 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10331 | break; |
| 10332 | case AINPUT_SOURCE_STYLUS: |
| 10333 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10334 | injectMotionEvent(*mDispatcher, |
| 10335 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10336 | AINPUT_SOURCE_STYLUS) |
| 10337 | .buttonState( |
| 10338 | AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 10339 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
| 10340 | .x(50) |
| 10341 | .y(50)) |
| 10342 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10343 | break; |
| 10344 | case AINPUT_SOURCE_MOUSE: |
| 10345 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10346 | injectMotionEvent(*mDispatcher, |
| 10347 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10348 | AINPUT_SOURCE_MOUSE) |
| 10349 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 10350 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 10351 | ToolType::MOUSE) |
| 10352 | .x(50) |
| 10353 | .y(50)) |
| 10354 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10355 | break; |
| 10356 | default: |
| 10357 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 10358 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10359 | |
| 10360 | // Window should receive motion event. |
| 10361 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10362 | // Spy window should also receive motion event |
| 10363 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10364 | } |
| 10365 | |
| 10366 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 10367 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 10368 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10369 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10370 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10371 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10372 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10373 | |
| 10374 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10375 | mDragWindow = |
| 10376 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10377 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10378 | mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), |
| 10379 | *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 10380 | {}, |
| 10381 | 0, |
| 10382 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10383 | |
| 10384 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10385 | bool transferred = |
| 10386 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10387 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10388 | if (transferred) { |
| 10389 | mWindow->consumeMotionCancel(); |
| 10390 | mDragWindow->consumeMotionDown(); |
| 10391 | } |
| 10392 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10393 | } |
| 10394 | }; |
| 10395 | |
| 10396 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10397 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10398 | |
| 10399 | // Move on window. |
| 10400 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10401 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10402 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10403 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10404 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10405 | mWindow->consumeDragEvent(false, 50, 50); |
| 10406 | mSecondWindow->assertNoEvents(); |
| 10407 | |
| 10408 | // Move to another window. |
| 10409 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10410 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10411 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10412 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10413 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10414 | mWindow->consumeDragEvent(true, 150, 50); |
| 10415 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10416 | |
| 10417 | // Move back to original window. |
| 10418 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10419 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10420 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10421 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10422 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10423 | mWindow->consumeDragEvent(false, 50, 50); |
| 10424 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 10425 | |
| 10426 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10427 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10428 | {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10429 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10430 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 10431 | mWindow->assertNoEvents(); |
| 10432 | mSecondWindow->assertNoEvents(); |
| 10433 | } |
| 10434 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10435 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10436 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10437 | |
| 10438 | // No cancel event after drag start |
| 10439 | mSpyWindow->assertNoEvents(); |
| 10440 | |
| 10441 | const MotionEvent secondFingerDownEvent = |
| 10442 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10443 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10444 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10445 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10446 | .build(); |
| 10447 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10448 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10449 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10450 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10451 | |
| 10452 | // Receives cancel for first pointer after next pointer down |
| 10453 | mSpyWindow->consumeMotionCancel(); |
| 10454 | mSpyWindow->consumeMotionDown(); |
| 10455 | |
| 10456 | mSpyWindow->assertNoEvents(); |
| 10457 | } |
| 10458 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10459 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10460 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10461 | |
| 10462 | // Move on window. |
| 10463 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10464 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10465 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10466 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10467 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10468 | mWindow->consumeDragEvent(false, 50, 50); |
| 10469 | mSecondWindow->assertNoEvents(); |
| 10470 | |
| 10471 | // Move to another window. |
| 10472 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10473 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10474 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10475 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10476 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10477 | mWindow->consumeDragEvent(true, 150, 50); |
| 10478 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10479 | |
| 10480 | // drop to another window. |
| 10481 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10482 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10483 | {150, 50})) |
| 10484 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10485 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10486 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10487 | mWindow->assertNoEvents(); |
| 10488 | mSecondWindow->assertNoEvents(); |
| 10489 | } |
| 10490 | |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10491 | TEST_F(InputDispatcherDragTests, DragAndDropNotCancelledIfSomeOtherPointerIsPilfered) { |
| 10492 | startDrag(); |
| 10493 | |
| 10494 | // No cancel event after drag start |
| 10495 | mSpyWindow->assertNoEvents(); |
| 10496 | |
| 10497 | const MotionEvent secondFingerDownEvent = |
| 10498 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10499 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 10500 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10501 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 10502 | .build(); |
| 10503 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10504 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 10505 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10506 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10507 | |
| 10508 | // Receives cancel for first pointer after next pointer down |
| 10509 | mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 10510 | mSpyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithPointerIds({1}))); |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10511 | mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 10512 | |
| 10513 | mSpyWindow->assertNoEvents(); |
| 10514 | |
| 10515 | // Spy window calls pilfer pointers |
| 10516 | EXPECT_EQ(OK, mDispatcher->pilferPointers(mSpyWindow->getToken())); |
| 10517 | mDragWindow->assertNoEvents(); |
| 10518 | |
| 10519 | const MotionEvent firstFingerMoveEvent = |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 10520 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10521 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 10522 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(60).y(60)) |
| 10523 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 10524 | .build(); |
| 10525 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 10526 | injectMotionEvent(*mDispatcher, firstFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10527 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10528 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10529 | |
| 10530 | // Drag window should still receive the new event |
| 10531 | mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 10532 | mDragWindow->assertNoEvents(); |
| 10533 | } |
| 10534 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10535 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10536 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10537 | |
| 10538 | // Move on window and keep button pressed. |
| 10539 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10540 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10541 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 10542 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10543 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10544 | .build())) |
| 10545 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10546 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10547 | mWindow->consumeDragEvent(false, 50, 50); |
| 10548 | mSecondWindow->assertNoEvents(); |
| 10549 | |
| 10550 | // Move to another window and release button, expect to drop item. |
| 10551 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10552 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10553 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 10554 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10555 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10556 | .build())) |
| 10557 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10558 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10559 | mWindow->assertNoEvents(); |
| 10560 | mSecondWindow->assertNoEvents(); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10561 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10562 | |
| 10563 | // nothing to the window. |
| 10564 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10565 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10566 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 10567 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10568 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10569 | .build())) |
| 10570 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10571 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 10572 | mWindow->assertNoEvents(); |
| 10573 | mSecondWindow->assertNoEvents(); |
| 10574 | } |
| 10575 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10576 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10577 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10578 | |
| 10579 | // Set second window invisible. |
| 10580 | mSecondWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10581 | mDispatcher->onWindowInfosChanged( |
| 10582 | {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10583 | |
| 10584 | // Move on window. |
| 10585 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10586 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10587 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10588 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10589 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10590 | mWindow->consumeDragEvent(false, 50, 50); |
| 10591 | mSecondWindow->assertNoEvents(); |
| 10592 | |
| 10593 | // Move to another window. |
| 10594 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10595 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10596 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10597 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10598 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10599 | mWindow->consumeDragEvent(true, 150, 50); |
| 10600 | mSecondWindow->assertNoEvents(); |
| 10601 | |
| 10602 | // drop to another window. |
| 10603 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10604 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10605 | {150, 50})) |
| 10606 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10607 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10608 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10609 | mWindow->assertNoEvents(); |
| 10610 | mSecondWindow->assertNoEvents(); |
| 10611 | } |
| 10612 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10613 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10614 | // Ensure window could track pointerIds if it didn't support split touch. |
| 10615 | mWindow->setPreventSplitting(true); |
| 10616 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10617 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10618 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10619 | {50, 50})) |
| 10620 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10621 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10622 | |
| 10623 | const MotionEvent secondFingerDownEvent = |
| 10624 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10625 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10626 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10627 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10628 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10629 | .build(); |
| 10630 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10631 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10632 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10633 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10634 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10635 | |
| 10636 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10637 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10638 | } |
| 10639 | |
| 10640 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 10641 | // First down on second window. |
| 10642 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10643 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10644 | {150, 50})) |
| 10645 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10646 | |
| 10647 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10648 | |
| 10649 | // Second down on first window. |
| 10650 | const MotionEvent secondFingerDownEvent = |
| 10651 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10652 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10653 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10654 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10655 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10656 | .build(); |
| 10657 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10658 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10659 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10660 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10661 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10662 | |
| 10663 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10664 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10665 | |
| 10666 | // Move on window. |
| 10667 | const MotionEvent secondFingerMoveEvent = |
| 10668 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 10669 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10670 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10671 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10672 | .build(); |
| 10673 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10674 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10675 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 10676 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10677 | mWindow->consumeDragEvent(false, 50, 50); |
| 10678 | mSecondWindow->consumeMotionMove(); |
| 10679 | |
| 10680 | // Release the drag pointer should perform drop. |
| 10681 | const MotionEvent secondFingerUpEvent = |
| 10682 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 10683 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10684 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10685 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10686 | .build(); |
| 10687 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10688 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10689 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 10690 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10691 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken()); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10692 | mWindow->assertNoEvents(); |
| 10693 | mSecondWindow->consumeMotionMove(); |
| 10694 | } |
| 10695 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10696 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10697 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10698 | |
| 10699 | // Update window of second display. |
| 10700 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10701 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10702 | mDispatcher->onWindowInfosChanged( |
| 10703 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 10704 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 10705 | {}, |
| 10706 | 0, |
| 10707 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10708 | |
| 10709 | // Let second display has a touch state. |
| 10710 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10711 | injectMotionEvent(*mDispatcher, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10712 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10713 | AINPUT_SOURCE_TOUCHSCREEN) |
| 10714 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10715 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10716 | .build())); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 10717 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10718 | // Update window again. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10719 | mDispatcher->onWindowInfosChanged( |
| 10720 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 10721 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 10722 | {}, |
| 10723 | 0, |
| 10724 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10725 | |
| 10726 | // Move on window. |
| 10727 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10728 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10729 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10730 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10731 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10732 | mWindow->consumeDragEvent(false, 50, 50); |
| 10733 | mSecondWindow->assertNoEvents(); |
| 10734 | |
| 10735 | // Move to another window. |
| 10736 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10737 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10738 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10739 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10740 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10741 | mWindow->consumeDragEvent(true, 150, 50); |
| 10742 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10743 | |
| 10744 | // drop to another window. |
| 10745 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10746 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10747 | {150, 50})) |
| 10748 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10749 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10750 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10751 | mWindow->assertNoEvents(); |
| 10752 | mSecondWindow->assertNoEvents(); |
| 10753 | } |
| 10754 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10755 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 10756 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 10757 | // Move on window. |
| 10758 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10759 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10760 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 10761 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10762 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10763 | .x(50) |
| 10764 | .y(50)) |
| 10765 | .build())) |
| 10766 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10767 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10768 | mWindow->consumeDragEvent(false, 50, 50); |
| 10769 | mSecondWindow->assertNoEvents(); |
| 10770 | |
| 10771 | // Move to another window. |
| 10772 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10773 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10774 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 10775 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10776 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10777 | .x(150) |
| 10778 | .y(50)) |
| 10779 | .build())) |
| 10780 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10781 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10782 | mWindow->consumeDragEvent(true, 150, 50); |
| 10783 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10784 | |
| 10785 | // drop to another window. |
| 10786 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10787 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10788 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 10789 | .buttonState(0) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10790 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10791 | .x(150) |
| 10792 | .y(50)) |
| 10793 | .build())) |
| 10794 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10795 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10796 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10797 | mWindow->assertNoEvents(); |
| 10798 | mSecondWindow->assertNoEvents(); |
| 10799 | } |
| 10800 | |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 10801 | /** |
| 10802 | * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag |
| 10803 | * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash. |
| 10804 | */ |
| 10805 | TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) { |
| 10806 | // Down on second window |
| 10807 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10808 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10809 | {150, 50})) |
| 10810 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10811 | |
| 10812 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown()); |
| 10813 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown()); |
| 10814 | |
| 10815 | // Down on first window |
| 10816 | const MotionEvent secondFingerDownEvent = |
| 10817 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10818 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10819 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10820 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 10821 | .build(); |
| 10822 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10823 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 10824 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10825 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10826 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 10827 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove()); |
| 10828 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1)); |
| 10829 | |
| 10830 | // Start drag on first window |
| 10831 | ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN)); |
| 10832 | |
| 10833 | // Trigger cancel |
| 10834 | mDispatcher->cancelCurrentTouch(); |
| 10835 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel()); |
| 10836 | ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel()); |
| 10837 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel()); |
| 10838 | |
| 10839 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10840 | // The D&D finished with nullptr |
| 10841 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
| 10842 | |
| 10843 | // Remove drag window |
| 10844 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
| 10845 | |
| 10846 | // Inject a simple gesture, ensure dispatcher not crashed |
| 10847 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10848 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10849 | PointF{50, 50})) |
| 10850 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10851 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 10852 | |
| 10853 | const MotionEvent moveEvent = |
| 10854 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 10855 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10856 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10857 | .build(); |
| 10858 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10859 | injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT, |
| 10860 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10861 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10862 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove()); |
| 10863 | |
| 10864 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10865 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10866 | {50, 50})) |
| 10867 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10868 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp()); |
| 10869 | } |
| 10870 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 10871 | TEST_F(InputDispatcherDragTests, NoDragAndDropWithHoveringPointer) { |
| 10872 | // Start hovering over the window. |
| 10873 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10874 | injectMotionEvent(*mDispatcher, ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE, |
| 10875 | ADISPLAY_ID_DEFAULT, {50, 50})); |
| 10876 | |
| 10877 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 10878 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 10879 | |
| 10880 | ASSERT_FALSE(startDrag(/*sendDown=*/false)) |
| 10881 | << "Drag and drop should not work with a hovering pointer"; |
| 10882 | } |
| 10883 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10884 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 10885 | |
| 10886 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 10887 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10888 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 10889 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10890 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10891 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 10892 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10893 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10894 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10895 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10896 | |
| 10897 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10898 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10899 | window->assertNoEvents(); |
| 10900 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10901 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10902 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 10903 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 10904 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 10905 | mDispatcher->waitForIdle(); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10906 | window->assertNoEvents(); |
| 10907 | |
| 10908 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10909 | window->setDropInput(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10910 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10911 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10912 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10913 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 10914 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10915 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10916 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10917 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10918 | window->assertNoEvents(); |
| 10919 | } |
| 10920 | |
| 10921 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 10922 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 10923 | std::make_shared<FakeApplicationHandle>(); |
| 10924 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10925 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 10926 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10927 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10928 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10929 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10930 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10931 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 10932 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10933 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10934 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10935 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 10936 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10937 | mDispatcher->onWindowInfosChanged( |
| 10938 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10939 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10940 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10941 | |
| 10942 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10943 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10944 | window->assertNoEvents(); |
| 10945 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10946 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10947 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 10948 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 10949 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10950 | window->assertNoEvents(); |
| 10951 | |
| 10952 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10953 | window->setDropInputIfObscured(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10954 | mDispatcher->onWindowInfosChanged( |
| 10955 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10956 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10957 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10958 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 10959 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10960 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10961 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10962 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 10963 | window->assertNoEvents(); |
| 10964 | } |
| 10965 | |
| 10966 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 10967 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 10968 | std::make_shared<FakeApplicationHandle>(); |
| 10969 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10970 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 10971 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10972 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10973 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10974 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10975 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10976 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 10977 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10978 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10979 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10980 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 10981 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10982 | mDispatcher->onWindowInfosChanged( |
| 10983 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10984 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10985 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10986 | |
| 10987 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10988 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10989 | window->assertNoEvents(); |
| 10990 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10991 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10992 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 10993 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 10994 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10995 | window->assertNoEvents(); |
| 10996 | |
| 10997 | // 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] | 10998 | mDispatcher->onWindowInfosChanged( |
| 10999 | {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11000 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11001 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11002 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 11003 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11004 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 11005 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 11006 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11007 | window->assertNoEvents(); |
| 11008 | } |
| 11009 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11010 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 11011 | protected: |
| 11012 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11013 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11014 | sp<FakeWindowHandle> mWindow; |
| 11015 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11016 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11017 | |
| 11018 | void SetUp() override { |
| 11019 | InputDispatcherTest::SetUp(); |
| 11020 | |
| 11021 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11022 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11023 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11024 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11025 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11026 | mSecondWindow = |
| 11027 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11028 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11029 | mThirdWindow = |
| 11030 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 11031 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 11032 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11033 | |
| 11034 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11035 | mDispatcher->onWindowInfosChanged( |
| 11036 | {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()}, |
| 11037 | {}, |
| 11038 | 0, |
| 11039 | 0}); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11040 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11041 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11042 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11043 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11044 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11045 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11046 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 11047 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11048 | mThirdWindow->assertNoEvents(); |
| 11049 | } |
| 11050 | |
| 11051 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 11052 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11053 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11054 | SECOND_DISPLAY_ID)) { |
| 11055 | mWindow->assertNoEvents(); |
| 11056 | mSecondWindow->assertNoEvents(); |
| 11057 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11058 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11059 | } |
| 11060 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11061 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11062 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 11063 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 11064 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11065 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 11066 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11067 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11068 | } |
| 11069 | }; |
| 11070 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11071 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 11072 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11073 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 11074 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11075 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11076 | } |
| 11077 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11078 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 11079 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11080 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11081 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11082 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11083 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11084 | ownerUid, /*hasPermission=*/false, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 11085 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11086 | mWindow->assertNoEvents(); |
| 11087 | mSecondWindow->assertNoEvents(); |
| 11088 | } |
| 11089 | |
| 11090 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 11091 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11092 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11093 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11094 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11095 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11096 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11097 | } |
| 11098 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11099 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 11100 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11101 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 11102 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11103 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11104 | mWindow->assertNoEvents(); |
| 11105 | mSecondWindow->assertNoEvents(); |
| 11106 | } |
| 11107 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11108 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 11109 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 11110 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 11111 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11112 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11113 | mWindow->assertNoEvents(); |
| 11114 | mSecondWindow->assertNoEvents(); |
| 11115 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 11116 | } |
| 11117 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11118 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 11119 | // Interact with the window first. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11120 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11121 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11122 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 11123 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 11124 | |
| 11125 | // Then remove focus. |
| 11126 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11127 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11128 | |
| 11129 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 11130 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 11131 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 11132 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11133 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11134 | } |
| 11135 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11136 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 11137 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11138 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11139 | std::shared_ptr<FakeApplicationHandle> application = |
| 11140 | std::make_shared<FakeApplicationHandle>(); |
| 11141 | std::string name = "Fake Spy "; |
| 11142 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11143 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 11144 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11145 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 11146 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11147 | return spy; |
| 11148 | } |
| 11149 | |
| 11150 | sp<FakeWindowHandle> createForeground() { |
| 11151 | std::shared_ptr<FakeApplicationHandle> application = |
| 11152 | std::make_shared<FakeApplicationHandle>(); |
| 11153 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11154 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 11155 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11156 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11157 | return window; |
| 11158 | } |
| 11159 | |
| 11160 | private: |
| 11161 | int mSpyCount{0}; |
| 11162 | }; |
| 11163 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11164 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11165 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 11166 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 11167 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11168 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
Siarhei Vishniakou | 21f77bd | 2023-07-18 17:51:35 -0700 | [diff] [blame] | 11169 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11170 | ScopedSilentDeath _silentDeath; |
| 11171 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11172 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 11173 | spy->setTrustedOverlay(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11174 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 11175 | ".* not a trusted overlay"); |
| 11176 | } |
| 11177 | |
| 11178 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11179 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 11180 | */ |
| 11181 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11182 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11183 | mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11184 | |
| 11185 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11186 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11187 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11188 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11189 | } |
| 11190 | |
| 11191 | /** |
| 11192 | * Verify the order in which different input windows receive events. The touched foreground window |
| 11193 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 11194 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 11195 | * receive events before ones belows it. |
| 11196 | * |
| 11197 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 11198 | * spy1, spy2, window, spy3. |
| 11199 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 11200 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 11201 | * window. |
| 11202 | */ |
| 11203 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 11204 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11205 | auto spy1 = createSpy(); |
| 11206 | auto spy2 = createSpy(); |
| 11207 | auto spy3 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11208 | mDispatcher->onWindowInfosChanged( |
| 11209 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11210 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 11211 | const size_t numChannels = channels.size(); |
| 11212 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 11213 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11214 | if (!epollFd.ok()) { |
| 11215 | FAIL() << "Failed to create epoll fd"; |
| 11216 | } |
| 11217 | |
| 11218 | for (size_t i = 0; i < numChannels; i++) { |
| 11219 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 11220 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 11221 | FAIL() << "Failed to add fd to epoll"; |
| 11222 | } |
| 11223 | } |
| 11224 | |
| 11225 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11226 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11227 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11228 | |
| 11229 | std::vector<size_t> eventOrder; |
| 11230 | std::vector<struct epoll_event> events(numChannels); |
| 11231 | for (;;) { |
| 11232 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 11233 | (100ms).count()); |
| 11234 | if (nFds < 0) { |
| 11235 | FAIL() << "Failed to call epoll_wait"; |
| 11236 | } |
| 11237 | if (nFds == 0) { |
| 11238 | break; // epoll_wait timed out |
| 11239 | } |
| 11240 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 11241 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 11242 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11243 | channels[i]->consumeMotionDown(); |
| 11244 | } |
| 11245 | } |
| 11246 | |
| 11247 | // Verify the order in which the events were received. |
| 11248 | EXPECT_EQ(3u, eventOrder.size()); |
| 11249 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 11250 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 11251 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 11252 | } |
| 11253 | |
| 11254 | /** |
| 11255 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 11256 | */ |
| 11257 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 11258 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11259 | auto spy = createSpy(); |
| 11260 | spy->setTouchable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11261 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11262 | |
| 11263 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11264 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11265 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11266 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11267 | spy->assertNoEvents(); |
| 11268 | } |
| 11269 | |
| 11270 | /** |
| 11271 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 11272 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 11273 | * to the window. |
| 11274 | */ |
| 11275 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 11276 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11277 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11278 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11279 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11280 | |
| 11281 | // Inject an event outside the spy window's touchable region. |
| 11282 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11283 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11284 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11285 | window->consumeMotionDown(); |
| 11286 | spy->assertNoEvents(); |
| 11287 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11288 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11289 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11290 | window->consumeMotionUp(); |
| 11291 | spy->assertNoEvents(); |
| 11292 | |
| 11293 | // Inject an event inside the spy window's touchable region. |
| 11294 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11295 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11296 | {5, 10})) |
| 11297 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11298 | window->consumeMotionDown(); |
| 11299 | spy->consumeMotionDown(); |
| 11300 | } |
| 11301 | |
| 11302 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11303 | * 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] | 11304 | * 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] | 11305 | */ |
| 11306 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 11307 | auto window = createForeground(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11308 | window->setOwnerInfo(gui::Pid{12}, gui::Uid{34}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11309 | auto spy = createSpy(); |
| 11310 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11311 | spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11312 | spy->setFrame(Rect{0, 0, 20, 20}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11313 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11314 | |
| 11315 | // Inject an event outside the spy window's frame and touchable region. |
| 11316 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11317 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 11318 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11319 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11320 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 11321 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11322 | } |
| 11323 | |
| 11324 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11325 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 11326 | * pointers that are down within its bounds. |
| 11327 | */ |
| 11328 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 11329 | auto windowLeft = createForeground(); |
| 11330 | windowLeft->setFrame({0, 0, 100, 200}); |
| 11331 | auto windowRight = createForeground(); |
| 11332 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11333 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11334 | spy->setFrame({0, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11335 | mDispatcher->onWindowInfosChanged( |
| 11336 | {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11337 | |
| 11338 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11339 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11340 | {50, 50})) |
| 11341 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11342 | windowLeft->consumeMotionDown(); |
| 11343 | spy->consumeMotionDown(); |
| 11344 | |
| 11345 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 11346 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11347 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11348 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11349 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11350 | .build(); |
| 11351 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11352 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11353 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11354 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11355 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11356 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11357 | } |
| 11358 | |
| 11359 | /** |
| 11360 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 11361 | * the spy should receive the second pointer with ACTION_DOWN. |
| 11362 | */ |
| 11363 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 11364 | auto window = createForeground(); |
| 11365 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11366 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11367 | spyRight->setFrame({100, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11368 | mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11369 | |
| 11370 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11371 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11372 | {50, 50})) |
| 11373 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11374 | window->consumeMotionDown(); |
| 11375 | spyRight->assertNoEvents(); |
| 11376 | |
| 11377 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 11378 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11379 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11380 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11381 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11382 | .build(); |
| 11383 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11384 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11385 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11386 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11387 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11388 | spyRight->consumeMotionDown(); |
| 11389 | } |
| 11390 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11391 | /** |
| 11392 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 11393 | * windows should be allowed to control split touch. |
| 11394 | */ |
| 11395 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 11396 | // 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] | 11397 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11398 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 11399 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11400 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11401 | auto window = createForeground(); |
| 11402 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11403 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11404 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11405 | |
| 11406 | // First finger down, no window touched. |
| 11407 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11408 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11409 | {100, 200})) |
| 11410 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11411 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11412 | window->assertNoEvents(); |
| 11413 | |
| 11414 | // Second finger down on window, the window should receive touch down. |
| 11415 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 11416 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11417 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11418 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11419 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 11420 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11421 | .build(); |
| 11422 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11423 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11424 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11425 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11426 | |
| 11427 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11428 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11429 | } |
| 11430 | |
| 11431 | /** |
| 11432 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 11433 | * do not receive key events. |
| 11434 | */ |
| 11435 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11436 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11437 | spy->setFocusable(false); |
| 11438 | |
| 11439 | auto window = createForeground(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11440 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11441 | setFocusedWindow(window); |
| 11442 | window->consumeFocusEvent(true); |
| 11443 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11444 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11445 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 11446 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 11447 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11448 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11449 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 11450 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 11451 | |
| 11452 | spy->assertNoEvents(); |
| 11453 | } |
| 11454 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11455 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 11456 | |
| 11457 | /** |
| 11458 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 11459 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 11460 | */ |
| 11461 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 11462 | auto window = createForeground(); |
| 11463 | auto spy1 = createSpy(); |
| 11464 | auto spy2 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11465 | mDispatcher->onWindowInfosChanged( |
| 11466 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11467 | |
| 11468 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11469 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11470 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11471 | window->consumeMotionDown(); |
| 11472 | spy1->consumeMotionDown(); |
| 11473 | spy2->consumeMotionDown(); |
| 11474 | |
| 11475 | // Pilfer pointers from the second spy window. |
| 11476 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 11477 | spy2->assertNoEvents(); |
| 11478 | spy1->consumeMotionCancel(); |
| 11479 | window->consumeMotionCancel(); |
| 11480 | |
| 11481 | // The rest of the gesture should only be sent to the second spy window. |
| 11482 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11483 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11484 | ADISPLAY_ID_DEFAULT)) |
| 11485 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11486 | spy2->consumeMotionMove(); |
| 11487 | spy1->assertNoEvents(); |
| 11488 | window->assertNoEvents(); |
| 11489 | } |
| 11490 | |
| 11491 | /** |
| 11492 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 11493 | * in the middle of the gesture. |
| 11494 | */ |
| 11495 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 11496 | auto window = createForeground(); |
| 11497 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11498 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11499 | |
| 11500 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11501 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11502 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11503 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11504 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11505 | |
| 11506 | window->releaseChannel(); |
| 11507 | |
| 11508 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11509 | |
| 11510 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11511 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11512 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11513 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 11514 | } |
| 11515 | |
| 11516 | /** |
| 11517 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 11518 | * the spy, but not to any other windows. |
| 11519 | */ |
| 11520 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 11521 | auto spy = createSpy(); |
| 11522 | auto window = createForeground(); |
| 11523 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11524 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11525 | |
| 11526 | // First finger down on the window and the spy. |
| 11527 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11528 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11529 | {100, 200})) |
| 11530 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11531 | spy->consumeMotionDown(); |
| 11532 | window->consumeMotionDown(); |
| 11533 | |
| 11534 | // Spy window pilfers the pointers. |
| 11535 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11536 | window->consumeMotionCancel(); |
| 11537 | |
| 11538 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 11539 | const MotionEvent secondFingerDownEvent = |
| 11540 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11541 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11542 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11543 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 11544 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11545 | .build(); |
| 11546 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11547 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11548 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11549 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11550 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11551 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11552 | |
| 11553 | // Third finger goes down outside all windows, so injection should fail. |
| 11554 | const MotionEvent thirdFingerDownEvent = |
| 11555 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11556 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11557 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11558 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 11559 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 11560 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11561 | .build(); |
| 11562 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11563 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11564 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 11565 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11566 | |
| 11567 | spy->assertNoEvents(); |
| 11568 | window->assertNoEvents(); |
| 11569 | } |
| 11570 | |
| 11571 | /** |
| 11572 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 11573 | */ |
| 11574 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 11575 | auto spy = createSpy(); |
| 11576 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 11577 | auto window = createForeground(); |
| 11578 | window->setFrame(Rect(0, 0, 200, 200)); |
| 11579 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11580 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11581 | |
| 11582 | // First finger down on the window only |
| 11583 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11584 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11585 | {150, 150})) |
| 11586 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11587 | window->consumeMotionDown(); |
| 11588 | |
| 11589 | // Second finger down on the spy and window |
| 11590 | const MotionEvent secondFingerDownEvent = |
| 11591 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11592 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11593 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11594 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 11595 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11596 | .build(); |
| 11597 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11598 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11599 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11600 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11601 | spy->consumeMotionDown(); |
| 11602 | window->consumeMotionPointerDown(1); |
| 11603 | |
| 11604 | // Third finger down on the spy and window |
| 11605 | const MotionEvent thirdFingerDownEvent = |
| 11606 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11607 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11608 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11609 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 11610 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 11611 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11612 | .build(); |
| 11613 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11614 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11615 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11616 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11617 | spy->consumeMotionPointerDown(1); |
| 11618 | window->consumeMotionPointerDown(2); |
| 11619 | |
| 11620 | // Spy window pilfers the pointers. |
| 11621 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 11622 | window->consumeMotionPointerUp(/*idx=*/2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 11623 | window->consumeMotionPointerUp(/*idx=*/1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11624 | |
| 11625 | spy->assertNoEvents(); |
| 11626 | window->assertNoEvents(); |
| 11627 | } |
| 11628 | |
| 11629 | /** |
| 11630 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 11631 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 11632 | * window, then that window should receive ACTION_CANCEL. |
| 11633 | */ |
| 11634 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 11635 | auto spy = createSpy(); |
| 11636 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 11637 | auto window = createForeground(); |
| 11638 | window->setFrame(Rect(0, 0, 200, 200)); |
| 11639 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11640 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11641 | |
| 11642 | // First finger down on both spy and window |
| 11643 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11644 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11645 | {10, 10})) |
| 11646 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11647 | window->consumeMotionDown(); |
| 11648 | spy->consumeMotionDown(); |
| 11649 | |
| 11650 | // Second finger down on the spy and window |
| 11651 | const MotionEvent secondFingerDownEvent = |
| 11652 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11653 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11654 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11655 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 11656 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11657 | .build(); |
| 11658 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11659 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11660 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11661 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11662 | spy->consumeMotionPointerDown(1); |
| 11663 | window->consumeMotionPointerDown(1); |
| 11664 | |
| 11665 | // Spy window pilfers the pointers. |
| 11666 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11667 | window->consumeMotionCancel(); |
| 11668 | |
| 11669 | spy->assertNoEvents(); |
| 11670 | window->assertNoEvents(); |
| 11671 | } |
| 11672 | |
| 11673 | /** |
| 11674 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 11675 | * be sent to other windows |
| 11676 | */ |
| 11677 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 11678 | auto spy = createSpy(); |
| 11679 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 11680 | auto window = createForeground(); |
| 11681 | window->setFrame(Rect(0, 0, 200, 200)); |
| 11682 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11683 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11684 | |
| 11685 | // First finger down on both window and spy |
| 11686 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11687 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11688 | {10, 10})) |
| 11689 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11690 | window->consumeMotionDown(); |
| 11691 | spy->consumeMotionDown(); |
| 11692 | |
| 11693 | // Spy window pilfers the pointers. |
| 11694 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11695 | window->consumeMotionCancel(); |
| 11696 | |
| 11697 | // Second finger down on the window only |
| 11698 | const MotionEvent secondFingerDownEvent = |
| 11699 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11700 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11701 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11702 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 11703 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11704 | .build(); |
| 11705 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11706 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11707 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11708 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11709 | window->consumeMotionDown(); |
| 11710 | window->assertNoEvents(); |
| 11711 | |
| 11712 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 11713 | spy->consumeMotionMove(); |
| 11714 | spy->assertNoEvents(); |
| 11715 | } |
| 11716 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11717 | /** |
| 11718 | * A window on the left and a window on the right. Also, a spy window that's above all of the |
| 11719 | * windows, and spanning both left and right windows. |
| 11720 | * Send simultaneous motion streams from two different devices, one to the left window, and another |
| 11721 | * to the right window. |
| 11722 | * Pilfer from spy window. |
| 11723 | * Check that the pilfering only affects the pointers that are actually being received by the spy. |
| 11724 | */ |
| 11725 | TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) { |
| 11726 | sp<FakeWindowHandle> spy = createSpy(); |
| 11727 | spy->setFrame(Rect(0, 0, 200, 200)); |
| 11728 | sp<FakeWindowHandle> leftWindow = createForeground(); |
| 11729 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 11730 | |
| 11731 | sp<FakeWindowHandle> rightWindow = createForeground(); |
| 11732 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 11733 | |
| 11734 | constexpr int32_t stylusDeviceId = 1; |
| 11735 | constexpr int32_t touchDeviceId = 2; |
| 11736 | |
| 11737 | mDispatcher->onWindowInfosChanged( |
| 11738 | {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 11739 | |
| 11740 | // Stylus down on left window and spy |
| 11741 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 11742 | .deviceId(stylusDeviceId) |
| 11743 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 11744 | .build()); |
| 11745 | leftWindow->consumeMotionEvent( |
| 11746 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 11747 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 11748 | |
| 11749 | // Finger down on right window and spy - but spy already has stylus |
| 11750 | mDispatcher->notifyMotion( |
| 11751 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11752 | .deviceId(touchDeviceId) |
| 11753 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 11754 | .build()); |
| 11755 | rightWindow->consumeMotionEvent( |
| 11756 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11757 | spy->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11758 | |
| 11759 | // Act: pilfer from spy. Spy is currently receiving touch events. |
| 11760 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11761 | leftWindow->consumeMotionEvent( |
| 11762 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11763 | rightWindow->consumeMotionEvent( |
| 11764 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 11765 | |
| 11766 | // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus |
| 11767 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 11768 | .deviceId(stylusDeviceId) |
| 11769 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52)) |
| 11770 | .build()); |
| 11771 | mDispatcher->notifyMotion( |
| 11772 | MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 11773 | .deviceId(touchDeviceId) |
| 11774 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52)) |
| 11775 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11776 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11777 | |
| 11778 | spy->assertNoEvents(); |
| 11779 | leftWindow->assertNoEvents(); |
| 11780 | rightWindow->assertNoEvents(); |
| 11781 | } |
| 11782 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 11783 | TEST_F(InputDispatcherPilferPointersTest, NoPilferingWithHoveringPointers) { |
| 11784 | auto window = createForeground(); |
| 11785 | auto spy = createSpy(); |
| 11786 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 11787 | |
| 11788 | mDispatcher->notifyMotion( |
| 11789 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 11790 | .deviceId(1) |
| 11791 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(100).y(200)) |
| 11792 | .build()); |
| 11793 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 11794 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 11795 | |
| 11796 | // Pilfer pointers from the spy window should fail. |
| 11797 | EXPECT_NE(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11798 | spy->assertNoEvents(); |
| 11799 | window->assertNoEvents(); |
| 11800 | } |
| 11801 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11802 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 11803 | public: |
| 11804 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 11805 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 11806 | std::make_shared<FakeApplicationHandle>(); |
| 11807 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11808 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 11809 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11810 | overlay->setFocusable(false); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11811 | overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11812 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11813 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11814 | overlay->setTrustedOverlay(true); |
| 11815 | |
| 11816 | std::shared_ptr<FakeApplicationHandle> application = |
| 11817 | std::make_shared<FakeApplicationHandle>(); |
| 11818 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11819 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 11820 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11821 | window->setFocusable(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11822 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11823 | |
| 11824 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11825 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11826 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11827 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11828 | return {std::move(overlay), std::move(window)}; |
| 11829 | } |
| 11830 | |
| 11831 | void sendFingerEvent(int32_t action) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11832 | mDispatcher->notifyMotion( |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11833 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11834 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}})); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11835 | } |
| 11836 | |
| 11837 | void sendStylusEvent(int32_t action) { |
| 11838 | NotifyMotionArgs motionArgs = |
| 11839 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 11840 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11841 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11842 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11843 | } |
| 11844 | }; |
| 11845 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11846 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 11847 | |
| 11848 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
Siarhei Vishniakou | ad3b682 | 2023-06-22 14:17:35 -0700 | [diff] [blame] | 11849 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11850 | ScopedSilentDeath _silentDeath; |
| 11851 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11852 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 11853 | overlay->setTrustedOverlay(false); |
| 11854 | // 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] | 11855 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 11856 | {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11857 | ".* not a trusted overlay"); |
| 11858 | } |
| 11859 | |
| 11860 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 11861 | auto [overlay, window] = setupStylusOverlayScenario(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11862 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11863 | |
| 11864 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11865 | overlay->consumeMotionDown(); |
| 11866 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 11867 | overlay->consumeMotionUp(); |
| 11868 | |
| 11869 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11870 | window->consumeMotionDown(); |
| 11871 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 11872 | window->consumeMotionUp(); |
| 11873 | |
| 11874 | overlay->assertNoEvents(); |
| 11875 | window->assertNoEvents(); |
| 11876 | } |
| 11877 | |
| 11878 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 11879 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11880 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11881 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11882 | |
| 11883 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11884 | overlay->consumeMotionDown(); |
| 11885 | window->consumeMotionDown(); |
| 11886 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 11887 | overlay->consumeMotionUp(); |
| 11888 | window->consumeMotionUp(); |
| 11889 | |
| 11890 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11891 | window->consumeMotionDown(); |
| 11892 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 11893 | window->consumeMotionUp(); |
| 11894 | |
| 11895 | overlay->assertNoEvents(); |
| 11896 | window->assertNoEvents(); |
| 11897 | } |
| 11898 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 11899 | /** |
| 11900 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 11901 | * The scenario is as follows: |
| 11902 | * - The stylus interceptor overlay is configured as a spy window. |
| 11903 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 11904 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 11905 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 11906 | */ |
| 11907 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 11908 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 11909 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11910 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 11911 | |
| 11912 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11913 | overlay->consumeMotionDown(); |
| 11914 | window->consumeMotionDown(); |
| 11915 | |
| 11916 | // The interceptor pilfers the pointers. |
| 11917 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 11918 | window->consumeMotionCancel(); |
| 11919 | |
| 11920 | // The interceptor configures itself so that it is no longer a spy. |
| 11921 | overlay->setSpy(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11922 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 11923 | |
| 11924 | // It continues to receive the rest of the stylus gesture. |
| 11925 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 11926 | overlay->consumeMotionMove(); |
| 11927 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 11928 | overlay->consumeMotionUp(); |
| 11929 | |
| 11930 | window->assertNoEvents(); |
| 11931 | } |
| 11932 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11933 | struct User { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11934 | gui::Pid mPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11935 | gui::Uid mUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11936 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 11937 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 11938 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11939 | User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid) |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11940 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 11941 | |
| 11942 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11943 | return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11944 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 11945 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 11946 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 11947 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 11948 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 11949 | } |
| 11950 | |
| 11951 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11952 | return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11953 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11954 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11955 | mPolicyFlags); |
| 11956 | } |
| 11957 | |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11958 | sp<FakeWindowHandle> createWindow(const char* name) const { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11959 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 11960 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11961 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 11962 | name, ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11963 | window->setOwnerInfo(mPid, mUid); |
| 11964 | return window; |
| 11965 | } |
| 11966 | }; |
| 11967 | |
| 11968 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 11969 | |
| 11970 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11971 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11972 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11973 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11974 | |
| 11975 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11976 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11977 | window->consumeMotionDown(); |
| 11978 | |
| 11979 | setFocusedWindow(window); |
| 11980 | window->consumeFocusEvent(true); |
| 11981 | |
| 11982 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11983 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 11984 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 11985 | } |
| 11986 | |
| 11987 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11988 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11989 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11990 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11991 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11992 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11993 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 11994 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11995 | |
| 11996 | setFocusedWindow(window); |
| 11997 | window->consumeFocusEvent(true); |
| 11998 | |
| 11999 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 12000 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 12001 | window->assertNoEvents(); |
| 12002 | } |
| 12003 | |
| 12004 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12005 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12006 | auto window = owner.createWindow("Owned window"); |
| 12007 | auto spy = owner.createWindow("Owned spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12008 | spy->setSpy(true); |
| 12009 | spy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12010 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12011 | |
| 12012 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12013 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 12014 | spy->consumeMotionDown(); |
| 12015 | window->consumeMotionDown(); |
| 12016 | } |
| 12017 | |
| 12018 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12019 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12020 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12021 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12022 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12023 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12024 | randosSpy->setSpy(true); |
| 12025 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12026 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12027 | |
| 12028 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 12029 | // not receive the event. |
| 12030 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12031 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 12032 | randosSpy->assertNoEvents(); |
| 12033 | window->consumeMotionDown(); |
| 12034 | } |
| 12035 | |
| 12036 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12037 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12038 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12039 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12040 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12041 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12042 | randosSpy->setSpy(true); |
| 12043 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12044 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12045 | |
| 12046 | // A user that has injection permission can inject into any window. |
| 12047 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12048 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12049 | ADISPLAY_ID_DEFAULT)); |
| 12050 | randosSpy->consumeMotionDown(); |
| 12051 | window->consumeMotionDown(); |
| 12052 | |
| 12053 | setFocusedWindow(randosSpy); |
| 12054 | randosSpy->consumeFocusEvent(true); |
| 12055 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12056 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12057 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 12058 | window->assertNoEvents(); |
| 12059 | } |
| 12060 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 12061 | TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12062 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12063 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12064 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12065 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12066 | auto randosWindow = rando.createWindow("Rando's window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12067 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 12068 | randosWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12069 | mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12070 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 12071 | // 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] | 12072 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12073 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 12074 | window->consumeMotionDown(); |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 12075 | randosWindow->assertNoEvents(); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12076 | } |
| 12077 | |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 12078 | using InputDispatcherPointerInWindowTest = InputDispatcherTest; |
| 12079 | |
| 12080 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWhenHovering) { |
| 12081 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 12082 | |
| 12083 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 12084 | ADISPLAY_ID_DEFAULT); |
| 12085 | left->setFrame(Rect(0, 0, 100, 100)); |
| 12086 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 12087 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 12088 | right->setFrame(Rect(100, 0, 200, 100)); |
| 12089 | sp<FakeWindowHandle> spy = |
| 12090 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT); |
| 12091 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 12092 | spy->setTrustedOverlay(true); |
| 12093 | spy->setSpy(true); |
| 12094 | |
| 12095 | mDispatcher->onWindowInfosChanged( |
| 12096 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 12097 | |
| 12098 | // Hover into the left window. |
| 12099 | mDispatcher->notifyMotion( |
| 12100 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 12101 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(50).y(50)) |
| 12102 | .build()); |
| 12103 | |
| 12104 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12105 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12106 | |
| 12107 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12108 | /*pointerId=*/0)); |
| 12109 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12110 | /*pointerId=*/0)); |
| 12111 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12112 | /*pointerId=*/0)); |
| 12113 | |
| 12114 | // Hover move to the right window. |
| 12115 | mDispatcher->notifyMotion( |
| 12116 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 12117 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 12118 | .build()); |
| 12119 | |
| 12120 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12121 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12122 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 12123 | |
| 12124 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12125 | /*pointerId=*/0)); |
| 12126 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12127 | /*pointerId=*/0)); |
| 12128 | ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12129 | /*pointerId=*/0)); |
| 12130 | |
| 12131 | // Stop hovering. |
| 12132 | mDispatcher->notifyMotion( |
| 12133 | MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 12134 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 12135 | .build()); |
| 12136 | |
| 12137 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12138 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12139 | |
| 12140 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12141 | /*pointerId=*/0)); |
| 12142 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12143 | /*pointerId=*/0)); |
| 12144 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12145 | /*pointerId=*/0)); |
| 12146 | } |
| 12147 | |
| 12148 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWithSplitTouch) { |
| 12149 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 12150 | |
| 12151 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 12152 | ADISPLAY_ID_DEFAULT); |
| 12153 | left->setFrame(Rect(0, 0, 100, 100)); |
| 12154 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 12155 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 12156 | right->setFrame(Rect(100, 0, 200, 100)); |
| 12157 | sp<FakeWindowHandle> spy = |
| 12158 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT); |
| 12159 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 12160 | spy->setTrustedOverlay(true); |
| 12161 | spy->setSpy(true); |
| 12162 | |
| 12163 | mDispatcher->onWindowInfosChanged( |
| 12164 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 12165 | |
| 12166 | // First pointer down on left window. |
| 12167 | mDispatcher->notifyMotion( |
| 12168 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 12169 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12170 | .build()); |
| 12171 | |
| 12172 | left->consumeMotionDown(); |
| 12173 | spy->consumeMotionDown(); |
| 12174 | |
| 12175 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12176 | /*pointerId=*/0)); |
| 12177 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12178 | /*pointerId=*/0)); |
| 12179 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12180 | /*pointerId=*/0)); |
| 12181 | |
| 12182 | // Second pointer down on right window. |
| 12183 | mDispatcher->notifyMotion( |
| 12184 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 12185 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12186 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 12187 | .build()); |
| 12188 | |
| 12189 | left->consumeMotionMove(); |
| 12190 | right->consumeMotionDown(); |
| 12191 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 12192 | |
| 12193 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12194 | /*pointerId=*/0)); |
| 12195 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12196 | /*pointerId=*/0)); |
| 12197 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12198 | /*pointerId=*/0)); |
| 12199 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12200 | /*pointerId=*/1)); |
| 12201 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12202 | /*pointerId=*/1)); |
| 12203 | ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12204 | /*pointerId=*/1)); |
| 12205 | |
| 12206 | // Second pointer up. |
| 12207 | mDispatcher->notifyMotion( |
| 12208 | MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 12209 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12210 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 12211 | .build()); |
| 12212 | |
| 12213 | left->consumeMotionMove(); |
| 12214 | right->consumeMotionUp(); |
| 12215 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 12216 | |
| 12217 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12218 | /*pointerId=*/0)); |
| 12219 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12220 | /*pointerId=*/0)); |
| 12221 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12222 | /*pointerId=*/0)); |
| 12223 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12224 | /*pointerId=*/1)); |
| 12225 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12226 | /*pointerId=*/1)); |
| 12227 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12228 | /*pointerId=*/1)); |
| 12229 | |
| 12230 | // First pointer up. |
| 12231 | mDispatcher->notifyMotion( |
| 12232 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 12233 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12234 | .build()); |
| 12235 | |
| 12236 | left->consumeMotionUp(); |
| 12237 | spy->consumeMotionUp(); |
| 12238 | |
| 12239 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12240 | /*pointerId=*/0)); |
| 12241 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12242 | /*pointerId=*/0)); |
| 12243 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12244 | /*pointerId=*/0)); |
| 12245 | } |
| 12246 | |
| 12247 | TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse) { |
| 12248 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 12249 | |
| 12250 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 12251 | ADISPLAY_ID_DEFAULT); |
| 12252 | left->setFrame(Rect(0, 0, 100, 100)); |
| 12253 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 12254 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 12255 | right->setFrame(Rect(100, 0, 200, 100)); |
| 12256 | |
| 12257 | mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 12258 | |
| 12259 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12260 | /*pointerId=*/0)); |
| 12261 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12262 | /*pointerId=*/0)); |
| 12263 | |
| 12264 | // Hover move into the window. |
| 12265 | mDispatcher->notifyMotion( |
| 12266 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 12267 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50)) |
| 12268 | .rawXCursorPosition(50) |
| 12269 | .rawYCursorPosition(50) |
| 12270 | .deviceId(DEVICE_ID) |
| 12271 | .build()); |
| 12272 | |
| 12273 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12274 | |
| 12275 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12276 | /*pointerId=*/0)); |
| 12277 | |
| 12278 | // Move the mouse with another device. This cancels the hovering pointer from the first device. |
| 12279 | mDispatcher->notifyMotion( |
| 12280 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 12281 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50)) |
| 12282 | .rawXCursorPosition(51) |
| 12283 | .rawYCursorPosition(50) |
| 12284 | .deviceId(SECOND_DEVICE_ID) |
| 12285 | .build()); |
| 12286 | |
| 12287 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12288 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12289 | |
| 12290 | // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets |
| 12291 | // a HOVER_EXIT from the first device. |
| 12292 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12293 | /*pointerId=*/0)); |
| 12294 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, |
| 12295 | SECOND_DEVICE_ID, |
| 12296 | /*pointerId=*/0)); |
| 12297 | |
| 12298 | // Move the mouse outside the window. Document the current behavior, where the window does not |
| 12299 | // receive HOVER_EXIT even though the mouse left the window. |
| 12300 | mDispatcher->notifyMotion( |
| 12301 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 12302 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50)) |
| 12303 | .rawXCursorPosition(150) |
| 12304 | .rawYCursorPosition(50) |
| 12305 | .deviceId(SECOND_DEVICE_ID) |
| 12306 | .build()); |
| 12307 | |
| 12308 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12309 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12310 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12311 | /*pointerId=*/0)); |
| 12312 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, |
| 12313 | SECOND_DEVICE_ID, |
| 12314 | /*pointerId=*/0)); |
| 12315 | } |
| 12316 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 12317 | } // namespace android::inputdispatcher |