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 | e3b28dd | 2023-10-06 04:19:29 +0000 | [diff] [blame] | 20 | #include "TestEventMatchers.h" |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 21 | |
Cody Heiner | 166a5af | 2023-07-07 12:25:00 -0700 | [diff] [blame] | 22 | #include <NotifyArgsBuilders.h> |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 23 | #include <android-base/logging.h> |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 24 | #include <android-base/properties.h> |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 25 | #include <android-base/silent_death_test.h> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 26 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 27 | #include <android-base/thread_annotations.h> |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 28 | #include <binder/Binder.h> |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 29 | #include <com_android_input_flags.h> |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 30 | #include <fcntl.h> |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 31 | #include <flag_macros.h> |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 32 | #include <gmock/gmock.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 33 | #include <gtest/gtest.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 34 | #include <input/Input.h> |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 35 | #include <input/PrintTools.h> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 36 | #include <linux/input.h> |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 37 | #include <sys/epoll.h> |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 38 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 39 | #include <cinttypes> |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 40 | #include <compare> |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 41 | #include <thread> |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 42 | #include <unordered_set> |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 43 | #include <vector> |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 44 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 45 | using android::base::StringPrintf; |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 46 | using android::gui::FocusRequest; |
| 47 | using android::gui::TouchOcclusionMode; |
| 48 | using android::gui::WindowInfo; |
| 49 | using android::gui::WindowInfoHandle; |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 50 | using android::os::InputEventInjectionResult; |
| 51 | using android::os::InputEventInjectionSync; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 52 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 53 | namespace android::inputdispatcher { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 54 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 55 | using namespace ftl::flag_operators; |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 56 | using testing::AllOf; |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 57 | using testing::Not; |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 58 | |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 59 | namespace { |
| 60 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 61 | // An arbitrary time value. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 62 | static constexpr nsecs_t ARBITRARY_TIME = 1234; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 63 | |
| 64 | // An arbitrary device id. |
Prabir Pradhan | 9205e42 | 2023-05-16 20:06:13 +0000 | [diff] [blame] | 65 | static constexpr int32_t DEVICE_ID = DEFAULT_DEVICE_ID; |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 66 | static constexpr int32_t SECOND_DEVICE_ID = 2; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 67 | |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 68 | // An arbitrary display id. |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 69 | static constexpr int32_t DISPLAY_ID = ADISPLAY_ID_DEFAULT; |
| 70 | static constexpr int32_t SECOND_DISPLAY_ID = 1; |
Jeff Brown | f086ddb | 2014-02-11 14:28:48 -0800 | [diff] [blame] | 71 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 72 | // Ensure common actions are interchangeable between keys and motions for convenience. |
| 73 | static_assert(AMOTION_EVENT_ACTION_DOWN == AKEY_EVENT_ACTION_DOWN); |
| 74 | static_assert(AMOTION_EVENT_ACTION_UP == AKEY_EVENT_ACTION_UP); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 75 | static constexpr int32_t ACTION_DOWN = AMOTION_EVENT_ACTION_DOWN; |
| 76 | static constexpr int32_t ACTION_MOVE = AMOTION_EVENT_ACTION_MOVE; |
| 77 | static constexpr int32_t ACTION_UP = AMOTION_EVENT_ACTION_UP; |
| 78 | static constexpr int32_t ACTION_HOVER_ENTER = AMOTION_EVENT_ACTION_HOVER_ENTER; |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 79 | static constexpr int32_t ACTION_HOVER_MOVE = AMOTION_EVENT_ACTION_HOVER_MOVE; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 80 | static constexpr int32_t ACTION_HOVER_EXIT = AMOTION_EVENT_ACTION_HOVER_EXIT; |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 81 | static constexpr int32_t ACTION_SCROLL = AMOTION_EVENT_ACTION_SCROLL; |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 82 | static constexpr int32_t ACTION_OUTSIDE = AMOTION_EVENT_ACTION_OUTSIDE; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 83 | static constexpr int32_t ACTION_CANCEL = AMOTION_EVENT_ACTION_CANCEL; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 84 | /** |
| 85 | * The POINTER_DOWN(0) is an unusual, but valid, action. It just means that the new pointer in the |
| 86 | * MotionEvent is at the index 0 rather than 1 (or later). That is, the pointer id=0 (which is at |
| 87 | * index 0) is the new pointer going down. The same pointer could have been placed at a different |
| 88 | * index, and the action would become POINTER_1_DOWN, 2, etc..; these would all be valid. In |
| 89 | * general, we try to place pointer id = 0 at the index 0. Of course, this is not possible if |
| 90 | * pointer id=0 leaves but the pointer id=1 remains. |
| 91 | */ |
| 92 | static constexpr int32_t POINTER_0_DOWN = |
| 93 | AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 94 | static constexpr int32_t POINTER_1_DOWN = |
| 95 | AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 96 | static constexpr int32_t POINTER_2_DOWN = |
| 97 | AMOTION_EVENT_ACTION_POINTER_DOWN | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 98 | static constexpr int32_t POINTER_3_DOWN = |
| 99 | AMOTION_EVENT_ACTION_POINTER_DOWN | (3 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 100 | static constexpr int32_t POINTER_0_UP = |
| 101 | AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 102 | static constexpr int32_t POINTER_1_UP = |
| 103 | AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 104 | static constexpr int32_t POINTER_2_UP = |
| 105 | AMOTION_EVENT_ACTION_POINTER_UP | (2 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 106 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 107 | // 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] | 108 | static constexpr gui::Pid WINDOW_PID{999}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 109 | static constexpr gui::Uid WINDOW_UID{1001}; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 110 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 111 | // 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] | 112 | static constexpr gui::Pid SECONDARY_WINDOW_PID{1010}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 113 | static constexpr gui::Uid SECONDARY_WINDOW_UID{1012}; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 114 | |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 115 | // An arbitrary pid of the gesture monitor window |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 116 | static constexpr gui::Pid MONITOR_PID{2001}; |
Siarhei Vishniakou | 58cfc60 | 2020-12-14 23:21:30 +0000 | [diff] [blame] | 117 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 118 | /** |
| 119 | * If we expect to receive the event, the timeout can be made very long. When the test are running |
| 120 | * correctly, we will actually never wait until the end of the timeout because the wait will end |
| 121 | * when the event comes in. Still, this value shouldn't be infinite. During development, a local |
| 122 | * change may cause the test to fail. This timeout should be short enough to not annoy so that the |
| 123 | * developer can see the failure quickly (on human scale). |
| 124 | */ |
| 125 | static constexpr std::chrono::duration CONSUME_TIMEOUT_EVENT_EXPECTED = 1000ms; |
| 126 | /** |
| 127 | * When no event is expected, we can have a very short timeout. A large value here would slow down |
| 128 | * the tests. In the unlikely event of system being too slow, the event may still be present but the |
| 129 | * timeout would complete before it is consumed. This would result in test flakiness. If this |
| 130 | * occurs, the flakiness rate would be high. Since the flakes are treated with high priority, this |
| 131 | * would get noticed and addressed quickly. |
| 132 | */ |
| 133 | static constexpr std::chrono::duration CONSUME_TIMEOUT_NO_EVENT_EXPECTED = 10ms; |
| 134 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 135 | static constexpr int expectedWallpaperFlags = |
| 136 | AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED; |
| 137 | |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 138 | using ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID; |
| 139 | |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 140 | /** |
| 141 | * Return a DOWN key event with KEYCODE_A. |
| 142 | */ |
| 143 | static KeyEvent getTestKeyEvent() { |
| 144 | KeyEvent event; |
| 145 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 146 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 147 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, |
| 148 | ARBITRARY_TIME, ARBITRARY_TIME); |
Gang Wang | 342c927 | 2020-01-13 13:15:04 -0500 | [diff] [blame] | 149 | return event; |
| 150 | } |
| 151 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 152 | // --- FakeInputDispatcherPolicy --- |
| 153 | |
| 154 | class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 155 | struct AnrResult { |
| 156 | sp<IBinder> token{}; |
| 157 | gui::Pid pid{gui::Pid::INVALID}; |
| 158 | }; |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 159 | /* Stores data about a user-activity-poke event from the dispatcher. */ |
| 160 | struct UserActivityPokeEvent { |
| 161 | nsecs_t eventTime; |
| 162 | int32_t eventType; |
| 163 | int32_t displayId; |
| 164 | |
| 165 | bool operator==(const UserActivityPokeEvent& rhs) const = default; |
| 166 | |
| 167 | friend std::ostream& operator<<(std::ostream& os, const UserActivityPokeEvent& ev) { |
| 168 | os << "UserActivityPokeEvent[time=" << ev.eventTime << ", eventType=" << ev.eventType |
| 169 | << ", displayId=" << ev.displayId << "]"; |
| 170 | return os; |
| 171 | } |
| 172 | }; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 173 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 174 | public: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 175 | FakeInputDispatcherPolicy() = default; |
| 176 | virtual ~FakeInputDispatcherPolicy() = default; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 177 | |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 178 | void assertFilterInputEventWasCalled(const NotifyKeyArgs& args) { |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 179 | assertFilterInputEventWasCalledInternal([&args](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 180 | ASSERT_EQ(event.getType(), InputEventType::KEY); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 181 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 182 | |
| 183 | const auto& keyEvent = static_cast<const KeyEvent&>(event); |
| 184 | EXPECT_EQ(keyEvent.getEventTime(), args.eventTime); |
| 185 | EXPECT_EQ(keyEvent.getAction(), args.action); |
| 186 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 187 | } |
| 188 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 189 | void assertFilterInputEventWasCalled(const NotifyMotionArgs& args, vec2 point) { |
| 190 | assertFilterInputEventWasCalledInternal([&](const InputEvent& event) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 191 | ASSERT_EQ(event.getType(), InputEventType::MOTION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 192 | EXPECT_EQ(event.getDisplayId(), args.displayId); |
| 193 | |
| 194 | const auto& motionEvent = static_cast<const MotionEvent&>(event); |
| 195 | EXPECT_EQ(motionEvent.getEventTime(), args.eventTime); |
| 196 | EXPECT_EQ(motionEvent.getAction(), args.action); |
Prabir Pradhan | 00e029d | 2023-03-09 20:11:09 +0000 | [diff] [blame] | 197 | EXPECT_NEAR(motionEvent.getX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 198 | EXPECT_NEAR(motionEvent.getY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
| 199 | EXPECT_NEAR(motionEvent.getRawX(0), point.x, MotionEvent::ROUNDING_PRECISION); |
| 200 | EXPECT_NEAR(motionEvent.getRawY(0), point.y, MotionEvent::ROUNDING_PRECISION); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 201 | }); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 202 | } |
| 203 | |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 204 | void assertFilterInputEventWasNotCalled() { |
| 205 | std::scoped_lock lock(mLock); |
| 206 | ASSERT_EQ(nullptr, mFilteredEvent); |
| 207 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 208 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 209 | void assertNotifyConfigurationChangedWasCalled(nsecs_t when) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 210 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 211 | ASSERT_TRUE(mConfigurationChangedTime) |
| 212 | << "Timed out waiting for configuration changed call"; |
| 213 | ASSERT_EQ(*mConfigurationChangedTime, when); |
| 214 | mConfigurationChangedTime = std::nullopt; |
| 215 | } |
| 216 | |
| 217 | void assertNotifySwitchWasCalled(const NotifySwitchArgs& args) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 218 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 219 | ASSERT_TRUE(mLastNotifySwitch); |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 220 | // 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] | 221 | EXPECT_EQ(args.eventTime, mLastNotifySwitch->eventTime); |
| 222 | EXPECT_EQ(args.policyFlags, mLastNotifySwitch->policyFlags); |
| 223 | EXPECT_EQ(args.switchValues, mLastNotifySwitch->switchValues); |
| 224 | EXPECT_EQ(args.switchMask, mLastNotifySwitch->switchMask); |
| 225 | mLastNotifySwitch = std::nullopt; |
| 226 | } |
| 227 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 228 | void assertOnPointerDownEquals(const sp<IBinder>& touchedToken) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 229 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 230 | ASSERT_EQ(touchedToken, mOnPointerDownToken); |
| 231 | mOnPointerDownToken.clear(); |
| 232 | } |
| 233 | |
| 234 | void assertOnPointerDownWasNotCalled() { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 235 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 236 | ASSERT_TRUE(mOnPointerDownToken == nullptr) |
| 237 | << "Expected onPointerDownOutsideFocus to not have been called"; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 240 | // This function must be called soon after the expected ANR timer starts, |
| 241 | // because we are also checking how much time has passed. |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 242 | void assertNotifyNoFocusedWindowAnrWasCalled( |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 243 | std::chrono::nanoseconds timeout, |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 244 | const std::shared_ptr<InputApplicationHandle>& expectedApplication) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 245 | std::unique_lock lock(mLock); |
| 246 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 247 | std::shared_ptr<InputApplicationHandle> application; |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 248 | ASSERT_NO_FATAL_FAILURE( |
| 249 | application = getAnrTokenLockedInterruptible(timeout, mAnrApplications, lock)); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 250 | ASSERT_EQ(expectedApplication, application); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 253 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 254 | const sp<WindowInfoHandle>& window) { |
| 255 | LOG_ALWAYS_FATAL_IF(window == nullptr, "window should not be null"); |
| 256 | assertNotifyWindowUnresponsiveWasCalled(timeout, window->getToken(), |
| 257 | window->getInfo()->ownerPid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 258 | } |
| 259 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 260 | void assertNotifyWindowUnresponsiveWasCalled(std::chrono::nanoseconds timeout, |
| 261 | const sp<IBinder>& expectedToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 262 | gui::Pid expectedPid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 263 | std::unique_lock lock(mLock); |
| 264 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 265 | AnrResult result; |
| 266 | ASSERT_NO_FATAL_FAILURE(result = |
| 267 | getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 268 | ASSERT_EQ(expectedToken, result.token); |
| 269 | ASSERT_EQ(expectedPid, result.pid); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 270 | } |
| 271 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 272 | /** 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] | 273 | sp<IBinder> getUnresponsiveWindowToken(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 274 | std::unique_lock lock(mLock); |
| 275 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 276 | AnrResult result = getAnrTokenLockedInterruptible(timeout, mAnrWindows, lock); |
| 277 | const auto& [token, _] = result; |
| 278 | return token; |
Siarhei Vishniakou | 3c63fa4 | 2020-12-15 02:59:54 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 281 | void assertNotifyWindowResponsiveWasCalled(const sp<IBinder>& expectedToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 282 | gui::Pid expectedPid) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 283 | std::unique_lock lock(mLock); |
| 284 | android::base::ScopedLockAssertion assumeLocked(mLock); |
| 285 | AnrResult result; |
| 286 | ASSERT_NO_FATAL_FAILURE( |
| 287 | result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 288 | ASSERT_EQ(expectedToken, result.token); |
| 289 | ASSERT_EQ(expectedPid, result.pid); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /** 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] | 293 | sp<IBinder> getResponsiveWindowToken() { |
| 294 | std::unique_lock lock(mLock); |
| 295 | android::base::ScopedLockAssertion assumeLocked(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 296 | AnrResult result = getAnrTokenLockedInterruptible(0s, mResponsiveWindows, lock); |
| 297 | const auto& [token, _] = result; |
| 298 | return token; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | void assertNotifyAnrWasNotCalled() { |
| 302 | std::scoped_lock lock(mLock); |
| 303 | ASSERT_TRUE(mAnrApplications.empty()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 304 | ASSERT_TRUE(mAnrWindows.empty()); |
| 305 | ASSERT_TRUE(mResponsiveWindows.empty()) |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 306 | << "ANR was not called, but please also consume the 'connection is responsive' " |
| 307 | "signal"; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 310 | PointerCaptureRequest assertSetPointerCaptureCalled(bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 311 | std::unique_lock lock(mLock); |
| 312 | base::ScopedLockAssertion assumeLocked(mLock); |
| 313 | |
| 314 | if (!mPointerCaptureChangedCondition.wait_for(lock, 100ms, |
| 315 | [this, enabled]() REQUIRES(mLock) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 316 | return mPointerCaptureRequest->enable == |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 317 | enabled; |
| 318 | })) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 319 | ADD_FAILURE() << "Timed out waiting for setPointerCapture(" << enabled |
| 320 | << ") to be called."; |
| 321 | return {}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 322 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 323 | auto request = *mPointerCaptureRequest; |
| 324 | mPointerCaptureRequest.reset(); |
| 325 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | void assertSetPointerCaptureNotCalled() { |
| 329 | std::unique_lock lock(mLock); |
| 330 | base::ScopedLockAssertion assumeLocked(mLock); |
| 331 | |
| 332 | if (mPointerCaptureChangedCondition.wait_for(lock, 100ms) != std::cv_status::timeout) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 333 | FAIL() << "Expected setPointerCapture(request) to not be called, but was called. " |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 334 | "enabled = " |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 335 | << std::to_string(mPointerCaptureRequest->enable); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 336 | } |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 337 | mPointerCaptureRequest.reset(); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 340 | void assertDropTargetEquals(const InputDispatcherInterface& dispatcher, |
| 341 | const sp<IBinder>& targetToken) { |
| 342 | dispatcher.waitForIdle(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 343 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 344 | ASSERT_TRUE(mNotifyDropWindowWasCalled); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 345 | ASSERT_EQ(targetToken, mDropTargetWindowToken); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 346 | mNotifyDropWindowWasCalled = false; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 347 | } |
| 348 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 349 | void assertNotifyInputChannelBrokenWasCalled(const sp<IBinder>& token) { |
| 350 | std::unique_lock lock(mLock); |
| 351 | base::ScopedLockAssertion assumeLocked(mLock); |
| 352 | std::optional<sp<IBinder>> receivedToken = |
| 353 | getItemFromStorageLockedInterruptible(100ms, mBrokenInputChannels, lock, |
| 354 | mNotifyInputChannelBroken); |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 355 | ASSERT_TRUE(receivedToken.has_value()) << "Did not receive the broken channel token"; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 356 | ASSERT_EQ(token, *receivedToken); |
| 357 | } |
| 358 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 359 | /** |
| 360 | * Set policy timeout. A value of zero means next key will not be intercepted. |
| 361 | */ |
| 362 | void setInterceptKeyTimeout(std::chrono::milliseconds timeout) { |
| 363 | mInterceptKeyTimeout = timeout; |
| 364 | } |
| 365 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame^] | 366 | std::chrono::nanoseconds getKeyWaitingForEventsTimeout() override { return 500ms; } |
| 367 | |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 368 | void setStaleEventTimeout(std::chrono::nanoseconds timeout) { mStaleEventTimeout = timeout; } |
| 369 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 370 | void assertUserActivityNotPoked() { |
| 371 | std::unique_lock lock(mLock); |
| 372 | base::ScopedLockAssertion assumeLocked(mLock); |
| 373 | |
| 374 | std::optional<UserActivityPokeEvent> pokeEvent = |
| 375 | getItemFromStorageLockedInterruptible(500ms, mUserActivityPokeEvents, lock, |
| 376 | mNotifyUserActivity); |
| 377 | |
| 378 | ASSERT_FALSE(pokeEvent) << "Expected user activity not to have been poked"; |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 381 | /** |
| 382 | * Asserts that a user activity poke has happened. The earliest recorded poke event will be |
| 383 | * cleared after this call. |
| 384 | * |
| 385 | * If an expected UserActivityPokeEvent is provided, asserts that the given event is the |
| 386 | * earliest recorded poke event. |
| 387 | */ |
| 388 | void assertUserActivityPoked(std::optional<UserActivityPokeEvent> expectedPokeEvent = {}) { |
| 389 | std::unique_lock lock(mLock); |
| 390 | base::ScopedLockAssertion assumeLocked(mLock); |
| 391 | |
| 392 | std::optional<UserActivityPokeEvent> pokeEvent = |
| 393 | getItemFromStorageLockedInterruptible(500ms, mUserActivityPokeEvents, lock, |
| 394 | mNotifyUserActivity); |
| 395 | ASSERT_TRUE(pokeEvent) << "Expected a user poke event"; |
| 396 | |
| 397 | if (expectedPokeEvent) { |
| 398 | ASSERT_EQ(expectedPokeEvent, *pokeEvent); |
| 399 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 402 | void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids) { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 403 | ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms)); |
| 404 | } |
| 405 | |
| 406 | void assertNotifyDeviceInteractionWasNotCalled() { |
| 407 | ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms)); |
| 408 | } |
| 409 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 410 | void setUnhandledKeyHandler(std::function<std::optional<KeyEvent>(const KeyEvent&)> handler) { |
| 411 | std::scoped_lock lock(mLock); |
| 412 | mUnhandledKeyHandler = handler; |
| 413 | } |
| 414 | |
| 415 | void assertUnhandledKeyReported(int32_t keycode) { |
| 416 | std::unique_lock lock(mLock); |
| 417 | base::ScopedLockAssertion assumeLocked(mLock); |
| 418 | std::optional<int32_t> unhandledKeycode = |
| 419 | getItemFromStorageLockedInterruptible(100ms, mReportedUnhandledKeycodes, lock, |
| 420 | mNotifyUnhandledKey); |
| 421 | ASSERT_TRUE(unhandledKeycode) << "Expected unhandled key to be reported"; |
| 422 | ASSERT_EQ(unhandledKeycode, keycode); |
| 423 | } |
| 424 | |
| 425 | void assertUnhandledKeyNotReported() { |
| 426 | std::unique_lock lock(mLock); |
| 427 | base::ScopedLockAssertion assumeLocked(mLock); |
| 428 | std::optional<int32_t> unhandledKeycode = |
| 429 | getItemFromStorageLockedInterruptible(10ms, mReportedUnhandledKeycodes, lock, |
| 430 | mNotifyUnhandledKey); |
| 431 | ASSERT_FALSE(unhandledKeycode) << "Expected unhandled key NOT to be reported"; |
| 432 | } |
| 433 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 434 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 435 | std::mutex mLock; |
| 436 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 437 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 438 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 439 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 440 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 441 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 442 | |
| 443 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 444 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 445 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 446 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 447 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 448 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 449 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 450 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 451 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 452 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 453 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 454 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 455 | |
| 456 | std::condition_variable mNotifyUserActivity; |
| 457 | std::queue<UserActivityPokeEvent> mUserActivityPokeEvents; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 458 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 459 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 460 | |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 461 | std::chrono::nanoseconds mStaleEventTimeout = 1000ms; |
| 462 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 463 | BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 464 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 465 | std::condition_variable mNotifyUnhandledKey; |
| 466 | std::queue<int32_t> mReportedUnhandledKeycodes GUARDED_BY(mLock); |
| 467 | std::function<std::optional<KeyEvent>(const KeyEvent&)> mUnhandledKeyHandler GUARDED_BY(mLock); |
| 468 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 469 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 470 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 471 | // first entry from the container and erase it. |
| 472 | template <class T> |
| 473 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 474 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 475 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 476 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 477 | // before checking if ANR was called. |
| 478 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 479 | // to provide it some time to act. 100ms seems reasonable. |
| 480 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 481 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 482 | std::optional<T> token = |
| 483 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 484 | if (!token.has_value()) { |
| 485 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 486 | return {}; |
| 487 | } |
| 488 | |
| 489 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 490 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 491 | // the dispatcher started counting before this function was called |
| 492 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 493 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 494 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 495 | << "ms, but waited " |
| 496 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 497 | << "ms instead"; |
| 498 | } |
| 499 | return *token; |
| 500 | } |
| 501 | |
| 502 | template <class T> |
| 503 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 504 | std::queue<T>& storage, |
| 505 | std::unique_lock<std::mutex>& lock, |
| 506 | std::condition_variable& condition) |
| 507 | REQUIRES(mLock) { |
| 508 | condition.wait_for(lock, timeout, |
| 509 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 510 | if (storage.empty()) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 511 | return std::nullopt; |
| 512 | } |
| 513 | T item = storage.front(); |
| 514 | storage.pop(); |
| 515 | return std::make_optional(item); |
| 516 | } |
| 517 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 518 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 519 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 520 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 521 | } |
| 522 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 523 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<gui::Pid> pid, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 524 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 525 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 526 | ASSERT_TRUE(pid.has_value()); |
| 527 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 528 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 529 | } |
| 530 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 531 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 532 | std::optional<gui::Pid> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 533 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 534 | ASSERT_TRUE(pid.has_value()); |
| 535 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 536 | mNotifyAnr.notify_all(); |
| 537 | } |
| 538 | |
| 539 | void notifyNoFocusedWindowAnr( |
| 540 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 541 | std::scoped_lock lock(mLock); |
| 542 | mAnrApplications.push(applicationHandle); |
| 543 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 544 | } |
| 545 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 546 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 547 | std::scoped_lock lock(mLock); |
| 548 | mBrokenInputChannels.push(connectionToken); |
| 549 | mNotifyInputChannelBroken.notify_all(); |
| 550 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 551 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 552 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 553 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 554 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 555 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 556 | const std::vector<float>& values) override {} |
| 557 | |
| 558 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 559 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 560 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 561 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 562 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 563 | bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 564 | std::scoped_lock lock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 565 | switch (inputEvent.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 566 | case InputEventType::KEY: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 567 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent); |
| 568 | mFilteredEvent = std::make_unique<KeyEvent>(keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 569 | break; |
| 570 | } |
| 571 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 572 | case InputEventType::MOTION: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 573 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent); |
| 574 | mFilteredEvent = std::make_unique<MotionEvent>(motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 575 | break; |
| 576 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 577 | default: { |
| 578 | ADD_FAILURE() << "Should only filter keys or motions"; |
| 579 | break; |
| 580 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 581 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 582 | return true; |
| 583 | } |
| 584 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 585 | void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override { |
| 586 | if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 587 | // Clear intercept state when we handled the event. |
| 588 | mInterceptKeyTimeout = 0ms; |
| 589 | } |
| 590 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 591 | |
Yeabkal Wubshit | 88a9041 | 2023-12-21 18:23:04 -0800 | [diff] [blame] | 592 | 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] | 593 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 594 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 595 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 596 | // Clear intercept state so we could dispatch the event in next wake. |
| 597 | mInterceptKeyTimeout = 0ms; |
| 598 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 599 | } |
| 600 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 601 | std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent& event, |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 602 | uint32_t) override { |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 603 | std::scoped_lock lock(mLock); |
| 604 | mReportedUnhandledKeycodes.emplace(event.getKeyCode()); |
| 605 | mNotifyUnhandledKey.notify_all(); |
| 606 | return mUnhandledKeyHandler != nullptr ? mUnhandledKeyHandler(event) : std::nullopt; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 607 | } |
| 608 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 609 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 610 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 611 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 612 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 613 | * essentially a passthrough for notifySwitch. |
| 614 | */ |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 615 | mLastNotifySwitch = |
| 616 | NotifySwitchArgs(InputEvent::nextId(), when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 617 | } |
| 618 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 619 | 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] | 620 | std::scoped_lock lock(mLock); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 621 | mNotifyUserActivity.notify_all(); |
| 622 | mUserActivityPokeEvents.push({eventTime, eventType, displayId}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 623 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 624 | |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 625 | bool isStaleEvent(nsecs_t currentTime, nsecs_t eventTime) override { |
| 626 | return std::chrono::nanoseconds(currentTime - eventTime) >= mStaleEventTimeout; |
| 627 | } |
| 628 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 629 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 630 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 631 | mOnPointerDownToken = newToken; |
| 632 | } |
| 633 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 634 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 635 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 636 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 637 | mPointerCaptureChangedCondition.notify_all(); |
| 638 | } |
| 639 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 640 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 641 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 642 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 643 | mDropTargetWindowToken = token; |
| 644 | } |
| 645 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 646 | void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 647 | const std::set<gui::Uid>& uids) override { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 648 | ASSERT_TRUE(mNotifiedInteractions.emplace(deviceId, uids)); |
| 649 | } |
| 650 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 651 | void assertFilterInputEventWasCalledInternal( |
| 652 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 653 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 654 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 655 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 656 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 657 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 658 | }; |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 659 | } // namespace |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 660 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 661 | // --- InputDispatcherTest --- |
| 662 | |
| 663 | class InputDispatcherTest : public testing::Test { |
| 664 | protected: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 665 | std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 666 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 667 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 668 | void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 669 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
Prabir Pradhan | 9fb98f7 | 2024-02-02 19:47:19 +0000 | [diff] [blame] | 670 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy, nullptr); |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 671 | |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 672 | mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 673 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 674 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 675 | } |
| 676 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 677 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 678 | ASSERT_EQ(OK, mDispatcher->stop()); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 679 | mFakePolicy.reset(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 680 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 681 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 682 | |
| 683 | /** |
| 684 | * Used for debugging when writing the test |
| 685 | */ |
| 686 | void dumpDispatcherState() { |
| 687 | std::string dump; |
| 688 | mDispatcher->dump(dump); |
| 689 | std::stringstream ss(dump); |
| 690 | std::string to; |
| 691 | |
| 692 | while (std::getline(ss, to, '\n')) { |
| 693 | ALOGE("%s", to.c_str()); |
| 694 | } |
| 695 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 696 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 697 | void setFocusedWindow(const sp<WindowInfoHandle>& window) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 698 | FocusRequest request; |
| 699 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 700 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 701 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 702 | request.displayId = window->getInfo()->displayId; |
| 703 | mDispatcher->setFocusedWindow(request); |
| 704 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 705 | }; |
| 706 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 707 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 708 | KeyEvent event; |
| 709 | |
| 710 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 711 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 712 | INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 713 | /*action=*/-1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 714 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 715 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 716 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 717 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 718 | << "Should reject key events with undefined action."; |
| 719 | |
| 720 | // 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] | 721 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 722 | 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] | 723 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 724 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 725 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 726 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 727 | << "Should reject key events with ACTION_MULTIPLE."; |
| 728 | } |
| 729 | |
| 730 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 731 | MotionEvent event; |
| 732 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 733 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 734 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 735 | pointerProperties[i].clear(); |
| 736 | pointerProperties[i].id = i; |
| 737 | pointerCoords[i].clear(); |
| 738 | } |
| 739 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 740 | // Some constants commonly used below |
| 741 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 742 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 743 | constexpr int32_t metaState = AMETA_NONE; |
| 744 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 745 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 746 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 747 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 748 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 749 | /*action=*/-1, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 750 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 751 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 752 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 753 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 754 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 755 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 756 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 757 | << "Should reject motion events with undefined action."; |
| 758 | |
| 759 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 760 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 761 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 762 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 763 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 764 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 765 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 766 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 767 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 768 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 769 | << "Should reject motion events with pointer down index too large."; |
| 770 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 771 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 772 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 773 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 774 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 775 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 776 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 777 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 778 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 779 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 780 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 781 | << "Should reject motion events with pointer down index too small."; |
| 782 | |
| 783 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 784 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 785 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 786 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 787 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 788 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 789 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 790 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 791 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 792 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 793 | << "Should reject motion events with pointer up index too large."; |
| 794 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 795 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 796 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 797 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 798 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 799 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 800 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 801 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 802 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 803 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 804 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 805 | << "Should reject motion events with pointer up index too small."; |
| 806 | |
| 807 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 808 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 809 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 810 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 811 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 812 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 813 | /*pointerCount=*/0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 814 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 815 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 816 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 817 | << "Should reject motion events with 0 pointers."; |
| 818 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 819 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 820 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 821 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 822 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 823 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 824 | /*pointerCount=*/MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 825 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 826 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 827 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 828 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 829 | |
| 830 | // Rejects motion events with invalid pointer ids. |
| 831 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 832 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 833 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 834 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 835 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 836 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 837 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 838 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 839 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 840 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 841 | << "Should reject motion events with pointer ids less than 0."; |
| 842 | |
| 843 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 844 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 845 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 846 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 847 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 848 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 849 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 850 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 851 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 852 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 853 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 854 | |
| 855 | // Rejects motion events with duplicate pointer ids. |
| 856 | pointerProperties[0].id = 1; |
| 857 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 858 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 859 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 860 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 861 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 862 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 863 | /*pointerCount=*/2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 864 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 865 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 866 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 867 | << "Should reject motion events with duplicate pointer ids."; |
| 868 | } |
| 869 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 870 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 871 | |
| 872 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 873 | constexpr nsecs_t eventTime = 20; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 874 | mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 875 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 876 | |
| 877 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 878 | } |
| 879 | |
| 880 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 881 | NotifySwitchArgs args(InputEvent::nextId(), /*eventTime=*/20, /*policyFlags=*/0, |
| 882 | /*switchValues=*/1, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 883 | /*switchMask=*/2); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 884 | mDispatcher->notifySwitch(args); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 885 | |
| 886 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 887 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 888 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 889 | } |
| 890 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 891 | namespace { |
| 892 | |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 893 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 894 | // Default input dispatching timeout if there is no focused application or paused window |
| 895 | // from which to determine an appropriate dispatching timeout. |
| 896 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 897 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 898 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 899 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 900 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 901 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 902 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 903 | : mConsumer(std::move(clientChannel)), mName(name) {} |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 904 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 905 | std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = false) { |
| 906 | auto [consumeSeq, event] = receiveEvent(timeout); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 907 | if (!consumeSeq) { |
| 908 | return nullptr; |
| 909 | } |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 910 | finishEvent(*consumeSeq, handled); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 911 | return std::move(event); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | /** |
| 915 | * Receive an event without acknowledging it. |
| 916 | * Return the sequence number that could later be used to send finished signal. |
| 917 | */ |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 918 | std::pair<std::optional<uint32_t>, std::unique_ptr<InputEvent>> receiveEvent( |
| 919 | std::chrono::milliseconds timeout) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 920 | uint32_t consumeSeq; |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 921 | std::unique_ptr<InputEvent> event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 922 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 923 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 924 | status_t status = WOULD_BLOCK; |
| 925 | while (status == WOULD_BLOCK) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 926 | InputEvent* rawEventPtr = nullptr; |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 927 | status = mConsumer.consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 928 | &rawEventPtr); |
| 929 | event = std::unique_ptr<InputEvent>(rawEventPtr); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 930 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 931 | if (elapsed > timeout) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 932 | break; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | if (status == WOULD_BLOCK) { |
| 937 | // Just means there's no event available. |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 938 | return std::make_pair(std::nullopt, nullptr); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | if (status != OK) { |
| 942 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 943 | return std::make_pair(std::nullopt, nullptr); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 944 | } |
| 945 | if (event == nullptr) { |
| 946 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 947 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 948 | return std::make_pair(consumeSeq, std::move(event)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 949 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 950 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 951 | /** |
| 952 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 953 | */ |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 954 | void finishEvent(uint32_t consumeSeq, bool handled = true) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 955 | const status_t status = mConsumer.sendFinishedSignal(consumeSeq, handled); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 956 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 957 | } |
| 958 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 959 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 960 | const status_t status = mConsumer.sendTimeline(inputEventId, timeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 961 | ASSERT_EQ(OK, status); |
| 962 | } |
| 963 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 964 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 965 | std::optional<int32_t> expectedDisplayId, |
| 966 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 967 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 968 | |
| 969 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 970 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 971 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 972 | << mName.c_str() << " expected " << ftl::enum_string(expectedEventType) |
| 973 | << " event, got " << *event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 974 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 975 | if (expectedDisplayId.has_value()) { |
| 976 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 977 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 978 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 979 | switch (expectedEventType) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 980 | case InputEventType::KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 981 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 982 | ASSERT_THAT(keyEvent, WithKeyAction(expectedAction)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 983 | if (expectedFlags.has_value()) { |
| 984 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 985 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 986 | break; |
| 987 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 988 | case InputEventType::MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 989 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 990 | ASSERT_THAT(motionEvent, WithMotionAction(expectedAction)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 991 | if (expectedFlags.has_value()) { |
| 992 | EXPECT_EQ(expectedFlags.value(), motionEvent.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::FOCUS: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 997 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 998 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 999 | case InputEventType::CAPTURE: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1000 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 1001 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1002 | case InputEventType::TOUCH_MODE: { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1003 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 1004 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1005 | case InputEventType::DRAG: { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1006 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 1007 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 1008 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1009 | } |
| 1010 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1011 | std::unique_ptr<MotionEvent> consumeMotion() { |
| 1012 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1013 | |
| 1014 | if (event == nullptr) { |
| 1015 | ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one."; |
| 1016 | return nullptr; |
| 1017 | } |
| 1018 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1019 | if (event->getType() != InputEventType::MOTION) { |
| 1020 | ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1021 | return nullptr; |
| 1022 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1023 | return std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event.release())); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1027 | std::unique_ptr<MotionEvent> motionEvent = consumeMotion(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1028 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; |
| 1029 | ASSERT_THAT(*motionEvent, matcher); |
| 1030 | } |
| 1031 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1032 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1033 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1034 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1035 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1036 | ASSERT_EQ(InputEventType::FOCUS, event->getType()) |
| 1037 | << "Instead of FocusEvent, got " << *event; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1038 | |
| 1039 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1040 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1041 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1042 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1043 | EXPECT_EQ(hasFocus, focusEvent.getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1044 | } |
| 1045 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1046 | void consumeCaptureEvent(bool hasCapture) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1047 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1048 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1049 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1050 | ASSERT_EQ(InputEventType::CAPTURE, event->getType()) |
| 1051 | << "Instead of CaptureEvent, got " << *event; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1052 | |
| 1053 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1054 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1055 | |
| 1056 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 1057 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 1058 | } |
| 1059 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1060 | void consumeDragEvent(bool isExiting, float x, float y) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1061 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1062 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1063 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1064 | ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1065 | |
| 1066 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1067 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1068 | |
| 1069 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 1070 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 1071 | EXPECT_EQ(x, dragEvent.getX()); |
| 1072 | EXPECT_EQ(y, dragEvent.getY()); |
| 1073 | } |
| 1074 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1075 | void consumeTouchModeEvent(bool inTouchMode) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1076 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1077 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1078 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1079 | ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType()) |
| 1080 | << "Instead of TouchModeEvent, got " << *event; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1081 | |
| 1082 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1083 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1084 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 1085 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 1086 | } |
| 1087 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame^] | 1088 | void assertNoEvents(std::chrono::milliseconds timeout) { |
| 1089 | std::unique_ptr<InputEvent> event = consume(timeout); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1090 | if (event == nullptr) { |
| 1091 | return; |
| 1092 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1093 | if (event->getType() == InputEventType::KEY) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1094 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1095 | ADD_FAILURE() << "Received key event " << keyEvent; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1096 | } else if (event->getType() == InputEventType::MOTION) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1097 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1098 | ADD_FAILURE() << "Received motion event " << motionEvent; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1099 | } else if (event->getType() == InputEventType::FOCUS) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1100 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1101 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 1102 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1103 | } else if (event->getType() == InputEventType::CAPTURE) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1104 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 1105 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 1106 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1107 | } else if (event->getType() == InputEventType::TOUCH_MODE) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1108 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 1109 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 1110 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1111 | } |
| 1112 | FAIL() << mName.c_str() |
| 1113 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1114 | } |
| 1115 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1116 | sp<IBinder> getToken() { return mConsumer.getChannel()->getConnectionToken(); } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1117 | |
Siarhei Vishniakou | 8d66013 | 2024-01-11 16:48:44 -0800 | [diff] [blame] | 1118 | int getChannelFd() { return mConsumer.getChannel()->getFd(); } |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1119 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1120 | private: |
| 1121 | InputConsumer mConsumer; |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1122 | DynamicInputEventFactory mEventFactory; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1123 | |
| 1124 | std::string mName; |
| 1125 | }; |
| 1126 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1127 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1128 | public: |
| 1129 | static const int32_t WIDTH = 600; |
| 1130 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1131 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1132 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1133 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1134 | int32_t displayId, bool createInputChannel = true) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1135 | : mName(name) { |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1136 | sp<IBinder> token; |
| 1137 | if (createInputChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1138 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1139 | dispatcher->createInputChannel(name); |
| 1140 | token = (*channel)->getConnectionToken(); |
| 1141 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | inputApplicationHandle->updateInfo(); |
| 1145 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1146 | |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1147 | mInfo.token = token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1148 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1149 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1150 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1151 | mInfo.alpha = 1.0; |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 1152 | mInfo.frame = Rect(0, 0, WIDTH, HEIGHT); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1153 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1154 | mInfo.globalScaleFactor = 1.0; |
| 1155 | mInfo.touchableRegion.clear(); |
| 1156 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1157 | mInfo.ownerPid = WINDOW_PID; |
| 1158 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1159 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1160 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1161 | } |
| 1162 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1163 | sp<FakeWindowHandle> clone(int32_t displayId) { |
| 1164 | sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)"); |
| 1165 | handle->mInfo = mInfo; |
| 1166 | handle->mInfo.displayId = displayId; |
| 1167 | handle->mInfo.id = sId++; |
| 1168 | handle->mInputReceiver = mInputReceiver; |
| 1169 | return handle; |
| 1170 | } |
| 1171 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1172 | void setTouchable(bool touchable) { |
| 1173 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1174 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1175 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1176 | void setFocusable(bool focusable) { |
| 1177 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1178 | } |
| 1179 | |
| 1180 | void setVisible(bool visible) { |
| 1181 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1182 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1183 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1184 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1185 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1188 | void setPaused(bool paused) { |
| 1189 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1190 | } |
| 1191 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1192 | void setPreventSplitting(bool preventSplitting) { |
| 1193 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | void setSlippery(bool slippery) { |
| 1197 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1198 | } |
| 1199 | |
| 1200 | void setWatchOutsideTouch(bool watchOutside) { |
| 1201 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1202 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1203 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1204 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1205 | |
| 1206 | void setInterceptsStylus(bool interceptsStylus) { |
| 1207 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1208 | } |
| 1209 | |
| 1210 | void setDropInput(bool dropInput) { |
| 1211 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1212 | } |
| 1213 | |
| 1214 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1215 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1216 | } |
| 1217 | |
| 1218 | void setNoInputChannel(bool noInputChannel) { |
| 1219 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1220 | } |
| 1221 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1222 | void setDisableUserActivity(bool disableUserActivity) { |
| 1223 | mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity); |
| 1224 | } |
| 1225 | |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 1226 | void setGlobalStylusBlocksTouch(bool shouldGlobalStylusBlockTouch) { |
| 1227 | mInfo.setInputConfig(WindowInfo::InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH, |
| 1228 | shouldGlobalStylusBlockTouch); |
| 1229 | } |
| 1230 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1231 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1232 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1233 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1234 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1235 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1236 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1237 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 1238 | mInfo.frame = frame; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1239 | mInfo.touchableRegion.clear(); |
| 1240 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1241 | |
| 1242 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1243 | ui::Transform translate; |
| 1244 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1245 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1246 | } |
| 1247 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1248 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1249 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1250 | void setIsWallpaper(bool isWallpaper) { |
| 1251 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1252 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1253 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1254 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1255 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1256 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1257 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1258 | void setTrustedOverlay(bool trustedOverlay) { |
| 1259 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1260 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1261 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1262 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1263 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1264 | } |
| 1265 | |
| 1266 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1267 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1268 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1269 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1270 | std::unique_ptr<KeyEvent> consumeKey(bool handled = true) { |
| 1271 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED, handled); |
| 1272 | if (event == nullptr) { |
| 1273 | ADD_FAILURE() << "No event"; |
| 1274 | return nullptr; |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1275 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1276 | if (event->getType() != InputEventType::KEY) { |
| 1277 | ADD_FAILURE() << "Instead of key event, got " << event; |
| 1278 | return nullptr; |
| 1279 | } |
| 1280 | return std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event.release())); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | void consumeKeyEvent(const ::testing::Matcher<KeyEvent>& matcher) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1284 | std::unique_ptr<KeyEvent> keyEvent = consumeKey(); |
| 1285 | ASSERT_NE(nullptr, keyEvent); |
| 1286 | ASSERT_THAT(*keyEvent, matcher); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1289 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1290 | consumeKeyEvent(AllOf(WithKeyAction(ACTION_DOWN), WithDisplayId(expectedDisplayId), |
| 1291 | WithFlags(expectedFlags))); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1292 | } |
| 1293 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1294 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1295 | consumeKeyEvent(AllOf(WithKeyAction(ACTION_UP), WithDisplayId(expectedDisplayId), |
| 1296 | WithFlags(expectedFlags))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1299 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1300 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1301 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId), |
| 1302 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1306 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1307 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 1308 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1312 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1313 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1314 | } |
| 1315 | |
| 1316 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1317 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1318 | consumeMotionEvent( |
| 1319 | AllOf(WithMotionAction(ACTION_DOWN), |
| 1320 | testing::Conditional(expectedDisplayId.has_value(), |
| 1321 | WithDisplayId(*expectedDisplayId), testing::_), |
| 1322 | testing::Conditional(expectedFlags.has_value(), WithFlags(*expectedFlags), |
| 1323 | testing::_))); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1324 | } |
| 1325 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1326 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1327 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1328 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1329 | const int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1330 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1331 | consumeMotionEvent(AllOf(WithMotionAction(action), WithDisplayId(expectedDisplayId), |
| 1332 | WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 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_UP | |
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 consumeMotionUp(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 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDisplayId(expectedDisplayId), |
| 1346 | WithFlags(expectedFlags))); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1349 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1350 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1351 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE), |
| 1352 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1353 | } |
| 1354 | |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1355 | void consumeMotionOutsideWithZeroedCoords() { |
| 1356 | consumeMotionEvent( |
| 1357 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE), WithRawCoords(0, 0))); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1358 | } |
| 1359 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1360 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1361 | ASSERT_NE(mInputReceiver, nullptr) |
| 1362 | << "Cannot consume events from a window with no receiver"; |
| 1363 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1364 | } |
| 1365 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1366 | void consumeCaptureEvent(bool hasCapture) { |
| 1367 | ASSERT_NE(mInputReceiver, nullptr) |
| 1368 | << "Cannot consume events from a window with no receiver"; |
| 1369 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1370 | } |
| 1371 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1372 | std::unique_ptr<MotionEvent> consumeMotionEvent( |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1373 | const ::testing::Matcher<MotionEvent>& matcher = testing::_) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1374 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
| 1375 | if (event == nullptr) { |
| 1376 | ADD_FAILURE() << "No event"; |
| 1377 | return nullptr; |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 1378 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1379 | if (event->getType() != InputEventType::MOTION) { |
| 1380 | ADD_FAILURE() << "Instead of motion event, got " << *event; |
| 1381 | return nullptr; |
| 1382 | } |
| 1383 | std::unique_ptr<MotionEvent> motionEvent = |
| 1384 | std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event.release())); |
| 1385 | EXPECT_THAT(*motionEvent, matcher); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1386 | return motionEvent; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1387 | } |
| 1388 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1389 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1390 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1391 | } |
| 1392 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1393 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1394 | ASSERT_NE(mInputReceiver, nullptr) |
| 1395 | << "Cannot consume events from a window with no receiver"; |
| 1396 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1397 | } |
| 1398 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1399 | std::pair<std::optional<uint32_t>, std::unique_ptr<InputEvent>> receiveEvent() { |
Prabir Pradhan | 9fb98f7 | 2024-02-02 19:47:19 +0000 | [diff] [blame] | 1400 | if (mInputReceiver == nullptr) { |
| 1401 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
| 1402 | return std::make_pair(std::nullopt, nullptr); |
| 1403 | } |
| 1404 | return mInputReceiver->receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | void finishEvent(uint32_t sequenceNum) { |
| 1408 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1409 | mInputReceiver->finishEvent(sequenceNum); |
| 1410 | } |
| 1411 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1412 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1413 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1414 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1415 | } |
| 1416 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame^] | 1417 | void assertNoEvents(std::chrono::milliseconds timeout = CONSUME_TIMEOUT_NO_EVENT_EXPECTED) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1418 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1419 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1420 | return; // Can't receive events if the window does not have input channel |
| 1421 | } |
| 1422 | ASSERT_NE(nullptr, mInputReceiver) |
| 1423 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame^] | 1424 | mInputReceiver->assertNoEvents(timeout); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1425 | } |
| 1426 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1427 | sp<IBinder> getToken() { return mInfo.token; } |
| 1428 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1429 | const std::string& getName() { return mName; } |
| 1430 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 1431 | void setOwnerInfo(gui::Pid ownerPid, gui::Uid ownerUid) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1432 | mInfo.ownerPid = ownerPid; |
| 1433 | mInfo.ownerUid = ownerUid; |
| 1434 | } |
| 1435 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 1436 | gui::Pid getPid() const { return mInfo.ownerPid; } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1437 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1438 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1439 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1440 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1441 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1442 | std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = true) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1443 | if (mInputReceiver == nullptr) { |
| 1444 | LOG(FATAL) << "Cannot consume event from a window with no input event receiver"; |
| 1445 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1446 | std::unique_ptr<InputEvent> event = mInputReceiver->consume(timeout, handled); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1447 | if (event == nullptr) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1448 | ADD_FAILURE() << "Consume failed: no event"; |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1449 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1450 | return event; |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1451 | } |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame^] | 1452 | |
| 1453 | private: |
| 1454 | FakeWindowHandle(std::string name) : mName(name){}; |
| 1455 | const std::string mName; |
| 1456 | std::shared_ptr<FakeInputReceiver> mInputReceiver; |
| 1457 | static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger |
| 1458 | friend class sp<FakeWindowHandle>; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1459 | }; |
| 1460 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1461 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1462 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1463 | class FakeMonitorReceiver { |
| 1464 | public: |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1465 | FakeMonitorReceiver(InputDispatcher& dispatcher, const std::string name, int32_t displayId) |
| 1466 | : mInputReceiver(*dispatcher.createInputMonitor(displayId, name, MONITOR_PID), name) {} |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1467 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1468 | sp<IBinder> getToken() { return mInputReceiver.getToken(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1469 | |
| 1470 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1471 | mInputReceiver.consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1472 | expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | std::optional<int32_t> receiveEvent() { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1476 | const auto [sequenceNum, _] = mInputReceiver.receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED); |
| 1477 | return sequenceNum; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1480 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver.finishEvent(consumeSeq); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1481 | |
| 1482 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1483 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 1484 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1488 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 1489 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1493 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, |
| 1494 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1498 | mInputReceiver.consumeMotionEvent( |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1499 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 1500 | WithDisplayId(expectedDisplayId), |
| 1501 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
| 1502 | } |
| 1503 | |
| 1504 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 1505 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1506 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1507 | mInputReceiver.consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT, |
| 1508 | /*expectedFlags=*/0); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1512 | mInputReceiver.consumeMotionEvent(matcher); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1515 | std::unique_ptr<MotionEvent> consumeMotion() { return mInputReceiver.consumeMotion(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1516 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame^] | 1517 | void assertNoEvents() { mInputReceiver.assertNoEvents(CONSUME_TIMEOUT_NO_EVENT_EXPECTED); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1518 | |
| 1519 | private: |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1520 | FakeInputReceiver mInputReceiver; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1521 | }; |
| 1522 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1523 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1524 | InputDispatcher& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1525 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1526 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1527 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1528 | bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1529 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1530 | KeyEvent event; |
| 1531 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1532 | |
| 1533 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1534 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1535 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 1536 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1537 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1538 | if (!allowKeyRepeat) { |
| 1539 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1540 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1541 | // Inject event until dispatch out. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1542 | return dispatcher.injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1543 | } |
| 1544 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1545 | static void assertInjectedKeyTimesOut(InputDispatcher& dispatcher) { |
| 1546 | InputEventInjectionResult result = |
| 1547 | injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
| 1548 | InputEventInjectionSync::WAIT_FOR_RESULT, CONSUME_TIMEOUT_NO_EVENT_EXPECTED); |
| 1549 | if (result != InputEventInjectionResult::TIMED_OUT) { |
| 1550 | FAIL() << "Injection should have timed out, but got " << ftl::enum_string(result); |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | static InputEventInjectionResult injectKeyDown(InputDispatcher& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1555 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1556 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1557 | } |
| 1558 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1559 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1560 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1561 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1562 | static InputEventInjectionResult injectKeyDownNoRepeat(InputDispatcher& dispatcher, |
| 1563 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1564 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1565 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1566 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1567 | } |
| 1568 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1569 | static InputEventInjectionResult injectKeyUp(InputDispatcher& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1570 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1571 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1572 | } |
| 1573 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1574 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1575 | InputDispatcher& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1576 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1577 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1578 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1579 | return dispatcher.injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1580 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1581 | } |
| 1582 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1583 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1584 | InputDispatcher& dispatcher, int32_t action, int32_t source, int32_t displayId, |
| 1585 | const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1586 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1587 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1588 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1589 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1590 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1591 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1592 | MotionEventBuilder motionBuilder = |
| 1593 | MotionEventBuilder(action, source) |
| 1594 | .displayId(displayId) |
| 1595 | .eventTime(eventTime) |
| 1596 | .rawXCursorPosition(cursorPosition.x) |
| 1597 | .rawYCursorPosition(cursorPosition.y) |
| 1598 | .pointer( |
| 1599 | PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y)); |
| 1600 | if (MotionEvent::getActionMasked(action) == ACTION_DOWN) { |
| 1601 | motionBuilder.downTime(eventTime); |
| 1602 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1603 | |
| 1604 | // Inject event until dispatch out. |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1605 | return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode, |
| 1606 | targetUid, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1607 | } |
| 1608 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1609 | static InputEventInjectionResult injectMotionDown(InputDispatcher& dispatcher, int32_t source, |
| 1610 | int32_t displayId, |
| 1611 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1612 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1615 | static InputEventInjectionResult injectMotionUp(InputDispatcher& dispatcher, int32_t source, |
| 1616 | int32_t displayId, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1617 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1618 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1621 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1622 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1623 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1624 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 1625 | AINPUT_SOURCE_KEYBOARD, displayId, POLICY_FLAG_PASS_TO_USER, action, |
| 1626 | /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1627 | |
| 1628 | return args; |
| 1629 | } |
| 1630 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1631 | static NotifyKeyArgs generateSystemShortcutArgs(int32_t action, |
| 1632 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1633 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1634 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1635 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 1636 | AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_C, KEY_C, |
| 1637 | AMETA_META_ON, currentTime); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1638 | |
| 1639 | return args; |
| 1640 | } |
| 1641 | |
| 1642 | static NotifyKeyArgs generateAssistantKeyArgs(int32_t action, |
| 1643 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1644 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1645 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1646 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 1647 | AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_ASSIST, |
| 1648 | KEY_ASSISTANT, AMETA_NONE, currentTime); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1649 | |
| 1650 | return args; |
| 1651 | } |
| 1652 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1653 | [[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
| 1654 | int32_t displayId, |
| 1655 | const std::vector<PointF>& points) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1656 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1657 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1658 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1659 | } |
| 1660 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1661 | PointerProperties pointerProperties[pointerCount]; |
| 1662 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1663 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1664 | for (size_t i = 0; i < pointerCount; i++) { |
| 1665 | pointerProperties[i].clear(); |
| 1666 | pointerProperties[i].id = i; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1667 | pointerProperties[i].toolType = ToolType::FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1668 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1669 | pointerCoords[i].clear(); |
| 1670 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1671 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1672 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1673 | |
| 1674 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1675 | // Define a valid motion event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1676 | NotifyMotionArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, source, |
| 1677 | displayId, POLICY_FLAG_PASS_TO_USER, action, /*actionButton=*/0, |
| 1678 | /*flags=*/0, AMETA_NONE, /*buttonState=*/0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1679 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1680 | pointerCoords, /*xPrecision=*/0, /*yPrecision=*/0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1681 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1682 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /*videoFrames=*/{}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1683 | |
| 1684 | return args; |
| 1685 | } |
| 1686 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1687 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1688 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1689 | } |
| 1690 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1691 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1692 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1693 | } |
| 1694 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1695 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1696 | const PointerCaptureRequest& request) { |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1697 | return NotifyPointerCaptureChangedArgs(InputEvent::nextId(), systemTime(SYSTEM_TIME_MONOTONIC), |
| 1698 | request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1699 | } |
| 1700 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 1701 | } // namespace |
| 1702 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1703 | /** |
| 1704 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1705 | * broken channel. |
| 1706 | */ |
| 1707 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1708 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1709 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1710 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1711 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1712 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1713 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1714 | |
| 1715 | // Window closes its channel, but the window remains. |
| 1716 | window->destroyReceiver(); |
| 1717 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1718 | } |
| 1719 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1720 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1721 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1722 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1723 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1724 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1725 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1726 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1727 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1728 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1729 | |
| 1730 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1731 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1732 | } |
| 1733 | |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 1734 | using InputDispatcherDeathTest = InputDispatcherTest; |
| 1735 | |
| 1736 | /** |
| 1737 | * When 'onWindowInfosChanged' arguments contain a duplicate entry for the same window, dispatcher |
| 1738 | * should crash. |
| 1739 | */ |
| 1740 | TEST_F(InputDispatcherDeathTest, DuplicateWindowInfosAbortDispatcher) { |
| 1741 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
| 1742 | ScopedSilentDeath _silentDeath; |
| 1743 | |
| 1744 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1745 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1746 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1747 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 1748 | {{*window->getInfo(), *window->getInfo()}, {}, 0, 0}), |
| 1749 | "Incorrect WindowInfosUpdate provided"); |
| 1750 | } |
| 1751 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1752 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1753 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1754 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1755 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1756 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1757 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1758 | // Inject a MotionEvent to an unknown display. |
| 1759 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1760 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1761 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1762 | |
| 1763 | // Window should receive motion event. |
| 1764 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1765 | } |
| 1766 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1767 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1768 | * Calling onWindowInfosChanged once should not cause any issues. |
| 1769 | * 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] | 1770 | * called twice. |
| 1771 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1772 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1773 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1774 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1775 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1776 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1777 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1778 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1779 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1780 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1781 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1782 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1783 | |
| 1784 | // Window should receive motion event. |
| 1785 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1786 | } |
| 1787 | |
| 1788 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1789 | * Calling onWindowInfosChanged twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1790 | */ |
| 1791 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1792 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1793 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1794 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1795 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1796 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1797 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1798 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1799 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1800 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1801 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1802 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1803 | |
| 1804 | // Window should receive motion event. |
| 1805 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1806 | } |
| 1807 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1808 | // The foreground window should receive the first touch down event. |
| 1809 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1810 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1811 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1812 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1813 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1814 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1815 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1816 | mDispatcher->onWindowInfosChanged( |
| 1817 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1818 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1819 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1820 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1821 | |
| 1822 | // 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] | 1823 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1824 | windowSecond->assertNoEvents(); |
| 1825 | } |
| 1826 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1827 | /** |
| 1828 | * Two windows: A top window, and a wallpaper behind the window. |
| 1829 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1830 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1831 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1832 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1833 | */ |
| 1834 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1835 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1836 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1837 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1838 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1839 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1840 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1841 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1842 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1843 | mDispatcher->onWindowInfosChanged( |
| 1844 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1845 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1846 | injectMotionEvent(*mDispatcher, |
| 1847 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1848 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(200)) |
| 1849 | .build())) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1850 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1851 | |
| 1852 | // Both foreground window and its wallpaper should receive the touch down |
| 1853 | foregroundWindow->consumeMotionDown(); |
| 1854 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1855 | |
| 1856 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1857 | injectMotionEvent(*mDispatcher, |
| 1858 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1859 | .pointer(PointerBuilder(0, ToolType::FINGER).x(110).y(200)) |
| 1860 | .build())) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1861 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1862 | |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1863 | foregroundWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1864 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1865 | |
| 1866 | // Now the foreground window goes away, but the wallpaper stays |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1867 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1868 | foregroundWindow->consumeMotionCancel(); |
| 1869 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1870 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1871 | } |
| 1872 | |
| 1873 | /** |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1874 | * Two fingers down on the window, and lift off the first finger. |
| 1875 | * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event |
| 1876 | * contains a single pointer. |
| 1877 | */ |
| 1878 | TEST_F(InputDispatcherTest, CancelAfterPointer0Up) { |
| 1879 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1880 | sp<FakeWindowHandle> window = |
| 1881 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1882 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1883 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1884 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1885 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1886 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1887 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1888 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1889 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1890 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1891 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1892 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1893 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1894 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1895 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1896 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1897 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1898 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 1899 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1900 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 1901 | |
| 1902 | // Remove the window. The gesture should be canceled |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1903 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1904 | const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}}; |
| 1905 | window->consumeMotionEvent( |
| 1906 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers))); |
| 1907 | } |
| 1908 | |
| 1909 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1910 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1911 | * with the following differences: |
| 1912 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1913 | * clean up the connection. |
| 1914 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1915 | * Ensure that there's no crash in the dispatcher. |
| 1916 | */ |
| 1917 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1918 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1919 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1920 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1921 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1922 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1923 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1924 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1925 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1926 | mDispatcher->onWindowInfosChanged( |
| 1927 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1928 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1929 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1930 | {100, 200})) |
| 1931 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1932 | |
| 1933 | // Both foreground window and its wallpaper should receive the touch down |
| 1934 | foregroundWindow->consumeMotionDown(); |
| 1935 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1936 | |
| 1937 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1938 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1939 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1940 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1941 | |
| 1942 | foregroundWindow->consumeMotionMove(); |
| 1943 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1944 | |
| 1945 | // Wallpaper closes its channel, but the window remains. |
| 1946 | wallpaperWindow->destroyReceiver(); |
| 1947 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 1948 | |
| 1949 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 1950 | // is no longer valid. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1951 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1952 | foregroundWindow->consumeMotionCancel(); |
| 1953 | } |
| 1954 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1955 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 1956 | public ::testing::WithParamInterface<bool> {}; |
| 1957 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 1958 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1959 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1960 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1961 | * The top window gets a multitouch gesture. |
| 1962 | * Ensure that wallpaper gets the same gesture. |
| 1963 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1964 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1965 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1966 | sp<FakeWindowHandle> foregroundWindow = |
| 1967 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 1968 | foregroundWindow->setDupTouchToWallpaper(true); |
| 1969 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1970 | |
| 1971 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1972 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1973 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1974 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1975 | mDispatcher->onWindowInfosChanged( |
| 1976 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1977 | |
| 1978 | // Touch down on top window |
| 1979 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1980 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1981 | {100, 100})) |
| 1982 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1983 | |
| 1984 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1985 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1986 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1987 | |
| 1988 | // Second finger down on the top window |
| 1989 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1990 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1991 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1992 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1993 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1994 | .build(); |
| 1995 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1996 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1997 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1998 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1999 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2000 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
| 2001 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2002 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2003 | |
| 2004 | const MotionEvent secondFingerUpEvent = |
| 2005 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 2006 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2007 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2008 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2009 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2010 | .build(); |
| 2011 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2012 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2013 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2014 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2015 | foregroundWindow->consumeMotionPointerUp(0); |
| 2016 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2017 | |
| 2018 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2019 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 2020 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2021 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2022 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2023 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 2024 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 2025 | .x(100) |
| 2026 | .y(100)) |
| 2027 | .build(), |
| 2028 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2029 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2030 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2031 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | /** |
| 2035 | * Two windows: a window on the left and window on the right. |
| 2036 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 2037 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 2038 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 2039 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 2040 | * ACTION_POINTER_DOWN(1). |
| 2041 | */ |
| 2042 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 2043 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2044 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2045 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2046 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2047 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2048 | |
| 2049 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2050 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2051 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2052 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2053 | |
| 2054 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2055 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2056 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2057 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2058 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2059 | mDispatcher->onWindowInfosChanged( |
| 2060 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 2061 | {}, |
| 2062 | 0, |
| 2063 | 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2064 | |
| 2065 | // Touch down on left window |
| 2066 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2067 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2068 | {100, 100})) |
| 2069 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2070 | |
| 2071 | // Both foreground window and its wallpaper should receive the touch down |
| 2072 | leftWindow->consumeMotionDown(); |
| 2073 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2074 | |
| 2075 | // Second finger down on the right window |
| 2076 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2077 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2078 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2079 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2080 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2081 | .build(); |
| 2082 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2083 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2084 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2085 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2086 | |
| 2087 | leftWindow->consumeMotionMove(); |
| 2088 | // Since the touch is split, right window gets ACTION_DOWN |
| 2089 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2090 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2091 | expectedWallpaperFlags); |
| 2092 | |
| 2093 | // Now, leftWindow, which received the first finger, disappears. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2094 | mDispatcher->onWindowInfosChanged( |
| 2095 | {{*rightWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2096 | leftWindow->consumeMotionCancel(); |
| 2097 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 2098 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2099 | |
| 2100 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 2101 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 2102 | const MotionEvent secondFingerMoveEvent = |
| 2103 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2104 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2105 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2106 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2107 | .build(); |
| 2108 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2109 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2110 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 2111 | rightWindow->consumeMotionMove(); |
| 2112 | |
| 2113 | leftWindow->assertNoEvents(); |
| 2114 | rightWindow->assertNoEvents(); |
| 2115 | wallpaperWindow->assertNoEvents(); |
| 2116 | } |
| 2117 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2118 | /** |
| 2119 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 2120 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 2121 | * The right window should receive ACTION_DOWN. |
| 2122 | */ |
| 2123 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2124 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2125 | sp<FakeWindowHandle> leftWindow = |
| 2126 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2127 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2128 | leftWindow->setDupTouchToWallpaper(true); |
| 2129 | leftWindow->setSlippery(true); |
| 2130 | |
| 2131 | sp<FakeWindowHandle> rightWindow = |
| 2132 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2133 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2134 | |
| 2135 | sp<FakeWindowHandle> wallpaperWindow = |
| 2136 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2137 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2138 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2139 | mDispatcher->onWindowInfosChanged( |
| 2140 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 2141 | {}, |
| 2142 | 0, |
| 2143 | 0}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2144 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2145 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2146 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2147 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2148 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2149 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2150 | |
| 2151 | // Both foreground window and its wallpaper should receive the touch down |
| 2152 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2153 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2154 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2155 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2156 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2157 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2158 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2159 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2160 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2161 | leftWindow->consumeMotionCancel(); |
| 2162 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2163 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2166 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2167 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2168 | * interactive, it might stop sending this flag. |
| 2169 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 2170 | * to have a consistent input stream. |
| 2171 | * |
| 2172 | * Test procedure: |
| 2173 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 2174 | * DOWN (new gesture). |
| 2175 | * |
| 2176 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 2177 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 2178 | * |
| 2179 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 2180 | * regular window below) to emulate the actual situation where it happens on the device. |
| 2181 | */ |
| 2182 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 2183 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2184 | sp<FakeWindowHandle> spyWindow = |
| 2185 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2186 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2187 | spyWindow->setTrustedOverlay(true); |
| 2188 | spyWindow->setSpy(true); |
| 2189 | |
| 2190 | sp<FakeWindowHandle> window = |
| 2191 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2192 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2193 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2194 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2195 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2196 | |
| 2197 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2198 | mDispatcher->notifyMotion( |
| 2199 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2200 | .deviceId(touchDeviceId) |
| 2201 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2202 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2203 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2204 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2205 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2206 | .deviceId(touchDeviceId) |
| 2207 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2208 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2209 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2210 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2211 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2212 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2213 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2214 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2215 | |
| 2216 | // Cancel the current gesture. Send the cancel without the default policy flags. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2217 | mDispatcher->notifyMotion( |
| 2218 | MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 2219 | .deviceId(touchDeviceId) |
| 2220 | .policyFlags(0) |
| 2221 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2222 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2223 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2224 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2225 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2226 | |
| 2227 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 2228 | // 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] | 2229 | mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2230 | |
| 2231 | // Start new gesture |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2232 | mDispatcher->notifyMotion( |
| 2233 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2234 | .deviceId(touchDeviceId) |
| 2235 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2236 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2237 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2238 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2239 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2240 | |
| 2241 | // No more events |
| 2242 | spyWindow->assertNoEvents(); |
| 2243 | window->assertNoEvents(); |
| 2244 | } |
| 2245 | |
| 2246 | /** |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 2247 | * Same as the above 'TwoPointerCancelInconsistentPolicy' test, but for hovers. |
| 2248 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2249 | * interactive, it might stop sending this flag. |
| 2250 | * We've already ensured the consistency of the touch event in this case, and we should also ensure |
| 2251 | * the consistency of the hover event in this case. |
| 2252 | * |
| 2253 | * Test procedure: |
| 2254 | * HOVER_ENTER -> HOVER_MOVE -> (stop sending POLICY_FLAG_PASS_TO_USER) -> HOVER_EXIT |
| 2255 | * HOVER_ENTER -> HOVER_MOVE -> HOVER_EXIT |
| 2256 | * |
| 2257 | * We expect to receive two full streams of hover events. |
| 2258 | */ |
| 2259 | TEST_F(InputDispatcherTest, HoverEventInconsistentPolicy) { |
| 2260 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2261 | |
| 2262 | sp<FakeWindowHandle> window = |
| 2263 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2264 | window->setFrame(Rect(0, 0, 300, 300)); |
| 2265 | |
| 2266 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2267 | |
| 2268 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2269 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2270 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2271 | .build()); |
| 2272 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2273 | |
| 2274 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2275 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2276 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2277 | .build()); |
| 2278 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2279 | |
| 2280 | // Send hover exit without the default policy flags. |
| 2281 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2282 | .policyFlags(0) |
| 2283 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2284 | .build()); |
| 2285 | |
| 2286 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2287 | |
| 2288 | // Send a simple hover event stream, ensure dispatcher not crashed and window can receive |
| 2289 | // right event. |
| 2290 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2291 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2292 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(200).y(201)) |
| 2293 | .build()); |
| 2294 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2295 | |
| 2296 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2297 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2298 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 2299 | .build()); |
| 2300 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2301 | |
| 2302 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2303 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2304 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 2305 | .build()); |
| 2306 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2307 | } |
| 2308 | |
| 2309 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2310 | * Two windows: a window on the left and a window on the right. |
| 2311 | * Mouse is hovered from the right window into the left window. |
| 2312 | * Next, we tap on the left window, where the cursor was last seen. |
| 2313 | * The second tap is done onto the right window. |
| 2314 | * The mouse and tap are from two different devices. |
| 2315 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 2316 | * explicitly helps during debugging. |
| 2317 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2318 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 2319 | */ |
| 2320 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 2321 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2322 | sp<FakeWindowHandle> leftWindow = |
| 2323 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2324 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2325 | |
| 2326 | sp<FakeWindowHandle> rightWindow = |
| 2327 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2328 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2329 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2330 | mDispatcher->onWindowInfosChanged( |
| 2331 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2332 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 2333 | // stale. |
| 2334 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2335 | const int32_t mouseDeviceId = 6; |
| 2336 | const int32_t touchDeviceId = 4; |
| 2337 | // Move the cursor from right |
| 2338 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2339 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2340 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2341 | AINPUT_SOURCE_MOUSE) |
| 2342 | .deviceId(mouseDeviceId) |
| 2343 | .downTime(baseTime + 10) |
| 2344 | .eventTime(baseTime + 20) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2345 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2346 | .build())); |
| 2347 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2348 | |
| 2349 | // .. to the left window |
| 2350 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2351 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2352 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2353 | AINPUT_SOURCE_MOUSE) |
| 2354 | .deviceId(mouseDeviceId) |
| 2355 | .downTime(baseTime + 10) |
| 2356 | .eventTime(baseTime + 30) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2357 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2358 | .build())); |
| 2359 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2360 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2361 | // Now tap the left window |
| 2362 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2363 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2364 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2365 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2366 | .deviceId(touchDeviceId) |
| 2367 | .downTime(baseTime + 40) |
| 2368 | .eventTime(baseTime + 40) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2369 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2370 | .build())); |
| 2371 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2372 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2373 | |
| 2374 | // release tap |
| 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_UP, |
| 2378 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2379 | .deviceId(touchDeviceId) |
| 2380 | .downTime(baseTime + 40) |
| 2381 | .eventTime(baseTime + 50) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2382 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2383 | .build())); |
| 2384 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2385 | |
| 2386 | // Tap the window on the right |
| 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_DOWN, |
| 2390 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2391 | .deviceId(touchDeviceId) |
| 2392 | .downTime(baseTime + 60) |
| 2393 | .eventTime(baseTime + 60) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2394 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2395 | .build())); |
| 2396 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2397 | |
| 2398 | // release tap |
| 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_UP, |
| 2402 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2403 | .deviceId(touchDeviceId) |
| 2404 | .downTime(baseTime + 60) |
| 2405 | .eventTime(baseTime + 70) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2406 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2407 | .build())); |
| 2408 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2409 | |
| 2410 | // No more events |
| 2411 | leftWindow->assertNoEvents(); |
| 2412 | rightWindow->assertNoEvents(); |
| 2413 | } |
| 2414 | |
| 2415 | /** |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2416 | * Start hovering in a window. While this hover is still active, make another window appear on top. |
| 2417 | * The top, obstructing window has no input channel, so it's not supposed to receive input. |
| 2418 | * While the top window is present, the hovering is stopped. |
| 2419 | * Later, hovering gets resumed again. |
| 2420 | * Ensure that new hover gesture is handled correctly. |
| 2421 | * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly |
| 2422 | * to the window that's currently being hovered over. |
| 2423 | */ |
| 2424 | TEST_F(InputDispatcherTest, HoverWhileWindowAppears) { |
| 2425 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2426 | sp<FakeWindowHandle> window = |
| 2427 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2428 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2429 | |
| 2430 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2431 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2432 | |
| 2433 | // Start hovering in the window |
| 2434 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2435 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2436 | .build()); |
| 2437 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2438 | |
| 2439 | // Now, an obscuring window appears! |
| 2440 | sp<FakeWindowHandle> obscuringWindow = |
| 2441 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2442 | ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 2443 | /*createInputChannel=*/false); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2444 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2445 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2446 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2447 | obscuringWindow->setNoInputChannel(true); |
| 2448 | obscuringWindow->setFocusable(false); |
| 2449 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2450 | mDispatcher->onWindowInfosChanged( |
| 2451 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2452 | |
| 2453 | // While this new obscuring window is present, the hovering is stopped |
| 2454 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2455 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2456 | .build()); |
| 2457 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2458 | |
| 2459 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2460 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2461 | |
| 2462 | // And a new hover gesture starts. |
| 2463 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2464 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2465 | .build()); |
| 2466 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2467 | } |
| 2468 | |
| 2469 | /** |
| 2470 | * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to |
| 2471 | * the obscuring window. |
| 2472 | */ |
| 2473 | TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) { |
| 2474 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2475 | sp<FakeWindowHandle> window = |
| 2476 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2477 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2478 | |
| 2479 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2480 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2481 | |
| 2482 | // Start hovering in the window |
| 2483 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2484 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2485 | .build()); |
| 2486 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2487 | |
| 2488 | // Now, an obscuring window appears! |
| 2489 | sp<FakeWindowHandle> obscuringWindow = |
| 2490 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2491 | ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 2492 | /*createInputChannel=*/false); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2493 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2494 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2495 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2496 | obscuringWindow->setNoInputChannel(true); |
| 2497 | obscuringWindow->setFocusable(false); |
| 2498 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2499 | mDispatcher->onWindowInfosChanged( |
| 2500 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2501 | |
| 2502 | // While this new obscuring window is present, the hovering continues. The event can't go to the |
| 2503 | // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window. |
| 2504 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2505 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2506 | .build()); |
| 2507 | obscuringWindow->assertNoEvents(); |
| 2508 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2509 | |
| 2510 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2511 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2512 | |
| 2513 | // Hovering continues in the same position. The hovering pointer re-enters the bottom window, |
| 2514 | // so it should generate a HOVER_ENTER |
| 2515 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2516 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2517 | .build()); |
| 2518 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2519 | |
| 2520 | // Now the MOVE should be getting dispatched normally |
| 2521 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2522 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2523 | .build()); |
| 2524 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2525 | } |
| 2526 | |
| 2527 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2528 | * Hover mouse over a window, and then send ACTION_SCROLL. Ensure both the hover and the scroll |
| 2529 | * events are delivered to the window. |
| 2530 | */ |
| 2531 | TEST_F(InputDispatcherTest, HoverMoveAndScroll) { |
| 2532 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2533 | sp<FakeWindowHandle> window = |
| 2534 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2535 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2536 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2537 | |
| 2538 | // Start hovering in the window |
| 2539 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2540 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 2541 | .build()); |
| 2542 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2543 | |
| 2544 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 2545 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2546 | .build()); |
| 2547 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2548 | |
| 2549 | // Scroll with the mouse |
| 2550 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_SCROLL, AINPUT_SOURCE_MOUSE) |
| 2551 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2552 | .build()); |
| 2553 | window->consumeMotionEvent(WithMotionAction(ACTION_SCROLL)); |
| 2554 | } |
| 2555 | |
| 2556 | using InputDispatcherMultiDeviceTest = InputDispatcherTest; |
| 2557 | |
| 2558 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2559 | * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that |
| 2560 | * touch is dropped, because stylus should be preferred over touch. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2561 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2562 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2563 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2564 | sp<FakeWindowHandle> window = |
| 2565 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2566 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2567 | |
| 2568 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2569 | |
| 2570 | constexpr int32_t touchDeviceId = 4; |
| 2571 | constexpr int32_t stylusDeviceId = 2; |
| 2572 | |
| 2573 | // Stylus down |
| 2574 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2575 | .deviceId(stylusDeviceId) |
| 2576 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2577 | .build()); |
| 2578 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2579 | |
| 2580 | // Touch down |
| 2581 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2582 | .deviceId(touchDeviceId) |
| 2583 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2584 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2585 | |
| 2586 | // Touch move |
| 2587 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2588 | .deviceId(touchDeviceId) |
| 2589 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2590 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2591 | // Touch is ignored because stylus is already down |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2592 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2593 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2594 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2595 | .deviceId(stylusDeviceId) |
| 2596 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2597 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2598 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2599 | WithCoords(101, 111))); |
| 2600 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2601 | window->assertNoEvents(); |
| 2602 | } |
| 2603 | |
| 2604 | /** |
| 2605 | * 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] | 2606 | * 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] | 2607 | * Similar test as above, but with added SPY window. |
| 2608 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2609 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2610 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2611 | sp<FakeWindowHandle> window = |
| 2612 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2613 | sp<FakeWindowHandle> spyWindow = |
| 2614 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2615 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2616 | spyWindow->setTrustedOverlay(true); |
| 2617 | spyWindow->setSpy(true); |
| 2618 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2619 | |
| 2620 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 2621 | |
| 2622 | constexpr int32_t touchDeviceId = 4; |
| 2623 | constexpr int32_t stylusDeviceId = 2; |
| 2624 | |
| 2625 | // Stylus down |
| 2626 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2627 | .deviceId(stylusDeviceId) |
| 2628 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2629 | .build()); |
| 2630 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2631 | spyWindow->consumeMotionEvent( |
| 2632 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2633 | |
| 2634 | // Touch down |
| 2635 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2636 | .deviceId(touchDeviceId) |
| 2637 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2638 | .build()); |
| 2639 | |
| 2640 | // Touch move |
| 2641 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2642 | .deviceId(touchDeviceId) |
| 2643 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2644 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2645 | |
| 2646 | // Touch is ignored because stylus is already down |
| 2647 | |
| 2648 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2649 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2650 | .deviceId(stylusDeviceId) |
| 2651 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2652 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2653 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2654 | WithCoords(101, 111))); |
| 2655 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2656 | WithCoords(101, 111))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2657 | |
| 2658 | window->assertNoEvents(); |
| 2659 | spyWindow->assertNoEvents(); |
| 2660 | } |
| 2661 | |
| 2662 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2663 | * 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] | 2664 | * touch is dropped, because stylus hover takes precedence. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2665 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2666 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2667 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2668 | sp<FakeWindowHandle> window = |
| 2669 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2670 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2671 | |
| 2672 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2673 | |
| 2674 | constexpr int32_t touchDeviceId = 4; |
| 2675 | constexpr int32_t stylusDeviceId = 2; |
| 2676 | |
| 2677 | // Stylus down on the window |
| 2678 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2679 | .deviceId(stylusDeviceId) |
| 2680 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2681 | .build()); |
| 2682 | window->consumeMotionEvent( |
| 2683 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2684 | |
| 2685 | // Touch down on window |
| 2686 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2687 | .deviceId(touchDeviceId) |
| 2688 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2689 | .build()); |
| 2690 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2691 | .deviceId(touchDeviceId) |
| 2692 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2693 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2694 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2695 | // Touch is ignored because stylus is hovering |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2696 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2697 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2698 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2699 | .deviceId(stylusDeviceId) |
| 2700 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2701 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2702 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2703 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2704 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2705 | // and subsequent touches continue to be ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2706 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2707 | .deviceId(touchDeviceId) |
| 2708 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2709 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2710 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2711 | } |
| 2712 | |
| 2713 | /** |
| 2714 | * 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] | 2715 | * Ensure that touch is canceled, because stylus hover should take precedence. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2716 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2717 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusHover) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2718 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2719 | sp<FakeWindowHandle> window = |
| 2720 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2721 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2722 | |
| 2723 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2724 | |
| 2725 | constexpr int32_t touchDeviceId = 4; |
| 2726 | constexpr int32_t stylusDeviceId = 2; |
| 2727 | |
| 2728 | // Touch down on window |
| 2729 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2730 | .deviceId(touchDeviceId) |
| 2731 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2732 | .build()); |
| 2733 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2734 | .deviceId(touchDeviceId) |
| 2735 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2736 | .build()); |
| 2737 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2738 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2739 | |
| 2740 | // Stylus hover on the window |
| 2741 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2742 | .deviceId(stylusDeviceId) |
| 2743 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2744 | .build()); |
| 2745 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2746 | .deviceId(stylusDeviceId) |
| 2747 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2748 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2749 | // Stylus hover movement causes touch to be canceled |
| 2750 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 2751 | WithCoords(141, 146))); |
| 2752 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 2753 | WithDeviceId(stylusDeviceId), WithCoords(100, 110))); |
| 2754 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2755 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2756 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2757 | // Subsequent touch movements are ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2758 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2759 | .deviceId(touchDeviceId) |
| 2760 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2761 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2762 | |
| 2763 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2764 | } |
| 2765 | |
| 2766 | /** |
| 2767 | * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that |
| 2768 | * the latest stylus takes over. That is, old stylus should be canceled and the new stylus should |
| 2769 | * become active. |
| 2770 | */ |
| 2771 | TEST_F(InputDispatcherMultiDeviceTest, LatestStylusWins) { |
| 2772 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2773 | sp<FakeWindowHandle> window = |
| 2774 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2775 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2776 | |
| 2777 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2778 | |
| 2779 | constexpr int32_t stylusDeviceId1 = 3; |
| 2780 | constexpr int32_t stylusDeviceId2 = 5; |
| 2781 | |
| 2782 | // Touch down on window |
| 2783 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2784 | .deviceId(stylusDeviceId1) |
| 2785 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100)) |
| 2786 | .build()); |
| 2787 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2788 | .deviceId(stylusDeviceId1) |
| 2789 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2790 | .build()); |
| 2791 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1))); |
| 2792 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2793 | |
| 2794 | // Second stylus down |
| 2795 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2796 | .deviceId(stylusDeviceId2) |
| 2797 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10)) |
| 2798 | .build()); |
| 2799 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2800 | .deviceId(stylusDeviceId2) |
| 2801 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11)) |
| 2802 | .build()); |
| 2803 | |
| 2804 | // First stylus is canceled, second one takes over. |
| 2805 | window->consumeMotionEvent( |
| 2806 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId1))); |
| 2807 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2))); |
| 2808 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2))); |
| 2809 | |
| 2810 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2811 | .deviceId(stylusDeviceId1) |
| 2812 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2813 | .build()); |
| 2814 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2815 | window->assertNoEvents(); |
| 2816 | } |
| 2817 | |
| 2818 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2819 | * One window. Touch down on the window. Then, stylus down on the window from another device. |
| 2820 | * Ensure that is canceled, because stylus down should be preferred over touch. |
| 2821 | */ |
| 2822 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusDown) { |
| 2823 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2824 | sp<FakeWindowHandle> window = |
| 2825 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2826 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2827 | |
| 2828 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2829 | |
| 2830 | constexpr int32_t touchDeviceId = 4; |
| 2831 | constexpr int32_t stylusDeviceId = 2; |
| 2832 | |
| 2833 | // Touch down on window |
| 2834 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2835 | .deviceId(touchDeviceId) |
| 2836 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2837 | .build()); |
| 2838 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2839 | .deviceId(touchDeviceId) |
| 2840 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2841 | .build()); |
| 2842 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2843 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2844 | |
| 2845 | // Stylus down on the window |
| 2846 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2847 | .deviceId(stylusDeviceId) |
| 2848 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2849 | .build()); |
| 2850 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 2851 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2852 | |
| 2853 | // Subsequent stylus movements are delivered correctly |
| 2854 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2855 | .deviceId(stylusDeviceId) |
| 2856 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2857 | .build()); |
| 2858 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2859 | WithCoords(101, 111))); |
| 2860 | } |
| 2861 | |
| 2862 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2863 | * Two windows: a window on the left and a window on the right. |
| 2864 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2865 | * down. Then, on the left window, also place second touch pointer down. |
| 2866 | * This test tries to reproduce a crash. |
| 2867 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2868 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2869 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2870 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2871 | sp<FakeWindowHandle> leftWindow = |
| 2872 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2873 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2874 | |
| 2875 | sp<FakeWindowHandle> rightWindow = |
| 2876 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2877 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2878 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2879 | mDispatcher->onWindowInfosChanged( |
| 2880 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2881 | |
| 2882 | const int32_t touchDeviceId = 4; |
| 2883 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2884 | |
| 2885 | // Start hovering over the left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2886 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2887 | .deviceId(mouseDeviceId) |
| 2888 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2889 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2890 | leftWindow->consumeMotionEvent( |
| 2891 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2892 | |
| 2893 | // Mouse down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2894 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2895 | .deviceId(mouseDeviceId) |
| 2896 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2897 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2898 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2899 | |
| 2900 | leftWindow->consumeMotionEvent( |
| 2901 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2902 | leftWindow->consumeMotionEvent( |
| 2903 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2904 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2905 | mDispatcher->notifyMotion( |
| 2906 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2907 | .deviceId(mouseDeviceId) |
| 2908 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2909 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2910 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2911 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2912 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2913 | |
| 2914 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2915 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2916 | .deviceId(touchDeviceId) |
| 2917 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2918 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2919 | leftWindow->assertNoEvents(); |
| 2920 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2921 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2922 | |
| 2923 | // Second touch pointer down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2924 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2925 | .deviceId(touchDeviceId) |
| 2926 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2927 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2928 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2929 | // Since this is now a new splittable pointer going down on the left window, and it's coming |
| 2930 | // from a different device, the current gesture in the left window (pointer down) should first |
| 2931 | // be canceled. |
| 2932 | leftWindow->consumeMotionEvent( |
| 2933 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2934 | leftWindow->consumeMotionEvent( |
| 2935 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2936 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2937 | // current implementation. |
| 2938 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2939 | rightWindow->consumeMotionEvent( |
| 2940 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2941 | |
| 2942 | leftWindow->assertNoEvents(); |
| 2943 | rightWindow->assertNoEvents(); |
| 2944 | } |
| 2945 | |
| 2946 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2947 | * Two windows: a window on the left and a window on the right. |
| 2948 | * Mouse is hovered on the left window and stylus is hovered on the right window. |
| 2949 | */ |
| 2950 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHover) { |
| 2951 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2952 | sp<FakeWindowHandle> leftWindow = |
| 2953 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2954 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2955 | |
| 2956 | sp<FakeWindowHandle> rightWindow = |
| 2957 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2958 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2959 | |
| 2960 | mDispatcher->onWindowInfosChanged( |
| 2961 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2962 | |
| 2963 | const int32_t stylusDeviceId = 3; |
| 2964 | const int32_t mouseDeviceId = 6; |
| 2965 | |
| 2966 | // Start hovering over the left window |
| 2967 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2968 | .deviceId(mouseDeviceId) |
| 2969 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 2970 | .build()); |
| 2971 | leftWindow->consumeMotionEvent( |
| 2972 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2973 | |
| 2974 | // Stylus hovered on right window |
| 2975 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2976 | .deviceId(stylusDeviceId) |
| 2977 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(300).y(100)) |
| 2978 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2979 | rightWindow->consumeMotionEvent( |
| 2980 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2981 | |
| 2982 | // Subsequent HOVER_MOVE events are dispatched correctly. |
| 2983 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 2984 | .deviceId(mouseDeviceId) |
| 2985 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2986 | .build()); |
| 2987 | leftWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2988 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2989 | |
| 2990 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2991 | .deviceId(stylusDeviceId) |
| 2992 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(310).y(110)) |
| 2993 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2994 | rightWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2995 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2996 | |
| 2997 | leftWindow->assertNoEvents(); |
| 2998 | rightWindow->assertNoEvents(); |
| 2999 | } |
| 3000 | |
| 3001 | /** |
| 3002 | * Three windows: a window on the left and a window on the right. |
| 3003 | * And a spy window that's positioned above all of them. |
| 3004 | * Stylus down on the left window and remains down. Touch goes down on the right and remains down. |
| 3005 | * Check the stream that's received by the spy. |
| 3006 | */ |
| 3007 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy) { |
| 3008 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3009 | |
| 3010 | sp<FakeWindowHandle> spyWindow = |
| 3011 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3012 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 3013 | spyWindow->setTrustedOverlay(true); |
| 3014 | spyWindow->setSpy(true); |
| 3015 | |
| 3016 | sp<FakeWindowHandle> leftWindow = |
| 3017 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 3018 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3019 | |
| 3020 | sp<FakeWindowHandle> rightWindow = |
| 3021 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 3022 | |
| 3023 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3024 | |
| 3025 | mDispatcher->onWindowInfosChanged( |
| 3026 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3027 | |
| 3028 | const int32_t stylusDeviceId = 1; |
| 3029 | const int32_t touchDeviceId = 2; |
| 3030 | |
| 3031 | // Stylus down on the left window |
| 3032 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3033 | .deviceId(stylusDeviceId) |
| 3034 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 3035 | .build()); |
| 3036 | leftWindow->consumeMotionEvent( |
| 3037 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 3038 | spyWindow->consumeMotionEvent( |
| 3039 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 3040 | |
| 3041 | // Touch down on the right window |
| 3042 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3043 | .deviceId(touchDeviceId) |
| 3044 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3045 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3046 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3047 | rightWindow->consumeMotionEvent( |
| 3048 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3049 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3050 | // Spy window does not receive touch events, because stylus events take precedence, and it |
| 3051 | // already has an active stylus gesture. |
| 3052 | |
| 3053 | // 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] | 3054 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 3055 | .deviceId(stylusDeviceId) |
| 3056 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 3057 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3058 | leftWindow->consumeMotionEvent( |
| 3059 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 3060 | spyWindow->consumeMotionEvent( |
| 3061 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3062 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3063 | // Further MOVE events keep going to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3064 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3065 | .deviceId(touchDeviceId) |
| 3066 | .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110)) |
| 3067 | .build()); |
| 3068 | rightWindow->consumeMotionEvent( |
| 3069 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3070 | |
| 3071 | spyWindow->assertNoEvents(); |
| 3072 | leftWindow->assertNoEvents(); |
| 3073 | rightWindow->assertNoEvents(); |
| 3074 | } |
| 3075 | |
| 3076 | /** |
| 3077 | * Three windows: a window on the left, a window on the right, and a spy window positioned above |
| 3078 | * both. |
| 3079 | * Check hover in left window and touch down in the right window. |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3080 | * 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] | 3081 | * At the same time, left and right should be getting independent streams of hovering and touch, |
| 3082 | * respectively. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3083 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3084 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverBlocksTouchWithSpy) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3085 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3086 | |
| 3087 | sp<FakeWindowHandle> spyWindow = |
| 3088 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3089 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 3090 | spyWindow->setTrustedOverlay(true); |
| 3091 | spyWindow->setSpy(true); |
| 3092 | |
| 3093 | sp<FakeWindowHandle> leftWindow = |
| 3094 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 3095 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3096 | |
| 3097 | sp<FakeWindowHandle> rightWindow = |
| 3098 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 3099 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3100 | |
| 3101 | mDispatcher->onWindowInfosChanged( |
| 3102 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3103 | |
| 3104 | const int32_t stylusDeviceId = 1; |
| 3105 | const int32_t touchDeviceId = 2; |
| 3106 | |
| 3107 | // Stylus hover on the left window |
| 3108 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3109 | .deviceId(stylusDeviceId) |
| 3110 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 3111 | .build()); |
| 3112 | leftWindow->consumeMotionEvent( |
| 3113 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3114 | spyWindow->consumeMotionEvent( |
| 3115 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3116 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3117 | // Touch down on the right window. Spy doesn't receive this touch because it already has |
| 3118 | // stylus hovering there. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3119 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3120 | .deviceId(touchDeviceId) |
| 3121 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3122 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3123 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3124 | spyWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3125 | rightWindow->consumeMotionEvent( |
| 3126 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3127 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3128 | // 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] | 3129 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3130 | .deviceId(stylusDeviceId) |
| 3131 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 3132 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3133 | leftWindow->consumeMotionEvent( |
| 3134 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3135 | spyWindow->consumeMotionEvent( |
| 3136 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3137 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3138 | // Touch movements continue. They should be delivered to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3139 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3140 | .deviceId(touchDeviceId) |
| 3141 | .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101)) |
| 3142 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3143 | rightWindow->consumeMotionEvent( |
| 3144 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3145 | |
| 3146 | spyWindow->assertNoEvents(); |
| 3147 | leftWindow->assertNoEvents(); |
| 3148 | rightWindow->assertNoEvents(); |
| 3149 | } |
| 3150 | |
| 3151 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3152 | * On a single window, use two different devices: mouse and touch. |
| 3153 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 3154 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 3155 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 3156 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 3157 | * represent a new gesture. |
| 3158 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3159 | TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3160 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3161 | sp<FakeWindowHandle> window = |
| 3162 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3163 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3164 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3165 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3166 | |
| 3167 | const int32_t touchDeviceId = 4; |
| 3168 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3169 | |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3170 | // First touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3171 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3172 | .deviceId(touchDeviceId) |
| 3173 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3174 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3175 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3176 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3177 | .deviceId(touchDeviceId) |
| 3178 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3179 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3180 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3181 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3182 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3183 | .deviceId(touchDeviceId) |
| 3184 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3185 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3186 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3187 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3188 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3189 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 3190 | |
| 3191 | // Mouse down. The touch should be canceled |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3192 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3193 | .deviceId(mouseDeviceId) |
| 3194 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3195 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3196 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3197 | |
| 3198 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 3199 | WithPointerCount(1u))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3200 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3201 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3202 | mDispatcher->notifyMotion( |
| 3203 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3204 | .deviceId(mouseDeviceId) |
| 3205 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3206 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3207 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3208 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3209 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3210 | |
| 3211 | // Second touch pointer down. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3212 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3213 | .deviceId(touchDeviceId) |
| 3214 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3215 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3216 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3217 | // Since we already canceled this touch gesture, it will be ignored until a completely new |
| 3218 | // gesture is started. This is easier to implement than trying to keep track of the new pointer |
| 3219 | // and generating an ACTION_DOWN instead of ACTION_POINTER_DOWN. |
| 3220 | // However, mouse movements should continue to work. |
| 3221 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3222 | .deviceId(mouseDeviceId) |
| 3223 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3224 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110)) |
| 3225 | .build()); |
| 3226 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 3227 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3228 | window->assertNoEvents(); |
| 3229 | } |
| 3230 | |
| 3231 | /** |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3232 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels |
| 3233 | * the injected event. |
| 3234 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3235 | TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent) { |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3236 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3237 | sp<FakeWindowHandle> window = |
| 3238 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3239 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3240 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3241 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3242 | |
| 3243 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3244 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 3245 | // completion. |
| 3246 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3247 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3248 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3249 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3250 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3251 | .build())); |
| 3252 | window->consumeMotionEvent( |
| 3253 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3254 | |
| 3255 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer |
| 3256 | // should be canceled and the new gesture should take over. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3257 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3258 | .deviceId(touchDeviceId) |
| 3259 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3260 | .build()); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3261 | |
| 3262 | window->consumeMotionEvent( |
| 3263 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3264 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3265 | } |
| 3266 | |
| 3267 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3268 | * This test is similar to the test above, but the sequence of injected events is different. |
| 3269 | * |
| 3270 | * Two windows: a window on the left and a window on the right. |
| 3271 | * Mouse is hovered over the left window. |
| 3272 | * Next, we tap on the left window, where the cursor was last seen. |
| 3273 | * |
| 3274 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 3275 | * the left window. |
| 3276 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 3277 | * window (first), and then another on the left window (second). |
| 3278 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 3279 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 3280 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3281 | TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch) { |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3282 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3283 | sp<FakeWindowHandle> leftWindow = |
| 3284 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 3285 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3286 | |
| 3287 | sp<FakeWindowHandle> rightWindow = |
| 3288 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 3289 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3290 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3291 | mDispatcher->onWindowInfosChanged( |
| 3292 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3293 | |
| 3294 | const int32_t mouseDeviceId = 6; |
| 3295 | const int32_t touchDeviceId = 4; |
| 3296 | // Hover over the left window. Keep the cursor there. |
| 3297 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3298 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3299 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3300 | AINPUT_SOURCE_MOUSE) |
| 3301 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3302 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3303 | .build())); |
| 3304 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3305 | |
| 3306 | // Tap on left window |
| 3307 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3308 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3309 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3310 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3311 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3312 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3313 | .build())); |
| 3314 | |
| 3315 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3316 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3317 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3318 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3319 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3320 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3321 | .build())); |
| 3322 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3323 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3324 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 3325 | |
| 3326 | // First finger down on right window |
| 3327 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3328 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3329 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3330 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3331 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3332 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3333 | .build())); |
| 3334 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3335 | |
| 3336 | // Second finger down on the left window |
| 3337 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3338 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3339 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3340 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3341 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3342 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3343 | .build())); |
| 3344 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3345 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 3346 | |
| 3347 | // No more events |
| 3348 | leftWindow->assertNoEvents(); |
| 3349 | rightWindow->assertNoEvents(); |
| 3350 | } |
| 3351 | |
| 3352 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3353 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 3354 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 3355 | * touch is gone, stylus hovering should start working again. |
| 3356 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3357 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverIgnoresTouchTap) { |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3358 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3359 | sp<FakeWindowHandle> window = |
| 3360 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3361 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3362 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3363 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3364 | |
| 3365 | const int32_t stylusDeviceId = 5; |
| 3366 | const int32_t touchDeviceId = 4; |
| 3367 | // Start hovering with stylus |
| 3368 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3369 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3370 | MotionEventBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3371 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3372 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3373 | .build())); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3374 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3375 | |
| 3376 | // Finger down on the window |
| 3377 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3378 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3379 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3380 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3381 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3382 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3383 | // The touch device should be ignored! |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3384 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3385 | // Continue hovering with stylus. |
| 3386 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3387 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3388 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3389 | AINPUT_SOURCE_STYLUS) |
| 3390 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3391 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3392 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3393 | // Hovers continue to work |
| 3394 | window->consumeMotionEvent( |
| 3395 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3396 | |
| 3397 | // Lift up the finger |
| 3398 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3399 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3400 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3401 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3402 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3403 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3404 | .build())); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3405 | |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3406 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3407 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3408 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3409 | AINPUT_SOURCE_STYLUS) |
| 3410 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3411 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3412 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3413 | window->consumeMotionEvent( |
| 3414 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3415 | window->assertNoEvents(); |
| 3416 | } |
| 3417 | |
| 3418 | /** |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3419 | * If stylus is down anywhere on the screen, then touches should not be delivered to windows that |
| 3420 | * have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 3421 | * |
| 3422 | * Two windows: one on the left and one on the right. |
| 3423 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 3424 | * Stylus down on the left window, and then touch down on the right window. |
| 3425 | * Check that the right window doesn't get touches while the stylus is down on the left window. |
| 3426 | */ |
| 3427 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusDownBlocksTouch) { |
| 3428 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3429 | sp<FakeWindowHandle> leftWindow = |
| 3430 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
| 3431 | ADISPLAY_ID_DEFAULT); |
| 3432 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3433 | |
| 3434 | sp<FakeWindowHandle> sbtRightWindow = |
| 3435 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3436 | "Stylus blocks touch (right) window", ADISPLAY_ID_DEFAULT); |
| 3437 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3438 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 3439 | |
| 3440 | mDispatcher->onWindowInfosChanged( |
| 3441 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 3442 | |
| 3443 | const int32_t stylusDeviceId = 5; |
| 3444 | const int32_t touchDeviceId = 4; |
| 3445 | |
| 3446 | // Stylus down in the left window |
| 3447 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3448 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 3449 | .deviceId(stylusDeviceId) |
| 3450 | .build()); |
| 3451 | leftWindow->consumeMotionEvent( |
| 3452 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 3453 | |
| 3454 | // Finger tap on the right window |
| 3455 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3456 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3457 | .deviceId(touchDeviceId) |
| 3458 | .build()); |
| 3459 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3460 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3461 | .deviceId(touchDeviceId) |
| 3462 | .build()); |
| 3463 | |
| 3464 | // The touch should be blocked, because stylus is down somewhere else on screen! |
| 3465 | sbtRightWindow->assertNoEvents(); |
| 3466 | |
| 3467 | // Continue stylus motion, and ensure it's not impacted. |
| 3468 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 3469 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3470 | .deviceId(stylusDeviceId) |
| 3471 | .build()); |
| 3472 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3473 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3474 | .deviceId(stylusDeviceId) |
| 3475 | .build()); |
| 3476 | leftWindow->consumeMotionEvent( |
| 3477 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 3478 | leftWindow->consumeMotionEvent( |
| 3479 | AllOf(WithMotionAction(ACTION_UP), WithDeviceId(stylusDeviceId))); |
| 3480 | |
| 3481 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3482 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3483 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3484 | .deviceId(touchDeviceId) |
| 3485 | .build()); |
| 3486 | sbtRightWindow->consumeMotionEvent( |
| 3487 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3488 | } |
| 3489 | |
| 3490 | /** |
| 3491 | * If stylus is hovering anywhere on the screen, then touches should not be delivered to windows |
| 3492 | * that have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 3493 | * |
| 3494 | * Two windows: one on the left and one on the right. |
| 3495 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 3496 | * Stylus hover on the left window, and then touch down on the right window. |
| 3497 | * Check that the right window doesn't get touches while the stylus is hovering on the left window. |
| 3498 | */ |
| 3499 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusHoverBlocksTouch) { |
| 3500 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3501 | sp<FakeWindowHandle> leftWindow = |
| 3502 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
| 3503 | ADISPLAY_ID_DEFAULT); |
| 3504 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3505 | |
| 3506 | sp<FakeWindowHandle> sbtRightWindow = |
| 3507 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3508 | "Stylus blocks touch (right) window", ADISPLAY_ID_DEFAULT); |
| 3509 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3510 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 3511 | |
| 3512 | mDispatcher->onWindowInfosChanged( |
| 3513 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 3514 | |
| 3515 | const int32_t stylusDeviceId = 5; |
| 3516 | const int32_t touchDeviceId = 4; |
| 3517 | |
| 3518 | // Stylus hover in the left window |
| 3519 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3520 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 3521 | .deviceId(stylusDeviceId) |
| 3522 | .build()); |
| 3523 | leftWindow->consumeMotionEvent( |
| 3524 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3525 | |
| 3526 | // Finger tap on the right window |
| 3527 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3528 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3529 | .deviceId(touchDeviceId) |
| 3530 | .build()); |
| 3531 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3532 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3533 | .deviceId(touchDeviceId) |
| 3534 | .build()); |
| 3535 | |
| 3536 | // The touch should be blocked, because stylus is hovering somewhere else on screen! |
| 3537 | sbtRightWindow->assertNoEvents(); |
| 3538 | |
| 3539 | // Continue stylus motion, and ensure it's not impacted. |
| 3540 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3541 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3542 | .deviceId(stylusDeviceId) |
| 3543 | .build()); |
| 3544 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3545 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3546 | .deviceId(stylusDeviceId) |
| 3547 | .build()); |
| 3548 | leftWindow->consumeMotionEvent( |
| 3549 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3550 | leftWindow->consumeMotionEvent( |
| 3551 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId))); |
| 3552 | |
| 3553 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3554 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3555 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3556 | .deviceId(touchDeviceId) |
| 3557 | .build()); |
| 3558 | sbtRightWindow->consumeMotionEvent( |
| 3559 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3560 | } |
| 3561 | |
| 3562 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3563 | * A spy window above a window with no input channel. |
| 3564 | * Start hovering with a stylus device, and then tap with it. |
| 3565 | * Ensure spy window receives the entire sequence. |
| 3566 | */ |
| 3567 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 3568 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3569 | sp<FakeWindowHandle> spyWindow = |
| 3570 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3571 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3572 | spyWindow->setTrustedOverlay(true); |
| 3573 | spyWindow->setSpy(true); |
| 3574 | sp<FakeWindowHandle> window = |
| 3575 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3576 | window->setNoInputChannel(true); |
| 3577 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3578 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3579 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3580 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3581 | // Start hovering with stylus |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3582 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3583 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3584 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3585 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3586 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3587 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3588 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3589 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3590 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3591 | |
| 3592 | // Stylus touches down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3593 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3594 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3595 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3596 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3597 | |
| 3598 | // Stylus goes up |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3599 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3600 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3601 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3602 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 3603 | |
| 3604 | // Again hover |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3605 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3606 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3607 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3608 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3609 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3610 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3611 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3612 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3613 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3614 | |
| 3615 | // No more events |
| 3616 | spyWindow->assertNoEvents(); |
| 3617 | window->assertNoEvents(); |
| 3618 | } |
| 3619 | |
| 3620 | /** |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 3621 | * A stale stylus HOVER_EXIT event is injected. Since it's a stale event, it should generally be |
| 3622 | * rejected. But since we already have an ongoing gesture, this event should be processed. |
| 3623 | * This prevents inconsistent events being handled inside the dispatcher. |
| 3624 | */ |
| 3625 | TEST_F(InputDispatcherTest, StaleStylusHoverGestureIsComplete) { |
| 3626 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3627 | |
| 3628 | sp<FakeWindowHandle> window = |
| 3629 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3630 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3631 | |
| 3632 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3633 | |
| 3634 | // Start hovering with stylus |
| 3635 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3636 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3637 | .build()); |
| 3638 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3639 | |
| 3640 | NotifyMotionArgs hoverExit = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3641 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3642 | .build(); |
| 3643 | // Make this 'hoverExit' event stale |
| 3644 | mFakePolicy->setStaleEventTimeout(100ms); |
| 3645 | std::this_thread::sleep_for(100ms); |
| 3646 | |
| 3647 | // It shouldn't be dropped by the dispatcher, even though it's stale. |
| 3648 | mDispatcher->notifyMotion(hoverExit); |
| 3649 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3650 | |
| 3651 | // Stylus starts hovering again! There should be no crash. |
| 3652 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3653 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(51)) |
| 3654 | .build()); |
| 3655 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3656 | } |
| 3657 | |
| 3658 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3659 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 3660 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 3661 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 3662 | * While the mouse is down, new move events from the touch device should be ignored. |
| 3663 | */ |
| 3664 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 3665 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3666 | sp<FakeWindowHandle> spyWindow = |
| 3667 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3668 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3669 | spyWindow->setTrustedOverlay(true); |
| 3670 | spyWindow->setSpy(true); |
| 3671 | sp<FakeWindowHandle> window = |
| 3672 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3673 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3674 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3675 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3676 | |
| 3677 | const int32_t mouseDeviceId = 7; |
| 3678 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3679 | |
| 3680 | // Hover a bit with mouse first |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3681 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3682 | .deviceId(mouseDeviceId) |
| 3683 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3684 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3685 | spyWindow->consumeMotionEvent( |
| 3686 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3687 | window->consumeMotionEvent( |
| 3688 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3689 | |
| 3690 | // Start touching |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3691 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3692 | .deviceId(touchDeviceId) |
| 3693 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3694 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3695 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3696 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3697 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3698 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3699 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3700 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3701 | .deviceId(touchDeviceId) |
| 3702 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 3703 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3704 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3705 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3706 | |
| 3707 | // Pilfer the stream |
| 3708 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3709 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 3710 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3711 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3712 | .deviceId(touchDeviceId) |
| 3713 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 3714 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3715 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3716 | |
| 3717 | // Mouse down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3718 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3719 | .deviceId(mouseDeviceId) |
| 3720 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3721 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3722 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3723 | |
| 3724 | spyWindow->consumeMotionEvent( |
| 3725 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 3726 | spyWindow->consumeMotionEvent( |
| 3727 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3728 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3729 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3730 | mDispatcher->notifyMotion( |
| 3731 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3732 | .deviceId(mouseDeviceId) |
| 3733 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3734 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3735 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3736 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3737 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3738 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3739 | |
| 3740 | // Mouse move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3741 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3742 | .deviceId(mouseDeviceId) |
| 3743 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3744 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 3745 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3746 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3747 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3748 | |
| 3749 | // Touch move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3750 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3751 | .deviceId(touchDeviceId) |
| 3752 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 3753 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3754 | |
| 3755 | // No more events |
| 3756 | spyWindow->assertNoEvents(); |
| 3757 | window->assertNoEvents(); |
| 3758 | } |
| 3759 | |
| 3760 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3761 | * On the display, have a single window, and also an area where there's no window. |
| 3762 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 3763 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 3764 | */ |
| 3765 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 3766 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3767 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3768 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3769 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3770 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3771 | |
| 3772 | // Touch down on the empty space |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3773 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3774 | |
| 3775 | mDispatcher->waitForIdle(); |
| 3776 | window->assertNoEvents(); |
| 3777 | |
| 3778 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3779 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3780 | mDispatcher->waitForIdle(); |
| 3781 | window->consumeMotionDown(); |
| 3782 | } |
| 3783 | |
| 3784 | /** |
| 3785 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 3786 | * non-touchable window. |
| 3787 | */ |
| 3788 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 3789 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3790 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3791 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3792 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3793 | window1->setTouchable(false); |
| 3794 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3795 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3796 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3797 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3798 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3799 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3800 | // Touch down on the non-touchable window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3801 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3802 | |
| 3803 | mDispatcher->waitForIdle(); |
| 3804 | window1->assertNoEvents(); |
| 3805 | window2->assertNoEvents(); |
| 3806 | |
| 3807 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3808 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3809 | mDispatcher->waitForIdle(); |
| 3810 | window2->consumeMotionDown(); |
| 3811 | } |
| 3812 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3813 | /** |
| 3814 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 3815 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 3816 | */ |
| 3817 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 3818 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3819 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3820 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3821 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3822 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3823 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3824 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3825 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3826 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3827 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3828 | // Touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3829 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3830 | |
| 3831 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3832 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 3833 | std::unique_ptr<MotionEvent> motionEvent1 = window1->consumeMotionEvent(); |
| 3834 | ASSERT_NE(nullptr, motionEvent1); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3835 | window2->assertNoEvents(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 3836 | nsecs_t downTimeForWindow1 = motionEvent1->getDownTime(); |
| 3837 | ASSERT_EQ(motionEvent1->getDownTime(), motionEvent1->getEventTime()); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3838 | |
| 3839 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3840 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3841 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 3842 | std::unique_ptr<MotionEvent> motionEvent2 = window2->consumeMotionEvent(); |
| 3843 | ASSERT_NE(nullptr, motionEvent2); |
| 3844 | nsecs_t downTimeForWindow2 = motionEvent2->getDownTime(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3845 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 3846 | ASSERT_EQ(motionEvent2->getDownTime(), motionEvent2->getEventTime()); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3847 | |
| 3848 | // Now move the pointer on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3849 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3850 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3851 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3852 | |
| 3853 | // Now add new touch down on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3854 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3855 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3856 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3857 | |
| 3858 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 3859 | window1->consumeMotionMove(); |
| 3860 | window1->assertNoEvents(); |
| 3861 | |
| 3862 | // Now move the pointer on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3863 | mDispatcher->notifyMotion( |
| 3864 | generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3865 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3866 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3867 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3868 | mDispatcher->notifyMotion( |
| 3869 | generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3870 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3871 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3872 | } |
| 3873 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3874 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3875 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3876 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3877 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3878 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3879 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3880 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3881 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3882 | |
| 3883 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3884 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3885 | mDispatcher->onWindowInfosChanged( |
| 3886 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3887 | |
| 3888 | // 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] | 3889 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3890 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3891 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3892 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3893 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3894 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3895 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3896 | |
| 3897 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3898 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3899 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3900 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3901 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3902 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3903 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3904 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3905 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3906 | |
| 3907 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3908 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3909 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3910 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3911 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3912 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3913 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3914 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3915 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3916 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3917 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3918 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3919 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3920 | AINPUT_SOURCE_MOUSE) |
| 3921 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3922 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3923 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3924 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3925 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3926 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3927 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3928 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3929 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3930 | AINPUT_SOURCE_MOUSE) |
| 3931 | .buttonState(0) |
| 3932 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3933 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3934 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3935 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3936 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3937 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3938 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3939 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3940 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3941 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3942 | .build())); |
| 3943 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3944 | |
| 3945 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3946 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3947 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3948 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3949 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3950 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3951 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3952 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3953 | |
| 3954 | // No more events |
| 3955 | windowLeft->assertNoEvents(); |
| 3956 | windowRight->assertNoEvents(); |
| 3957 | } |
| 3958 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3959 | /** |
| 3960 | * Put two fingers down (and don't release them) and click the mouse button. |
| 3961 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 3962 | * currently active gesture should be canceled, and the new one should proceed. |
| 3963 | */ |
| 3964 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 3965 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3966 | sp<FakeWindowHandle> window = |
| 3967 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3968 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3969 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3970 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3971 | |
| 3972 | const int32_t touchDeviceId = 4; |
| 3973 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3974 | |
| 3975 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3976 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3977 | .deviceId(touchDeviceId) |
| 3978 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3979 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3980 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3981 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3982 | .deviceId(touchDeviceId) |
| 3983 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3984 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 3985 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3986 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3987 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3988 | |
| 3989 | // Inject a series of mouse events for a mouse click |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3990 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3991 | .deviceId(mouseDeviceId) |
| 3992 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3993 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3994 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3995 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 3996 | WithPointerCount(2u))); |
| 3997 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3998 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3999 | mDispatcher->notifyMotion( |
| 4000 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 4001 | .deviceId(mouseDeviceId) |
| 4002 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4003 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4004 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4005 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4006 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4007 | |
| 4008 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 4009 | // already canceled gesture, it should be ignored. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4010 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 4011 | .deviceId(touchDeviceId) |
| 4012 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 4013 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 4014 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4015 | window->assertNoEvents(); |
| 4016 | } |
| 4017 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4018 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 4019 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4020 | |
| 4021 | sp<FakeWindowHandle> spyWindow = |
| 4022 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 4023 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4024 | spyWindow->setTrustedOverlay(true); |
| 4025 | spyWindow->setSpy(true); |
| 4026 | sp<FakeWindowHandle> window = |
| 4027 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4028 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4029 | |
| 4030 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4031 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4032 | |
| 4033 | // Send mouse cursor to the window |
| 4034 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4035 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4036 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4037 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4038 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4039 | .build())); |
| 4040 | |
| 4041 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4042 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4043 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4044 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4045 | |
| 4046 | window->assertNoEvents(); |
| 4047 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4048 | } |
| 4049 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4050 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 4051 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4052 | |
| 4053 | sp<FakeWindowHandle> spyWindow = |
| 4054 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 4055 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4056 | spyWindow->setTrustedOverlay(true); |
| 4057 | spyWindow->setSpy(true); |
| 4058 | sp<FakeWindowHandle> window = |
| 4059 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4060 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4061 | |
| 4062 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4063 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4064 | |
| 4065 | // Send mouse cursor to the window |
| 4066 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4067 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4068 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4069 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4070 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4071 | .build())); |
| 4072 | |
| 4073 | // Move mouse cursor |
| 4074 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4075 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4076 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4077 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4078 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4079 | .build())); |
| 4080 | |
| 4081 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4082 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4083 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4084 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4085 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4086 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4087 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4088 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4089 | // Touch down on the window |
| 4090 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4091 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4092 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 4093 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4094 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4095 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4096 | .build())); |
| 4097 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4098 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4099 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4100 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4101 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4102 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4103 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4104 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4105 | |
| 4106 | // pilfer the motion, retaining the gesture on the spy window. |
| 4107 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 4108 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 4109 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4110 | |
| 4111 | // Touch UP on the window |
| 4112 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4113 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4114 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 4115 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4116 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4117 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4118 | .build())); |
| 4119 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4120 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4121 | |
| 4122 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 4123 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 4124 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 4125 | |
| 4126 | // One more tap - DOWN |
| 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(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4133 | .build())); |
| 4134 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4135 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4136 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4137 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4138 | |
| 4139 | // Touch UP on the window |
| 4140 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4141 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4142 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 4143 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4144 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4145 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4146 | .build())); |
| 4147 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4148 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4149 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4150 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4151 | |
| 4152 | window->assertNoEvents(); |
| 4153 | spyWindow->assertNoEvents(); |
| 4154 | } |
| 4155 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4156 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 4157 | // directly in this test. |
| 4158 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4159 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4160 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4161 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4162 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4163 | |
| 4164 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4165 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4166 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4167 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4168 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4169 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4170 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4171 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4172 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4173 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4174 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4175 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4176 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4177 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4178 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4179 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4180 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4181 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4182 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4183 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4184 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4185 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4186 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4187 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 4188 | AINPUT_SOURCE_MOUSE) |
| 4189 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4190 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4191 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4192 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4193 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4194 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4195 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4196 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4197 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 4198 | AINPUT_SOURCE_MOUSE) |
| 4199 | .buttonState(0) |
| 4200 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4201 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4202 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4203 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
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_UP, AINPUT_SOURCE_MOUSE) |
| 4208 | .buttonState(0) |
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())); |
| 4211 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 4212 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 4213 | // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the |
| 4214 | // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail. |
| 4215 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4216 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4217 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 4218 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4219 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4220 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4221 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4222 | } |
| 4223 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4224 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4225 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 4226 | * is generated. |
| 4227 | */ |
| 4228 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 4229 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4230 | sp<FakeWindowHandle> window = |
| 4231 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4232 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 4233 | |
| 4234 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4235 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4236 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4237 | |
| 4238 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4239 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4240 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4241 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4242 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4243 | .build())); |
| 4244 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4245 | |
| 4246 | // Remove the window, but keep the channel. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4247 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4248 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 4249 | } |
| 4250 | |
| 4251 | /** |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4252 | * Test that invalid HOVER events sent by accessibility do not cause a fatal crash. |
| 4253 | */ |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 4254 | TEST_F_WITH_FLAGS(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash, |
| 4255 | REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(com::android::input::flags, |
| 4256 | a11y_crash_on_inconsistent_event_stream))) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4257 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4258 | sp<FakeWindowHandle> window = |
| 4259 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4260 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 4261 | |
| 4262 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4263 | |
| 4264 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 4265 | |
| 4266 | MotionEventBuilder hoverEnterBuilder = |
| 4267 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4268 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4269 | .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 4270 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4271 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 4272 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4273 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 4274 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4275 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4276 | } |
| 4277 | |
| 4278 | /** |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4279 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. |
| 4280 | */ |
| 4281 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { |
| 4282 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4283 | sp<FakeWindowHandle> window = |
| 4284 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4285 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4286 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4287 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4288 | |
| 4289 | const int32_t mouseDeviceId = 7; |
| 4290 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4291 | |
| 4292 | // Start hovering with the mouse |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4293 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4294 | .deviceId(mouseDeviceId) |
| 4295 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 4296 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4297 | window->consumeMotionEvent( |
| 4298 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 4299 | |
| 4300 | // Touch goes down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4301 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4302 | .deviceId(touchDeviceId) |
| 4303 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4304 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4305 | |
| 4306 | window->consumeMotionEvent( |
| 4307 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 4308 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 4309 | } |
| 4310 | |
| 4311 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4312 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4313 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 4314 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4315 | */ |
| 4316 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 4317 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4318 | sp<FakeWindowHandle> window = |
| 4319 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4320 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4321 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4322 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4323 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4324 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 4325 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4326 | ASSERT_NO_FATAL_FAILURE( |
| 4327 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4328 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4329 | |
| 4330 | // Tap on the window |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4331 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4332 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4333 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4334 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4335 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4336 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 4337 | |
| 4338 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4339 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4340 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4341 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4342 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4343 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4344 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4345 | ASSERT_NO_FATAL_FAILURE( |
| 4346 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4347 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4348 | } |
| 4349 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4350 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 4351 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4352 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 4353 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 4354 | ADISPLAY_ID_DEFAULT); |
| 4355 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 4356 | sp<FakeWindowHandle> windowSecondDisplay = |
| 4357 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 4358 | SECOND_DISPLAY_ID); |
| 4359 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 4360 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4361 | mDispatcher->onWindowInfosChanged( |
| 4362 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4363 | |
| 4364 | // Set cursor position in window in default display and check that hover enter and move |
| 4365 | // events are generated. |
| 4366 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4367 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4368 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4369 | AINPUT_SOURCE_MOUSE) |
| 4370 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4371 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4372 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4373 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4374 | |
| 4375 | // Remove all windows in secondary display and check that no event happens on window in |
| 4376 | // primary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4377 | mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0}); |
| 4378 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4379 | windowDefaultDisplay->assertNoEvents(); |
| 4380 | |
| 4381 | // Move cursor position in window in default display and check that only hover move |
| 4382 | // event is generated and not hover enter event. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4383 | mDispatcher->onWindowInfosChanged( |
| 4384 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4385 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4386 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4387 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4388 | AINPUT_SOURCE_MOUSE) |
| 4389 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4390 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4391 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4392 | windowDefaultDisplay->consumeMotionEvent( |
| 4393 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4394 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4395 | windowDefaultDisplay->assertNoEvents(); |
| 4396 | } |
| 4397 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4398 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4399 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4400 | |
| 4401 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4402 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4403 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4404 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4405 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4406 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4407 | |
| 4408 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4409 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4410 | mDispatcher->onWindowInfosChanged( |
| 4411 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4412 | |
| 4413 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 4414 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4415 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4416 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4417 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4418 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4419 | windowRight->assertNoEvents(); |
| 4420 | } |
| 4421 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4422 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4423 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4424 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4425 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4426 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4427 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4428 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4429 | setFocusedWindow(window); |
| 4430 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4431 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4432 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4433 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4434 | |
| 4435 | // Window should receive key down event. |
| 4436 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4437 | |
| 4438 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 4439 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4440 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 4441 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT, AKEY_EVENT_FLAG_CANCELED); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4442 | } |
| 4443 | |
| 4444 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4445 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4446 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4447 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4448 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4449 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4450 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4451 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4452 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4453 | |
| 4454 | // Window should receive motion down event. |
| 4455 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4456 | |
| 4457 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 4458 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4459 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 4460 | window->consumeMotionEvent( |
| 4461 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4462 | } |
| 4463 | |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 4464 | TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) { |
| 4465 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4466 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4467 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4468 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4469 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 4470 | |
| 4471 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 4472 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10)) |
| 4473 | .build()); |
| 4474 | |
| 4475 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 4476 | |
| 4477 | // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT |
| 4478 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
| 4479 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4480 | |
| 4481 | // After the device has been reset, a new hovering stream can be sent to the window |
| 4482 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 4483 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15)) |
| 4484 | .build()); |
| 4485 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 4486 | } |
| 4487 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4488 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 4489 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4490 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4491 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4492 | window->setFocusable(true); |
| 4493 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4494 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4495 | setFocusedWindow(window); |
| 4496 | |
| 4497 | window->consumeFocusEvent(true); |
| 4498 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4499 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4500 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 4501 | const nsecs_t injectTime = keyArgs.eventTime; |
| 4502 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4503 | mDispatcher->notifyKey(keyArgs); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4504 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 4505 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4506 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 4507 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 4508 | } |
| 4509 | |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 4510 | /** |
| 4511 | * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set. |
| 4512 | */ |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4513 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 4514 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4515 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4516 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4517 | window->setFocusable(true); |
| 4518 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4519 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4520 | setFocusedWindow(window); |
| 4521 | |
| 4522 | window->consumeFocusEvent(true); |
| 4523 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4524 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4525 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 4526 | |
| 4527 | // Set a value that's significantly larger than the default consumption timeout. If the |
| 4528 | // implementation is correct, the actual value doesn't matter; it won't slow down the test. |
| 4529 | mFakePolicy->setInterceptKeyTimeout(600ms); |
| 4530 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
| 4531 | // Window should receive key event immediately when same key up. |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4532 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 4533 | } |
| 4534 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4535 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4536 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 4537 | * WATCH_OUTSIDE_TOUCH. |
| 4538 | * Both windows are owned by the same UID. |
| 4539 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 4540 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 4541 | */ |
| 4542 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 4543 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4544 | sp<FakeWindowHandle> window = |
| 4545 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4546 | window->setFrame(Rect{0, 0, 100, 100}); |
| 4547 | |
| 4548 | sp<FakeWindowHandle> outsideWindow = |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4549 | sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window", |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4550 | ADISPLAY_ID_DEFAULT); |
| 4551 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 4552 | outsideWindow->setWatchOutsideTouch(true); |
| 4553 | // 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] | 4554 | mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4555 | |
| 4556 | // Tap on first window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4557 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4558 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4559 | {PointF{50, 50}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4560 | window->consumeMotionDown(); |
| 4561 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 4562 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 4563 | outsideWindow->consumeMotionEvent( |
| 4564 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 4565 | |
| 4566 | // Ensure outsideWindow doesn't get any more events for the gesture. |
| 4567 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4568 | ADISPLAY_ID_DEFAULT, {PointF{51, 51}})); |
| 4569 | window->consumeMotionMove(); |
| 4570 | outsideWindow->assertNoEvents(); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4571 | } |
| 4572 | |
| 4573 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4574 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 4575 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 4576 | * ACTION_OUTSIDE event is sent per gesture. |
| 4577 | */ |
| 4578 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 4579 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 4580 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4581 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4582 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4583 | window->setWatchOutsideTouch(true); |
| 4584 | window->setFrame(Rect{0, 0, 100, 100}); |
| 4585 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4586 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4587 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4588 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 4589 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4590 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 4591 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4592 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4593 | mDispatcher->onWindowInfosChanged( |
| 4594 | {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4595 | |
| 4596 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4597 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4598 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4599 | {PointF{-10, -10}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4600 | window->assertNoEvents(); |
| 4601 | secondWindow->assertNoEvents(); |
| 4602 | |
| 4603 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 4604 | // Now, `window` should get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4605 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4606 | ADISPLAY_ID_DEFAULT, |
| 4607 | {PointF{-10, -10}, PointF{105, 105}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4608 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 4609 | window->consumeMotionEvent( |
| 4610 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4611 | secondWindow->consumeMotionDown(); |
| 4612 | thirdWindow->assertNoEvents(); |
| 4613 | |
| 4614 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 4615 | // 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] | 4616 | mDispatcher->notifyMotion( |
| 4617 | generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4618 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4619 | window->assertNoEvents(); |
| 4620 | secondWindow->consumeMotionMove(); |
| 4621 | thirdWindow->consumeMotionDown(); |
| 4622 | } |
| 4623 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4624 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 4625 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4626 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4627 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4628 | window->setFocusable(true); |
| 4629 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4630 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4631 | setFocusedWindow(window); |
| 4632 | |
| 4633 | window->consumeFocusEvent(true); |
| 4634 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4635 | const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 4636 | const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 4637 | mDispatcher->notifyKey(keyDown); |
| 4638 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4639 | |
| 4640 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4641 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 4642 | |
| 4643 | // 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] | 4644 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4645 | |
| 4646 | window->consumeFocusEvent(false); |
| 4647 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4648 | mDispatcher->notifyKey(keyDown); |
| 4649 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4650 | window->assertNoEvents(); |
| 4651 | } |
| 4652 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4653 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 4654 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4655 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4656 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4657 | // Ensure window is non-split and have some transform. |
| 4658 | window->setPreventSplitting(true); |
| 4659 | window->setWindowOffset(20, 40); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4660 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4661 | |
| 4662 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4663 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4664 | {50, 50})) |
| 4665 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4666 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4667 | |
| 4668 | const MotionEvent secondFingerDownEvent = |
| 4669 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4670 | .displayId(ADISPLAY_ID_DEFAULT) |
| 4671 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4672 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 4673 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4674 | .build(); |
| 4675 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4676 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4677 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 4678 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4679 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 4680 | std::unique_ptr<MotionEvent> event = window->consumeMotionEvent(); |
| 4681 | ASSERT_NE(nullptr, event); |
| 4682 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 4683 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 4684 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 4685 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 4686 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4687 | } |
| 4688 | |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 4689 | /** |
| 4690 | * Two windows: a splittable and a non-splittable. |
| 4691 | * The non-splittable window shouldn't receive any "incomplete" gestures. |
| 4692 | * Send the first pointer to the splittable window, and then touch the non-splittable window. |
| 4693 | * The second pointer should be dropped because the initial window is splittable, so it won't get |
| 4694 | * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any |
| 4695 | * "incomplete" gestures. |
| 4696 | */ |
| 4697 | TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) { |
| 4698 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4699 | sp<FakeWindowHandle> leftWindow = |
| 4700 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window", |
| 4701 | ADISPLAY_ID_DEFAULT); |
| 4702 | leftWindow->setPreventSplitting(false); |
| 4703 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4704 | sp<FakeWindowHandle> rightWindow = |
| 4705 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window", |
| 4706 | ADISPLAY_ID_DEFAULT); |
| 4707 | rightWindow->setPreventSplitting(true); |
| 4708 | rightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 4709 | mDispatcher->onWindowInfosChanged( |
| 4710 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 4711 | |
| 4712 | // Touch down on left, splittable window |
| 4713 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4714 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4715 | .build()); |
| 4716 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4717 | |
| 4718 | mDispatcher->notifyMotion( |
| 4719 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4720 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 4721 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
| 4722 | .build()); |
| 4723 | leftWindow->assertNoEvents(); |
| 4724 | rightWindow->assertNoEvents(); |
| 4725 | } |
| 4726 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4727 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) { |
| 4728 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4729 | sp<FakeWindowHandle> window = |
| 4730 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4731 | window->setFrame(Rect(0, 0, 400, 400)); |
| 4732 | sp<FakeWindowHandle> trustedOverlay = |
| 4733 | sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay", |
| 4734 | ADISPLAY_ID_DEFAULT); |
| 4735 | trustedOverlay->setSpy(true); |
| 4736 | trustedOverlay->setTrustedOverlay(true); |
| 4737 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4738 | mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4739 | |
| 4740 | // Start a three-finger touchpad swipe |
| 4741 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4742 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4743 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4744 | .build()); |
| 4745 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 4746 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4747 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4748 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4749 | .build()); |
| 4750 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 4751 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4752 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4753 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 4754 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4755 | .build()); |
| 4756 | |
| 4757 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4758 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4759 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN)); |
| 4760 | |
| 4761 | // Move the swipe a bit |
| 4762 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4763 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4764 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4765 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4766 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4767 | .build()); |
| 4768 | |
| 4769 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 4770 | |
| 4771 | // End the swipe |
| 4772 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 4773 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4774 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4775 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4776 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4777 | .build()); |
| 4778 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 4779 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4780 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4781 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4782 | .build()); |
| 4783 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4784 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4785 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4786 | .build()); |
| 4787 | |
| 4788 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP)); |
| 4789 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 4790 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 4791 | |
| 4792 | window->assertNoEvents(); |
| 4793 | } |
| 4794 | |
| 4795 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) { |
| 4796 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4797 | sp<FakeWindowHandle> window = |
| 4798 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4799 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4800 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4801 | |
| 4802 | // Start a three-finger touchpad swipe |
| 4803 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4804 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4805 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4806 | .build()); |
| 4807 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 4808 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4809 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4810 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4811 | .build()); |
| 4812 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 4813 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4814 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4815 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 4816 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4817 | .build()); |
| 4818 | |
| 4819 | // Move the swipe a bit |
| 4820 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4821 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4822 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4823 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4824 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4825 | .build()); |
| 4826 | |
| 4827 | // End the swipe |
| 4828 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 4829 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4830 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4831 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4832 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4833 | .build()); |
| 4834 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 4835 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4836 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4837 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4838 | .build()); |
| 4839 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4840 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4841 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4842 | .build()); |
| 4843 | |
| 4844 | window->assertNoEvents(); |
| 4845 | } |
| 4846 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4847 | /** |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4848 | * Send a two-pointer gesture to a single window. The window's orientation changes in response to |
| 4849 | * the first pointer. |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4850 | * 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] | 4851 | */ |
| 4852 | TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) { |
| 4853 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4854 | sp<FakeWindowHandle> window = |
| 4855 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4856 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4857 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4858 | |
| 4859 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4860 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4861 | .downTime(baseTime + 10) |
| 4862 | .eventTime(baseTime + 10) |
| 4863 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4864 | .build()); |
| 4865 | |
| 4866 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4867 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4868 | // Change the transform so that the orientation is now different from original. |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4869 | window->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4870 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4871 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4872 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4873 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4874 | .downTime(baseTime + 10) |
| 4875 | .eventTime(baseTime + 30) |
| 4876 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4877 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 4878 | .build()); |
| 4879 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4880 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4881 | |
| 4882 | // 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] | 4883 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4884 | .downTime(baseTime + 10) |
| 4885 | .eventTime(baseTime + 40) |
| 4886 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4887 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 4888 | .build()); |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4889 | |
| 4890 | window->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 4891 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4892 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4893 | .downTime(baseTime + 10) |
| 4894 | .eventTime(baseTime + 50) |
| 4895 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4896 | .build()); |
| 4897 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4898 | window->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 4899 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4900 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4901 | .downTime(baseTime + 60) |
| 4902 | .eventTime(baseTime + 60) |
| 4903 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40)) |
| 4904 | .build()); |
| 4905 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4906 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4907 | } |
| 4908 | |
| 4909 | /** |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 4910 | * When there are multiple screens, such as screen projection to TV or screen recording, if the |
| 4911 | * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and |
| 4912 | * its coordinates should be converted by the transform of the windows of target screen. |
| 4913 | */ |
| 4914 | TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) { |
| 4915 | // This case will create a window and a spy window on the default display and mirror |
| 4916 | // window on the second display. cancel event is sent through spy window pilferPointers |
| 4917 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4918 | |
| 4919 | sp<FakeWindowHandle> spyWindowDefaultDisplay = |
| 4920 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 4921 | spyWindowDefaultDisplay->setTrustedOverlay(true); |
| 4922 | spyWindowDefaultDisplay->setSpy(true); |
| 4923 | |
| 4924 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 4925 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 4926 | ADISPLAY_ID_DEFAULT); |
| 4927 | windowDefaultDisplay->setWindowTransform(1, 0, 0, 1); |
| 4928 | |
| 4929 | sp<FakeWindowHandle> windowSecondDisplay = windowDefaultDisplay->clone(SECOND_DISPLAY_ID); |
| 4930 | windowSecondDisplay->setWindowTransform(2, 0, 0, 2); |
| 4931 | |
| 4932 | // Add the windows to the dispatcher |
| 4933 | mDispatcher->onWindowInfosChanged( |
| 4934 | {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(), |
| 4935 | *windowSecondDisplay->getInfo()}, |
| 4936 | {}, |
| 4937 | 0, |
| 4938 | 0}); |
| 4939 | |
| 4940 | // Send down to ADISPLAY_ID_DEFAULT |
| 4941 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4942 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4943 | {100, 100})) |
| 4944 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4945 | |
| 4946 | spyWindowDefaultDisplay->consumeMotionDown(); |
| 4947 | windowDefaultDisplay->consumeMotionDown(); |
| 4948 | |
| 4949 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken())); |
| 4950 | |
| 4951 | // windowDefaultDisplay gets cancel |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 4952 | std::unique_ptr<MotionEvent> event = windowDefaultDisplay->consumeMotionEvent(); |
| 4953 | ASSERT_NE(nullptr, event); |
| 4954 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event->getAction()); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 4955 | |
| 4956 | // The cancel event is sent to windowDefaultDisplay of the ADISPLAY_ID_DEFAULT display, so the |
| 4957 | // coordinates of the cancel are converted by windowDefaultDisplay's transform, the x and y |
| 4958 | // coordinates are both 100, otherwise if the cancel event is sent to windowSecondDisplay of |
| 4959 | // SECOND_DISPLAY_ID, the x and y coordinates are 200 |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 4960 | EXPECT_EQ(100, event->getX(0)); |
| 4961 | EXPECT_EQ(100, event->getY(0)); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 4962 | } |
| 4963 | |
| 4964 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4965 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 4966 | * |
| 4967 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 4968 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 4969 | * space. |
| 4970 | */ |
| 4971 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 4972 | public: |
| 4973 | void SetUp() override { |
| 4974 | InputDispatcherTest::SetUp(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4975 | removeAllWindowsAndDisplays(); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4976 | } |
| 4977 | |
| 4978 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 4979 | gui::DisplayInfo info; |
| 4980 | info.displayId = displayId; |
| 4981 | info.transform = transform; |
| 4982 | mDisplayInfos.push_back(std::move(info)); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4983 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4984 | } |
| 4985 | |
| 4986 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 4987 | mWindowInfos.push_back(*windowHandle->getInfo()); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4988 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4989 | } |
| 4990 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4991 | void removeAllWindowsAndDisplays() { |
| 4992 | mDisplayInfos.clear(); |
| 4993 | mWindowInfos.clear(); |
| 4994 | } |
| 4995 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4996 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 4997 | // on the display. |
| 4998 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 4999 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 5000 | // respectively. |
| 5001 | ui::Transform displayTransform; |
| 5002 | displayTransform.set(2, 0, 0, 4); |
| 5003 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 5004 | |
| 5005 | std::shared_ptr<FakeApplicationHandle> application = |
| 5006 | std::make_shared<FakeApplicationHandle>(); |
| 5007 | |
| 5008 | // Add two windows to the display. Their frames are represented in the display space. |
| 5009 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5010 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5011 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5012 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 5013 | addWindow(firstWindow); |
| 5014 | |
| 5015 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5016 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5017 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5018 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 5019 | addWindow(secondWindow); |
| 5020 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 5021 | } |
| 5022 | |
| 5023 | private: |
| 5024 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 5025 | std::vector<gui::WindowInfo> mWindowInfos; |
| 5026 | }; |
| 5027 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5028 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5029 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5030 | // 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] | 5031 | // selected so that if the hit test was performed with the point and the bounds being in |
| 5032 | // different coordinate spaces, the event would end up in the incorrect window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5033 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5034 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5035 | {PointF{75, 55}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5036 | |
| 5037 | firstWindow->consumeMotionDown(); |
| 5038 | secondWindow->assertNoEvents(); |
| 5039 | } |
| 5040 | |
| 5041 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 5042 | // the event should be treated as being in the logical display space. |
| 5043 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 5044 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5045 | // Send down to the first window. The point is represented in the logical display space. The |
| 5046 | // point is selected so that if the hit test was done in logical display space, then it would |
| 5047 | // end up in the incorrect window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5048 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5049 | PointF{75 * 2, 55 * 4}); |
| 5050 | |
| 5051 | firstWindow->consumeMotionDown(); |
| 5052 | secondWindow->assertNoEvents(); |
| 5053 | } |
| 5054 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5055 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 5056 | // event should be treated as being in the logical display space. |
| 5057 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 5058 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5059 | |
| 5060 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5061 | ui::Transform injectedEventTransform; |
| 5062 | injectedEventTransform.set(matrix); |
| 5063 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 5064 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 5065 | |
| 5066 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5067 | .displayId(ADISPLAY_ID_DEFAULT) |
| 5068 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 5069 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5070 | .x(untransformedPoint.x) |
| 5071 | .y(untransformedPoint.y)) |
| 5072 | .build(); |
| 5073 | event.transform(matrix); |
| 5074 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5075 | injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5076 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 5077 | |
| 5078 | firstWindow->consumeMotionDown(); |
| 5079 | secondWindow->assertNoEvents(); |
| 5080 | } |
| 5081 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5082 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 5083 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5084 | |
| 5085 | // Send down to the second window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5086 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5087 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5088 | {PointF{150, 220}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5089 | |
| 5090 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5091 | std::unique_ptr<MotionEvent> event = secondWindow->consumeMotionEvent(); |
| 5092 | ASSERT_NE(nullptr, event); |
| 5093 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5094 | |
| 5095 | // 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] | 5096 | EXPECT_EQ(300, event->getRawX(0)); |
| 5097 | EXPECT_EQ(880, event->getRawY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5098 | |
| 5099 | // Ensure that the x and y values are in the window's coordinate space. |
| 5100 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 5101 | // 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] | 5102 | EXPECT_EQ(100, event->getX(0)); |
| 5103 | EXPECT_EQ(80, event->getY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5104 | } |
| 5105 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5106 | TEST_F(InputDispatcherDisplayProjectionTest, CancelMotionWithCorrectCoordinates) { |
| 5107 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5108 | // The monitor will always receive events in the logical display's coordinate space, because |
| 5109 | // it does not have a window. |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5110 | FakeMonitorReceiver monitor{*mDispatcher, "Monitor", ADISPLAY_ID_DEFAULT}; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5111 | |
| 5112 | // Send down to the first window. |
| 5113 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5114 | ADISPLAY_ID_DEFAULT, {PointF{50, 100}})); |
| 5115 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5116 | monitor.consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5117 | |
| 5118 | // Second pointer goes down on second window. |
| 5119 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5120 | ADISPLAY_ID_DEFAULT, |
| 5121 | {PointF{50, 100}, PointF{150, 220}})); |
| 5122 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80))); |
| 5123 | const std::map<int32_t, PointF> expectedMonitorPointers{{0, PointF{100, 400}}, |
| 5124 | {1, PointF{300, 880}}}; |
| 5125 | monitor.consumeMotionEvent( |
| 5126 | AllOf(WithMotionAction(POINTER_1_DOWN), WithPointers(expectedMonitorPointers))); |
| 5127 | |
| 5128 | mDispatcher->cancelCurrentTouch(); |
| 5129 | |
| 5130 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 5131 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 80))); |
| 5132 | monitor.consumeMotionEvent( |
| 5133 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedMonitorPointers))); |
| 5134 | } |
| 5135 | |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 5136 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeDownWithCorrectCoordinates) { |
| 5137 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5138 | |
| 5139 | // Send down to the first window. |
| 5140 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5141 | ADISPLAY_ID_DEFAULT, {PointF{50, 100}})); |
| 5142 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5143 | |
| 5144 | // The pointer is transferred to the second window, and the second window receives it in the |
| 5145 | // correct coordinate space. |
| 5146 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 5147 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 5148 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(-100, -400))); |
| 5149 | } |
| 5150 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5151 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverEnterExitWithCorrectCoordinates) { |
| 5152 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5153 | |
| 5154 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 5155 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 5156 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5157 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5158 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5159 | |
| 5160 | // Touch down at the same location and ensure a hover exit is synthesized. |
| 5161 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
| 5162 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5163 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5164 | WithRawCoords(300, 880))); |
| 5165 | secondWindow->consumeMotionEvent( |
| 5166 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5167 | secondWindow->assertNoEvents(); |
| 5168 | firstWindow->assertNoEvents(); |
| 5169 | } |
| 5170 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5171 | // Same as above, but while the window is being mirrored. |
| 5172 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 5173 | SynthesizeHoverEnterExitWithCorrectCoordinatesWhenMirrored) { |
| 5174 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5175 | |
| 5176 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5177 | ui::Transform secondDisplayTransform; |
| 5178 | secondDisplayTransform.set(matrix); |
| 5179 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 5180 | |
| 5181 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 5182 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 5183 | addWindow(secondWindowClone); |
| 5184 | |
| 5185 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 5186 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 5187 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5188 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5189 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5190 | |
| 5191 | // Touch down at the same location and ensure a hover exit is synthesized for the correct |
| 5192 | // display. |
| 5193 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
| 5194 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5195 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5196 | WithRawCoords(300, 880))); |
| 5197 | secondWindow->consumeMotionEvent( |
| 5198 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5199 | secondWindow->assertNoEvents(); |
| 5200 | firstWindow->assertNoEvents(); |
| 5201 | } |
| 5202 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5203 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverCancelationWithCorrectCoordinates) { |
| 5204 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5205 | |
| 5206 | // Send hover enter to second window |
| 5207 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
| 5208 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5209 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5210 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5211 | |
| 5212 | mDispatcher->cancelCurrentTouch(); |
| 5213 | |
| 5214 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5215 | WithRawCoords(300, 880))); |
| 5216 | secondWindow->assertNoEvents(); |
| 5217 | firstWindow->assertNoEvents(); |
| 5218 | } |
| 5219 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5220 | // Same as above, but while the window is being mirrored. |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5221 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 5222 | SynthesizeHoverCancelationWithCorrectCoordinatesWhenMirrored) { |
| 5223 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5224 | |
| 5225 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5226 | ui::Transform secondDisplayTransform; |
| 5227 | secondDisplayTransform.set(matrix); |
| 5228 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 5229 | |
| 5230 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 5231 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 5232 | addWindow(secondWindowClone); |
| 5233 | |
| 5234 | // Send hover enter to second window |
| 5235 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
| 5236 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5237 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5238 | WithCoords(100, 80), WithRawCoords(300, 880), |
| 5239 | WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 5240 | |
| 5241 | mDispatcher->cancelCurrentTouch(); |
| 5242 | |
| 5243 | // Ensure the cancelation happens with the correct displayId and the correct coordinates. |
| 5244 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5245 | WithRawCoords(300, 880), |
| 5246 | WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 5247 | secondWindow->assertNoEvents(); |
| 5248 | firstWindow->assertNoEvents(); |
| 5249 | } |
| 5250 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5251 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ |
| 5252 | class InputDispatcherDisplayOrientationFixture |
| 5253 | : public InputDispatcherDisplayProjectionTest, |
| 5254 | public ::testing::WithParamInterface<ui::Rotation> {}; |
| 5255 | |
| 5256 | // This test verifies the touchable region of a window for all rotations of the display by tapping |
| 5257 | // in different locations on the display, specifically points close to the four corners of a |
| 5258 | // window. |
| 5259 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { |
| 5260 | constexpr static int32_t displayWidth = 400; |
| 5261 | constexpr static int32_t displayHeight = 800; |
| 5262 | |
| 5263 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5264 | |
| 5265 | const auto rotation = GetParam(); |
| 5266 | |
| 5267 | // Set up the display with the specified rotation. |
| 5268 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 5269 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 5270 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 5271 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 5272 | logicalDisplayWidth, logicalDisplayHeight); |
| 5273 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 5274 | |
| 5275 | // Create a window with its bounds determined in the logical display. |
| 5276 | const Rect frameInLogicalDisplay(100, 100, 200, 300); |
| 5277 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); |
| 5278 | sp<FakeWindowHandle> window = |
| 5279 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 5280 | window->setFrame(frameInDisplay, displayTransform); |
| 5281 | addWindow(window); |
| 5282 | |
| 5283 | // The following points in logical display space should be inside the window. |
| 5284 | static const std::array<vec2, 4> insidePoints{ |
| 5285 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 5286 | for (const auto pointInsideWindow : insidePoints) { |
| 5287 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); |
| 5288 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5289 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5290 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5291 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5292 | window->consumeMotionDown(); |
| 5293 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5294 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 5295 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5296 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5297 | window->consumeMotionUp(); |
| 5298 | } |
| 5299 | |
| 5300 | // The following points in logical display space should be outside the window. |
| 5301 | static const std::array<vec2, 5> outsidePoints{ |
| 5302 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 5303 | for (const auto pointOutsideWindow : outsidePoints) { |
| 5304 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); |
| 5305 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5306 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5307 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5308 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5309 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5310 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 5311 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5312 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5313 | } |
| 5314 | window->assertNoEvents(); |
| 5315 | } |
| 5316 | |
| 5317 | // Run the precision tests for all rotations. |
| 5318 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, |
| 5319 | InputDispatcherDisplayOrientationFixture, |
| 5320 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, |
| 5321 | ui::ROTATION_270), |
| 5322 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { |
| 5323 | return ftl::enum_string(testParamInfo.param); |
| 5324 | }); |
| 5325 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5326 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 5327 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5328 | |
| 5329 | class TransferTouchFixture : public InputDispatcherTest, |
| 5330 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 5331 | |
| 5332 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5333 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5334 | |
| 5335 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5336 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5337 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5338 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5339 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5340 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5341 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5342 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5343 | sp<FakeWindowHandle> wallpaper = |
| 5344 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 5345 | wallpaper->setIsWallpaper(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5346 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5347 | mDispatcher->onWindowInfosChanged( |
| 5348 | {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5349 | |
| 5350 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5351 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5352 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5353 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5354 | // Only the first window should get the down event |
| 5355 | firstWindow->consumeMotionDown(); |
| 5356 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5357 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5358 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5359 | // Transfer touch to the second window |
| 5360 | TransferFunction f = GetParam(); |
| 5361 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5362 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5363 | // The first window gets cancel and the second gets down |
| 5364 | firstWindow->consumeMotionCancel(); |
| 5365 | secondWindow->consumeMotionDown(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5366 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5367 | |
| 5368 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5369 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5370 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5371 | // The first window gets no events and the second gets up |
| 5372 | firstWindow->assertNoEvents(); |
| 5373 | secondWindow->consumeMotionUp(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5374 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5375 | } |
| 5376 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5377 | /** |
| 5378 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 5379 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 5380 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 5381 | * natural to the user. |
| 5382 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 5383 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 5384 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 5385 | * the first window to the second. |
| 5386 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 5387 | * the other API, as well. |
| 5388 | */ |
| 5389 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 5390 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5391 | |
| 5392 | // Create a couple of windows + a spy window |
| 5393 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5394 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5395 | spyWindow->setTrustedOverlay(true); |
| 5396 | spyWindow->setSpy(true); |
| 5397 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5398 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5399 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5400 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5401 | |
| 5402 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5403 | mDispatcher->onWindowInfosChanged( |
| 5404 | {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5405 | |
| 5406 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5407 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5408 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5409 | // Only the first window and spy should get the down event |
| 5410 | spyWindow->consumeMotionDown(); |
| 5411 | firstWindow->consumeMotionDown(); |
| 5412 | |
| 5413 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 5414 | // if f === 'transferTouch'. |
| 5415 | TransferFunction f = GetParam(); |
| 5416 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5417 | ASSERT_TRUE(success); |
| 5418 | // The first window gets cancel and the second gets down |
| 5419 | firstWindow->consumeMotionCancel(); |
| 5420 | secondWindow->consumeMotionDown(); |
| 5421 | |
| 5422 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5423 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5424 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5425 | // The first window gets no events and the second+spy get up |
| 5426 | firstWindow->assertNoEvents(); |
| 5427 | spyWindow->consumeMotionUp(); |
| 5428 | secondWindow->consumeMotionUp(); |
| 5429 | } |
| 5430 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5431 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5432 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5433 | |
| 5434 | PointF touchPoint = {10, 10}; |
| 5435 | |
| 5436 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5437 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5438 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5439 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5440 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5441 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5442 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5443 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5444 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5445 | |
| 5446 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5447 | mDispatcher->onWindowInfosChanged( |
| 5448 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5449 | |
| 5450 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5451 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5452 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5453 | {touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5454 | // Only the first window should get the down event |
| 5455 | firstWindow->consumeMotionDown(); |
| 5456 | secondWindow->assertNoEvents(); |
| 5457 | |
| 5458 | // Send pointer down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5459 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5460 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5461 | // Only the first window should get the pointer down event |
| 5462 | firstWindow->consumeMotionPointerDown(1); |
| 5463 | secondWindow->assertNoEvents(); |
| 5464 | |
| 5465 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5466 | TransferFunction f = GetParam(); |
| 5467 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5468 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5469 | // The first window gets cancel and the second gets down and pointer down |
| 5470 | firstWindow->consumeMotionCancel(); |
| 5471 | secondWindow->consumeMotionDown(); |
| 5472 | secondWindow->consumeMotionPointerDown(1); |
| 5473 | |
| 5474 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5475 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5476 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5477 | // The first window gets nothing and the second gets pointer up |
| 5478 | firstWindow->assertNoEvents(); |
| 5479 | secondWindow->consumeMotionPointerUp(1); |
| 5480 | |
| 5481 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5482 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5483 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5484 | // The first window gets nothing and the second gets up |
| 5485 | firstWindow->assertNoEvents(); |
| 5486 | secondWindow->consumeMotionUp(); |
| 5487 | } |
| 5488 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5489 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 5490 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5491 | |
| 5492 | // Create a couple of windows |
| 5493 | sp<FakeWindowHandle> firstWindow = |
| 5494 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5495 | ADISPLAY_ID_DEFAULT); |
| 5496 | firstWindow->setDupTouchToWallpaper(true); |
| 5497 | sp<FakeWindowHandle> secondWindow = |
| 5498 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5499 | ADISPLAY_ID_DEFAULT); |
| 5500 | secondWindow->setDupTouchToWallpaper(true); |
| 5501 | |
| 5502 | sp<FakeWindowHandle> wallpaper1 = |
| 5503 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 5504 | wallpaper1->setIsWallpaper(true); |
| 5505 | |
| 5506 | sp<FakeWindowHandle> wallpaper2 = |
| 5507 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 5508 | wallpaper2->setIsWallpaper(true); |
| 5509 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5510 | mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(), |
| 5511 | *secondWindow->getInfo(), *wallpaper2->getInfo()}, |
| 5512 | {}, |
| 5513 | 0, |
| 5514 | 0}); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5515 | |
| 5516 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5517 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5518 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5519 | |
| 5520 | // Only the first window should get the down event |
| 5521 | firstWindow->consumeMotionDown(); |
| 5522 | secondWindow->assertNoEvents(); |
| 5523 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5524 | wallpaper2->assertNoEvents(); |
| 5525 | |
| 5526 | // Transfer touch focus to the second window |
| 5527 | TransferFunction f = GetParam(); |
| 5528 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5529 | ASSERT_TRUE(success); |
| 5530 | |
| 5531 | // The first window gets cancel and the second gets down |
| 5532 | firstWindow->consumeMotionCancel(); |
| 5533 | secondWindow->consumeMotionDown(); |
| 5534 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5535 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5536 | |
| 5537 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5538 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5539 | ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5540 | // The first window gets no events and the second gets up |
| 5541 | firstWindow->assertNoEvents(); |
| 5542 | secondWindow->consumeMotionUp(); |
| 5543 | wallpaper1->assertNoEvents(); |
| 5544 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5545 | } |
| 5546 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5547 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 5548 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 5549 | // for the case where there are multiple pointers split across several windows. |
| 5550 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 5551 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5552 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 5553 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5554 | return dispatcher->transferTouch(destChannelToken, |
| 5555 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5556 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5557 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 5558 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5559 | return dispatcher->transferTouchFocus(from, to, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5560 | /*isDragAndDrop=*/false); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5561 | })); |
| 5562 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5563 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5564 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5565 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5566 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5567 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5568 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5569 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5570 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5571 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5572 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5573 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5574 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5575 | |
| 5576 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5577 | mDispatcher->onWindowInfosChanged( |
| 5578 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5579 | |
| 5580 | PointF pointInFirst = {300, 200}; |
| 5581 | PointF pointInSecond = {300, 600}; |
| 5582 | |
| 5583 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5584 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5585 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5586 | {pointInFirst})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5587 | // Only the first window should get the down event |
| 5588 | firstWindow->consumeMotionDown(); |
| 5589 | secondWindow->assertNoEvents(); |
| 5590 | |
| 5591 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5592 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5593 | ADISPLAY_ID_DEFAULT, |
| 5594 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5595 | // The first window gets a move and the second a down |
| 5596 | firstWindow->consumeMotionMove(); |
| 5597 | secondWindow->consumeMotionDown(); |
| 5598 | |
| 5599 | // Transfer touch focus to the second window |
| 5600 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 5601 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 5602 | firstWindow->consumeMotionCancel(); |
| 5603 | secondWindow->consumeMotionPointerDown(1); |
| 5604 | |
| 5605 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5606 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5607 | ADISPLAY_ID_DEFAULT, |
| 5608 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5609 | // The first window gets nothing and the second gets pointer up |
| 5610 | firstWindow->assertNoEvents(); |
| 5611 | secondWindow->consumeMotionPointerUp(1); |
| 5612 | |
| 5613 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5614 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5615 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5616 | // The first window gets nothing and the second gets up |
| 5617 | firstWindow->assertNoEvents(); |
| 5618 | secondWindow->consumeMotionUp(); |
| 5619 | } |
| 5620 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5621 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 5622 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 5623 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 5624 | // window should get nothing. |
| 5625 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 5626 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5627 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5628 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5629 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5630 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5631 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5632 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5633 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5634 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5635 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5636 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5637 | |
| 5638 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5639 | mDispatcher->onWindowInfosChanged( |
| 5640 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5641 | |
| 5642 | PointF pointInFirst = {300, 200}; |
| 5643 | PointF pointInSecond = {300, 600}; |
| 5644 | |
| 5645 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5646 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5647 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5648 | {pointInFirst})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5649 | // Only the first window should get the down event |
| 5650 | firstWindow->consumeMotionDown(); |
| 5651 | secondWindow->assertNoEvents(); |
| 5652 | |
| 5653 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5654 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5655 | ADISPLAY_ID_DEFAULT, |
| 5656 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5657 | // The first window gets a move and the second a down |
| 5658 | firstWindow->consumeMotionMove(); |
| 5659 | secondWindow->consumeMotionDown(); |
| 5660 | |
| 5661 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5662 | const bool transferred = |
| 5663 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5664 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 5665 | ASSERT_FALSE(transferred); |
| 5666 | firstWindow->assertNoEvents(); |
| 5667 | secondWindow->assertNoEvents(); |
| 5668 | |
| 5669 | // The rest of the dispatch should proceed as normal |
| 5670 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5671 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5672 | ADISPLAY_ID_DEFAULT, |
| 5673 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5674 | // The first window gets MOVE and the second gets pointer up |
| 5675 | firstWindow->consumeMotionMove(); |
| 5676 | secondWindow->consumeMotionUp(); |
| 5677 | |
| 5678 | // Send up event to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5679 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5680 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5681 | // The first window gets nothing and the second gets up |
| 5682 | firstWindow->consumeMotionUp(); |
| 5683 | secondWindow->assertNoEvents(); |
| 5684 | } |
| 5685 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5686 | // This case will create two windows and one mirrored window on the default display and mirror |
| 5687 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 5688 | // the windows info of second display before default display. |
| 5689 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 5690 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5691 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5692 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5693 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5694 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5695 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5696 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5697 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5698 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5699 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5700 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5701 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5702 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5703 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5704 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5705 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5706 | |
| 5707 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5708 | mDispatcher->onWindowInfosChanged( |
| 5709 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 5710 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 5711 | *secondWindowInPrimary->getInfo()}, |
| 5712 | {}, |
| 5713 | 0, |
| 5714 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5715 | |
| 5716 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5717 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5718 | {50, 50})) |
| 5719 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5720 | |
| 5721 | // Window should receive motion event. |
| 5722 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5723 | |
| 5724 | // Transfer touch focus |
| 5725 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 5726 | secondWindowInPrimary->getToken())); |
| 5727 | // The first window gets cancel. |
| 5728 | firstWindowInPrimary->consumeMotionCancel(); |
| 5729 | secondWindowInPrimary->consumeMotionDown(); |
| 5730 | |
| 5731 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5732 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5733 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 5734 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5735 | firstWindowInPrimary->assertNoEvents(); |
| 5736 | secondWindowInPrimary->consumeMotionMove(); |
| 5737 | |
| 5738 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5739 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5740 | {150, 50})) |
| 5741 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5742 | firstWindowInPrimary->assertNoEvents(); |
| 5743 | secondWindowInPrimary->consumeMotionUp(); |
| 5744 | } |
| 5745 | |
| 5746 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 5747 | // 'transferTouch' api. |
| 5748 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 5749 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5750 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5751 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5752 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5753 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5754 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5755 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5756 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5757 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5758 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5759 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5760 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5761 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5762 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5763 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5764 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5765 | |
| 5766 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5767 | mDispatcher->onWindowInfosChanged( |
| 5768 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 5769 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 5770 | *secondWindowInPrimary->getInfo()}, |
| 5771 | {}, |
| 5772 | 0, |
| 5773 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5774 | |
| 5775 | // Touch on second display. |
| 5776 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5777 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 5778 | {50, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5779 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5780 | |
| 5781 | // Window should receive motion event. |
| 5782 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 5783 | |
| 5784 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5785 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5786 | |
| 5787 | // The first window gets cancel. |
| 5788 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 5789 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 5790 | |
| 5791 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5792 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5793 | SECOND_DISPLAY_ID, {150, 50})) |
| 5794 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5795 | firstWindowInPrimary->assertNoEvents(); |
| 5796 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 5797 | |
| 5798 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5799 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5800 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5801 | firstWindowInPrimary->assertNoEvents(); |
| 5802 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 5803 | } |
| 5804 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5805 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5806 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5807 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5808 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5809 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5810 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5811 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5812 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5813 | |
| 5814 | window->consumeFocusEvent(true); |
| 5815 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5816 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5817 | |
| 5818 | // Window should receive key down event. |
| 5819 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5820 | |
| 5821 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5822 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5823 | mFakePolicy->assertUserActivityPoked(); |
| 5824 | } |
| 5825 | |
| 5826 | TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) { |
| 5827 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5828 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5829 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5830 | |
| 5831 | window->setDisableUserActivity(true); |
| 5832 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5833 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5834 | setFocusedWindow(window); |
| 5835 | |
| 5836 | window->consumeFocusEvent(true); |
| 5837 | |
| 5838 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5839 | |
| 5840 | // Window should receive key down event. |
| 5841 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5842 | |
| 5843 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5844 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5845 | mFakePolicy->assertUserActivityNotPoked(); |
| 5846 | } |
| 5847 | |
| 5848 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) { |
| 5849 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5850 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5851 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5852 | |
| 5853 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5854 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5855 | setFocusedWindow(window); |
| 5856 | |
| 5857 | window->consumeFocusEvent(true); |
| 5858 | |
| 5859 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5860 | mDispatcher->waitForIdle(); |
| 5861 | |
| 5862 | // System key is not passed down |
| 5863 | window->assertNoEvents(); |
| 5864 | |
| 5865 | // Should have poked user activity |
| 5866 | mFakePolicy->assertUserActivityPoked(); |
| 5867 | } |
| 5868 | |
| 5869 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) { |
| 5870 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5871 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5872 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5873 | |
| 5874 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5875 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5876 | setFocusedWindow(window); |
| 5877 | |
| 5878 | window->consumeFocusEvent(true); |
| 5879 | |
| 5880 | mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5881 | mDispatcher->waitForIdle(); |
| 5882 | |
| 5883 | // System key is not passed down |
| 5884 | window->assertNoEvents(); |
| 5885 | |
| 5886 | // Should have poked user activity |
| 5887 | mFakePolicy->assertUserActivityPoked(); |
| 5888 | } |
| 5889 | |
| 5890 | TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) { |
| 5891 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5892 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5893 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5894 | |
| 5895 | window->setDisableUserActivity(true); |
| 5896 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5897 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5898 | setFocusedWindow(window); |
| 5899 | |
| 5900 | window->consumeFocusEvent(true); |
| 5901 | |
| 5902 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5903 | mDispatcher->waitForIdle(); |
| 5904 | |
| 5905 | // System key is not passed down |
| 5906 | window->assertNoEvents(); |
| 5907 | |
| 5908 | // Should have poked user activity |
| 5909 | mFakePolicy->assertUserActivityPoked(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5910 | } |
| 5911 | |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5912 | TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) { |
| 5913 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5914 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5915 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5916 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5917 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5918 | |
| 5919 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5920 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5921 | ADISPLAY_ID_DEFAULT, {100, 100})) |
| 5922 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5923 | |
| 5924 | window->consumeMotionEvent( |
| 5925 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 5926 | |
| 5927 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5928 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5929 | mFakePolicy->assertUserActivityPoked(); |
| 5930 | } |
| 5931 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5932 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5933 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5934 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5935 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5936 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5937 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5938 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5939 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5940 | mDispatcher->waitForIdle(); |
| 5941 | |
| 5942 | window->assertNoEvents(); |
| 5943 | } |
| 5944 | |
| 5945 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 5946 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5947 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5948 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5949 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5950 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5951 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5952 | |
| 5953 | // Send key |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5954 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5955 | // Send motion |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5956 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5957 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5958 | |
| 5959 | // Window should receive only the motion event |
| 5960 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5961 | window->assertNoEvents(); // Key event or focus event will not be received |
| 5962 | } |
| 5963 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5964 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 5965 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5966 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5967 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5968 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5969 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5970 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5971 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5972 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5973 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5974 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5975 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5976 | |
| 5977 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5978 | mDispatcher->onWindowInfosChanged( |
| 5979 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5980 | |
| 5981 | PointF pointInFirst = {300, 200}; |
| 5982 | PointF pointInSecond = {300, 600}; |
| 5983 | |
| 5984 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5985 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5986 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5987 | {pointInFirst})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5988 | // Only the first window should get the down event |
| 5989 | firstWindow->consumeMotionDown(); |
| 5990 | secondWindow->assertNoEvents(); |
| 5991 | |
| 5992 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5993 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5994 | ADISPLAY_ID_DEFAULT, |
| 5995 | {pointInFirst, pointInSecond})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5996 | // The first window gets a move and the second a down |
| 5997 | firstWindow->consumeMotionMove(); |
| 5998 | secondWindow->consumeMotionDown(); |
| 5999 | |
| 6000 | // Send pointer cancel to the second window |
| 6001 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6002 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6003 | {pointInFirst, pointInSecond}); |
| 6004 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6005 | mDispatcher->notifyMotion(pointerUpMotionArgs); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6006 | // The first window gets move and the second gets cancel. |
| 6007 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 6008 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 6009 | |
| 6010 | // Send up event. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6011 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6012 | ADISPLAY_ID_DEFAULT)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6013 | // The first window gets up and the second gets nothing. |
| 6014 | firstWindow->consumeMotionUp(); |
| 6015 | secondWindow->assertNoEvents(); |
| 6016 | } |
| 6017 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6018 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 6019 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6020 | |
| 6021 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6022 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6023 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6024 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 6025 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 6026 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 6027 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6028 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6029 | window->assertNoEvents(); |
| 6030 | mDispatcher->waitForIdle(); |
| 6031 | } |
| 6032 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6033 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 6034 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6035 | /** |
| 6036 | * Two entities that receive touch: A window, and a global monitor. |
| 6037 | * The touch goes to the window, and then the window disappears. |
| 6038 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 6039 | * for the monitor, as well. |
| 6040 | * 1. foregroundWindow |
| 6041 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 6042 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6043 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6044 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6045 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6046 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6047 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6048 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6049 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6050 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6051 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6052 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6053 | {100, 200})) |
| 6054 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6055 | |
| 6056 | // Both the foreground window and the global monitor should receive the touch down |
| 6057 | window->consumeMotionDown(); |
| 6058 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6059 | |
| 6060 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6061 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6062 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 6063 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6064 | |
| 6065 | window->consumeMotionMove(); |
| 6066 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6067 | |
| 6068 | // Now the foreground window goes away |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6069 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6070 | window->consumeMotionCancel(); |
| 6071 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 6072 | |
| 6073 | // If more events come in, there will be no more foreground window to send them to. This will |
| 6074 | // cause a cancel for the monitor, as well. |
| 6075 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6076 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6077 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 6078 | << "Injection should fail because the window was removed"; |
| 6079 | window->assertNoEvents(); |
| 6080 | // Global monitor now gets the cancel |
| 6081 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6082 | } |
| 6083 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6084 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6085 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6086 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6087 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6088 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6089 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6090 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6091 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6092 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6093 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6094 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6095 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6096 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6097 | } |
| 6098 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6099 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6100 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6101 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6102 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6103 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6104 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6105 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6106 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6107 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6108 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6109 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6110 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6111 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6112 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6113 | // Pilfer pointers from the monitor. |
| 6114 | // This should not do anything and the window should continue to receive events. |
| 6115 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6116 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6117 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6118 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6119 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6120 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6121 | |
| 6122 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6123 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6124 | } |
| 6125 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6126 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6127 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6128 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6129 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6130 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6131 | window->setWindowOffset(20, 40); |
| 6132 | window->setWindowTransform(0, 1, -1, 0); |
| 6133 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6134 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6135 | |
| 6136 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6137 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6138 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6139 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6140 | std::unique_ptr<MotionEvent> event = monitor.consumeMotion(); |
| 6141 | ASSERT_NE(nullptr, event); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6142 | // Even though window has transform, gesture monitor must not. |
| 6143 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 6144 | } |
| 6145 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6146 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6147 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6148 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6149 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6150 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6151 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6152 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 6153 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6154 | } |
| 6155 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6156 | /** |
| 6157 | * Two displays |
| 6158 | * The first monitor has a foreground window, a monitor |
| 6159 | * The second window has only one monitor. |
| 6160 | * We first inject a Down event into the first display, this injection should succeed and both |
| 6161 | * the foreground window and monitor should receive a down event, then inject a Down event into |
| 6162 | * the second display as well, this injection should fail, at this point, the first display |
| 6163 | * window and monitor should not receive a cancel or any other event. |
| 6164 | * Continue to inject Move and UP events to the first display, the events should be received |
| 6165 | * normally by the foreground window and monitor. |
| 6166 | */ |
| 6167 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCanceledWhenAnotherEmptyDisplayReceiveEvents) { |
| 6168 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6169 | sp<FakeWindowHandle> window = |
| 6170 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 6171 | |
| 6172 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6173 | FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 6174 | |
| 6175 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 6176 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6177 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6178 | {100, 200})) |
| 6179 | << "The down event injected into the first display should succeed"; |
| 6180 | |
| 6181 | window->consumeMotionDown(); |
| 6182 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6183 | |
| 6184 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6185 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 6186 | {100, 200})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6187 | << "The down event injected into the second display should fail since there's no " |
| 6188 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6189 | |
| 6190 | // Continue to inject event to first display. |
| 6191 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6192 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6193 | ADISPLAY_ID_DEFAULT, {110, 220})) |
| 6194 | << "The move event injected into the first display should succeed"; |
| 6195 | |
| 6196 | window->consumeMotionMove(); |
| 6197 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6198 | |
| 6199 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6200 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6201 | {110, 220})) |
| 6202 | << "The up event injected into the first display should succeed"; |
| 6203 | |
| 6204 | window->consumeMotionUp(); |
| 6205 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6206 | |
| 6207 | window->assertNoEvents(); |
| 6208 | monitor.assertNoEvents(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6209 | secondMonitor.assertNoEvents(); |
| 6210 | } |
| 6211 | |
| 6212 | /** |
| 6213 | * Two displays |
| 6214 | * There is a monitor and foreground window on each display. |
| 6215 | * First, we inject down events into each of the two displays, at this point, the foreground windows |
| 6216 | * and monitors on both displays should receive down events. |
| 6217 | * At this point, the foreground window of the second display goes away, the gone window should |
| 6218 | * receive the cancel event, and the other windows and monitors should not receive any events. |
| 6219 | * Inject a move event into the second display. At this point, the injection should fail because |
| 6220 | * the second display no longer has a foreground window. At this point, the monitor on the second |
| 6221 | * display should receive a cancel event, and any windows or monitors on the first display should |
| 6222 | * not receive any events, and any subsequent injection of events into the second display should |
| 6223 | * also fail. |
| 6224 | * Continue to inject events into the first display, and the events should all be injected |
| 6225 | * successfully and received normally. |
| 6226 | */ |
| 6227 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCancelWhenAnotherDisplayMonitorTouchCanceled) { |
| 6228 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6229 | sp<FakeWindowHandle> window = |
| 6230 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 6231 | sp<FakeWindowHandle> secondWindow = |
| 6232 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondForeground", |
| 6233 | SECOND_DISPLAY_ID); |
| 6234 | |
| 6235 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6236 | FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 6237 | |
| 6238 | // There is a foreground window on both displays. |
| 6239 | mDispatcher->onWindowInfosChanged({{*window->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
| 6240 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6241 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6242 | {100, 200})) |
| 6243 | << "The down event injected into the first display should succeed"; |
| 6244 | |
| 6245 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6246 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6247 | |
| 6248 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6249 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 6250 | {100, 200})) |
| 6251 | << "The down event injected into the second display should succeed"; |
| 6252 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6253 | secondWindow->consumeMotionDown(SECOND_DISPLAY_ID); |
| 6254 | secondMonitor.consumeMotionDown(SECOND_DISPLAY_ID); |
| 6255 | |
| 6256 | // Now second window is gone away. |
| 6257 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 6258 | |
| 6259 | // The gone window should receive a cancel, and the monitor on the second display should not |
| 6260 | // receive any events. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6261 | secondWindow->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 6262 | secondMonitor.assertNoEvents(); |
| 6263 | |
| 6264 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6265 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6266 | SECOND_DISPLAY_ID, {110, 220})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6267 | << "The move event injected into the second display should fail because there's no " |
| 6268 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6269 | // Now the monitor on the second display should receive a cancel event. |
| 6270 | secondMonitor.consumeMotionCancel(SECOND_DISPLAY_ID); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6271 | |
| 6272 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6273 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6274 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 6275 | << "The move event injected into the first display should succeed"; |
| 6276 | |
| 6277 | window->consumeMotionMove(); |
| 6278 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6279 | |
| 6280 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6281 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 6282 | {110, 220})) |
| 6283 | << "The up event injected into the second display should fail because there's no " |
| 6284 | "touchable window"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6285 | |
| 6286 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6287 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6288 | {110, 220})) |
| 6289 | << "The up event injected into the first display should succeed"; |
| 6290 | |
| 6291 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6292 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6293 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6294 | window->assertNoEvents(); |
| 6295 | monitor.assertNoEvents(); |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6296 | secondWindow->assertNoEvents(); |
| 6297 | secondMonitor.assertNoEvents(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6298 | } |
| 6299 | |
| 6300 | /** |
| 6301 | * One display with transform |
| 6302 | * There is a foreground window and a monitor on the display |
| 6303 | * Inject down event and move event sequentially, the foreground window and monitor can receive down |
| 6304 | * event and move event, then let the foreground window go away, the foreground window receives |
| 6305 | * cancel event, inject move event again, the monitor receives cancel event, all the events received |
| 6306 | * by the monitor should be with the same transform as the display |
| 6307 | */ |
| 6308 | TEST_F(InputDispatcherMonitorTest, MonitorTouchCancelEventWithDisplayTransform) { |
| 6309 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6310 | sp<FakeWindowHandle> window = |
| 6311 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 6312 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6313 | |
| 6314 | ui::Transform transform; |
| 6315 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 6316 | |
| 6317 | gui::DisplayInfo displayInfo; |
| 6318 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 6319 | displayInfo.transform = transform; |
| 6320 | |
| 6321 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
| 6322 | |
| 6323 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6324 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6325 | {100, 200})) |
| 6326 | << "The down event injected should succeed"; |
| 6327 | |
| 6328 | window->consumeMotionDown(); |
| 6329 | std::unique_ptr<MotionEvent> downMotionEvent = monitor.consumeMotion(); |
| 6330 | EXPECT_EQ(transform, downMotionEvent->getTransform()); |
| 6331 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, downMotionEvent->getAction()); |
| 6332 | |
| 6333 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6334 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6335 | ADISPLAY_ID_DEFAULT, {110, 220})) |
| 6336 | << "The move event injected should succeed"; |
| 6337 | |
| 6338 | window->consumeMotionMove(); |
| 6339 | std::unique_ptr<MotionEvent> moveMotionEvent = monitor.consumeMotion(); |
| 6340 | EXPECT_EQ(transform, moveMotionEvent->getTransform()); |
| 6341 | EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, moveMotionEvent->getAction()); |
| 6342 | |
| 6343 | // Let foreground window gone |
| 6344 | mDispatcher->onWindowInfosChanged({{}, {displayInfo}, 0, 0}); |
| 6345 | |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6346 | // Foreground window should receive a cancel event, but not the monitor. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6347 | window->consumeMotionCancel(); |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6348 | |
| 6349 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6350 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6351 | ADISPLAY_ID_DEFAULT, {110, 220})) |
| 6352 | << "The move event injected should failed"; |
| 6353 | // Now foreground should not receive any events, but monitor should receive a cancel event |
| 6354 | // with transform that same as display's display. |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6355 | std::unique_ptr<MotionEvent> cancelMotionEvent = monitor.consumeMotion(); |
| 6356 | EXPECT_EQ(transform, cancelMotionEvent->getTransform()); |
| 6357 | EXPECT_EQ(ADISPLAY_ID_DEFAULT, cancelMotionEvent->getDisplayId()); |
| 6358 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, cancelMotionEvent->getAction()); |
| 6359 | |
| 6360 | // Other event inject to this display should fail. |
| 6361 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6362 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6363 | ADISPLAY_ID_DEFAULT, {110, 220})) |
Prabir Pradhan | a0d43d4 | 2024-01-30 00:27:08 +0000 | [diff] [blame] | 6364 | << "The up event injected should fail because the touched window was removed"; |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame] | 6365 | window->assertNoEvents(); |
| 6366 | monitor.assertNoEvents(); |
| 6367 | } |
| 6368 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6369 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6370 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6371 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6372 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6373 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6374 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6375 | |
| 6376 | NotifyMotionArgs motionArgs = |
| 6377 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6378 | ADISPLAY_ID_DEFAULT); |
| 6379 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6380 | mDispatcher->notifyMotion(motionArgs); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6381 | // Window should receive motion down event. |
| 6382 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6383 | |
| 6384 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6385 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6386 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6387 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 6388 | motionArgs.pointerCoords[0].getX() - 10); |
| 6389 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6390 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 6391 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT, /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6392 | } |
| 6393 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6394 | /** |
| 6395 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 6396 | * the device default right away. In the test scenario, we check both the default value, |
| 6397 | * and the action of enabling / disabling. |
| 6398 | */ |
| 6399 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6400 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6401 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6402 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6403 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6404 | |
| 6405 | // Set focused application. |
| 6406 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6407 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6408 | |
| 6409 | SCOPED_TRACE("Check default value of touch mode"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6410 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6411 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6412 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6413 | |
| 6414 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6415 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6416 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6417 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6418 | |
| 6419 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6420 | mDispatcher->setInTouchMode(false, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6421 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6422 | window->consumeTouchModeEvent(false); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6423 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6424 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6425 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6426 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6427 | |
| 6428 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6429 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6430 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6431 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6432 | |
| 6433 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6434 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6435 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6436 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6437 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6438 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6439 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6440 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6441 | |
| 6442 | window->assertNoEvents(); |
| 6443 | } |
| 6444 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6445 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6446 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6447 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6448 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6449 | |
| 6450 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6451 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6452 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6453 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6454 | setFocusedWindow(window); |
| 6455 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6456 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6457 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6458 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 6459 | mDispatcher->notifyKey(keyArgs); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6460 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6461 | std::unique_ptr<KeyEvent> event = window->consumeKey(); |
| 6462 | ASSERT_NE(event, nullptr); |
| 6463 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6464 | ASSERT_NE(verified, nullptr); |
| 6465 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 6466 | |
| 6467 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 6468 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 6469 | ASSERT_EQ(keyArgs.source, verified->source); |
| 6470 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 6471 | |
| 6472 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 6473 | |
| 6474 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6475 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6476 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6477 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 6478 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 6479 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 6480 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 6481 | } |
| 6482 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6483 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6484 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6485 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6486 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6487 | |
| 6488 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6489 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 6490 | ui::Transform transform; |
| 6491 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 6492 | |
| 6493 | gui::DisplayInfo displayInfo; |
| 6494 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 6495 | displayInfo.transform = transform; |
| 6496 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6497 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6498 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6499 | const NotifyMotionArgs motionArgs = |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6500 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6501 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6502 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6503 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6504 | std::unique_ptr<MotionEvent> event = window->consumeMotionEvent(); |
| 6505 | ASSERT_NE(nullptr, event); |
| 6506 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6507 | ASSERT_NE(verified, nullptr); |
| 6508 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 6509 | |
| 6510 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 6511 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 6512 | EXPECT_EQ(motionArgs.source, verified->source); |
| 6513 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 6514 | |
| 6515 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 6516 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 6517 | const vec2 rawXY = |
| 6518 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 6519 | motionArgs.pointerCoords[0].getXYValue()); |
| 6520 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 6521 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6522 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6523 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6524 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6525 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 6526 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 6527 | } |
| 6528 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 6529 | /** |
| 6530 | * Ensure that separate calls to sign the same data are generating the same key. |
| 6531 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 6532 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 6533 | * tests. |
| 6534 | */ |
| 6535 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 6536 | KeyEvent event = getTestKeyEvent(); |
| 6537 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 6538 | |
| 6539 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 6540 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 6541 | ASSERT_EQ(hmac1, hmac2); |
| 6542 | } |
| 6543 | |
| 6544 | /** |
| 6545 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 6546 | */ |
| 6547 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 6548 | KeyEvent event = getTestKeyEvent(); |
| 6549 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 6550 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 6551 | |
| 6552 | verifiedEvent.deviceId += 1; |
| 6553 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6554 | |
| 6555 | verifiedEvent.source += 1; |
| 6556 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6557 | |
| 6558 | verifiedEvent.eventTimeNanos += 1; |
| 6559 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6560 | |
| 6561 | verifiedEvent.displayId += 1; |
| 6562 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6563 | |
| 6564 | verifiedEvent.action += 1; |
| 6565 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6566 | |
| 6567 | verifiedEvent.downTimeNanos += 1; |
| 6568 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6569 | |
| 6570 | verifiedEvent.flags += 1; |
| 6571 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6572 | |
| 6573 | verifiedEvent.keyCode += 1; |
| 6574 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6575 | |
| 6576 | verifiedEvent.scanCode += 1; |
| 6577 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6578 | |
| 6579 | verifiedEvent.metaState += 1; |
| 6580 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6581 | |
| 6582 | verifiedEvent.repeatCount += 1; |
| 6583 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6584 | } |
| 6585 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6586 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 6587 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6588 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6589 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6590 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6591 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6592 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6593 | |
| 6594 | // Top window is also focusable but is not granted focus. |
| 6595 | windowTop->setFocusable(true); |
| 6596 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6597 | mDispatcher->onWindowInfosChanged( |
| 6598 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6599 | setFocusedWindow(windowSecond); |
| 6600 | |
| 6601 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6602 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6603 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6604 | |
| 6605 | // Focused window should receive event. |
| 6606 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6607 | windowTop->assertNoEvents(); |
| 6608 | } |
| 6609 | |
| 6610 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 6611 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6612 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6613 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6614 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6615 | |
| 6616 | window->setFocusable(true); |
| 6617 | // Release channel for window is no longer valid. |
| 6618 | window->releaseChannel(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6619 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6620 | setFocusedWindow(window); |
| 6621 | |
| 6622 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6623 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6624 | |
| 6625 | // window channel is invalid, so it should not receive any input event. |
| 6626 | window->assertNoEvents(); |
| 6627 | } |
| 6628 | |
| 6629 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 6630 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6631 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6632 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6633 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6634 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6635 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6636 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6637 | setFocusedWindow(window); |
| 6638 | |
| 6639 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6640 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6641 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6642 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6643 | window->assertNoEvents(); |
| 6644 | } |
| 6645 | |
| 6646 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 6647 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6648 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6649 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6650 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6651 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6652 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6653 | |
| 6654 | windowTop->setFocusable(true); |
| 6655 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6656 | mDispatcher->onWindowInfosChanged( |
| 6657 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6658 | setFocusedWindow(windowTop); |
| 6659 | windowTop->consumeFocusEvent(true); |
| 6660 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6661 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6662 | mDispatcher->onWindowInfosChanged( |
| 6663 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6664 | windowSecond->consumeFocusEvent(true); |
| 6665 | windowTop->consumeFocusEvent(false); |
| 6666 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6667 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6668 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6669 | |
| 6670 | // Focused window should receive event. |
| 6671 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6672 | } |
| 6673 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6674 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6675 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6676 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6677 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6678 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6679 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6680 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6681 | |
| 6682 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6683 | windowSecond->setFocusable(false); |
| 6684 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6685 | mDispatcher->onWindowInfosChanged( |
| 6686 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6687 | setFocusedWindow(windowTop); |
| 6688 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6689 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6690 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6691 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6692 | |
| 6693 | // Event should be dropped. |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6694 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6695 | windowSecond->assertNoEvents(); |
| 6696 | } |
| 6697 | |
| 6698 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 6699 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6700 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6701 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6702 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6703 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 6704 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6705 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6706 | |
| 6707 | window->setFocusable(true); |
| 6708 | previousFocusedWindow->setFocusable(true); |
| 6709 | window->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6710 | mDispatcher->onWindowInfosChanged( |
| 6711 | {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6712 | setFocusedWindow(previousFocusedWindow); |
| 6713 | previousFocusedWindow->consumeFocusEvent(true); |
| 6714 | |
| 6715 | // Requesting focus on invisible window takes focus from currently focused window. |
| 6716 | setFocusedWindow(window); |
| 6717 | previousFocusedWindow->consumeFocusEvent(false); |
| 6718 | |
| 6719 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6720 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6721 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 6722 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6723 | |
| 6724 | // Window does not get focus event or key down. |
| 6725 | window->assertNoEvents(); |
| 6726 | |
| 6727 | // Window becomes visible. |
| 6728 | window->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6729 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6730 | |
| 6731 | // Window receives focus event. |
| 6732 | window->consumeFocusEvent(true); |
| 6733 | // Focused window receives key down. |
| 6734 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6735 | } |
| 6736 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6737 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 6738 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6739 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6740 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6741 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6742 | |
| 6743 | // window is granted focus. |
| 6744 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6745 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6746 | setFocusedWindow(window); |
| 6747 | window->consumeFocusEvent(true); |
| 6748 | |
| 6749 | // When a display is removed window loses focus. |
| 6750 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 6751 | window->consumeFocusEvent(false); |
| 6752 | } |
| 6753 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6754 | /** |
| 6755 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 6756 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 6757 | * of the 'slipperyEnterWindow'. |
| 6758 | * |
| 6759 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 6760 | * a way so that the touched location is no longer covered by the top window. |
| 6761 | * |
| 6762 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 6763 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 6764 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 6765 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 6766 | * with ACTION_DOWN). |
| 6767 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 6768 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 6769 | * |
| 6770 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 6771 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 6772 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 6773 | * |
| 6774 | * In this test, we ensure that the event received by the bottom window has |
| 6775 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 6776 | */ |
| 6777 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6778 | constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6779 | constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1}; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6780 | |
| 6781 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6782 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6783 | |
| 6784 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6785 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6786 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6787 | // Make sure this one overlaps the bottom window |
| 6788 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 6789 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 6790 | // one. Windows with the same owner are not considered to be occluding each other. |
| 6791 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 6792 | |
| 6793 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6794 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6795 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6796 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6797 | mDispatcher->onWindowInfosChanged( |
| 6798 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6799 | |
| 6800 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6801 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 6802 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6803 | {{50, 50}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6804 | slipperyExitWindow->consumeMotionDown(); |
| 6805 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6806 | mDispatcher->onWindowInfosChanged( |
| 6807 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6808 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6809 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
| 6810 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6811 | {{51, 51}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6812 | |
| 6813 | slipperyExitWindow->consumeMotionCancel(); |
| 6814 | |
| 6815 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 6816 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 6817 | } |
| 6818 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 6819 | /** |
| 6820 | * Two windows, one on the left and another on the right. The left window is slippery. The right |
| 6821 | * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the |
| 6822 | * touch moves from the left window into the right window, the gesture should continue to go to the |
| 6823 | * left window. Touch shouldn't slip because the right window can't receive touches. This test |
| 6824 | * reproduces a crash. |
| 6825 | */ |
| 6826 | TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { |
| 6827 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6828 | |
| 6829 | sp<FakeWindowHandle> leftSlipperyWindow = |
| 6830 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 6831 | leftSlipperyWindow->setSlippery(true); |
| 6832 | leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6833 | |
| 6834 | sp<FakeWindowHandle> rightDropTouchesWindow = |
| 6835 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 6836 | rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100)); |
| 6837 | rightDropTouchesWindow->setDropInput(true); |
| 6838 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6839 | mDispatcher->onWindowInfosChanged( |
| 6840 | {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 6841 | |
| 6842 | // Start touch in the left window |
| 6843 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6844 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6845 | .build()); |
| 6846 | leftSlipperyWindow->consumeMotionDown(); |
| 6847 | |
| 6848 | // And move it into the right window |
| 6849 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6850 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 6851 | .build()); |
| 6852 | |
| 6853 | // Since the right window isn't eligible to receive input, touch does not slip. |
| 6854 | // The left window continues to receive the gesture. |
| 6855 | leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 6856 | rightDropTouchesWindow->assertNoEvents(); |
| 6857 | } |
| 6858 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 6859 | /** |
| 6860 | * A single window is on screen first. Touch is injected into that window. Next, a second window |
| 6861 | * appears. Since the first window is slippery, touch will move from the first window to the second. |
| 6862 | */ |
| 6863 | TEST_F(InputDispatcherTest, InjectedTouchSlips) { |
| 6864 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6865 | sp<FakeWindowHandle> originalWindow = |
| 6866 | sp<FakeWindowHandle>::make(application, mDispatcher, "Original", ADISPLAY_ID_DEFAULT); |
| 6867 | originalWindow->setFrame(Rect(0, 0, 200, 200)); |
| 6868 | originalWindow->setSlippery(true); |
| 6869 | |
| 6870 | sp<FakeWindowHandle> appearingWindow = |
| 6871 | sp<FakeWindowHandle>::make(application, mDispatcher, "Appearing", ADISPLAY_ID_DEFAULT); |
| 6872 | appearingWindow->setFrame(Rect(0, 0, 200, 200)); |
| 6873 | |
| 6874 | mDispatcher->onWindowInfosChanged({{*originalWindow->getInfo()}, {}, 0, 0}); |
| 6875 | |
| 6876 | // Touch down on the original window |
| 6877 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6878 | injectMotionEvent(*mDispatcher, |
| 6879 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6880 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 6881 | .build())); |
| 6882 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 6883 | |
| 6884 | // Now, a new window appears. This could be, for example, a notification shade that appears |
| 6885 | // after user starts to drag down on the launcher window. |
| 6886 | mDispatcher->onWindowInfosChanged( |
| 6887 | {{*appearingWindow->getInfo(), *originalWindow->getInfo()}, {}, 0, 0}); |
| 6888 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6889 | injectMotionEvent(*mDispatcher, |
| 6890 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6891 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(110)) |
| 6892 | .build())); |
| 6893 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 6894 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 6895 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6896 | injectMotionEvent(*mDispatcher, |
| 6897 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6898 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 6899 | .build())); |
| 6900 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 6901 | |
| 6902 | originalWindow->assertNoEvents(); |
| 6903 | appearingWindow->assertNoEvents(); |
| 6904 | } |
| 6905 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6906 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6907 | using Uid = gui::Uid; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6908 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6909 | |
| 6910 | sp<FakeWindowHandle> leftWindow = |
| 6911 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 6912 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6913 | leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6914 | |
| 6915 | sp<FakeWindowHandle> rightSpy = |
| 6916 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT); |
| 6917 | rightSpy->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6918 | rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6919 | rightSpy->setSpy(true); |
| 6920 | rightSpy->setTrustedOverlay(true); |
| 6921 | |
| 6922 | sp<FakeWindowHandle> rightWindow = |
| 6923 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 6924 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6925 | rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6926 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6927 | mDispatcher->onWindowInfosChanged( |
| 6928 | {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6929 | |
| 6930 | // Touch in the left window |
| 6931 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6932 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6933 | .build()); |
| 6934 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown()); |
| 6935 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6936 | ASSERT_NO_FATAL_FAILURE( |
| 6937 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6938 | |
| 6939 | // Touch another finger over the right windows |
| 6940 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6941 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6942 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6943 | .build()); |
| 6944 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown()); |
| 6945 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown()); |
| 6946 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove()); |
| 6947 | mDispatcher->waitForIdle(); |
| 6948 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6949 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, |
| 6950 | {Uid{101}, Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6951 | |
| 6952 | // Release finger over left window. The UP actions are not treated as device interaction. |
| 6953 | // The windows that did not receive the UP pointer will receive MOVE events, but since this |
| 6954 | // is part of the UP action, we do not treat this as device interaction. |
| 6955 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6956 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6957 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6958 | .build()); |
| 6959 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp()); |
| 6960 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 6961 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 6962 | mDispatcher->waitForIdle(); |
| 6963 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 6964 | |
| 6965 | // Move remaining finger |
| 6966 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6967 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6968 | .build()); |
| 6969 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 6970 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 6971 | mDispatcher->waitForIdle(); |
| 6972 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6973 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6974 | |
| 6975 | // Release all fingers |
| 6976 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6977 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6978 | .build()); |
| 6979 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp()); |
| 6980 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp()); |
| 6981 | mDispatcher->waitForIdle(); |
| 6982 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 6983 | } |
| 6984 | |
| 6985 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) { |
| 6986 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6987 | |
| 6988 | sp<FakeWindowHandle> window = |
| 6989 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 6990 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6991 | window->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6992 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6993 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6994 | setFocusedWindow(window); |
| 6995 | ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true)); |
| 6996 | |
| 6997 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build()); |
| 6998 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT)); |
| 6999 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7000 | ASSERT_NO_FATAL_FAILURE( |
| 7001 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7002 | |
| 7003 | // The UP actions are not treated as device interaction. |
| 7004 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build()); |
| 7005 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT)); |
| 7006 | mDispatcher->waitForIdle(); |
| 7007 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 7008 | } |
| 7009 | |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7010 | TEST_F(InputDispatcherTest, HoverEnterExitSynthesisUsesNewEventId) { |
| 7011 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7012 | |
| 7013 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 7014 | ADISPLAY_ID_DEFAULT); |
| 7015 | left->setFrame(Rect(0, 0, 100, 100)); |
| 7016 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7017 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 7018 | right->setFrame(Rect(100, 0, 200, 100)); |
| 7019 | sp<FakeWindowHandle> spy = |
| 7020 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT); |
| 7021 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 7022 | spy->setTrustedOverlay(true); |
| 7023 | spy->setSpy(true); |
| 7024 | |
| 7025 | mDispatcher->onWindowInfosChanged( |
| 7026 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 7027 | |
| 7028 | // Send hover move to the left window, and ensure hover enter is synthesized with a new eventId. |
| 7029 | NotifyMotionArgs notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 7030 | ADISPLAY_ID_DEFAULT, {PointF{50, 50}}); |
| 7031 | mDispatcher->notifyMotion(notifyArgs); |
| 7032 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7033 | std::unique_ptr<MotionEvent> leftEnter = left->consumeMotionEvent( |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7034 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), Not(WithEventId(notifyArgs.id)), |
| 7035 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7036 | ASSERT_NE(nullptr, leftEnter); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7037 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 7038 | Not(WithEventId(notifyArgs.id)), |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7039 | Not(WithEventId(leftEnter->getId())), |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7040 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 7041 | |
| 7042 | // Send move to the right window, and ensure hover exit and enter are synthesized with new ids. |
| 7043 | notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT, |
| 7044 | {PointF{150, 50}}); |
| 7045 | mDispatcher->notifyMotion(notifyArgs); |
| 7046 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7047 | std::unique_ptr<MotionEvent> leftExit = left->consumeMotionEvent( |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7048 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), Not(WithEventId(notifyArgs.id)), |
| 7049 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7050 | ASSERT_NE(nullptr, leftExit); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7051 | right->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 7052 | Not(WithEventId(notifyArgs.id)), |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7053 | Not(WithEventId(leftExit->getId())), |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7054 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 7055 | |
| 7056 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithEventId(notifyArgs.id))); |
| 7057 | } |
| 7058 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 7059 | class InputDispatcherFallbackKeyTest : public InputDispatcherTest { |
| 7060 | protected: |
| 7061 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7062 | sp<FakeWindowHandle> mWindow; |
| 7063 | |
| 7064 | virtual void SetUp() override { |
| 7065 | InputDispatcherTest::SetUp(); |
| 7066 | |
| 7067 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 7068 | |
| 7069 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 7070 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7071 | |
| 7072 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 7073 | setFocusedWindow(mWindow); |
| 7074 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeFocusEvent(/*hasFocus=*/true)); |
| 7075 | } |
| 7076 | |
| 7077 | void setFallback(int32_t keycode) { |
| 7078 | mFakePolicy->setUnhandledKeyHandler([keycode](const KeyEvent& event) { |
| 7079 | return KeyEventBuilder(event).keyCode(keycode).build(); |
| 7080 | }); |
| 7081 | } |
| 7082 | |
| 7083 | void consumeKey(bool handled, const ::testing::Matcher<KeyEvent>& matcher) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7084 | std::unique_ptr<KeyEvent> event = mWindow->consumeKey(handled); |
| 7085 | ASSERT_NE(nullptr, event); |
| 7086 | ASSERT_THAT(*event, matcher); |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 7087 | } |
| 7088 | }; |
| 7089 | |
| 7090 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotNotifiedForHandledKey) { |
| 7091 | mDispatcher->notifyKey( |
| 7092 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7093 | consumeKey(/*handled=*/true, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 7094 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7095 | } |
| 7096 | |
| 7097 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotifiedForUnhandledKey) { |
| 7098 | mDispatcher->notifyKey( |
| 7099 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7100 | consumeKey(/*handled=*/false, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 7101 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7102 | } |
| 7103 | |
| 7104 | TEST_F(InputDispatcherFallbackKeyTest, NoFallbackRequestedByPolicy) { |
| 7105 | mDispatcher->notifyKey( |
| 7106 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7107 | |
| 7108 | // Do not handle this key event. |
| 7109 | consumeKey(/*handled=*/false, |
| 7110 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7111 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7112 | |
| 7113 | // Since the policy did not request any fallback to be generated, ensure there are no events. |
| 7114 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7115 | } |
| 7116 | |
| 7117 | TEST_F(InputDispatcherFallbackKeyTest, FallbackDispatchForUnhandledKey) { |
| 7118 | setFallback(AKEYCODE_B); |
| 7119 | mDispatcher->notifyKey( |
| 7120 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7121 | |
| 7122 | // Do not handle this key event. |
| 7123 | consumeKey(/*handled=*/false, |
| 7124 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7125 | |
| 7126 | // Since the key was not handled, ensure the fallback event was dispatched instead. |
| 7127 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7128 | consumeKey(/*handled=*/true, |
| 7129 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7130 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7131 | |
| 7132 | // Release the original key, and ensure the fallback key is also released. |
| 7133 | mDispatcher->notifyKey( |
| 7134 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7135 | consumeKey(/*handled=*/false, |
| 7136 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7137 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7138 | consumeKey(/*handled=*/true, |
| 7139 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7140 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7141 | |
| 7142 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7143 | mWindow->assertNoEvents(); |
| 7144 | } |
| 7145 | |
| 7146 | TEST_F(InputDispatcherFallbackKeyTest, AppHandlesPreviouslyUnhandledKey) { |
| 7147 | setFallback(AKEYCODE_B); |
| 7148 | mDispatcher->notifyKey( |
| 7149 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7150 | |
| 7151 | // Do not handle this key event, but handle the fallback. |
| 7152 | consumeKey(/*handled=*/false, |
| 7153 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7154 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7155 | consumeKey(/*handled=*/true, |
| 7156 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7157 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7158 | |
| 7159 | // Release the original key, and ensure the fallback key is also released. |
| 7160 | mDispatcher->notifyKey( |
| 7161 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7162 | // But this time, the app handles the original key. |
| 7163 | consumeKey(/*handled=*/true, |
| 7164 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7165 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7166 | // Ensure the fallback key is canceled. |
| 7167 | consumeKey(/*handled=*/true, |
| 7168 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7169 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 7170 | |
| 7171 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7172 | mWindow->assertNoEvents(); |
| 7173 | } |
| 7174 | |
| 7175 | TEST_F(InputDispatcherFallbackKeyTest, AppDoesNotHandleFallback) { |
| 7176 | setFallback(AKEYCODE_B); |
| 7177 | mDispatcher->notifyKey( |
| 7178 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7179 | |
| 7180 | // Do not handle this key event. |
| 7181 | consumeKey(/*handled=*/false, |
| 7182 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7183 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7184 | // App does not handle the fallback either, so ensure another fallback is not generated. |
| 7185 | setFallback(AKEYCODE_C); |
| 7186 | consumeKey(/*handled=*/false, |
| 7187 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7188 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7189 | |
| 7190 | // Release the original key, and ensure the fallback key is also released. |
| 7191 | setFallback(AKEYCODE_B); |
| 7192 | mDispatcher->notifyKey( |
| 7193 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7194 | consumeKey(/*handled=*/false, |
| 7195 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7196 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7197 | consumeKey(/*handled=*/false, |
| 7198 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7199 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7200 | |
| 7201 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7202 | mWindow->assertNoEvents(); |
| 7203 | } |
| 7204 | |
| 7205 | TEST_F(InputDispatcherFallbackKeyTest, InconsistentPolicyCancelsFallback) { |
| 7206 | setFallback(AKEYCODE_B); |
| 7207 | mDispatcher->notifyKey( |
| 7208 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7209 | |
| 7210 | // Do not handle this key event, so fallback is generated. |
| 7211 | consumeKey(/*handled=*/false, |
| 7212 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7213 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7214 | consumeKey(/*handled=*/true, |
| 7215 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7216 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7217 | |
| 7218 | // Release the original key, but assume the policy is misbehaving and it |
| 7219 | // generates an inconsistent fallback to the one from the DOWN event. |
| 7220 | setFallback(AKEYCODE_C); |
| 7221 | mDispatcher->notifyKey( |
| 7222 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7223 | consumeKey(/*handled=*/false, |
| 7224 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7225 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7226 | // Ensure the fallback key reported before as DOWN is canceled due to the inconsistency. |
| 7227 | consumeKey(/*handled=*/true, |
| 7228 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7229 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 7230 | |
| 7231 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7232 | mWindow->assertNoEvents(); |
| 7233 | } |
| 7234 | |
| 7235 | TEST_F(InputDispatcherFallbackKeyTest, CanceledKeyCancelsFallback) { |
| 7236 | setFallback(AKEYCODE_B); |
| 7237 | mDispatcher->notifyKey( |
| 7238 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7239 | |
| 7240 | // Do not handle this key event, so fallback is generated. |
| 7241 | consumeKey(/*handled=*/false, |
| 7242 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7243 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7244 | consumeKey(/*handled=*/true, |
| 7245 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7246 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7247 | |
| 7248 | // The original key is canceled. |
| 7249 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
| 7250 | .keyCode(AKEYCODE_A) |
| 7251 | .addFlag(AKEY_EVENT_FLAG_CANCELED) |
| 7252 | .build()); |
| 7253 | consumeKey(/*handled=*/false, |
| 7254 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), |
| 7255 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 7256 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7257 | // Ensure the fallback key is also canceled due to the original key being canceled. |
| 7258 | consumeKey(/*handled=*/true, |
| 7259 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7260 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 7261 | |
| 7262 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7263 | mWindow->assertNoEvents(); |
| 7264 | } |
| 7265 | |
Prabir Pradhan | b13da8f | 2024-01-09 23:10:13 +0000 | [diff] [blame] | 7266 | TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedDuringPolicyCall) { |
| 7267 | setFallback(AKEYCODE_B); |
| 7268 | mDispatcher->notifyKey( |
| 7269 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7270 | |
| 7271 | // Do not handle this key event. |
| 7272 | consumeKey(/*handled=*/false, |
| 7273 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7274 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7275 | consumeKey(/*handled=*/true, |
| 7276 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7277 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7278 | |
| 7279 | mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) { |
| 7280 | // When the unhandled key is reported to the policy next, remove the input channel. |
| 7281 | mDispatcher->removeInputChannel(mWindow->getToken()); |
| 7282 | return KeyEventBuilder(event).keyCode(AKEYCODE_B).build(); |
| 7283 | }); |
| 7284 | // Release the original key, and let the app now handle the previously unhandled key. |
| 7285 | // This should result in the previously generated fallback key to be cancelled. |
| 7286 | // Since the policy was notified of the unhandled DOWN event earlier, it will also be notified |
| 7287 | // of the UP event for consistency. The Dispatcher calls into the policy from its own thread |
| 7288 | // without holding the lock, because it need to synchronously fetch the fallback key. While in |
| 7289 | // the policy call, we will now remove the input channel. Once the policy call returns, the |
| 7290 | // Dispatcher will no longer have a channel to send cancellation events to. Ensure this does |
| 7291 | // not cause any crashes. |
| 7292 | mDispatcher->notifyKey( |
| 7293 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7294 | consumeKey(/*handled=*/true, |
| 7295 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7296 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7297 | } |
| 7298 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7299 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 7300 | protected: |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 7301 | static constexpr std::chrono::nanoseconds KEY_REPEAT_TIMEOUT = 40ms; |
| 7302 | static constexpr std::chrono::nanoseconds KEY_REPEAT_DELAY = 40ms; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7303 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7304 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7305 | sp<FakeWindowHandle> mWindow; |
| 7306 | |
| 7307 | virtual void SetUp() override { |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 7308 | InputDispatcherTest::SetUp(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7309 | |
Prabir Pradhan | dae5279 | 2023-12-15 07:36:40 +0000 | [diff] [blame] | 7310 | mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7311 | setUpWindow(); |
| 7312 | } |
| 7313 | |
| 7314 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7315 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7316 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7317 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7318 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7319 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7320 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7321 | mWindow->consumeFocusEvent(true); |
| 7322 | } |
| 7323 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7324 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7325 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7326 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7327 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7328 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7329 | |
| 7330 | // Window should receive key down event. |
| 7331 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7332 | } |
| 7333 | |
| 7334 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 7335 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7336 | mWindow->consumeKeyEvent( |
| 7337 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount))); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7338 | } |
| 7339 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7340 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7341 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7342 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7343 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7344 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7345 | |
| 7346 | // Window should receive key down event. |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7347 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7348 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7349 | } |
| 7350 | }; |
| 7351 | |
| 7352 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7353 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7354 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 7355 | expectKeyRepeatOnce(repeatCount); |
| 7356 | } |
| 7357 | } |
| 7358 | |
| 7359 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7360 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7361 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 7362 | expectKeyRepeatOnce(repeatCount); |
| 7363 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7364 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7365 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7366 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 7367 | expectKeyRepeatOnce(repeatCount); |
| 7368 | } |
| 7369 | } |
| 7370 | |
| 7371 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7372 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 7373 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 7374 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7375 | mWindow->assertNoEvents(); |
| 7376 | } |
| 7377 | |
| 7378 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7379 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 7380 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 7381 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 7382 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7383 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7384 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7385 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7386 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 7387 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7388 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7389 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7390 | mWindow->assertNoEvents(); |
| 7391 | } |
| 7392 | |
| 7393 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7394 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 7395 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 7396 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 7397 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7398 | // 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] | 7399 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7400 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7401 | mWindow->assertNoEvents(); |
| 7402 | } |
| 7403 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 7404 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 7405 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7406 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7407 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 7408 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 7409 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 7410 | mWindow->assertNoEvents(); |
| 7411 | } |
| 7412 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7413 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 7414 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7415 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7416 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7417 | std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey(); |
| 7418 | ASSERT_NE(nullptr, repeatEvent); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7419 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7420 | IdGenerator::getSource(repeatEvent->getId())); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7421 | } |
| 7422 | } |
| 7423 | |
| 7424 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 7425 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7426 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7427 | |
| 7428 | std::unordered_set<int32_t> idSet; |
| 7429 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7430 | std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey(); |
| 7431 | ASSERT_NE(nullptr, repeatEvent); |
| 7432 | int32_t id = repeatEvent->getId(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7433 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 7434 | idSet.insert(id); |
| 7435 | } |
| 7436 | } |
| 7437 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7438 | /* Test InputDispatcher for MultiDisplay */ |
| 7439 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 7440 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7441 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7442 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7443 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7444 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7445 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7446 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7447 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7448 | // Set focus window for primary display, but focused display would be second one. |
| 7449 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7450 | windowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7451 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
| 7452 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7453 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7454 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7455 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7456 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7457 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7458 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7459 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7460 | // Set focus display to second one. |
| 7461 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 7462 | // Set focus window for second display. |
| 7463 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7464 | windowInSecondary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7465 | mDispatcher->onWindowInfosChanged( |
| 7466 | {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7467 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7468 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7469 | } |
| 7470 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7471 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7472 | InputDispatcherTest::TearDown(); |
| 7473 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7474 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7475 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7476 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7477 | windowInSecondary.clear(); |
| 7478 | } |
| 7479 | |
| 7480 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7481 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7482 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7483 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7484 | sp<FakeWindowHandle> windowInSecondary; |
| 7485 | }; |
| 7486 | |
| 7487 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 7488 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7489 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7490 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7491 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7492 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7493 | windowInSecondary->assertNoEvents(); |
| 7494 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7495 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7496 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7497 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7498 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7499 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7500 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7501 | } |
| 7502 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7503 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 7504 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 7505 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7506 | injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7507 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7508 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 7509 | windowInSecondary->assertNoEvents(); |
| 7510 | |
| 7511 | // Test inject a key down without display id specified. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7512 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7513 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7514 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7515 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7516 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7517 | // Remove all windows in secondary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7518 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7519 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7520 | // Old focus should receive a cancel event. |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7521 | windowInSecondary->consumeKeyUp(ADISPLAY_ID_NONE, AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7522 | |
| 7523 | // Test inject a key down, should timeout because of no target window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7524 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7525 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7526 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7527 | windowInSecondary->assertNoEvents(); |
| 7528 | } |
| 7529 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7530 | // Test per-display input monitors for motion event. |
| 7531 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7532 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7533 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7534 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7535 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7536 | |
| 7537 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7538 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7539 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7540 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7541 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7542 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7543 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7544 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7545 | |
| 7546 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7547 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7548 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7549 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7550 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7551 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7552 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7553 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7554 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 7555 | // Lift up the touch from the second display |
| 7556 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7557 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 7558 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7559 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 7560 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 7561 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7562 | // Test inject a non-pointer motion event. |
| 7563 | // If specific a display, it will dispatch to the focused window of particular display, |
| 7564 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7565 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7566 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7567 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7568 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7569 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7570 | windowInSecondary->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7571 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7572 | } |
| 7573 | |
| 7574 | // Test per-display input monitors for key event. |
| 7575 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7576 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7577 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7578 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7579 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7580 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7581 | |
| 7582 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7583 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7584 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7585 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7586 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7587 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7588 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7589 | } |
| 7590 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7591 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 7592 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7593 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7594 | secondWindowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7595 | mDispatcher->onWindowInfosChanged( |
| 7596 | {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(), |
| 7597 | *windowInSecondary->getInfo()}, |
| 7598 | {}, |
| 7599 | 0, |
| 7600 | 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7601 | setFocusedWindow(secondWindowInPrimary); |
| 7602 | windowInPrimary->consumeFocusEvent(false); |
| 7603 | secondWindowInPrimary->consumeFocusEvent(true); |
| 7604 | |
| 7605 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7606 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7607 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7608 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7609 | windowInPrimary->assertNoEvents(); |
| 7610 | windowInSecondary->assertNoEvents(); |
| 7611 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7612 | } |
| 7613 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7614 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 7615 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7616 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7617 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7618 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7619 | |
| 7620 | // Test touch down on primary display. |
| 7621 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7622 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7623 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7624 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7625 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7626 | |
| 7627 | // Test touch down on second display. |
| 7628 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7629 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7630 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7631 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 7632 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 7633 | |
| 7634 | // Trigger cancel touch. |
| 7635 | mDispatcher->cancelCurrentTouch(); |
| 7636 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 7637 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 7638 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 7639 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 7640 | |
| 7641 | // Test inject a move motion event, no window/monitor should receive the event. |
| 7642 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7643 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7644 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 7645 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 7646 | windowInPrimary->assertNoEvents(); |
| 7647 | monitorInPrimary.assertNoEvents(); |
| 7648 | |
| 7649 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7650 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7651 | SECOND_DISPLAY_ID, {110, 200})) |
| 7652 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 7653 | windowInSecondary->assertNoEvents(); |
| 7654 | monitorInSecondary.assertNoEvents(); |
| 7655 | } |
| 7656 | |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 7657 | /** |
| 7658 | * Send a key to the primary display and to the secondary display. |
| 7659 | * Then cause the key on the primary display to be canceled by sending in a stale key. |
| 7660 | * Ensure that the key on the primary display is canceled, and that the key on the secondary display |
| 7661 | * does not get canceled. |
| 7662 | */ |
| 7663 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture) { |
| 7664 | // Send a key down on primary display |
| 7665 | mDispatcher->notifyKey( |
| 7666 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 7667 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7668 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 7669 | .build()); |
| 7670 | windowInPrimary->consumeKeyEvent( |
| 7671 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 7672 | windowInSecondary->assertNoEvents(); |
| 7673 | |
| 7674 | // Send a key down on second display |
| 7675 | mDispatcher->notifyKey( |
| 7676 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 7677 | .displayId(SECOND_DISPLAY_ID) |
| 7678 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 7679 | .build()); |
| 7680 | windowInSecondary->consumeKeyEvent( |
| 7681 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID))); |
| 7682 | windowInPrimary->assertNoEvents(); |
| 7683 | |
| 7684 | // Send a valid key up event on primary display that will be dropped because it is stale |
| 7685 | NotifyKeyArgs staleKeyUp = |
| 7686 | KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
| 7687 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7688 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 7689 | .build(); |
| 7690 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms; |
| 7691 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
| 7692 | std::this_thread::sleep_for(STALE_EVENT_TIMEOUT); |
| 7693 | mDispatcher->notifyKey(staleKeyUp); |
| 7694 | |
| 7695 | // Only the key gesture corresponding to the dropped event should receive the cancel event. |
| 7696 | // Therefore, windowInPrimary should get the cancel event and windowInSecondary should not |
| 7697 | // receive any events. |
| 7698 | windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), |
| 7699 | WithDisplayId(ADISPLAY_ID_DEFAULT), |
| 7700 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 7701 | windowInSecondary->assertNoEvents(); |
| 7702 | } |
| 7703 | |
| 7704 | /** |
| 7705 | * Similar to 'WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture' but for motion events. |
| 7706 | */ |
| 7707 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropMotionEvent_OnlyCancelCorrespondingGesture) { |
| 7708 | // Send touch down on primary display. |
| 7709 | mDispatcher->notifyMotion( |
| 7710 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7711 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 7712 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7713 | .build()); |
| 7714 | windowInPrimary->consumeMotionEvent( |
| 7715 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 7716 | windowInSecondary->assertNoEvents(); |
| 7717 | |
| 7718 | // Send touch down on second display. |
| 7719 | mDispatcher->notifyMotion( |
| 7720 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7721 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 7722 | .displayId(SECOND_DISPLAY_ID) |
| 7723 | .build()); |
| 7724 | windowInPrimary->assertNoEvents(); |
| 7725 | windowInSecondary->consumeMotionEvent( |
| 7726 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID))); |
| 7727 | |
| 7728 | // inject a valid MotionEvent on primary display that will be stale when it arrives. |
| 7729 | NotifyMotionArgs staleMotionUp = |
| 7730 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7731 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7732 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 7733 | .build(); |
| 7734 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms; |
| 7735 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
| 7736 | std::this_thread::sleep_for(STALE_EVENT_TIMEOUT); |
| 7737 | mDispatcher->notifyMotion(staleMotionUp); |
| 7738 | |
| 7739 | // For stale motion events, we let the gesture to complete. This behaviour is different from key |
| 7740 | // events, where we would cancel the current keys instead. |
| 7741 | windowInPrimary->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 7742 | windowInSecondary->assertNoEvents(); |
| 7743 | } |
| 7744 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7745 | class InputFilterTest : public InputDispatcherTest { |
| 7746 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7747 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 7748 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7749 | NotifyMotionArgs motionArgs; |
| 7750 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7751 | motionArgs = |
| 7752 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7753 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7754 | motionArgs = |
| 7755 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7756 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7757 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7758 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 7759 | const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue()); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7760 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7761 | } else { |
| 7762 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 7763 | } |
| 7764 | } |
| 7765 | |
| 7766 | void testNotifyKey(bool expectToBeFiltered) { |
| 7767 | NotifyKeyArgs keyArgs; |
| 7768 | |
| 7769 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7770 | mDispatcher->notifyKey(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7771 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7772 | mDispatcher->notifyKey(keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7773 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7774 | |
| 7775 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 7776 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7777 | } else { |
| 7778 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 7779 | } |
| 7780 | } |
| 7781 | }; |
| 7782 | |
| 7783 | // Test InputFilter for MotionEvent |
| 7784 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 7785 | // 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] | 7786 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false); |
| 7787 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7788 | |
| 7789 | // Enable InputFilter |
| 7790 | mDispatcher->setInputFilterEnabled(true); |
| 7791 | // 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] | 7792 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true); |
| 7793 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7794 | |
| 7795 | // Disable InputFilter |
| 7796 | mDispatcher->setInputFilterEnabled(false); |
| 7797 | // 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] | 7798 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false); |
| 7799 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7800 | } |
| 7801 | |
| 7802 | // Test InputFilter for KeyEvent |
| 7803 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 7804 | // 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] | 7805 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7806 | |
| 7807 | // Enable InputFilter |
| 7808 | mDispatcher->setInputFilterEnabled(true); |
| 7809 | // Send a key event, and check if it is filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7810 | testNotifyKey(/*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7811 | |
| 7812 | // Disable InputFilter |
| 7813 | mDispatcher->setInputFilterEnabled(false); |
| 7814 | // Send a key event, and check if it isn't filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7815 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7816 | } |
| 7817 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7818 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 7819 | // logical display coordinate space. |
| 7820 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 7821 | ui::Transform firstDisplayTransform; |
| 7822 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 7823 | ui::Transform secondDisplayTransform; |
| 7824 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 7825 | |
| 7826 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 7827 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 7828 | displayInfos[0].transform = firstDisplayTransform; |
| 7829 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 7830 | displayInfos[1].transform = secondDisplayTransform; |
| 7831 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 7832 | mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0}); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7833 | |
| 7834 | // Enable InputFilter |
| 7835 | mDispatcher->setInputFilterEnabled(true); |
| 7836 | |
| 7837 | // Ensure the correct transforms are used for the displays. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7838 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true, firstDisplayTransform); |
| 7839 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7840 | } |
| 7841 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7842 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 7843 | protected: |
| 7844 | virtual void SetUp() override { |
| 7845 | InputDispatcherTest::SetUp(); |
| 7846 | |
| 7847 | /** |
| 7848 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 7849 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 7850 | * on. |
| 7851 | */ |
| 7852 | mDispatcher->setInputFilterEnabled(true); |
| 7853 | |
| 7854 | std::shared_ptr<InputApplicationHandle> application = |
| 7855 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7856 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 7857 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7858 | |
| 7859 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7860 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7861 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7862 | setFocusedWindow(mWindow); |
| 7863 | mWindow->consumeFocusEvent(true); |
| 7864 | } |
| 7865 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7866 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 7867 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7868 | KeyEvent event; |
| 7869 | |
| 7870 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 7871 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 7872 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7873 | KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7874 | const int32_t additionalPolicyFlags = |
| 7875 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 7876 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7877 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 7878 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7879 | policyFlags | additionalPolicyFlags)); |
| 7880 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7881 | mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags))); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7882 | } |
| 7883 | |
| 7884 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 7885 | int32_t flags) { |
| 7886 | MotionEvent event; |
| 7887 | PointerProperties pointerProperties[1]; |
| 7888 | PointerCoords pointerCoords[1]; |
| 7889 | pointerProperties[0].clear(); |
| 7890 | pointerProperties[0].id = 0; |
| 7891 | pointerCoords[0].clear(); |
| 7892 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 7893 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 7894 | |
| 7895 | ui::Transform identityTransform; |
| 7896 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 7897 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 7898 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 7899 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 7900 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 7901 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 7902 | eventTime, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7903 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7904 | |
| 7905 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 7906 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7907 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 7908 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7909 | policyFlags | additionalPolicyFlags)); |
| 7910 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7911 | mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId))); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7912 | } |
| 7913 | |
| 7914 | private: |
| 7915 | sp<FakeWindowHandle> mWindow; |
| 7916 | }; |
| 7917 | |
| 7918 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7919 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 7920 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 7921 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7922 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 7923 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7924 | } |
| 7925 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7926 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7927 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7928 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7929 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 7930 | } |
| 7931 | |
| 7932 | TEST_F(InputFilterInjectionPolicyTest, |
| 7933 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 7934 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7935 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7936 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7937 | } |
| 7938 | |
| 7939 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7940 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 7941 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7942 | } |
| 7943 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 7944 | class InputDispatcherUserActivityPokeTests : public InputDispatcherTest { |
| 7945 | protected: |
| 7946 | virtual void SetUp() override { |
| 7947 | InputDispatcherTest::SetUp(); |
| 7948 | |
| 7949 | std::shared_ptr<FakeApplicationHandle> application = |
| 7950 | std::make_shared<FakeApplicationHandle>(); |
| 7951 | application->setDispatchingTimeout(100ms); |
| 7952 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
| 7953 | ADISPLAY_ID_DEFAULT); |
Yeabkal Wubshit | 222d83d | 2024-01-24 18:00:09 +0000 | [diff] [blame] | 7954 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 7955 | mWindow->setDispatchingTimeout(100ms); |
| 7956 | mWindow->setFocusable(true); |
| 7957 | |
| 7958 | // Set focused application. |
| 7959 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7960 | |
| 7961 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 7962 | setFocusedWindow(mWindow); |
| 7963 | mWindow->consumeFocusEvent(true); |
| 7964 | } |
| 7965 | |
| 7966 | void notifyAndConsumeMotion(int32_t action, uint32_t source, int32_t displayId, |
| 7967 | nsecs_t eventTime) { |
| 7968 | mDispatcher->notifyMotion(MotionArgsBuilder(action, source) |
| 7969 | .displayId(displayId) |
| 7970 | .eventTime(eventTime) |
| 7971 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7972 | .build()); |
| 7973 | mWindow->consumeMotionEvent(WithMotionAction(action)); |
| 7974 | } |
| 7975 | |
| 7976 | private: |
| 7977 | sp<FakeWindowHandle> mWindow; |
| 7978 | }; |
| 7979 | |
| 7980 | TEST_F_WITH_FLAGS( |
| 7981 | InputDispatcherUserActivityPokeTests, MinPokeTimeObserved, |
| 7982 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 7983 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 7984 | mDispatcher->setMinTimeBetweenUserActivityPokes(50ms); |
| 7985 | |
| 7986 | // First event of type TOUCH. Should poke. |
| 7987 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7988 | milliseconds_to_nanoseconds(50)); |
| 7989 | mFakePolicy->assertUserActivityPoked( |
| 7990 | {{milliseconds_to_nanoseconds(50), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 7991 | |
| 7992 | // 80ns > 50ns has passed since previous TOUCH event. Should poke. |
| 7993 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 7994 | milliseconds_to_nanoseconds(130)); |
| 7995 | mFakePolicy->assertUserActivityPoked( |
| 7996 | {{milliseconds_to_nanoseconds(130), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 7997 | |
| 7998 | // First event of type OTHER. Should poke (despite being within 50ns of previous TOUCH event). |
| 7999 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT, |
| 8000 | milliseconds_to_nanoseconds(135)); |
| 8001 | mFakePolicy->assertUserActivityPoked( |
| 8002 | {{milliseconds_to_nanoseconds(135), USER_ACTIVITY_EVENT_OTHER, ADISPLAY_ID_DEFAULT}}); |
| 8003 | |
| 8004 | // Within 50ns of previous TOUCH event. Should NOT poke. |
| 8005 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8006 | milliseconds_to_nanoseconds(140)); |
| 8007 | mFakePolicy->assertUserActivityNotPoked(); |
| 8008 | |
| 8009 | // Within 50ns of previous OTHER event. Should NOT poke. |
| 8010 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT, |
| 8011 | milliseconds_to_nanoseconds(150)); |
| 8012 | mFakePolicy->assertUserActivityNotPoked(); |
| 8013 | |
| 8014 | // Within 50ns of previous TOUCH event (which was at time 130). Should NOT poke. |
| 8015 | // Note that STYLUS is mapped to TOUCH user activity, since it's a pointer-type source. |
| 8016 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT, |
| 8017 | milliseconds_to_nanoseconds(160)); |
| 8018 | mFakePolicy->assertUserActivityNotPoked(); |
| 8019 | |
| 8020 | // 65ns > 50ns has passed since previous OTHER event. Should poke. |
| 8021 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT, |
| 8022 | milliseconds_to_nanoseconds(200)); |
| 8023 | mFakePolicy->assertUserActivityPoked( |
| 8024 | {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_OTHER, ADISPLAY_ID_DEFAULT}}); |
| 8025 | |
| 8026 | // 170ns > 50ns has passed since previous TOUCH event. Should poke. |
| 8027 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT, |
| 8028 | milliseconds_to_nanoseconds(300)); |
| 8029 | mFakePolicy->assertUserActivityPoked( |
| 8030 | {{milliseconds_to_nanoseconds(300), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 8031 | |
| 8032 | // Assert that there's no more user activity poke event. |
| 8033 | mFakePolicy->assertUserActivityNotPoked(); |
| 8034 | } |
| 8035 | |
| 8036 | TEST_F_WITH_FLAGS( |
| 8037 | InputDispatcherUserActivityPokeTests, DefaultMinPokeTimeOf100MsUsed, |
| 8038 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 8039 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 8040 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8041 | milliseconds_to_nanoseconds(200)); |
| 8042 | mFakePolicy->assertUserActivityPoked( |
| 8043 | {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 8044 | |
| 8045 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8046 | milliseconds_to_nanoseconds(280)); |
| 8047 | mFakePolicy->assertUserActivityNotPoked(); |
| 8048 | |
| 8049 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8050 | milliseconds_to_nanoseconds(340)); |
| 8051 | mFakePolicy->assertUserActivityPoked( |
| 8052 | {{milliseconds_to_nanoseconds(340), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 8053 | } |
| 8054 | |
| 8055 | TEST_F_WITH_FLAGS( |
| 8056 | InputDispatcherUserActivityPokeTests, ZeroMinPokeTimeDisablesRateLimiting, |
| 8057 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 8058 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 8059 | mDispatcher->setMinTimeBetweenUserActivityPokes(0ms); |
| 8060 | |
| 8061 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20); |
| 8062 | mFakePolicy->assertUserActivityPoked(); |
| 8063 | |
| 8064 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 30); |
| 8065 | mFakePolicy->assertUserActivityPoked(); |
| 8066 | } |
| 8067 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8068 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 8069 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8070 | InputDispatcherTest::SetUp(); |
| 8071 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8072 | std::shared_ptr<FakeApplicationHandle> application = |
| 8073 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8074 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8075 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8076 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8077 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8078 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8079 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8080 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8081 | |
| 8082 | // Set focused application. |
| 8083 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8084 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8085 | |
| 8086 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8087 | mDispatcher->onWindowInfosChanged( |
| 8088 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8089 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8090 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8091 | } |
| 8092 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 8093 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8094 | InputDispatcherTest::TearDown(); |
| 8095 | |
| 8096 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8097 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8098 | } |
| 8099 | |
| 8100 | protected: |
| 8101 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8102 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 8103 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8104 | }; |
| 8105 | |
| 8106 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 8107 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 8108 | // the onPointerDownOutsideFocus callback. |
| 8109 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8110 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8111 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 8112 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8113 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 8114 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8115 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8116 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8117 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 8118 | } |
| 8119 | |
| 8120 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 8121 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 8122 | // onPointerDownOutsideFocus callback. |
| 8123 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8124 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8125 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, |
| 8126 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8127 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 8128 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8129 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8130 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8131 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8132 | } |
| 8133 | |
| 8134 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 8135 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 8136 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 8137 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8138 | injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8139 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 8140 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8141 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8142 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8143 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8144 | } |
| 8145 | |
| 8146 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 8147 | // DOWN on the window that already has focus. Ensure no window received the |
| 8148 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8149 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8150 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8151 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 8152 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8153 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 8154 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8155 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8156 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8157 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8158 | } |
| 8159 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 8160 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 8161 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 8162 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 8163 | const MotionEvent event = |
| 8164 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 8165 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8166 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 8167 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 8168 | .build(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8169 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 8170 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8171 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 8172 | |
| 8173 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8174 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 8175 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 8176 | mUnfocusedWindow->assertNoEvents(); |
| 8177 | } |
| 8178 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8179 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 8180 | // windows that point to the same client token. |
| 8181 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 8182 | virtual void SetUp() override { |
| 8183 | InputDispatcherTest::SetUp(); |
| 8184 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8185 | std::shared_ptr<FakeApplicationHandle> application = |
| 8186 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8187 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 8188 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8189 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 8190 | |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 8191 | mWindow2 = mWindow1->clone(ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8192 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 8193 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8194 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8195 | } |
| 8196 | |
| 8197 | protected: |
| 8198 | sp<FakeWindowHandle> mWindow1; |
| 8199 | sp<FakeWindowHandle> mWindow2; |
| 8200 | |
| 8201 | // 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] | 8202 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 8203 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 8204 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8205 | } |
| 8206 | |
| 8207 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 8208 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8209 | const std::string name = window->getName(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8210 | std::unique_ptr<MotionEvent> motionEvent = |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 8211 | window->consumeMotionEvent(WithMotionAction(expectedAction)); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8212 | ASSERT_NE(nullptr, motionEvent); |
| 8213 | ASSERT_EQ(points.size(), motionEvent->getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8214 | |
| 8215 | for (size_t i = 0; i < points.size(); i++) { |
| 8216 | float expectedX = points[i].x; |
| 8217 | float expectedY = points[i].y; |
| 8218 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8219 | EXPECT_EQ(expectedX, motionEvent->getX(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8220 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8221 | << ", got " << motionEvent->getX(i); |
| 8222 | EXPECT_EQ(expectedY, motionEvent->getY(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8223 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8224 | << ", got " << motionEvent->getY(i); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8225 | } |
| 8226 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8227 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 8228 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8229 | std::vector<PointF> expectedPoints) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8230 | mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 8231 | ADISPLAY_ID_DEFAULT, touchedPoints)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8232 | |
| 8233 | // Always consume from window1 since it's the window that has the InputReceiver |
| 8234 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 8235 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8236 | }; |
| 8237 | |
| 8238 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 8239 | // Touch Window 1 |
| 8240 | PointF touchedPoint = {10, 10}; |
| 8241 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8242 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8243 | |
| 8244 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8245 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8246 | |
| 8247 | // Touch Window 2 |
| 8248 | touchedPoint = {150, 150}; |
| 8249 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8250 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8251 | } |
| 8252 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8253 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 8254 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8255 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8256 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8257 | |
| 8258 | // Touch Window 1 |
| 8259 | PointF touchedPoint = {10, 10}; |
| 8260 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8261 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8262 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8263 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8264 | |
| 8265 | // Touch Window 2 |
| 8266 | touchedPoint = {150, 150}; |
| 8267 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8268 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 8269 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8270 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8271 | // Update the transform so rotation is set |
| 8272 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8273 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8274 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 8275 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8276 | } |
| 8277 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8278 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8279 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8280 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8281 | |
| 8282 | // Touch Window 1 |
| 8283 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 8284 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8285 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8286 | |
| 8287 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8288 | touchedPoints.push_back(PointF{150, 150}); |
| 8289 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8290 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8291 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8292 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8293 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8294 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8295 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8296 | // Update the transform so rotation is set for Window 2 |
| 8297 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8298 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8299 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8300 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8301 | } |
| 8302 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8303 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8304 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8305 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8306 | |
| 8307 | // Touch Window 1 |
| 8308 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 8309 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8310 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8311 | |
| 8312 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8313 | touchedPoints.push_back(PointF{150, 150}); |
| 8314 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8315 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8316 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8317 | |
| 8318 | // Move both windows |
| 8319 | touchedPoints = {{20, 20}, {175, 175}}; |
| 8320 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 8321 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 8322 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8323 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8324 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8325 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8326 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8327 | expectedPoints.pop_back(); |
| 8328 | |
| 8329 | // Touch Window 2 |
| 8330 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8331 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8332 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8333 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8334 | |
| 8335 | // Move both windows |
| 8336 | touchedPoints = {{20, 20}, {175, 175}}; |
| 8337 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 8338 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 8339 | |
| 8340 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8341 | } |
| 8342 | |
| 8343 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 8344 | mWindow1->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8345 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8346 | |
| 8347 | // Touch Window 1 |
| 8348 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 8349 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8350 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8351 | |
| 8352 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8353 | touchedPoints.push_back(PointF{150, 150}); |
| 8354 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8355 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8356 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8357 | |
| 8358 | // Move both windows |
| 8359 | touchedPoints = {{20, 20}, {175, 175}}; |
| 8360 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 8361 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 8362 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8363 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8364 | } |
| 8365 | |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 8366 | /** |
| 8367 | * When one of the windows is slippery, the touch should not slip into the other window with the |
| 8368 | * same input channel. |
| 8369 | */ |
| 8370 | TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) { |
| 8371 | mWindow1->setSlippery(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8372 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 8373 | |
| 8374 | // Touch down in window 1 |
| 8375 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8376 | ADISPLAY_ID_DEFAULT, {{50, 50}})); |
| 8377 | consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}}); |
| 8378 | |
| 8379 | // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip. |
| 8380 | // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN |
| 8381 | // getting generated. |
| 8382 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8383 | ADISPLAY_ID_DEFAULT, {{150, 150}})); |
| 8384 | |
| 8385 | consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}}); |
| 8386 | } |
| 8387 | |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8388 | /** |
| 8389 | * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and |
| 8390 | * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window |
| 8391 | * that the pointer is hovering over may have a different transform. |
| 8392 | */ |
| 8393 | TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) { |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8394 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8395 | |
| 8396 | // Start hover in window 1 |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 8397 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN) |
| 8398 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8399 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8400 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 8401 | {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8402 | // Move hover to window 2. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 8403 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8404 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 8405 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8406 | consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}}); |
| 8407 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 8408 | {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})}); |
| 8409 | } |
| 8410 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8411 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 8412 | virtual void SetUp() override { |
| 8413 | InputDispatcherTest::SetUp(); |
| 8414 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8415 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8416 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8417 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 8418 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8419 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8420 | mWindow->setDispatchingTimeout(100ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8421 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8422 | |
| 8423 | // Set focused application. |
| 8424 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 8425 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8426 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8427 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8428 | mWindow->consumeFocusEvent(true); |
| 8429 | } |
| 8430 | |
| 8431 | virtual void TearDown() override { |
| 8432 | InputDispatcherTest::TearDown(); |
| 8433 | mWindow.clear(); |
| 8434 | } |
| 8435 | |
| 8436 | protected: |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8437 | static constexpr std::chrono::duration SPY_TIMEOUT = 200ms; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8438 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8439 | sp<FakeWindowHandle> mWindow; |
| 8440 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 8441 | |
| 8442 | void tapOnWindow() { |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 8443 | const auto touchingPointer = PointerBuilder(/*id=*/0, ToolType::FINGER) |
| 8444 | .x(WINDOW_LOCATION.x) |
| 8445 | .y(WINDOW_LOCATION.y); |
| 8446 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8447 | .pointer(touchingPointer) |
| 8448 | .build()); |
| 8449 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8450 | .pointer(touchingPointer) |
| 8451 | .build()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8452 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8453 | |
| 8454 | sp<FakeWindowHandle> addSpyWindow() { |
| 8455 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8456 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8457 | spy->setTrustedOverlay(true); |
| 8458 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8459 | spy->setSpy(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8460 | spy->setDispatchingTimeout(SPY_TIMEOUT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8461 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8462 | return spy; |
| 8463 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8464 | }; |
| 8465 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8466 | // Send a tap and respond, which should not cause an ANR. |
| 8467 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 8468 | tapOnWindow(); |
| 8469 | mWindow->consumeMotionDown(); |
| 8470 | mWindow->consumeMotionUp(); |
| 8471 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8472 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8473 | } |
| 8474 | |
| 8475 | // Send a regular key and respond, which should not cause an ANR. |
| 8476 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8477 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8478 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8479 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8480 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8481 | } |
| 8482 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 8483 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 8484 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8485 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 8486 | mWindow->consumeFocusEvent(false); |
| 8487 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8488 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8489 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 8490 | InputEventInjectionSync::NONE, CONSUME_TIMEOUT_EVENT_EXPECTED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8491 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8492 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 8493 | // Key will not go to window because we have no focused window. |
| 8494 | // The 'no focused window' ANR timer should start instead. |
| 8495 | |
| 8496 | // Now, the focused application goes away. |
| 8497 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 8498 | // The key should get dropped and there should be no ANR. |
| 8499 | |
| 8500 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8501 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8502 | } |
| 8503 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8504 | // 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] | 8505 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 8506 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8507 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8508 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8509 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8510 | WINDOW_LOCATION)); |
| 8511 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8512 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8513 | ASSERT_TRUE(sequenceNum); |
| 8514 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8515 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8516 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8517 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8518 | mWindow->consumeMotionEvent( |
| 8519 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8520 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8521 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8522 | } |
| 8523 | |
| 8524 | // Send a key to the app and have the app not respond right away. |
| 8525 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 8526 | // Inject a key, and don't respond - expect that ANR is called. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8527 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8528 | const auto [sequenceNum, _] = mWindow->receiveEvent(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8529 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8530 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8531 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8532 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8533 | } |
| 8534 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8535 | // We have a focused application, but no focused window |
| 8536 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8537 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8538 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8539 | mWindow->consumeFocusEvent(false); |
| 8540 | |
| 8541 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8542 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8543 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8544 | WINDOW_LOCATION)); |
| 8545 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 8546 | mDispatcher->waitForIdle(); |
| 8547 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8548 | |
| 8549 | // Once a focused event arrives, we get an ANR for this application |
| 8550 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 8551 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8552 | const InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8553 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8554 | InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8555 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8556 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 8557 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8558 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8559 | } |
| 8560 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8561 | /** |
| 8562 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 8563 | * there will not be an ANR. |
| 8564 | */ |
| 8565 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 8566 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8567 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8568 | mWindow->consumeFocusEvent(false); |
| 8569 | |
| 8570 | KeyEvent event; |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 8571 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 8572 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8573 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 8574 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 8575 | |
| 8576 | // Define a valid key down event that is stale (too old). |
| 8577 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8578 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /*flags=*/0, AKEYCODE_A, KEY_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8579 | AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8580 | |
| 8581 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 8582 | |
| 8583 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8584 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8585 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 8586 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 8587 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 8588 | << "Injection should fail because the event is stale"; |
| 8589 | |
| 8590 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8591 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8592 | mWindow->assertNoEvents(); |
| 8593 | } |
| 8594 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8595 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8596 | // Make sure that we don't notify policy twice about the same ANR. |
| 8597 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 8598 | const std::chrono::duration appTimeout = 400ms; |
| 8599 | mApplication->setDispatchingTimeout(appTimeout); |
| 8600 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 8601 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8602 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8603 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8604 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8605 | |
| 8606 | // Once a focused event arrives, we get an ANR for this application |
| 8607 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 8608 | // injection times out (instead of failing). |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 8609 | const std::chrono::duration eventInjectionTimeout = 100ms; |
| 8610 | ASSERT_LT(eventInjectionTimeout, appTimeout); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8611 | const InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8612 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 8613 | InputEventInjectionSync::WAIT_FOR_RESULT, eventInjectionTimeout, |
| 8614 | /*allowKeyRepeat=*/false); |
| 8615 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result) |
| 8616 | << "result=" << ftl::enum_string(result); |
| 8617 | // We already waited for 'eventInjectionTimeout`, because the countdown started when the event |
| 8618 | // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait. |
| 8619 | std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout; |
| 8620 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8621 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 8622 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8623 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 8624 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8625 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8626 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 8627 | // '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] | 8628 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8629 | } |
| 8630 | |
| 8631 | // We have a focused application, but no focused window |
| 8632 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8633 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8634 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8635 | mWindow->consumeFocusEvent(false); |
| 8636 | |
| 8637 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8638 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8639 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 8640 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 8641 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8642 | |
| 8643 | // Future focused events get dropped right away |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8644 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8645 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8646 | mWindow->assertNoEvents(); |
| 8647 | } |
| 8648 | |
| 8649 | /** |
| 8650 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 8651 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 8652 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 8653 | * the ANR mechanism should still work. |
| 8654 | * |
| 8655 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 8656 | * DOWN event, while not responding on the second one. |
| 8657 | */ |
| 8658 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 8659 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8660 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8661 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 8662 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 8663 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8664 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8665 | |
| 8666 | // Now send ACTION_UP, with identical timestamp |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8667 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8668 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 8669 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 8670 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8671 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8672 | |
| 8673 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 8674 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8675 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8676 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8677 | } |
| 8678 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8679 | // A spy window can receive an ANR |
| 8680 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 8681 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 8682 | |
| 8683 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8684 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8685 | WINDOW_LOCATION)); |
| 8686 | mWindow->consumeMotionDown(); |
| 8687 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8688 | const auto [sequenceNum, _] = spy->receiveEvent(); // ACTION_DOWN |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8689 | ASSERT_TRUE(sequenceNum); |
| 8690 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8691 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8692 | |
| 8693 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8694 | spy->consumeMotionEvent( |
| 8695 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8696 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8697 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8698 | } |
| 8699 | |
| 8700 | // 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] | 8701 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8702 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 8703 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8704 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8705 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8706 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8707 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8708 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8709 | |
| 8710 | // Stuck on the ACTION_UP |
| 8711 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8712 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8713 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8714 | // 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] | 8715 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8716 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8717 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8718 | |
| 8719 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 8720 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8721 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8722 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8723 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8724 | } |
| 8725 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8726 | // 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] | 8727 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8728 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 8729 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8730 | |
| 8731 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8732 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8733 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8734 | |
| 8735 | mWindow->consumeMotionDown(); |
| 8736 | // Stuck on the ACTION_UP |
| 8737 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8738 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8739 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8740 | // 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] | 8741 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8742 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8743 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8744 | |
| 8745 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 8746 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8747 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8748 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8749 | spy->assertNoEvents(); |
| 8750 | } |
| 8751 | |
| 8752 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8753 | mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8754 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 8755 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8756 | |
| 8757 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8758 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8759 | WINDOW_LOCATION)); |
| 8760 | |
| 8761 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8762 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 8763 | ASSERT_TRUE(consumeSeq); |
| 8764 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8765 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(), |
| 8766 | MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8767 | |
| 8768 | monitor.finishEvent(*consumeSeq); |
| 8769 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 8770 | |
| 8771 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8772 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8773 | } |
| 8774 | |
| 8775 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 8776 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 8777 | // get an ANR again. |
| 8778 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 8779 | // 2. consume all pending events (= queue becomes healthy again) |
| 8780 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 8781 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 8782 | tapOnWindow(); |
| 8783 | |
| 8784 | mWindow->consumeMotionDown(); |
| 8785 | // Block on ACTION_UP |
| 8786 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8787 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8788 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 8789 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8790 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8791 | mWindow->assertNoEvents(); |
| 8792 | |
| 8793 | tapOnWindow(); |
| 8794 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8795 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8796 | mWindow->consumeMotionUp(); |
| 8797 | |
| 8798 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8799 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8800 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8801 | mWindow->assertNoEvents(); |
| 8802 | } |
| 8803 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8804 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 8805 | // it. |
| 8806 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8807 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8808 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8809 | WINDOW_LOCATION)); |
| 8810 | |
| 8811 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8812 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8813 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8814 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 8815 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8816 | // 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] | 8817 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8818 | mWindow->consumeMotionEvent( |
| 8819 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8820 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8821 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8822 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8823 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8824 | } |
| 8825 | |
| 8826 | /** |
| 8827 | * 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] | 8828 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8829 | */ |
| 8830 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame^] | 8831 | // The timeouts in this test are established by relying on the fact that the "key waiting for |
| 8832 | // events timeout" is equal to 500ms. |
| 8833 | ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8834 | 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] | 8835 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8836 | |
| 8837 | tapOnWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8838 | const auto& [downSequenceNum, downEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8839 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8840 | const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8841 | ASSERT_TRUE(upSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8842 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame^] | 8843 | // Don't finish the events yet, and send a key |
| 8844 | mDispatcher->notifyKey( |
| 8845 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 8846 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 8847 | .build()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8848 | // Key will not be sent to the window, yet, because the window is still processing events |
| 8849 | // 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] | 8850 | // Make sure that `assertNoEvents` doesn't wait too long, because it could cause an ANR. |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame^] | 8851 | mWindow->assertNoEvents(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8852 | |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame^] | 8853 | std::this_thread::sleep_for(400ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8854 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 8855 | // to the focused window, even though we have not yet finished the motion event |
| 8856 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8857 | mWindow->finishEvent(*downSequenceNum); |
| 8858 | mWindow->finishEvent(*upSequenceNum); |
| 8859 | } |
| 8860 | |
| 8861 | /** |
| 8862 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 8863 | * not go to the focused window until the motion is processed. |
| 8864 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 8865 | * focused window right away. |
| 8866 | */ |
| 8867 | TEST_F(InputDispatcherSingleWindowAnr, |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame^] | 8868 | PendingKey_IsDeliveredWhileMotionIsProcessingAndNewTouchComesIn) { |
| 8869 | // The timeouts in this test are established by relying on the fact that the "key waiting for |
| 8870 | // events timeout" is equal to 500ms. |
| 8871 | ASSERT_EQ(mFakePolicy->getKeyWaitingForEventsTimeout(), 500ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8872 | 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] | 8873 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8874 | |
| 8875 | tapOnWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8876 | const auto& [downSequenceNum, _] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8877 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8878 | const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8879 | ASSERT_TRUE(upSequenceNum); |
| 8880 | // Don't finish the events yet, and send a key |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 8881 | mDispatcher->notifyKey( |
| 8882 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 8883 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 8884 | .build()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8885 | // At this point, key is still pending, and should not be sent to the application yet. |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame^] | 8886 | mWindow->assertNoEvents(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8887 | |
| 8888 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 8889 | tapOnWindow(); |
Siarhei Vishniakou | ef2b450 | 2023-12-28 11:51:47 -0800 | [diff] [blame^] | 8890 | // Now that we tapped, we should receive the key immediately. |
| 8891 | // Since there's still room for slowness, we use 200ms, which is much less than |
| 8892 | // the "key waiting for events' timeout of 500ms minus the already waited 100ms duration. |
| 8893 | std::unique_ptr<InputEvent> keyEvent = mWindow->consume(200ms); |
| 8894 | ASSERT_NE(nullptr, keyEvent); |
| 8895 | ASSERT_EQ(InputEventType::KEY, keyEvent->getType()); |
| 8896 | ASSERT_THAT(static_cast<KeyEvent&>(*keyEvent), WithKeyAction(AKEY_EVENT_ACTION_DOWN)); |
| 8897 | // it doesn't matter that we haven't ack'd the other events yet. We can finish events in any |
| 8898 | // order. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8899 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 8900 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 8901 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 8902 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8903 | mWindow->assertNoEvents(); |
| 8904 | } |
| 8905 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 8906 | /** |
| 8907 | * Send an event to the app and have the app not respond right away. |
| 8908 | * When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 8909 | * So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 8910 | * At some point, the window becomes responsive again. |
| 8911 | * Ensure that subsequent events get dropped, and the next gesture is delivered. |
| 8912 | */ |
| 8913 | TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) { |
| 8914 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8915 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 8916 | .build()); |
| 8917 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8918 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 8919 | ASSERT_TRUE(sequenceNum); |
| 8920 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 8921 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 8922 | |
| 8923 | mWindow->finishEvent(*sequenceNum); |
| 8924 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 8925 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8926 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
| 8927 | |
| 8928 | // Now that the window is responsive, let's continue the gesture. |
| 8929 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8930 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8931 | .build()); |
| 8932 | |
| 8933 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8934 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8935 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 8936 | .build()); |
| 8937 | |
| 8938 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8939 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8940 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 8941 | .build()); |
| 8942 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8943 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8944 | .build()); |
| 8945 | // We already canceled this pointer, so the window shouldn't get any new events. |
| 8946 | mWindow->assertNoEvents(); |
| 8947 | |
| 8948 | // Start another one. |
| 8949 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8950 | .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15)) |
| 8951 | .build()); |
| 8952 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 8953 | } |
| 8954 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8955 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 8956 | virtual void SetUp() override { |
| 8957 | InputDispatcherTest::SetUp(); |
| 8958 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8959 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8960 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8961 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 8962 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8963 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8964 | // 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] | 8965 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8966 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8967 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 8968 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8969 | mFocusedWindow->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8970 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8971 | |
| 8972 | // Set focused application. |
| 8973 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8974 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8975 | |
| 8976 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8977 | mDispatcher->onWindowInfosChanged( |
| 8978 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8979 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8980 | mFocusedWindow->consumeFocusEvent(true); |
| 8981 | } |
| 8982 | |
| 8983 | virtual void TearDown() override { |
| 8984 | InputDispatcherTest::TearDown(); |
| 8985 | |
| 8986 | mUnfocusedWindow.clear(); |
| 8987 | mFocusedWindow.clear(); |
| 8988 | } |
| 8989 | |
| 8990 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8991 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8992 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 8993 | sp<FakeWindowHandle> mFocusedWindow; |
| 8994 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 8995 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 8996 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 8997 | |
| 8998 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 8999 | |
| 9000 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 9001 | |
| 9002 | private: |
| 9003 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9004 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9005 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9006 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9007 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9008 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9009 | location)); |
| 9010 | } |
| 9011 | }; |
| 9012 | |
| 9013 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 9014 | // should be ANR'd first. |
| 9015 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9016 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 9017 | injectMotionEvent(*mDispatcher, |
| 9018 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 9019 | AINPUT_SOURCE_TOUCHSCREEN) |
| 9020 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 9021 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 9022 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 9023 | .build())); |
| 9024 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9025 | injectMotionEvent(*mDispatcher, |
| 9026 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 9027 | AINPUT_SOURCE_TOUCHSCREEN) |
| 9028 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 9029 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 9030 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 9031 | .build())); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9032 | mFocusedWindow->consumeMotionDown(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 9033 | mFocusedWindow->consumeMotionUp(); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 9034 | mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9035 | // We consumed all events, so no ANR |
| 9036 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9037 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9038 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9039 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 9040 | injectMotionEvent(*mDispatcher, |
| 9041 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 9042 | AINPUT_SOURCE_TOUCHSCREEN) |
| 9043 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 9044 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 9045 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 9046 | .build())); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9047 | const auto [unfocusedSequenceNum, _] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9048 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9049 | |
| 9050 | const std::chrono::duration timeout = |
| 9051 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9052 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 9053 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9054 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9055 | mFocusedWindow->consumeMotionDown(); |
| 9056 | // This cancel is generated because the connection was unresponsive |
| 9057 | mFocusedWindow->consumeMotionCancel(); |
| 9058 | mFocusedWindow->assertNoEvents(); |
| 9059 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9060 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9061 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 9062 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9063 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9064 | } |
| 9065 | |
| 9066 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 9067 | // it doesn't matter which order they should have ANR. |
| 9068 | // But we should receive ANR for both. |
| 9069 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 9070 | // Set the timeout for unfocused window to match the focused window |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9071 | mUnfocusedWindow->setDispatchingTimeout( |
| 9072 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9073 | mDispatcher->onWindowInfosChanged( |
| 9074 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9075 | |
| 9076 | tapOnFocusedWindow(); |
| 9077 | // 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] | 9078 | // 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] | 9079 | std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken( |
| 9080 | mFocusedWindow->getDispatchingTimeout( |
| 9081 | DISPATCHING_TIMEOUT)), |
| 9082 | mFakePolicy->getUnresponsiveWindowToken(0ms)}; |
| 9083 | |
| 9084 | ASSERT_THAT(anrConnectionTokens, |
| 9085 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 9086 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9087 | |
| 9088 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9089 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9090 | |
| 9091 | mFocusedWindow->consumeMotionDown(); |
| 9092 | mFocusedWindow->consumeMotionUp(); |
| 9093 | mUnfocusedWindow->consumeMotionOutside(); |
| 9094 | |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9095 | std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(), |
| 9096 | mFakePolicy->getResponsiveWindowToken()}; |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9097 | |
| 9098 | // Both applications should be marked as responsive, in any order |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9099 | ASSERT_THAT(responsiveTokens, |
| 9100 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 9101 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9102 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9103 | } |
| 9104 | |
| 9105 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 9106 | // We should also log an error to account for the dropped event (not tested here). |
| 9107 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 9108 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 9109 | tapOnFocusedWindow(); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 9110 | mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9111 | // Receive the events, but don't respond |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9112 | const auto [downEventSequenceNum, downEvent] = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9113 | ASSERT_TRUE(downEventSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9114 | const auto [upEventSequenceNum, upEvent] = mFocusedWindow->receiveEvent(); // ACTION_UP |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9115 | ASSERT_TRUE(upEventSequenceNum); |
| 9116 | const std::chrono::duration timeout = |
| 9117 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9118 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9119 | |
| 9120 | // Tap once again |
| 9121 | // 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] | 9122 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9123 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9124 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9125 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9126 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9127 | FOCUSED_WINDOW_LOCATION)); |
| 9128 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 9129 | // valid touch target |
| 9130 | mUnfocusedWindow->assertNoEvents(); |
| 9131 | |
| 9132 | // Consume the first tap |
| 9133 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 9134 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 9135 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9136 | // The second tap did not go to the focused window |
| 9137 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9138 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9139 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 9140 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9141 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9142 | } |
| 9143 | |
| 9144 | // If you tap outside of all windows, there will not be ANR |
| 9145 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9146 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9147 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9148 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 9149 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9150 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9151 | } |
| 9152 | |
| 9153 | // Since the focused window is paused, tapping on it should not produce any events |
| 9154 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 9155 | mFocusedWindow->setPaused(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9156 | mDispatcher->onWindowInfosChanged( |
| 9157 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9158 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9159 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9160 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9161 | FOCUSED_WINDOW_LOCATION)); |
| 9162 | |
| 9163 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 9164 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9165 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 9166 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9167 | |
| 9168 | mFocusedWindow->assertNoEvents(); |
| 9169 | mUnfocusedWindow->assertNoEvents(); |
| 9170 | } |
| 9171 | |
| 9172 | /** |
| 9173 | * 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] | 9174 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9175 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 9176 | * |
| 9177 | * Warning!!! |
| 9178 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 9179 | * and the injection timeout that we specify when injecting the key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9180 | * We must have the injection timeout (100ms) be smaller than |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9181 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 9182 | * |
| 9183 | * If that value changes, this test should also change. |
| 9184 | */ |
| 9185 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 9186 | // Set a long ANR timeout to prevent it from triggering |
| 9187 | mFocusedWindow->setDispatchingTimeout(2s); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9188 | mDispatcher->onWindowInfosChanged( |
| 9189 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9190 | |
| 9191 | tapOnUnfocusedWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9192 | const auto [downSequenceNum, downEvent] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9193 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9194 | const auto [upSequenceNum, upEvent] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9195 | ASSERT_TRUE(upSequenceNum); |
| 9196 | // Don't finish the events yet, and send a key |
| 9197 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 9198 | // window even if motions are still being processed. |
| 9199 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9200 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9201 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 9202 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9203 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9204 | // 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] | 9205 | // and the key remains pending, waiting for the touch events to be processed. |
| 9206 | // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED |
| 9207 | // under the hood. |
| 9208 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 9209 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9210 | |
| 9211 | // 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] | 9212 | mFocusedWindow->setFocusable(false); |
| 9213 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9214 | mDispatcher->onWindowInfosChanged( |
| 9215 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9216 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9217 | |
| 9218 | // Focus events should precede the key events |
| 9219 | mUnfocusedWindow->consumeFocusEvent(true); |
| 9220 | mFocusedWindow->consumeFocusEvent(false); |
| 9221 | |
| 9222 | // Finish the tap events, which should unblock dispatcher |
| 9223 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 9224 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 9225 | |
| 9226 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 9227 | // can finally go to the newly focused "mUnfocusedWindow". |
| 9228 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 9229 | mFocusedWindow->assertNoEvents(); |
| 9230 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9231 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9232 | } |
| 9233 | |
| 9234 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 9235 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 9236 | // The other window should not be affected by that. |
| 9237 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 9238 | // Touch Window 1 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9239 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9240 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9241 | {FOCUSED_WINDOW_LOCATION})); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 9242 | mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9243 | |
| 9244 | // Touch Window 2 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9245 | mDispatcher->notifyMotion( |
| 9246 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9247 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9248 | |
| 9249 | const std::chrono::duration timeout = |
| 9250 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9251 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9252 | |
| 9253 | mUnfocusedWindow->consumeMotionDown(); |
| 9254 | mFocusedWindow->consumeMotionDown(); |
| 9255 | // Focused window may or may not receive ACTION_MOVE |
| 9256 | // But it should definitely receive ACTION_CANCEL due to the ANR |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9257 | const auto [moveOrCancelSequenceNum, event] = mFocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9258 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 9259 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 9260 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 9261 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9262 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 9263 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 9264 | mFocusedWindow->consumeMotionCancel(); |
| 9265 | } else { |
| 9266 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 9267 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9268 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9269 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 9270 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9271 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9272 | mUnfocusedWindow->assertNoEvents(); |
| 9273 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9274 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9275 | } |
| 9276 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9277 | /** |
| 9278 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 9279 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 9280 | * |
| 9281 | * If the user touches another application during this time, the key should be dropped. |
| 9282 | * Next, if a new focused window comes in, without toggling the focused application, |
| 9283 | * then no ANR should occur. |
| 9284 | * |
| 9285 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 9286 | * but in some cases the policy may not update the focused application. |
| 9287 | */ |
| 9288 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 9289 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 9290 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | c033dfb | 2023-10-03 10:45:16 -0700 | [diff] [blame] | 9291 | focusedApplication->setDispatchingTimeout(300ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9292 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 9293 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 9294 | mFocusedWindow->setFocusable(false); |
| 9295 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9296 | mDispatcher->onWindowInfosChanged( |
| 9297 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9298 | mFocusedWindow->consumeFocusEvent(false); |
| 9299 | |
| 9300 | // Send a key. The ANR timer should start because there is no focused window. |
| 9301 | // 'focusedApplication' will get blamed if this timer completes. |
| 9302 | // 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] | 9303 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9304 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 9305 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9306 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9307 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9308 | |
| 9309 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 9310 | // then the injected touches won't cause the focused event to get dropped. |
| 9311 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 9312 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 9313 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 9314 | // For this test, it means that the key would get delivered to the window once it becomes |
| 9315 | // focused. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9316 | std::this_thread::sleep_for(100ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9317 | |
| 9318 | // Touch unfocused window. This should force the pending key to get dropped. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9319 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9320 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9321 | {UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9322 | |
| 9323 | // We do not consume the motion right away, because that would require dispatcher to first |
| 9324 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 9325 | // Set the focused window first. |
| 9326 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9327 | mDispatcher->onWindowInfosChanged( |
| 9328 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9329 | setFocusedWindow(mFocusedWindow); |
| 9330 | mFocusedWindow->consumeFocusEvent(true); |
| 9331 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 9332 | // to another application. This could be a bug / behaviour in the policy. |
| 9333 | |
| 9334 | mUnfocusedWindow->consumeMotionDown(); |
| 9335 | |
| 9336 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9337 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 9338 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 9339 | } |
| 9340 | |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 9341 | /** |
| 9342 | * If we are pruning input queue, we should never drop pointer events. Otherwise, we risk having |
| 9343 | * an inconsistent event stream inside the dispatcher. In this test, we make sure that the |
| 9344 | * dispatcher doesn't prune pointer events incorrectly. |
| 9345 | * |
| 9346 | * This test reproduces a crash in InputDispatcher. |
| 9347 | * To reproduce the crash, we need to simulate the conditions for "pruning input queue" to occur. |
| 9348 | * |
| 9349 | * Keep the currently focused application (mApplication), and have no focused window. |
| 9350 | * We set up two additional windows: |
| 9351 | * 1) The navigation bar window. This simulates the system "NavigationBar", which is used in the |
| 9352 | * 3-button navigation mode. This window injects a BACK button when it's touched. 2) The application |
| 9353 | * window. This window is not focusable, but is touchable. |
| 9354 | * |
| 9355 | * We first touch the navigation bar, which causes it to inject a key. Since there's no focused |
| 9356 | * window, the dispatcher doesn't process this key, and all other events inside dispatcher are now |
| 9357 | * blocked. The dispatcher is waiting for 'mApplication' to add a focused window. |
| 9358 | * |
| 9359 | * Now, we touch "Another window". This window is owned by a different application than |
| 9360 | * 'mApplication'. This causes the dispatcher to stop waiting for 'mApplication' to add a focused |
| 9361 | * window. Now, the "pruning input queue" behaviour should kick in, and the dispatcher should start |
| 9362 | * dropping the events from its queue. Ensure that no crash occurs. |
| 9363 | * |
| 9364 | * In this test, we are setting long timeouts to prevent ANRs and events dropped due to being stale. |
| 9365 | * This does not affect the test running time. |
| 9366 | */ |
| 9367 | TEST_F(InputDispatcherMultiWindowAnr, PruningInputQueueShouldNotDropPointerEvents) { |
| 9368 | std::shared_ptr<FakeApplicationHandle> systemUiApplication = |
| 9369 | std::make_shared<FakeApplicationHandle>(); |
| 9370 | systemUiApplication->setDispatchingTimeout(3000ms); |
| 9371 | mFakePolicy->setStaleEventTimeout(3000ms); |
| 9372 | sp<FakeWindowHandle> navigationBar = |
| 9373 | sp<FakeWindowHandle>::make(systemUiApplication, mDispatcher, "NavigationBar", |
| 9374 | ADISPLAY_ID_DEFAULT); |
| 9375 | navigationBar->setFocusable(false); |
| 9376 | navigationBar->setWatchOutsideTouch(true); |
| 9377 | navigationBar->setFrame(Rect(0, 0, 100, 100)); |
| 9378 | |
| 9379 | mApplication->setDispatchingTimeout(3000ms); |
| 9380 | // 'mApplication' is already focused, but we call it again here to make it explicit. |
| 9381 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 9382 | |
| 9383 | std::shared_ptr<FakeApplicationHandle> anotherApplication = |
| 9384 | std::make_shared<FakeApplicationHandle>(); |
| 9385 | sp<FakeWindowHandle> appWindow = |
| 9386 | sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Another window", |
| 9387 | ADISPLAY_ID_DEFAULT); |
| 9388 | appWindow->setFocusable(false); |
| 9389 | appWindow->setFrame(Rect(100, 100, 200, 200)); |
| 9390 | |
| 9391 | mDispatcher->onWindowInfosChanged( |
| 9392 | {{*navigationBar->getInfo(), *appWindow->getInfo()}, {}, 0, 0}); |
| 9393 | // 'mFocusedWindow' is no longer in the dispatcher window list, and therefore loses focus |
| 9394 | mFocusedWindow->consumeFocusEvent(false); |
| 9395 | |
| 9396 | // Touch down the navigation bar. It consumes the touch and injects a key into the dispatcher |
| 9397 | // in response. |
| 9398 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9399 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 9400 | .build()); |
| 9401 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 9402 | |
| 9403 | // Key will not be sent anywhere because we have no focused window. It will remain pending. |
| 9404 | // Pretend we are injecting KEYCODE_BACK, but it doesn't actually matter what key it is. |
| 9405 | InputEventInjectionResult result = |
| 9406 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 9407 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms, |
| 9408 | /*allowKeyRepeat=*/false); |
| 9409 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
| 9410 | |
| 9411 | // Finish the gesture - lift up finger and inject ACTION_UP key event |
| 9412 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 9413 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 9414 | .build()); |
| 9415 | result = injectKey(*mDispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 9416 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms, |
| 9417 | /*allowKeyRepeat=*/false); |
| 9418 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
| 9419 | // The key that was injected is blocking the dispatcher, so the navigation bar shouldn't be |
| 9420 | // getting any events yet. |
| 9421 | navigationBar->assertNoEvents(); |
| 9422 | |
| 9423 | // Now touch "Another window". This touch is going to a different application than the one we |
| 9424 | // are waiting for (which is 'mApplication'). |
| 9425 | // This should cause the dispatcher to drop the pending focus-dispatched events (like the key |
| 9426 | // trying to be injected) and to continue processing the rest of the events in the original |
| 9427 | // order. |
| 9428 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9429 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 9430 | .build()); |
| 9431 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 9432 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_OUTSIDE)); |
| 9433 | appWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 9434 | |
| 9435 | appWindow->assertNoEvents(); |
| 9436 | navigationBar->assertNoEvents(); |
| 9437 | } |
| 9438 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9439 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 9440 | // that has feature NO_INPUT_CHANNEL. |
| 9441 | // Layout: |
| 9442 | // Top (closest to user) |
| 9443 | // mNoInputWindow (above all windows) |
| 9444 | // mBottomWindow |
| 9445 | // Bottom (furthest from user) |
| 9446 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 9447 | virtual void SetUp() override { |
| 9448 | InputDispatcherTest::SetUp(); |
| 9449 | |
| 9450 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9451 | mNoInputWindow = |
| 9452 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 9453 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 9454 | /*createInputChannel=*/false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9455 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9456 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 9457 | // It's perfectly valid for this window to not have an associated input channel |
| 9458 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9459 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 9460 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9461 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 9462 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9463 | mDispatcher->onWindowInfosChanged( |
| 9464 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9465 | } |
| 9466 | |
| 9467 | protected: |
| 9468 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 9469 | sp<FakeWindowHandle> mNoInputWindow; |
| 9470 | sp<FakeWindowHandle> mBottomWindow; |
| 9471 | }; |
| 9472 | |
| 9473 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 9474 | PointF touchedPoint = {10, 10}; |
| 9475 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9476 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9477 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9478 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9479 | |
| 9480 | mNoInputWindow->assertNoEvents(); |
| 9481 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 9482 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 9483 | // and therefore should prevent mBottomWindow from receiving touches |
| 9484 | mBottomWindow->assertNoEvents(); |
| 9485 | } |
| 9486 | |
| 9487 | /** |
| 9488 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 9489 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 9490 | */ |
| 9491 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 9492 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9493 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 9494 | "Window with input channel and NO_INPUT_CHANNEL", |
| 9495 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9496 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9497 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9498 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9499 | mDispatcher->onWindowInfosChanged( |
| 9500 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9501 | |
| 9502 | PointF touchedPoint = {10, 10}; |
| 9503 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9504 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9505 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9506 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9507 | |
| 9508 | mNoInputWindow->assertNoEvents(); |
| 9509 | mBottomWindow->assertNoEvents(); |
| 9510 | } |
| 9511 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9512 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 9513 | protected: |
| 9514 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 9515 | sp<FakeWindowHandle> mWindow; |
| 9516 | sp<FakeWindowHandle> mMirror; |
| 9517 | |
| 9518 | virtual void SetUp() override { |
| 9519 | InputDispatcherTest::SetUp(); |
| 9520 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9521 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 9522 | mMirror = mWindow->clone(ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9523 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 9524 | mWindow->setFocusable(true); |
| 9525 | mMirror->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9526 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9527 | } |
| 9528 | }; |
| 9529 | |
| 9530 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 9531 | // Request focus on a mirrored window |
| 9532 | setFocusedWindow(mMirror); |
| 9533 | |
| 9534 | // window gets focused |
| 9535 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9536 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9537 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9538 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9539 | } |
| 9540 | |
| 9541 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 9542 | // focusable. |
| 9543 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 9544 | setFocusedWindow(mMirror); |
| 9545 | |
| 9546 | // window gets focused |
| 9547 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9548 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9549 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9550 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9551 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9552 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9553 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9554 | |
| 9555 | mMirror->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9556 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9557 | |
| 9558 | // window loses focus since one of the windows associated with the token in not focusable |
| 9559 | mWindow->consumeFocusEvent(false); |
| 9560 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9561 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9562 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9563 | mWindow->assertNoEvents(); |
| 9564 | } |
| 9565 | |
| 9566 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 9567 | // invisible. |
| 9568 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 9569 | setFocusedWindow(mMirror); |
| 9570 | |
| 9571 | // window gets focused |
| 9572 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9573 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9574 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9575 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9576 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*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->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9579 | |
| 9580 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9581 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9582 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9583 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9584 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9585 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9586 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9587 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9588 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9589 | |
| 9590 | mWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9591 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9592 | |
| 9593 | // window loses focus only after all windows associated with the token become invisible. |
| 9594 | mWindow->consumeFocusEvent(false); |
| 9595 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9596 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9597 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9598 | mWindow->assertNoEvents(); |
| 9599 | } |
| 9600 | |
| 9601 | // A focused & mirrored window remains focused until both windows are removed. |
| 9602 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 9603 | setFocusedWindow(mMirror); |
| 9604 | |
| 9605 | // window gets focused |
| 9606 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9607 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9608 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9609 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9610 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9611 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9612 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9613 | |
| 9614 | // single window is removed but the window token remains focused |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9615 | mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9616 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9617 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9618 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9619 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9620 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9621 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9622 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9623 | |
| 9624 | // Both windows are removed |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9625 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9626 | mWindow->consumeFocusEvent(false); |
| 9627 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9628 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9629 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9630 | mWindow->assertNoEvents(); |
| 9631 | } |
| 9632 | |
| 9633 | // Focus request can be pending until one window becomes visible. |
| 9634 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 9635 | // Request focus on an invisible mirror. |
| 9636 | mWindow->setVisible(false); |
| 9637 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9638 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9639 | setFocusedWindow(mMirror); |
| 9640 | |
| 9641 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9642 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9643 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 9644 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9645 | |
| 9646 | mMirror->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9647 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9648 | |
| 9649 | // window gets focused |
| 9650 | mWindow->consumeFocusEvent(true); |
| 9651 | // window gets the pending key event |
| 9652 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 9653 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9654 | |
| 9655 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 9656 | protected: |
| 9657 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 9658 | sp<FakeWindowHandle> mWindow; |
| 9659 | sp<FakeWindowHandle> mSecondWindow; |
| 9660 | |
| 9661 | void SetUp() override { |
| 9662 | InputDispatcherTest::SetUp(); |
| 9663 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9664 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9665 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9666 | mSecondWindow = |
| 9667 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9668 | mSecondWindow->setFocusable(true); |
| 9669 | |
| 9670 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9671 | mDispatcher->onWindowInfosChanged( |
| 9672 | {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9673 | |
| 9674 | setFocusedWindow(mWindow); |
| 9675 | mWindow->consumeFocusEvent(true); |
| 9676 | } |
| 9677 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9678 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9679 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9680 | } |
| 9681 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9682 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 9683 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9684 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9685 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 9686 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9687 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9688 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9689 | } |
| 9690 | }; |
| 9691 | |
| 9692 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 9693 | // Ensure that capture cannot be obtained for unfocused windows. |
| 9694 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 9695 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 9696 | mSecondWindow->assertNoEvents(); |
| 9697 | |
| 9698 | // Ensure that capture can be enabled from the focus window. |
| 9699 | requestAndVerifyPointerCapture(mWindow, true); |
| 9700 | |
| 9701 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 9702 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 9703 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 9704 | |
| 9705 | // Ensure that capture can be disabled from the window with capture. |
| 9706 | requestAndVerifyPointerCapture(mWindow, false); |
| 9707 | } |
| 9708 | |
| 9709 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9710 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9711 | |
| 9712 | setFocusedWindow(mSecondWindow); |
| 9713 | |
| 9714 | // Ensure that the capture disabled event was sent first. |
| 9715 | mWindow->consumeCaptureEvent(false); |
| 9716 | mWindow->consumeFocusEvent(false); |
| 9717 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9718 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9719 | |
| 9720 | // 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] | 9721 | notifyPointerCaptureChanged({}); |
| 9722 | notifyPointerCaptureChanged(request); |
| 9723 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9724 | mWindow->assertNoEvents(); |
| 9725 | mSecondWindow->assertNoEvents(); |
| 9726 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 9727 | } |
| 9728 | |
| 9729 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9730 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9731 | |
| 9732 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9733 | notifyPointerCaptureChanged({}); |
| 9734 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9735 | |
| 9736 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9737 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9738 | mWindow->consumeCaptureEvent(false); |
| 9739 | mWindow->assertNoEvents(); |
| 9740 | } |
| 9741 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9742 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 9743 | requestAndVerifyPointerCapture(mWindow, true); |
| 9744 | |
| 9745 | // The first window loses focus. |
| 9746 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9747 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9748 | mWindow->consumeCaptureEvent(false); |
| 9749 | |
| 9750 | // Request Pointer Capture from the second window before the notification from InputReader |
| 9751 | // arrives. |
| 9752 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9753 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9754 | |
| 9755 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9756 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9757 | |
| 9758 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9759 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9760 | |
| 9761 | mSecondWindow->consumeFocusEvent(true); |
| 9762 | mSecondWindow->consumeCaptureEvent(true); |
| 9763 | } |
| 9764 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9765 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 9766 | // App repeatedly enables and disables capture. |
| 9767 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 9768 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 9769 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 9770 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 9771 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 9772 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 9773 | |
| 9774 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 9775 | // first request is now stale, this should do nothing. |
| 9776 | notifyPointerCaptureChanged(firstRequest); |
| 9777 | mWindow->assertNoEvents(); |
| 9778 | |
| 9779 | // InputReader notifies that the second request was enabled. |
| 9780 | notifyPointerCaptureChanged(secondRequest); |
| 9781 | mWindow->consumeCaptureEvent(true); |
| 9782 | } |
| 9783 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 9784 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 9785 | requestAndVerifyPointerCapture(mWindow, true); |
| 9786 | |
| 9787 | // App toggles pointer capture off and on. |
| 9788 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 9789 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 9790 | |
| 9791 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 9792 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 9793 | |
| 9794 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 9795 | // preceding "disable" request. |
| 9796 | notifyPointerCaptureChanged(enableRequest); |
| 9797 | |
| 9798 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 9799 | // any notifications. |
| 9800 | mWindow->assertNoEvents(); |
| 9801 | } |
| 9802 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 9803 | /** |
| 9804 | * One window. Hover mouse in the window, and then start capture. Make sure that the relative |
| 9805 | * mouse movements don't affect the previous mouse hovering state. |
| 9806 | * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no |
| 9807 | * HOVER_MOVE events). |
| 9808 | */ |
| 9809 | TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) { |
| 9810 | // Mouse hover on the window |
| 9811 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 9812 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 9813 | .build()); |
| 9814 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 9815 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 9816 | .build()); |
| 9817 | |
| 9818 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER))); |
| 9819 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE))); |
| 9820 | |
| 9821 | // Start pointer capture |
| 9822 | requestAndVerifyPointerCapture(mWindow, true); |
| 9823 | |
| 9824 | // Send some relative mouse movements and receive them in the window. |
| 9825 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE) |
| 9826 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11)) |
| 9827 | .build()); |
| 9828 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11), |
| 9829 | WithSource(AINPUT_SOURCE_MOUSE_RELATIVE))); |
| 9830 | |
| 9831 | // Stop pointer capture |
| 9832 | requestAndVerifyPointerCapture(mWindow, false); |
| 9833 | |
| 9834 | // Continue hovering on the window |
| 9835 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 9836 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115)) |
| 9837 | .build()); |
| 9838 | mWindow->consumeMotionEvent( |
| 9839 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 9840 | |
| 9841 | mWindow->assertNoEvents(); |
| 9842 | } |
| 9843 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9844 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 9845 | protected: |
| 9846 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9847 | |
| 9848 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 9849 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 9850 | |
| 9851 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 9852 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 9853 | |
| 9854 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 9855 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 9856 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 9857 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 9858 | MAXIMUM_OBSCURING_OPACITY); |
| 9859 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9860 | static constexpr gui::Uid TOUCHED_APP_UID{10001}; |
| 9861 | static constexpr gui::Uid APP_B_UID{10002}; |
| 9862 | static constexpr gui::Uid APP_C_UID{10003}; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9863 | |
| 9864 | sp<FakeWindowHandle> mTouchWindow; |
| 9865 | |
| 9866 | virtual void SetUp() override { |
| 9867 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9868 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9869 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 9870 | } |
| 9871 | |
| 9872 | virtual void TearDown() override { |
| 9873 | InputDispatcherTest::TearDown(); |
| 9874 | mTouchWindow.clear(); |
| 9875 | } |
| 9876 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9877 | sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 9878 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9879 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9880 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9881 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9882 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9883 | return window; |
| 9884 | } |
| 9885 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9886 | sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9887 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 9888 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9889 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9890 | // Generate an arbitrary PID based on the UID |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9891 | 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] | 9892 | return window; |
| 9893 | } |
| 9894 | |
| 9895 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9896 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9897 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9898 | points)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9899 | } |
| 9900 | }; |
| 9901 | |
| 9902 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9903 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9904 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9905 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9906 | |
| 9907 | touch(); |
| 9908 | |
| 9909 | mTouchWindow->assertNoEvents(); |
| 9910 | } |
| 9911 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9912 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9913 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 9914 | const sp<FakeWindowHandle>& w = |
| 9915 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9916 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9917 | |
| 9918 | touch(); |
| 9919 | |
| 9920 | mTouchWindow->assertNoEvents(); |
| 9921 | } |
| 9922 | |
| 9923 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9924 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 9925 | const sp<FakeWindowHandle>& w = |
| 9926 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9927 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9928 | |
| 9929 | touch(); |
| 9930 | |
| 9931 | w->assertNoEvents(); |
| 9932 | } |
| 9933 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9934 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9935 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9936 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9937 | |
| 9938 | touch(); |
| 9939 | |
| 9940 | mTouchWindow->consumeAnyMotionDown(); |
| 9941 | } |
| 9942 | |
| 9943 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9944 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9945 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9946 | w->setFrame(Rect(0, 0, 50, 50)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9947 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9948 | |
| 9949 | touch({PointF{100, 100}}); |
| 9950 | |
| 9951 | mTouchWindow->consumeAnyMotionDown(); |
| 9952 | } |
| 9953 | |
| 9954 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9955 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9956 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9957 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9958 | |
| 9959 | touch(); |
| 9960 | |
| 9961 | mTouchWindow->consumeAnyMotionDown(); |
| 9962 | } |
| 9963 | |
| 9964 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 9965 | const sp<FakeWindowHandle>& w = |
| 9966 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9967 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9968 | |
| 9969 | touch(); |
| 9970 | |
| 9971 | mTouchWindow->consumeAnyMotionDown(); |
| 9972 | } |
| 9973 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9974 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 9975 | const sp<FakeWindowHandle>& w = |
| 9976 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9977 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9978 | |
| 9979 | touch(); |
| 9980 | |
| 9981 | w->assertNoEvents(); |
| 9982 | } |
| 9983 | |
| 9984 | /** |
| 9985 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 9986 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 9987 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 9988 | */ |
| 9989 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 9990 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 9991 | const sp<FakeWindowHandle>& w = |
| 9992 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9993 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9994 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9995 | |
| 9996 | touch(); |
| 9997 | |
| 9998 | w->consumeMotionOutside(); |
| 9999 | } |
| 10000 | |
| 10001 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 10002 | const sp<FakeWindowHandle>& w = |
| 10003 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10004 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10005 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 10006 | |
| 10007 | touch(); |
| 10008 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 10009 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 10010 | } |
| 10011 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10012 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10013 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10014 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10015 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10016 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10017 | |
| 10018 | touch(); |
| 10019 | |
| 10020 | mTouchWindow->consumeAnyMotionDown(); |
| 10021 | } |
| 10022 | |
| 10023 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 10024 | const sp<FakeWindowHandle>& w = |
| 10025 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10026 | MAXIMUM_OBSCURING_OPACITY); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10027 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10028 | |
| 10029 | touch(); |
| 10030 | |
| 10031 | mTouchWindow->consumeAnyMotionDown(); |
| 10032 | } |
| 10033 | |
| 10034 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10035 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10036 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10037 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10038 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10039 | |
| 10040 | touch(); |
| 10041 | |
| 10042 | mTouchWindow->assertNoEvents(); |
| 10043 | } |
| 10044 | |
| 10045 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 10046 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 10047 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10048 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 10049 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10050 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10051 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 10052 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10053 | mDispatcher->onWindowInfosChanged( |
| 10054 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10055 | |
| 10056 | touch(); |
| 10057 | |
| 10058 | mTouchWindow->assertNoEvents(); |
| 10059 | } |
| 10060 | |
| 10061 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 10062 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 10063 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10064 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 10065 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10066 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10067 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 10068 | OPACITY_FAR_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10069 | mDispatcher->onWindowInfosChanged( |
| 10070 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10071 | |
| 10072 | touch(); |
| 10073 | |
| 10074 | mTouchWindow->consumeAnyMotionDown(); |
| 10075 | } |
| 10076 | |
| 10077 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10078 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 10079 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10080 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10081 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10082 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10083 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 10084 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10085 | mDispatcher->onWindowInfosChanged( |
| 10086 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10087 | |
| 10088 | touch(); |
| 10089 | |
| 10090 | mTouchWindow->consumeAnyMotionDown(); |
| 10091 | } |
| 10092 | |
| 10093 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 10094 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10095 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10096 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10097 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10098 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 10099 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10100 | mDispatcher->onWindowInfosChanged( |
| 10101 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10102 | |
| 10103 | touch(); |
| 10104 | |
| 10105 | mTouchWindow->assertNoEvents(); |
| 10106 | } |
| 10107 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10108 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10109 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 10110 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10111 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 10112 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10113 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10114 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10115 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10116 | mDispatcher->onWindowInfosChanged( |
| 10117 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10118 | |
| 10119 | touch(); |
| 10120 | |
| 10121 | mTouchWindow->assertNoEvents(); |
| 10122 | } |
| 10123 | |
| 10124 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10125 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 10126 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10127 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 10128 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10129 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10130 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10131 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10132 | mDispatcher->onWindowInfosChanged( |
| 10133 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10134 | |
| 10135 | touch(); |
| 10136 | |
| 10137 | mTouchWindow->consumeAnyMotionDown(); |
| 10138 | } |
| 10139 | |
| 10140 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 10141 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10142 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 10143 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10144 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10145 | |
| 10146 | touch(); |
| 10147 | |
| 10148 | mTouchWindow->consumeAnyMotionDown(); |
| 10149 | } |
| 10150 | |
| 10151 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 10152 | const sp<FakeWindowHandle>& w = |
| 10153 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10154 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10155 | |
| 10156 | touch(); |
| 10157 | |
| 10158 | mTouchWindow->consumeAnyMotionDown(); |
| 10159 | } |
| 10160 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 10161 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10162 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 10163 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 10164 | const sp<FakeWindowHandle>& w = |
| 10165 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10166 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 10167 | |
| 10168 | touch(); |
| 10169 | |
| 10170 | mTouchWindow->assertNoEvents(); |
| 10171 | } |
| 10172 | |
| 10173 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 10174 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 10175 | const sp<FakeWindowHandle>& w = |
| 10176 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10177 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 10178 | |
| 10179 | touch(); |
| 10180 | |
| 10181 | mTouchWindow->consumeAnyMotionDown(); |
| 10182 | } |
| 10183 | |
| 10184 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10185 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 10186 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 10187 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10188 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10189 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10190 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10191 | |
| 10192 | touch(); |
| 10193 | |
| 10194 | mTouchWindow->consumeAnyMotionDown(); |
| 10195 | } |
| 10196 | |
| 10197 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10198 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 10199 | const sp<FakeWindowHandle>& w1 = |
| 10200 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 10201 | OPACITY_BELOW_THRESHOLD); |
| 10202 | const sp<FakeWindowHandle>& w2 = |
| 10203 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10204 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10205 | mDispatcher->onWindowInfosChanged( |
| 10206 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10207 | |
| 10208 | touch(); |
| 10209 | |
| 10210 | mTouchWindow->assertNoEvents(); |
| 10211 | } |
| 10212 | |
| 10213 | /** |
| 10214 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 10215 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 10216 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 10217 | */ |
| 10218 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10219 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 10220 | const sp<FakeWindowHandle>& wB = |
| 10221 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 10222 | OPACITY_BELOW_THRESHOLD); |
| 10223 | const sp<FakeWindowHandle>& wC = |
| 10224 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 10225 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10226 | mDispatcher->onWindowInfosChanged( |
| 10227 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10228 | |
| 10229 | touch(); |
| 10230 | |
| 10231 | mTouchWindow->assertNoEvents(); |
| 10232 | } |
| 10233 | |
| 10234 | /** |
| 10235 | * This test is testing that a window from a different UID but with same application token doesn't |
| 10236 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 10237 | */ |
| 10238 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10239 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 10240 | const sp<FakeWindowHandle>& w = |
| 10241 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 10242 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10243 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 10244 | |
| 10245 | touch(); |
| 10246 | |
| 10247 | mTouchWindow->consumeAnyMotionDown(); |
| 10248 | } |
| 10249 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10250 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 10251 | protected: |
| 10252 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 10253 | sp<FakeWindowHandle> mWindow; |
| 10254 | sp<FakeWindowHandle> mSecondWindow; |
| 10255 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10256 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10257 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 10258 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10259 | |
| 10260 | void SetUp() override { |
| 10261 | InputDispatcherTest::SetUp(); |
| 10262 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10263 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10264 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10265 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10266 | mSecondWindow = |
| 10267 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10268 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10269 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10270 | mSpyWindow = |
| 10271 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10272 | mSpyWindow->setSpy(true); |
| 10273 | mSpyWindow->setTrustedOverlay(true); |
| 10274 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 10275 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10276 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10277 | mDispatcher->onWindowInfosChanged( |
| 10278 | {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 10279 | {}, |
| 10280 | 0, |
| 10281 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10282 | } |
| 10283 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10284 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 10285 | switch (fromSource) { |
| 10286 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 10287 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10288 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10289 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10290 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10291 | break; |
| 10292 | case AINPUT_SOURCE_STYLUS: |
| 10293 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10294 | injectMotionEvent(*mDispatcher, |
| 10295 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10296 | AINPUT_SOURCE_STYLUS) |
| 10297 | .buttonState( |
| 10298 | AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 10299 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
| 10300 | .x(50) |
| 10301 | .y(50)) |
| 10302 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10303 | break; |
| 10304 | case AINPUT_SOURCE_MOUSE: |
| 10305 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10306 | injectMotionEvent(*mDispatcher, |
| 10307 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10308 | AINPUT_SOURCE_MOUSE) |
| 10309 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 10310 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 10311 | ToolType::MOUSE) |
| 10312 | .x(50) |
| 10313 | .y(50)) |
| 10314 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10315 | break; |
| 10316 | default: |
| 10317 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 10318 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10319 | |
| 10320 | // Window should receive motion event. |
| 10321 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10322 | // Spy window should also receive motion event |
| 10323 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10324 | } |
| 10325 | |
| 10326 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 10327 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 10328 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10329 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10330 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10331 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10332 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10333 | |
| 10334 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10335 | mDragWindow = |
| 10336 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10337 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10338 | mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), |
| 10339 | *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 10340 | {}, |
| 10341 | 0, |
| 10342 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10343 | |
| 10344 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10345 | bool transferred = |
| 10346 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10347 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10348 | if (transferred) { |
| 10349 | mWindow->consumeMotionCancel(); |
| 10350 | mDragWindow->consumeMotionDown(); |
| 10351 | } |
| 10352 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10353 | } |
| 10354 | }; |
| 10355 | |
| 10356 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10357 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10358 | |
| 10359 | // Move on window. |
| 10360 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10361 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10362 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10363 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10364 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10365 | mWindow->consumeDragEvent(false, 50, 50); |
| 10366 | mSecondWindow->assertNoEvents(); |
| 10367 | |
| 10368 | // Move to another window. |
| 10369 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10370 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10371 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10372 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10373 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10374 | mWindow->consumeDragEvent(true, 150, 50); |
| 10375 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10376 | |
| 10377 | // Move back to original window. |
| 10378 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10379 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10380 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10381 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10382 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10383 | mWindow->consumeDragEvent(false, 50, 50); |
| 10384 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 10385 | |
| 10386 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10387 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10388 | {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10389 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10390 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 10391 | mWindow->assertNoEvents(); |
| 10392 | mSecondWindow->assertNoEvents(); |
| 10393 | } |
| 10394 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10395 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10396 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10397 | |
| 10398 | // No cancel event after drag start |
| 10399 | mSpyWindow->assertNoEvents(); |
| 10400 | |
| 10401 | const MotionEvent secondFingerDownEvent = |
| 10402 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10403 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10404 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10405 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10406 | .build(); |
| 10407 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10408 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10409 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10410 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10411 | |
| 10412 | // Receives cancel for first pointer after next pointer down |
| 10413 | mSpyWindow->consumeMotionCancel(); |
| 10414 | mSpyWindow->consumeMotionDown(); |
| 10415 | |
| 10416 | mSpyWindow->assertNoEvents(); |
| 10417 | } |
| 10418 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10419 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10420 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10421 | |
| 10422 | // Move on window. |
| 10423 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10424 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10425 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10426 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10427 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10428 | mWindow->consumeDragEvent(false, 50, 50); |
| 10429 | mSecondWindow->assertNoEvents(); |
| 10430 | |
| 10431 | // Move to another window. |
| 10432 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10433 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10434 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10435 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10436 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10437 | mWindow->consumeDragEvent(true, 150, 50); |
| 10438 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10439 | |
| 10440 | // drop to another window. |
| 10441 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10442 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10443 | {150, 50})) |
| 10444 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10445 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10446 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10447 | mWindow->assertNoEvents(); |
| 10448 | mSecondWindow->assertNoEvents(); |
| 10449 | } |
| 10450 | |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10451 | TEST_F(InputDispatcherDragTests, DragAndDropNotCancelledIfSomeOtherPointerIsPilfered) { |
| 10452 | startDrag(); |
| 10453 | |
| 10454 | // No cancel event after drag start |
| 10455 | mSpyWindow->assertNoEvents(); |
| 10456 | |
| 10457 | const MotionEvent secondFingerDownEvent = |
| 10458 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10459 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 10460 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10461 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 10462 | .build(); |
| 10463 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10464 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 10465 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10466 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10467 | |
| 10468 | // Receives cancel for first pointer after next pointer down |
| 10469 | mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 10470 | mSpyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithPointerIds({1}))); |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10471 | mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 10472 | |
| 10473 | mSpyWindow->assertNoEvents(); |
| 10474 | |
| 10475 | // Spy window calls pilfer pointers |
| 10476 | EXPECT_EQ(OK, mDispatcher->pilferPointers(mSpyWindow->getToken())); |
| 10477 | mDragWindow->assertNoEvents(); |
| 10478 | |
| 10479 | const MotionEvent firstFingerMoveEvent = |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 10480 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10481 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 10482 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(60).y(60)) |
| 10483 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 10484 | .build(); |
| 10485 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 10486 | injectMotionEvent(*mDispatcher, firstFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10487 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10488 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10489 | |
| 10490 | // Drag window should still receive the new event |
| 10491 | mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 10492 | mDragWindow->assertNoEvents(); |
| 10493 | } |
| 10494 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10495 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10496 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10497 | |
| 10498 | // Move on window and keep button pressed. |
| 10499 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10500 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10501 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 10502 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10503 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10504 | .build())) |
| 10505 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10506 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10507 | mWindow->consumeDragEvent(false, 50, 50); |
| 10508 | mSecondWindow->assertNoEvents(); |
| 10509 | |
| 10510 | // Move to another window and release button, expect to drop item. |
| 10511 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10512 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10513 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 10514 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10515 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10516 | .build())) |
| 10517 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10518 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10519 | mWindow->assertNoEvents(); |
| 10520 | mSecondWindow->assertNoEvents(); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10521 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10522 | |
| 10523 | // nothing to the window. |
| 10524 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10525 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10526 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 10527 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10528 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10529 | .build())) |
| 10530 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10531 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 10532 | mWindow->assertNoEvents(); |
| 10533 | mSecondWindow->assertNoEvents(); |
| 10534 | } |
| 10535 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10536 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10537 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10538 | |
| 10539 | // Set second window invisible. |
| 10540 | mSecondWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10541 | mDispatcher->onWindowInfosChanged( |
| 10542 | {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10543 | |
| 10544 | // Move on window. |
| 10545 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10546 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10547 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10548 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10549 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10550 | mWindow->consumeDragEvent(false, 50, 50); |
| 10551 | mSecondWindow->assertNoEvents(); |
| 10552 | |
| 10553 | // Move to another window. |
| 10554 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10555 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10556 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10557 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10558 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10559 | mWindow->consumeDragEvent(true, 150, 50); |
| 10560 | mSecondWindow->assertNoEvents(); |
| 10561 | |
| 10562 | // drop to another window. |
| 10563 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10564 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10565 | {150, 50})) |
| 10566 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10567 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10568 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10569 | mWindow->assertNoEvents(); |
| 10570 | mSecondWindow->assertNoEvents(); |
| 10571 | } |
| 10572 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10573 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10574 | // Ensure window could track pointerIds if it didn't support split touch. |
| 10575 | mWindow->setPreventSplitting(true); |
| 10576 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10577 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10578 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10579 | {50, 50})) |
| 10580 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10581 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10582 | |
| 10583 | const MotionEvent secondFingerDownEvent = |
| 10584 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10585 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10586 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10587 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10588 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10589 | .build(); |
| 10590 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10591 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10592 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10593 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10594 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10595 | |
| 10596 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10597 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10598 | } |
| 10599 | |
| 10600 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 10601 | // First down on second window. |
| 10602 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10603 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10604 | {150, 50})) |
| 10605 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10606 | |
| 10607 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10608 | |
| 10609 | // Second down on first window. |
| 10610 | const MotionEvent secondFingerDownEvent = |
| 10611 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10612 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10613 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10614 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10615 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10616 | .build(); |
| 10617 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10618 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10619 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10620 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10621 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10622 | |
| 10623 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10624 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10625 | |
| 10626 | // Move on window. |
| 10627 | const MotionEvent secondFingerMoveEvent = |
| 10628 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 10629 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10630 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10631 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10632 | .build(); |
| 10633 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10634 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10635 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 10636 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10637 | mWindow->consumeDragEvent(false, 50, 50); |
| 10638 | mSecondWindow->consumeMotionMove(); |
| 10639 | |
| 10640 | // Release the drag pointer should perform drop. |
| 10641 | const MotionEvent secondFingerUpEvent = |
| 10642 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 10643 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10644 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10645 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10646 | .build(); |
| 10647 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10648 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10649 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 10650 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10651 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken()); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10652 | mWindow->assertNoEvents(); |
| 10653 | mSecondWindow->consumeMotionMove(); |
| 10654 | } |
| 10655 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10656 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10657 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10658 | |
| 10659 | // Update window of second display. |
| 10660 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10661 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10662 | mDispatcher->onWindowInfosChanged( |
| 10663 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 10664 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 10665 | {}, |
| 10666 | 0, |
| 10667 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10668 | |
| 10669 | // Let second display has a touch state. |
| 10670 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10671 | injectMotionEvent(*mDispatcher, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10672 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10673 | AINPUT_SOURCE_TOUCHSCREEN) |
| 10674 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10675 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10676 | .build())); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 10677 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10678 | // Update window again. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10679 | mDispatcher->onWindowInfosChanged( |
| 10680 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 10681 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 10682 | {}, |
| 10683 | 0, |
| 10684 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10685 | |
| 10686 | // Move on window. |
| 10687 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10688 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10689 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10690 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10691 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10692 | mWindow->consumeDragEvent(false, 50, 50); |
| 10693 | mSecondWindow->assertNoEvents(); |
| 10694 | |
| 10695 | // Move to another window. |
| 10696 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10697 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10698 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10699 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10700 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10701 | mWindow->consumeDragEvent(true, 150, 50); |
| 10702 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10703 | |
| 10704 | // drop to another window. |
| 10705 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10706 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10707 | {150, 50})) |
| 10708 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10709 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10710 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10711 | mWindow->assertNoEvents(); |
| 10712 | mSecondWindow->assertNoEvents(); |
| 10713 | } |
| 10714 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10715 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 10716 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 10717 | // Move on window. |
| 10718 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10719 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10720 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 10721 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10722 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10723 | .x(50) |
| 10724 | .y(50)) |
| 10725 | .build())) |
| 10726 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10727 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10728 | mWindow->consumeDragEvent(false, 50, 50); |
| 10729 | mSecondWindow->assertNoEvents(); |
| 10730 | |
| 10731 | // Move to another window. |
| 10732 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10733 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10734 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 10735 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10736 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10737 | .x(150) |
| 10738 | .y(50)) |
| 10739 | .build())) |
| 10740 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10741 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10742 | mWindow->consumeDragEvent(true, 150, 50); |
| 10743 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10744 | |
| 10745 | // drop to another window. |
| 10746 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10747 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10748 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 10749 | .buttonState(0) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10750 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10751 | .x(150) |
| 10752 | .y(50)) |
| 10753 | .build())) |
| 10754 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10755 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10756 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10757 | mWindow->assertNoEvents(); |
| 10758 | mSecondWindow->assertNoEvents(); |
| 10759 | } |
| 10760 | |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 10761 | /** |
| 10762 | * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag |
| 10763 | * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash. |
| 10764 | */ |
| 10765 | TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) { |
| 10766 | // Down on second window |
| 10767 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10768 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10769 | {150, 50})) |
| 10770 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10771 | |
| 10772 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown()); |
| 10773 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown()); |
| 10774 | |
| 10775 | // Down on first window |
| 10776 | const MotionEvent secondFingerDownEvent = |
| 10777 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10778 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10779 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10780 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 10781 | .build(); |
| 10782 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10783 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 10784 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10785 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10786 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 10787 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove()); |
| 10788 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1)); |
| 10789 | |
| 10790 | // Start drag on first window |
| 10791 | ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN)); |
| 10792 | |
| 10793 | // Trigger cancel |
| 10794 | mDispatcher->cancelCurrentTouch(); |
| 10795 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel()); |
| 10796 | ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel()); |
| 10797 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel()); |
| 10798 | |
| 10799 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10800 | // The D&D finished with nullptr |
| 10801 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
| 10802 | |
| 10803 | // Remove drag window |
| 10804 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
| 10805 | |
| 10806 | // Inject a simple gesture, ensure dispatcher not crashed |
| 10807 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10808 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10809 | PointF{50, 50})) |
| 10810 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10811 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 10812 | |
| 10813 | const MotionEvent moveEvent = |
| 10814 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 10815 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10816 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10817 | .build(); |
| 10818 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10819 | injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT, |
| 10820 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10821 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10822 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove()); |
| 10823 | |
| 10824 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10825 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10826 | {50, 50})) |
| 10827 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10828 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp()); |
| 10829 | } |
| 10830 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 10831 | TEST_F(InputDispatcherDragTests, NoDragAndDropWithHoveringPointer) { |
| 10832 | // Start hovering over the window. |
| 10833 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10834 | injectMotionEvent(*mDispatcher, ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE, |
| 10835 | ADISPLAY_ID_DEFAULT, {50, 50})); |
| 10836 | |
| 10837 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 10838 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 10839 | |
| 10840 | ASSERT_FALSE(startDrag(/*sendDown=*/false)) |
| 10841 | << "Drag and drop should not work with a hovering pointer"; |
| 10842 | } |
| 10843 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10844 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 10845 | |
| 10846 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 10847 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10848 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 10849 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10850 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10851 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 10852 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10853 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10854 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10855 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10856 | |
| 10857 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10858 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10859 | window->assertNoEvents(); |
| 10860 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10861 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10862 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 10863 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 10864 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 10865 | mDispatcher->waitForIdle(); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10866 | window->assertNoEvents(); |
| 10867 | |
| 10868 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10869 | window->setDropInput(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10870 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10871 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10872 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10873 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 10874 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10875 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10876 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10877 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10878 | window->assertNoEvents(); |
| 10879 | } |
| 10880 | |
| 10881 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 10882 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 10883 | std::make_shared<FakeApplicationHandle>(); |
| 10884 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10885 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 10886 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10887 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10888 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10889 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10890 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10891 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 10892 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10893 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10894 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10895 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 10896 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10897 | mDispatcher->onWindowInfosChanged( |
| 10898 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10899 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10900 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10901 | |
| 10902 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10903 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10904 | window->assertNoEvents(); |
| 10905 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10906 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10907 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 10908 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 10909 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10910 | window->assertNoEvents(); |
| 10911 | |
| 10912 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10913 | window->setDropInputIfObscured(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10914 | mDispatcher->onWindowInfosChanged( |
| 10915 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10916 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10917 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10918 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 10919 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10920 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10921 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10922 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 10923 | window->assertNoEvents(); |
| 10924 | } |
| 10925 | |
| 10926 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 10927 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 10928 | std::make_shared<FakeApplicationHandle>(); |
| 10929 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10930 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 10931 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10932 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10933 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10934 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10935 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10936 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 10937 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10938 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10939 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10940 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 10941 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10942 | mDispatcher->onWindowInfosChanged( |
| 10943 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10944 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10945 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10946 | |
| 10947 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10948 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10949 | window->assertNoEvents(); |
| 10950 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10951 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10952 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 10953 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 10954 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10955 | window->assertNoEvents(); |
| 10956 | |
| 10957 | // 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] | 10958 | mDispatcher->onWindowInfosChanged( |
| 10959 | {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10960 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10961 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10962 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 10963 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10964 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10965 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10966 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10967 | window->assertNoEvents(); |
| 10968 | } |
| 10969 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10970 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 10971 | protected: |
| 10972 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10973 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10974 | sp<FakeWindowHandle> mWindow; |
| 10975 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10976 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10977 | |
| 10978 | void SetUp() override { |
| 10979 | InputDispatcherTest::SetUp(); |
| 10980 | |
| 10981 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10982 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10983 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10984 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 10985 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10986 | mSecondWindow = |
| 10987 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10988 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10989 | mThirdWindow = |
| 10990 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 10991 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 10992 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10993 | |
| 10994 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10995 | mDispatcher->onWindowInfosChanged( |
| 10996 | {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()}, |
| 10997 | {}, |
| 10998 | 0, |
| 10999 | 0}); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11000 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11001 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11002 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11003 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11004 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11005 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11006 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 11007 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11008 | mThirdWindow->assertNoEvents(); |
| 11009 | } |
| 11010 | |
| 11011 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 11012 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11013 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11014 | SECOND_DISPLAY_ID)) { |
| 11015 | mWindow->assertNoEvents(); |
| 11016 | mSecondWindow->assertNoEvents(); |
| 11017 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11018 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11019 | } |
| 11020 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11021 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11022 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 11023 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 11024 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11025 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 11026 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11027 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11028 | } |
| 11029 | }; |
| 11030 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11031 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 11032 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11033 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 11034 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11035 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11036 | } |
| 11037 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11038 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 11039 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11040 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11041 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11042 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11043 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11044 | ownerUid, /*hasPermission=*/false, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 11045 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11046 | mWindow->assertNoEvents(); |
| 11047 | mSecondWindow->assertNoEvents(); |
| 11048 | } |
| 11049 | |
| 11050 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 11051 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11052 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11053 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11054 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11055 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11056 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11057 | } |
| 11058 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11059 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 11060 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11061 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 11062 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11063 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11064 | mWindow->assertNoEvents(); |
| 11065 | mSecondWindow->assertNoEvents(); |
| 11066 | } |
| 11067 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11068 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 11069 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 11070 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 11071 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11072 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11073 | mWindow->assertNoEvents(); |
| 11074 | mSecondWindow->assertNoEvents(); |
| 11075 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 11076 | } |
| 11077 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11078 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 11079 | // Interact with the window first. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11080 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11081 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11082 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 11083 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 11084 | |
| 11085 | // Then remove focus. |
| 11086 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11087 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11088 | |
| 11089 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 11090 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 11091 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 11092 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11093 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11094 | } |
| 11095 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11096 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 11097 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11098 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11099 | std::shared_ptr<FakeApplicationHandle> application = |
| 11100 | std::make_shared<FakeApplicationHandle>(); |
| 11101 | std::string name = "Fake Spy "; |
| 11102 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11103 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 11104 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11105 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 11106 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11107 | return spy; |
| 11108 | } |
| 11109 | |
| 11110 | sp<FakeWindowHandle> createForeground() { |
| 11111 | std::shared_ptr<FakeApplicationHandle> application = |
| 11112 | std::make_shared<FakeApplicationHandle>(); |
| 11113 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11114 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 11115 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11116 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11117 | return window; |
| 11118 | } |
| 11119 | |
| 11120 | private: |
| 11121 | int mSpyCount{0}; |
| 11122 | }; |
| 11123 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11124 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11125 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 11126 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 11127 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11128 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
Siarhei Vishniakou | 21f77bd | 2023-07-18 17:51:35 -0700 | [diff] [blame] | 11129 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11130 | ScopedSilentDeath _silentDeath; |
| 11131 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11132 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 11133 | spy->setTrustedOverlay(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11134 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 11135 | ".* not a trusted overlay"); |
| 11136 | } |
| 11137 | |
| 11138 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11139 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 11140 | */ |
| 11141 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11142 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11143 | mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11144 | |
| 11145 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11146 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11147 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11148 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11149 | } |
| 11150 | |
| 11151 | /** |
| 11152 | * Verify the order in which different input windows receive events. The touched foreground window |
| 11153 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 11154 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 11155 | * receive events before ones belows it. |
| 11156 | * |
| 11157 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 11158 | * spy1, spy2, window, spy3. |
| 11159 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 11160 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 11161 | * window. |
| 11162 | */ |
| 11163 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 11164 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11165 | auto spy1 = createSpy(); |
| 11166 | auto spy2 = createSpy(); |
| 11167 | auto spy3 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11168 | mDispatcher->onWindowInfosChanged( |
| 11169 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11170 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 11171 | const size_t numChannels = channels.size(); |
| 11172 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 11173 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11174 | if (!epollFd.ok()) { |
| 11175 | FAIL() << "Failed to create epoll fd"; |
| 11176 | } |
| 11177 | |
| 11178 | for (size_t i = 0; i < numChannels; i++) { |
| 11179 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 11180 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 11181 | FAIL() << "Failed to add fd to epoll"; |
| 11182 | } |
| 11183 | } |
| 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 | |
| 11189 | std::vector<size_t> eventOrder; |
| 11190 | std::vector<struct epoll_event> events(numChannels); |
| 11191 | for (;;) { |
| 11192 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 11193 | (100ms).count()); |
| 11194 | if (nFds < 0) { |
| 11195 | FAIL() << "Failed to call epoll_wait"; |
| 11196 | } |
| 11197 | if (nFds == 0) { |
| 11198 | break; // epoll_wait timed out |
| 11199 | } |
| 11200 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 11201 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 11202 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11203 | channels[i]->consumeMotionDown(); |
| 11204 | } |
| 11205 | } |
| 11206 | |
| 11207 | // Verify the order in which the events were received. |
| 11208 | EXPECT_EQ(3u, eventOrder.size()); |
| 11209 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 11210 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 11211 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 11212 | } |
| 11213 | |
| 11214 | /** |
| 11215 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 11216 | */ |
| 11217 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 11218 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11219 | auto spy = createSpy(); |
| 11220 | spy->setTouchable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11221 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11222 | |
| 11223 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11224 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11225 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11226 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11227 | spy->assertNoEvents(); |
| 11228 | } |
| 11229 | |
| 11230 | /** |
| 11231 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 11232 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 11233 | * to the window. |
| 11234 | */ |
| 11235 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 11236 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11237 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11238 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11239 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11240 | |
| 11241 | // Inject an event outside the spy window's touchable region. |
| 11242 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11243 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11244 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11245 | window->consumeMotionDown(); |
| 11246 | spy->assertNoEvents(); |
| 11247 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11248 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11249 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11250 | window->consumeMotionUp(); |
| 11251 | spy->assertNoEvents(); |
| 11252 | |
| 11253 | // Inject an event inside the spy window's touchable region. |
| 11254 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11255 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11256 | {5, 10})) |
| 11257 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11258 | window->consumeMotionDown(); |
| 11259 | spy->consumeMotionDown(); |
| 11260 | } |
| 11261 | |
| 11262 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11263 | * 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] | 11264 | * 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] | 11265 | */ |
| 11266 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 11267 | auto window = createForeground(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11268 | window->setOwnerInfo(gui::Pid{12}, gui::Uid{34}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11269 | auto spy = createSpy(); |
| 11270 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11271 | spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11272 | spy->setFrame(Rect{0, 0, 20, 20}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11273 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11274 | |
| 11275 | // Inject an event outside the spy window's frame and touchable region. |
| 11276 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11277 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 11278 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11279 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11280 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 11281 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11282 | } |
| 11283 | |
| 11284 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11285 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 11286 | * pointers that are down within its bounds. |
| 11287 | */ |
| 11288 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 11289 | auto windowLeft = createForeground(); |
| 11290 | windowLeft->setFrame({0, 0, 100, 200}); |
| 11291 | auto windowRight = createForeground(); |
| 11292 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11293 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11294 | spy->setFrame({0, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11295 | mDispatcher->onWindowInfosChanged( |
| 11296 | {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11297 | |
| 11298 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11299 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11300 | {50, 50})) |
| 11301 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11302 | windowLeft->consumeMotionDown(); |
| 11303 | spy->consumeMotionDown(); |
| 11304 | |
| 11305 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 11306 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11307 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11308 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11309 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11310 | .build(); |
| 11311 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11312 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11313 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11314 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11315 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11316 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11317 | } |
| 11318 | |
| 11319 | /** |
| 11320 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 11321 | * the spy should receive the second pointer with ACTION_DOWN. |
| 11322 | */ |
| 11323 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 11324 | auto window = createForeground(); |
| 11325 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11326 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11327 | spyRight->setFrame({100, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11328 | mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11329 | |
| 11330 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11331 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11332 | {50, 50})) |
| 11333 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11334 | window->consumeMotionDown(); |
| 11335 | spyRight->assertNoEvents(); |
| 11336 | |
| 11337 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 11338 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11339 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11340 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11341 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11342 | .build(); |
| 11343 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11344 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11345 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11346 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11347 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11348 | spyRight->consumeMotionDown(); |
| 11349 | } |
| 11350 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11351 | /** |
| 11352 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 11353 | * windows should be allowed to control split touch. |
| 11354 | */ |
| 11355 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 11356 | // 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] | 11357 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11358 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 11359 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11360 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11361 | auto window = createForeground(); |
| 11362 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11363 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11364 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11365 | |
| 11366 | // First finger down, no window touched. |
| 11367 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11368 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11369 | {100, 200})) |
| 11370 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11371 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11372 | window->assertNoEvents(); |
| 11373 | |
| 11374 | // Second finger down on window, the window should receive touch down. |
| 11375 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 11376 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11377 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11378 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11379 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 11380 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11381 | .build(); |
| 11382 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11383 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11384 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11385 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11386 | |
| 11387 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11388 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11389 | } |
| 11390 | |
| 11391 | /** |
| 11392 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 11393 | * do not receive key events. |
| 11394 | */ |
| 11395 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11396 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11397 | spy->setFocusable(false); |
| 11398 | |
| 11399 | auto window = createForeground(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11400 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11401 | setFocusedWindow(window); |
| 11402 | window->consumeFocusEvent(true); |
| 11403 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11404 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11405 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 11406 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 11407 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11408 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11409 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 11410 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 11411 | |
| 11412 | spy->assertNoEvents(); |
| 11413 | } |
| 11414 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11415 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 11416 | |
| 11417 | /** |
| 11418 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 11419 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 11420 | */ |
| 11421 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 11422 | auto window = createForeground(); |
| 11423 | auto spy1 = createSpy(); |
| 11424 | auto spy2 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11425 | mDispatcher->onWindowInfosChanged( |
| 11426 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11427 | |
| 11428 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11429 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11430 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11431 | window->consumeMotionDown(); |
| 11432 | spy1->consumeMotionDown(); |
| 11433 | spy2->consumeMotionDown(); |
| 11434 | |
| 11435 | // Pilfer pointers from the second spy window. |
| 11436 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 11437 | spy2->assertNoEvents(); |
| 11438 | spy1->consumeMotionCancel(); |
| 11439 | window->consumeMotionCancel(); |
| 11440 | |
| 11441 | // The rest of the gesture should only be sent to the second spy window. |
| 11442 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11443 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11444 | ADISPLAY_ID_DEFAULT)) |
| 11445 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11446 | spy2->consumeMotionMove(); |
| 11447 | spy1->assertNoEvents(); |
| 11448 | window->assertNoEvents(); |
| 11449 | } |
| 11450 | |
| 11451 | /** |
| 11452 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 11453 | * in the middle of the gesture. |
| 11454 | */ |
| 11455 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 11456 | auto window = createForeground(); |
| 11457 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11458 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11459 | |
| 11460 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11461 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11462 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11463 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11464 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11465 | |
| 11466 | window->releaseChannel(); |
| 11467 | |
| 11468 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11469 | |
| 11470 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11471 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11472 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11473 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 11474 | } |
| 11475 | |
| 11476 | /** |
| 11477 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 11478 | * the spy, but not to any other windows. |
| 11479 | */ |
| 11480 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 11481 | auto spy = createSpy(); |
| 11482 | auto window = createForeground(); |
| 11483 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11484 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11485 | |
| 11486 | // First finger down on the window and the spy. |
| 11487 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11488 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11489 | {100, 200})) |
| 11490 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11491 | spy->consumeMotionDown(); |
| 11492 | window->consumeMotionDown(); |
| 11493 | |
| 11494 | // Spy window pilfers the pointers. |
| 11495 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11496 | window->consumeMotionCancel(); |
| 11497 | |
| 11498 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 11499 | const MotionEvent secondFingerDownEvent = |
| 11500 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11501 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11502 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11503 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 11504 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11505 | .build(); |
| 11506 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11507 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11508 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11509 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11510 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11511 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11512 | |
| 11513 | // Third finger goes down outside all windows, so injection should fail. |
| 11514 | const MotionEvent thirdFingerDownEvent = |
| 11515 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11516 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11517 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11518 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 11519 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 11520 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11521 | .build(); |
| 11522 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11523 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11524 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 11525 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11526 | |
| 11527 | spy->assertNoEvents(); |
| 11528 | window->assertNoEvents(); |
| 11529 | } |
| 11530 | |
| 11531 | /** |
| 11532 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 11533 | */ |
| 11534 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 11535 | auto spy = createSpy(); |
| 11536 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 11537 | auto window = createForeground(); |
| 11538 | window->setFrame(Rect(0, 0, 200, 200)); |
| 11539 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11540 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11541 | |
| 11542 | // First finger down on the window only |
| 11543 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11544 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11545 | {150, 150})) |
| 11546 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11547 | window->consumeMotionDown(); |
| 11548 | |
| 11549 | // Second finger down on the spy and window |
| 11550 | const MotionEvent secondFingerDownEvent = |
| 11551 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11552 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11553 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11554 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 11555 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11556 | .build(); |
| 11557 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11558 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11559 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11560 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11561 | spy->consumeMotionDown(); |
| 11562 | window->consumeMotionPointerDown(1); |
| 11563 | |
| 11564 | // Third finger down on the spy and window |
| 11565 | const MotionEvent thirdFingerDownEvent = |
| 11566 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11567 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11568 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11569 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 11570 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 11571 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11572 | .build(); |
| 11573 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11574 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11575 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11576 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11577 | spy->consumeMotionPointerDown(1); |
| 11578 | window->consumeMotionPointerDown(2); |
| 11579 | |
| 11580 | // Spy window pilfers the pointers. |
| 11581 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 11582 | window->consumeMotionPointerUp(/*idx=*/2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 11583 | window->consumeMotionPointerUp(/*idx=*/1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11584 | |
| 11585 | spy->assertNoEvents(); |
| 11586 | window->assertNoEvents(); |
| 11587 | } |
| 11588 | |
| 11589 | /** |
| 11590 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 11591 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 11592 | * window, then that window should receive ACTION_CANCEL. |
| 11593 | */ |
| 11594 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 11595 | auto spy = createSpy(); |
| 11596 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 11597 | auto window = createForeground(); |
| 11598 | window->setFrame(Rect(0, 0, 200, 200)); |
| 11599 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11600 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11601 | |
| 11602 | // First finger down on both spy and window |
| 11603 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11604 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11605 | {10, 10})) |
| 11606 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11607 | window->consumeMotionDown(); |
| 11608 | spy->consumeMotionDown(); |
| 11609 | |
| 11610 | // Second finger down on the spy and window |
| 11611 | const MotionEvent secondFingerDownEvent = |
| 11612 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11613 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11614 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11615 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 11616 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11617 | .build(); |
| 11618 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11619 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11620 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11621 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11622 | spy->consumeMotionPointerDown(1); |
| 11623 | window->consumeMotionPointerDown(1); |
| 11624 | |
| 11625 | // Spy window pilfers the pointers. |
| 11626 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11627 | window->consumeMotionCancel(); |
| 11628 | |
| 11629 | spy->assertNoEvents(); |
| 11630 | window->assertNoEvents(); |
| 11631 | } |
| 11632 | |
| 11633 | /** |
| 11634 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 11635 | * be sent to other windows |
| 11636 | */ |
| 11637 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 11638 | auto spy = createSpy(); |
| 11639 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 11640 | auto window = createForeground(); |
| 11641 | window->setFrame(Rect(0, 0, 200, 200)); |
| 11642 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11643 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11644 | |
| 11645 | // First finger down on both window and spy |
| 11646 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11647 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11648 | {10, 10})) |
| 11649 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11650 | window->consumeMotionDown(); |
| 11651 | spy->consumeMotionDown(); |
| 11652 | |
| 11653 | // Spy window pilfers the pointers. |
| 11654 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11655 | window->consumeMotionCancel(); |
| 11656 | |
| 11657 | // Second finger down on the window only |
| 11658 | const MotionEvent secondFingerDownEvent = |
| 11659 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11660 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11661 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11662 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 11663 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11664 | .build(); |
| 11665 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11666 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11667 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11668 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11669 | window->consumeMotionDown(); |
| 11670 | window->assertNoEvents(); |
| 11671 | |
| 11672 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 11673 | spy->consumeMotionMove(); |
| 11674 | spy->assertNoEvents(); |
| 11675 | } |
| 11676 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11677 | /** |
| 11678 | * A window on the left and a window on the right. Also, a spy window that's above all of the |
| 11679 | * windows, and spanning both left and right windows. |
| 11680 | * Send simultaneous motion streams from two different devices, one to the left window, and another |
| 11681 | * to the right window. |
| 11682 | * Pilfer from spy window. |
| 11683 | * Check that the pilfering only affects the pointers that are actually being received by the spy. |
| 11684 | */ |
| 11685 | TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) { |
| 11686 | sp<FakeWindowHandle> spy = createSpy(); |
| 11687 | spy->setFrame(Rect(0, 0, 200, 200)); |
| 11688 | sp<FakeWindowHandle> leftWindow = createForeground(); |
| 11689 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 11690 | |
| 11691 | sp<FakeWindowHandle> rightWindow = createForeground(); |
| 11692 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 11693 | |
| 11694 | constexpr int32_t stylusDeviceId = 1; |
| 11695 | constexpr int32_t touchDeviceId = 2; |
| 11696 | |
| 11697 | mDispatcher->onWindowInfosChanged( |
| 11698 | {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 11699 | |
| 11700 | // Stylus down on left window and spy |
| 11701 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 11702 | .deviceId(stylusDeviceId) |
| 11703 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 11704 | .build()); |
| 11705 | leftWindow->consumeMotionEvent( |
| 11706 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 11707 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 11708 | |
| 11709 | // Finger down on right window and spy - but spy already has stylus |
| 11710 | mDispatcher->notifyMotion( |
| 11711 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11712 | .deviceId(touchDeviceId) |
| 11713 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 11714 | .build()); |
| 11715 | rightWindow->consumeMotionEvent( |
| 11716 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11717 | spy->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11718 | |
| 11719 | // Act: pilfer from spy. Spy is currently receiving touch events. |
| 11720 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11721 | leftWindow->consumeMotionEvent( |
| 11722 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11723 | rightWindow->consumeMotionEvent( |
| 11724 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 11725 | |
| 11726 | // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus |
| 11727 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 11728 | .deviceId(stylusDeviceId) |
| 11729 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52)) |
| 11730 | .build()); |
| 11731 | mDispatcher->notifyMotion( |
| 11732 | MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 11733 | .deviceId(touchDeviceId) |
| 11734 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52)) |
| 11735 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11736 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11737 | |
| 11738 | spy->assertNoEvents(); |
| 11739 | leftWindow->assertNoEvents(); |
| 11740 | rightWindow->assertNoEvents(); |
| 11741 | } |
| 11742 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 11743 | TEST_F(InputDispatcherPilferPointersTest, NoPilferingWithHoveringPointers) { |
| 11744 | auto window = createForeground(); |
| 11745 | auto spy = createSpy(); |
| 11746 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 11747 | |
| 11748 | mDispatcher->notifyMotion( |
| 11749 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 11750 | .deviceId(1) |
| 11751 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(100).y(200)) |
| 11752 | .build()); |
| 11753 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 11754 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 11755 | |
| 11756 | // Pilfer pointers from the spy window should fail. |
| 11757 | EXPECT_NE(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11758 | spy->assertNoEvents(); |
| 11759 | window->assertNoEvents(); |
| 11760 | } |
| 11761 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11762 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 11763 | public: |
| 11764 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 11765 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 11766 | std::make_shared<FakeApplicationHandle>(); |
| 11767 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11768 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 11769 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11770 | overlay->setFocusable(false); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11771 | overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11772 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11773 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11774 | overlay->setTrustedOverlay(true); |
| 11775 | |
| 11776 | std::shared_ptr<FakeApplicationHandle> application = |
| 11777 | std::make_shared<FakeApplicationHandle>(); |
| 11778 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11779 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 11780 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11781 | window->setFocusable(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11782 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11783 | |
| 11784 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11785 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11786 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11787 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11788 | return {std::move(overlay), std::move(window)}; |
| 11789 | } |
| 11790 | |
| 11791 | void sendFingerEvent(int32_t action) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11792 | mDispatcher->notifyMotion( |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11793 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11794 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}})); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11795 | } |
| 11796 | |
| 11797 | void sendStylusEvent(int32_t action) { |
| 11798 | NotifyMotionArgs motionArgs = |
| 11799 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 11800 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11801 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11802 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11803 | } |
| 11804 | }; |
| 11805 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11806 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 11807 | |
| 11808 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
Siarhei Vishniakou | ad3b682 | 2023-06-22 14:17:35 -0700 | [diff] [blame] | 11809 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11810 | ScopedSilentDeath _silentDeath; |
| 11811 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11812 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 11813 | overlay->setTrustedOverlay(false); |
| 11814 | // 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] | 11815 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 11816 | {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11817 | ".* not a trusted overlay"); |
| 11818 | } |
| 11819 | |
| 11820 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 11821 | auto [overlay, window] = setupStylusOverlayScenario(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11822 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11823 | |
| 11824 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11825 | overlay->consumeMotionDown(); |
| 11826 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 11827 | overlay->consumeMotionUp(); |
| 11828 | |
| 11829 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11830 | window->consumeMotionDown(); |
| 11831 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 11832 | window->consumeMotionUp(); |
| 11833 | |
| 11834 | overlay->assertNoEvents(); |
| 11835 | window->assertNoEvents(); |
| 11836 | } |
| 11837 | |
| 11838 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 11839 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11840 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11841 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11842 | |
| 11843 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11844 | overlay->consumeMotionDown(); |
| 11845 | window->consumeMotionDown(); |
| 11846 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 11847 | overlay->consumeMotionUp(); |
| 11848 | window->consumeMotionUp(); |
| 11849 | |
| 11850 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11851 | window->consumeMotionDown(); |
| 11852 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 11853 | window->consumeMotionUp(); |
| 11854 | |
| 11855 | overlay->assertNoEvents(); |
| 11856 | window->assertNoEvents(); |
| 11857 | } |
| 11858 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 11859 | /** |
| 11860 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 11861 | * The scenario is as follows: |
| 11862 | * - The stylus interceptor overlay is configured as a spy window. |
| 11863 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 11864 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 11865 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 11866 | */ |
| 11867 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 11868 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 11869 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11870 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 11871 | |
| 11872 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11873 | overlay->consumeMotionDown(); |
| 11874 | window->consumeMotionDown(); |
| 11875 | |
| 11876 | // The interceptor pilfers the pointers. |
| 11877 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 11878 | window->consumeMotionCancel(); |
| 11879 | |
| 11880 | // The interceptor configures itself so that it is no longer a spy. |
| 11881 | overlay->setSpy(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11882 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 11883 | |
| 11884 | // It continues to receive the rest of the stylus gesture. |
| 11885 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 11886 | overlay->consumeMotionMove(); |
| 11887 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 11888 | overlay->consumeMotionUp(); |
| 11889 | |
| 11890 | window->assertNoEvents(); |
| 11891 | } |
| 11892 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11893 | struct User { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11894 | gui::Pid mPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11895 | gui::Uid mUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11896 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 11897 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 11898 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11899 | User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid) |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11900 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 11901 | |
| 11902 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11903 | return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11904 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 11905 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 11906 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 11907 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 11908 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 11909 | } |
| 11910 | |
| 11911 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11912 | return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11913 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11914 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11915 | mPolicyFlags); |
| 11916 | } |
| 11917 | |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11918 | sp<FakeWindowHandle> createWindow(const char* name) const { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11919 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 11920 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11921 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 11922 | name, ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11923 | window->setOwnerInfo(mPid, mUid); |
| 11924 | return window; |
| 11925 | } |
| 11926 | }; |
| 11927 | |
| 11928 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 11929 | |
| 11930 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11931 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11932 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11933 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11934 | |
| 11935 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11936 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11937 | window->consumeMotionDown(); |
| 11938 | |
| 11939 | setFocusedWindow(window); |
| 11940 | window->consumeFocusEvent(true); |
| 11941 | |
| 11942 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11943 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 11944 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 11945 | } |
| 11946 | |
| 11947 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11948 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11949 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11950 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11951 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11952 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11953 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 11954 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11955 | |
| 11956 | setFocusedWindow(window); |
| 11957 | window->consumeFocusEvent(true); |
| 11958 | |
| 11959 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 11960 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 11961 | window->assertNoEvents(); |
| 11962 | } |
| 11963 | |
| 11964 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11965 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11966 | auto window = owner.createWindow("Owned window"); |
| 11967 | auto spy = owner.createWindow("Owned spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11968 | spy->setSpy(true); |
| 11969 | spy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11970 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11971 | |
| 11972 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11973 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11974 | spy->consumeMotionDown(); |
| 11975 | window->consumeMotionDown(); |
| 11976 | } |
| 11977 | |
| 11978 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11979 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11980 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11981 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11982 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11983 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11984 | randosSpy->setSpy(true); |
| 11985 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11986 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11987 | |
| 11988 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 11989 | // not receive the event. |
| 11990 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11991 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11992 | randosSpy->assertNoEvents(); |
| 11993 | window->consumeMotionDown(); |
| 11994 | } |
| 11995 | |
| 11996 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11997 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11998 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11999 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12000 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12001 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12002 | randosSpy->setSpy(true); |
| 12003 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12004 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12005 | |
| 12006 | // A user that has injection permission can inject into any window. |
| 12007 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12008 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12009 | ADISPLAY_ID_DEFAULT)); |
| 12010 | randosSpy->consumeMotionDown(); |
| 12011 | window->consumeMotionDown(); |
| 12012 | |
| 12013 | setFocusedWindow(randosSpy); |
| 12014 | randosSpy->consumeFocusEvent(true); |
| 12015 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12016 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12017 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 12018 | window->assertNoEvents(); |
| 12019 | } |
| 12020 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 12021 | TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12022 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12023 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12024 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12025 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12026 | auto randosWindow = rando.createWindow("Rando's window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12027 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 12028 | randosWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12029 | mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12030 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 12031 | // 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] | 12032 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12033 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 12034 | window->consumeMotionDown(); |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 12035 | randosWindow->assertNoEvents(); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12036 | } |
| 12037 | |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 12038 | using InputDispatcherPointerInWindowTest = InputDispatcherTest; |
| 12039 | |
| 12040 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWhenHovering) { |
| 12041 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 12042 | |
| 12043 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 12044 | ADISPLAY_ID_DEFAULT); |
| 12045 | left->setFrame(Rect(0, 0, 100, 100)); |
| 12046 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 12047 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 12048 | right->setFrame(Rect(100, 0, 200, 100)); |
| 12049 | sp<FakeWindowHandle> spy = |
| 12050 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT); |
| 12051 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 12052 | spy->setTrustedOverlay(true); |
| 12053 | spy->setSpy(true); |
| 12054 | |
| 12055 | mDispatcher->onWindowInfosChanged( |
| 12056 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 12057 | |
| 12058 | // Hover into the left window. |
| 12059 | mDispatcher->notifyMotion( |
| 12060 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 12061 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(50).y(50)) |
| 12062 | .build()); |
| 12063 | |
| 12064 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12065 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12066 | |
| 12067 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12068 | /*pointerId=*/0)); |
| 12069 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12070 | /*pointerId=*/0)); |
| 12071 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12072 | /*pointerId=*/0)); |
| 12073 | |
| 12074 | // Hover move to the right window. |
| 12075 | mDispatcher->notifyMotion( |
| 12076 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 12077 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 12078 | .build()); |
| 12079 | |
| 12080 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12081 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12082 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 12083 | |
| 12084 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12085 | /*pointerId=*/0)); |
| 12086 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12087 | /*pointerId=*/0)); |
| 12088 | ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12089 | /*pointerId=*/0)); |
| 12090 | |
| 12091 | // Stop hovering. |
| 12092 | mDispatcher->notifyMotion( |
| 12093 | MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 12094 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 12095 | .build()); |
| 12096 | |
| 12097 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12098 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12099 | |
| 12100 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12101 | /*pointerId=*/0)); |
| 12102 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12103 | /*pointerId=*/0)); |
| 12104 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12105 | /*pointerId=*/0)); |
| 12106 | } |
| 12107 | |
| 12108 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWithSplitTouch) { |
| 12109 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 12110 | |
| 12111 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 12112 | ADISPLAY_ID_DEFAULT); |
| 12113 | left->setFrame(Rect(0, 0, 100, 100)); |
| 12114 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 12115 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 12116 | right->setFrame(Rect(100, 0, 200, 100)); |
| 12117 | sp<FakeWindowHandle> spy = |
| 12118 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT); |
| 12119 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 12120 | spy->setTrustedOverlay(true); |
| 12121 | spy->setSpy(true); |
| 12122 | |
| 12123 | mDispatcher->onWindowInfosChanged( |
| 12124 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 12125 | |
| 12126 | // First pointer down on left window. |
| 12127 | mDispatcher->notifyMotion( |
| 12128 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 12129 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12130 | .build()); |
| 12131 | |
| 12132 | left->consumeMotionDown(); |
| 12133 | spy->consumeMotionDown(); |
| 12134 | |
| 12135 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12136 | /*pointerId=*/0)); |
| 12137 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12138 | /*pointerId=*/0)); |
| 12139 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12140 | /*pointerId=*/0)); |
| 12141 | |
| 12142 | // Second pointer down on right window. |
| 12143 | mDispatcher->notifyMotion( |
| 12144 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 12145 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12146 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 12147 | .build()); |
| 12148 | |
| 12149 | left->consumeMotionMove(); |
| 12150 | right->consumeMotionDown(); |
| 12151 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 12152 | |
| 12153 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12154 | /*pointerId=*/0)); |
| 12155 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12156 | /*pointerId=*/0)); |
| 12157 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12158 | /*pointerId=*/0)); |
| 12159 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12160 | /*pointerId=*/1)); |
| 12161 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12162 | /*pointerId=*/1)); |
| 12163 | ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12164 | /*pointerId=*/1)); |
| 12165 | |
| 12166 | // Second pointer up. |
| 12167 | mDispatcher->notifyMotion( |
| 12168 | MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 12169 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12170 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 12171 | .build()); |
| 12172 | |
| 12173 | left->consumeMotionMove(); |
| 12174 | right->consumeMotionUp(); |
| 12175 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 12176 | |
| 12177 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12178 | /*pointerId=*/0)); |
| 12179 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12180 | /*pointerId=*/0)); |
| 12181 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12182 | /*pointerId=*/0)); |
| 12183 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12184 | /*pointerId=*/1)); |
| 12185 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12186 | /*pointerId=*/1)); |
| 12187 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12188 | /*pointerId=*/1)); |
| 12189 | |
| 12190 | // First pointer up. |
| 12191 | mDispatcher->notifyMotion( |
| 12192 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 12193 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12194 | .build()); |
| 12195 | |
| 12196 | left->consumeMotionUp(); |
| 12197 | spy->consumeMotionUp(); |
| 12198 | |
| 12199 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12200 | /*pointerId=*/0)); |
| 12201 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12202 | /*pointerId=*/0)); |
| 12203 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12204 | /*pointerId=*/0)); |
| 12205 | } |
| 12206 | |
| 12207 | TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse) { |
| 12208 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 12209 | |
| 12210 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 12211 | ADISPLAY_ID_DEFAULT); |
| 12212 | left->setFrame(Rect(0, 0, 100, 100)); |
| 12213 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 12214 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 12215 | right->setFrame(Rect(100, 0, 200, 100)); |
| 12216 | |
| 12217 | mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 12218 | |
| 12219 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->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 | |
| 12224 | // Hover move into the window. |
| 12225 | mDispatcher->notifyMotion( |
| 12226 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 12227 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50)) |
| 12228 | .rawXCursorPosition(50) |
| 12229 | .rawYCursorPosition(50) |
| 12230 | .deviceId(DEVICE_ID) |
| 12231 | .build()); |
| 12232 | |
| 12233 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12234 | |
| 12235 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12236 | /*pointerId=*/0)); |
| 12237 | |
| 12238 | // Move the mouse with another device. This cancels the hovering pointer from the first device. |
| 12239 | mDispatcher->notifyMotion( |
| 12240 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 12241 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50)) |
| 12242 | .rawXCursorPosition(51) |
| 12243 | .rawYCursorPosition(50) |
| 12244 | .deviceId(SECOND_DEVICE_ID) |
| 12245 | .build()); |
| 12246 | |
| 12247 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12248 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12249 | |
| 12250 | // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets |
| 12251 | // a HOVER_EXIT from the first device. |
| 12252 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12253 | /*pointerId=*/0)); |
| 12254 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, |
| 12255 | SECOND_DEVICE_ID, |
| 12256 | /*pointerId=*/0)); |
| 12257 | |
| 12258 | // Move the mouse outside the window. Document the current behavior, where the window does not |
| 12259 | // receive HOVER_EXIT even though the mouse left the window. |
| 12260 | mDispatcher->notifyMotion( |
| 12261 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 12262 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50)) |
| 12263 | .rawXCursorPosition(150) |
| 12264 | .rawYCursorPosition(50) |
| 12265 | .deviceId(SECOND_DEVICE_ID) |
| 12266 | .build()); |
| 12267 | |
| 12268 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12269 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12270 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12271 | /*pointerId=*/0)); |
| 12272 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, |
| 12273 | SECOND_DEVICE_ID, |
| 12274 | /*pointerId=*/0)); |
| 12275 | } |
| 12276 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 12277 | } // namespace android::inputdispatcher |