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 | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 366 | void setStaleEventTimeout(std::chrono::nanoseconds timeout) { mStaleEventTimeout = timeout; } |
| 367 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 368 | void assertUserActivityNotPoked() { |
| 369 | std::unique_lock lock(mLock); |
| 370 | base::ScopedLockAssertion assumeLocked(mLock); |
| 371 | |
| 372 | std::optional<UserActivityPokeEvent> pokeEvent = |
| 373 | getItemFromStorageLockedInterruptible(500ms, mUserActivityPokeEvents, lock, |
| 374 | mNotifyUserActivity); |
| 375 | |
| 376 | ASSERT_FALSE(pokeEvent) << "Expected user activity not to have been poked"; |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 379 | /** |
| 380 | * Asserts that a user activity poke has happened. The earliest recorded poke event will be |
| 381 | * cleared after this call. |
| 382 | * |
| 383 | * If an expected UserActivityPokeEvent is provided, asserts that the given event is the |
| 384 | * earliest recorded poke event. |
| 385 | */ |
| 386 | void assertUserActivityPoked(std::optional<UserActivityPokeEvent> expectedPokeEvent = {}) { |
| 387 | std::unique_lock lock(mLock); |
| 388 | base::ScopedLockAssertion assumeLocked(mLock); |
| 389 | |
| 390 | std::optional<UserActivityPokeEvent> pokeEvent = |
| 391 | getItemFromStorageLockedInterruptible(500ms, mUserActivityPokeEvents, lock, |
| 392 | mNotifyUserActivity); |
| 393 | ASSERT_TRUE(pokeEvent) << "Expected a user poke event"; |
| 394 | |
| 395 | if (expectedPokeEvent) { |
| 396 | ASSERT_EQ(expectedPokeEvent, *pokeEvent); |
| 397 | } |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 400 | void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids) { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 401 | ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms)); |
| 402 | } |
| 403 | |
| 404 | void assertNotifyDeviceInteractionWasNotCalled() { |
| 405 | ASSERT_FALSE(mNotifiedInteractions.popWithTimeout(10ms)); |
| 406 | } |
| 407 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 408 | void setUnhandledKeyHandler(std::function<std::optional<KeyEvent>(const KeyEvent&)> handler) { |
| 409 | std::scoped_lock lock(mLock); |
| 410 | mUnhandledKeyHandler = handler; |
| 411 | } |
| 412 | |
| 413 | void assertUnhandledKeyReported(int32_t keycode) { |
| 414 | std::unique_lock lock(mLock); |
| 415 | base::ScopedLockAssertion assumeLocked(mLock); |
| 416 | std::optional<int32_t> unhandledKeycode = |
| 417 | getItemFromStorageLockedInterruptible(100ms, mReportedUnhandledKeycodes, lock, |
| 418 | mNotifyUnhandledKey); |
| 419 | ASSERT_TRUE(unhandledKeycode) << "Expected unhandled key to be reported"; |
| 420 | ASSERT_EQ(unhandledKeycode, keycode); |
| 421 | } |
| 422 | |
| 423 | void assertUnhandledKeyNotReported() { |
| 424 | std::unique_lock lock(mLock); |
| 425 | base::ScopedLockAssertion assumeLocked(mLock); |
| 426 | std::optional<int32_t> unhandledKeycode = |
| 427 | getItemFromStorageLockedInterruptible(10ms, mReportedUnhandledKeycodes, lock, |
| 428 | mNotifyUnhandledKey); |
| 429 | ASSERT_FALSE(unhandledKeycode) << "Expected unhandled key NOT to be reported"; |
| 430 | } |
| 431 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 432 | private: |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 433 | std::mutex mLock; |
| 434 | std::unique_ptr<InputEvent> mFilteredEvent GUARDED_BY(mLock); |
| 435 | std::optional<nsecs_t> mConfigurationChangedTime GUARDED_BY(mLock); |
| 436 | sp<IBinder> mOnPointerDownToken GUARDED_BY(mLock); |
| 437 | std::optional<NotifySwitchArgs> mLastNotifySwitch GUARDED_BY(mLock); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 438 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 439 | std::condition_variable mPointerCaptureChangedCondition; |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 440 | |
| 441 | std::optional<PointerCaptureRequest> mPointerCaptureRequest GUARDED_BY(mLock); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 442 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 443 | // ANR handling |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 444 | std::queue<std::shared_ptr<InputApplicationHandle>> mAnrApplications GUARDED_BY(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 445 | std::queue<AnrResult> mAnrWindows GUARDED_BY(mLock); |
| 446 | std::queue<AnrResult> mResponsiveWindows GUARDED_BY(mLock); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 447 | std::condition_variable mNotifyAnr; |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 448 | std::queue<sp<IBinder>> mBrokenInputChannels GUARDED_BY(mLock); |
| 449 | std::condition_variable mNotifyInputChannelBroken; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 450 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 451 | sp<IBinder> mDropTargetWindowToken GUARDED_BY(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 452 | bool mNotifyDropWindowWasCalled GUARDED_BY(mLock) = false; |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 453 | |
| 454 | std::condition_variable mNotifyUserActivity; |
| 455 | std::queue<UserActivityPokeEvent> mUserActivityPokeEvents; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 456 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 457 | std::chrono::milliseconds mInterceptKeyTimeout = 0ms; |
| 458 | |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 459 | std::chrono::nanoseconds mStaleEventTimeout = 1000ms; |
| 460 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 461 | BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 462 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 463 | std::condition_variable mNotifyUnhandledKey; |
| 464 | std::queue<int32_t> mReportedUnhandledKeycodes GUARDED_BY(mLock); |
| 465 | std::function<std::optional<KeyEvent>(const KeyEvent&)> mUnhandledKeyHandler GUARDED_BY(mLock); |
| 466 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 467 | // All three ANR-related callbacks behave the same way, so we use this generic function to wait |
| 468 | // for a specific container to become non-empty. When the container is non-empty, return the |
| 469 | // first entry from the container and erase it. |
| 470 | template <class T> |
| 471 | T getAnrTokenLockedInterruptible(std::chrono::nanoseconds timeout, std::queue<T>& storage, |
| 472 | std::unique_lock<std::mutex>& lock) REQUIRES(mLock) { |
| 473 | // If there is an ANR, Dispatcher won't be idle because there are still events |
| 474 | // in the waitQueue that we need to check on. So we can't wait for dispatcher to be idle |
| 475 | // before checking if ANR was called. |
| 476 | // Since dispatcher is not guaranteed to call notifyNoFocusedWindowAnr right away, we need |
| 477 | // to provide it some time to act. 100ms seems reasonable. |
| 478 | std::chrono::duration timeToWait = timeout + 100ms; // provide some slack |
| 479 | const std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 480 | std::optional<T> token = |
| 481 | getItemFromStorageLockedInterruptible(timeToWait, storage, lock, mNotifyAnr); |
| 482 | if (!token.has_value()) { |
| 483 | ADD_FAILURE() << "Did not receive the ANR callback"; |
| 484 | return {}; |
| 485 | } |
| 486 | |
| 487 | const std::chrono::duration waited = std::chrono::steady_clock::now() - start; |
| 488 | // Ensure that the ANR didn't get raised too early. We can't be too strict here because |
| 489 | // the dispatcher started counting before this function was called |
| 490 | if (std::chrono::abs(timeout - waited) > 100ms) { |
| 491 | ADD_FAILURE() << "ANR was raised too early or too late. Expected " |
| 492 | << std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count() |
| 493 | << "ms, but waited " |
| 494 | << std::chrono::duration_cast<std::chrono::milliseconds>(waited).count() |
| 495 | << "ms instead"; |
| 496 | } |
| 497 | return *token; |
| 498 | } |
| 499 | |
| 500 | template <class T> |
| 501 | std::optional<T> getItemFromStorageLockedInterruptible(std::chrono::nanoseconds timeout, |
| 502 | std::queue<T>& storage, |
| 503 | std::unique_lock<std::mutex>& lock, |
| 504 | std::condition_variable& condition) |
| 505 | REQUIRES(mLock) { |
| 506 | condition.wait_for(lock, timeout, |
| 507 | [&storage]() REQUIRES(mLock) { return !storage.empty(); }); |
| 508 | if (storage.empty()) { |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 509 | return std::nullopt; |
| 510 | } |
| 511 | T item = storage.front(); |
| 512 | storage.pop(); |
| 513 | return std::make_optional(item); |
| 514 | } |
| 515 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 516 | void notifyConfigurationChanged(nsecs_t when) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 517 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 518 | mConfigurationChangedTime = when; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 519 | } |
| 520 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 521 | void notifyWindowUnresponsive(const sp<IBinder>& connectionToken, std::optional<gui::Pid> pid, |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 522 | const std::string&) override { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 523 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 524 | ASSERT_TRUE(pid.has_value()); |
| 525 | mAnrWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 526 | mNotifyAnr.notify_all(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 527 | } |
| 528 | |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 529 | void notifyWindowResponsive(const sp<IBinder>& connectionToken, |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 530 | std::optional<gui::Pid> pid) override { |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 531 | std::scoped_lock lock(mLock); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 532 | ASSERT_TRUE(pid.has_value()); |
| 533 | mResponsiveWindows.push({connectionToken, *pid}); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 534 | mNotifyAnr.notify_all(); |
| 535 | } |
| 536 | |
| 537 | void notifyNoFocusedWindowAnr( |
| 538 | const std::shared_ptr<InputApplicationHandle>& applicationHandle) override { |
| 539 | std::scoped_lock lock(mLock); |
| 540 | mAnrApplications.push(applicationHandle); |
| 541 | mNotifyAnr.notify_all(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 542 | } |
| 543 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 544 | void notifyInputChannelBroken(const sp<IBinder>& connectionToken) override { |
| 545 | std::scoped_lock lock(mLock); |
| 546 | mBrokenInputChannels.push(connectionToken); |
| 547 | mNotifyInputChannelBroken.notify_all(); |
| 548 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 549 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 550 | void notifyFocusChanged(const sp<IBinder>&, const sp<IBinder>&) override {} |
Robert Carr | 740167f | 2018-10-11 19:03:41 -0700 | [diff] [blame] | 551 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 552 | void notifySensorEvent(int32_t deviceId, InputDeviceSensorType sensorType, |
| 553 | InputDeviceSensorAccuracy accuracy, nsecs_t timestamp, |
| 554 | const std::vector<float>& values) override {} |
| 555 | |
| 556 | void notifySensorAccuracy(int deviceId, InputDeviceSensorType sensorType, |
| 557 | InputDeviceSensorAccuracy accuracy) override {} |
Bernardo Rufino | 2e1f651 | 2020-10-08 13:42:07 +0000 | [diff] [blame] | 558 | |
Chris Ye | fb55290 | 2021-02-03 17:18:37 -0800 | [diff] [blame] | 559 | void notifyVibratorState(int32_t deviceId, bool isOn) override {} |
| 560 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 561 | bool filterInputEvent(const InputEvent& inputEvent, uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 562 | std::scoped_lock lock(mLock); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 563 | switch (inputEvent.getType()) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 564 | case InputEventType::KEY: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 565 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(inputEvent); |
| 566 | mFilteredEvent = std::make_unique<KeyEvent>(keyEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 567 | break; |
| 568 | } |
| 569 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 570 | case InputEventType::MOTION: { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 571 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(inputEvent); |
| 572 | mFilteredEvent = std::make_unique<MotionEvent>(motionEvent); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 573 | break; |
| 574 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 575 | default: { |
| 576 | ADD_FAILURE() << "Should only filter keys or motions"; |
| 577 | break; |
| 578 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 579 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 580 | return true; |
| 581 | } |
| 582 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 583 | void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override { |
| 584 | if (inputEvent.getAction() == AKEY_EVENT_ACTION_UP) { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 585 | // Clear intercept state when we handled the event. |
| 586 | mInterceptKeyTimeout = 0ms; |
| 587 | } |
| 588 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 589 | |
Yeabkal Wubshit | 88a9041 | 2023-12-21 18:23:04 -0800 | [diff] [blame] | 590 | 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] | 591 | |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 592 | nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override { |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 593 | nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count(); |
| 594 | // Clear intercept state so we could dispatch the event in next wake. |
| 595 | mInterceptKeyTimeout = 0ms; |
| 596 | return delay; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 597 | } |
| 598 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 599 | std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent& event, |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 600 | uint32_t) override { |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 601 | std::scoped_lock lock(mLock); |
| 602 | mReportedUnhandledKeycodes.emplace(event.getKeyCode()); |
| 603 | mNotifyUnhandledKey.notify_all(); |
| 604 | return mUnhandledKeyHandler != nullptr ? mUnhandledKeyHandler(event) : std::nullopt; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 605 | } |
| 606 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 607 | void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
| 608 | uint32_t policyFlags) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 609 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 610 | /** We simply reconstruct NotifySwitchArgs in policy because InputDispatcher is |
| 611 | * essentially a passthrough for notifySwitch. |
| 612 | */ |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 613 | mLastNotifySwitch = |
| 614 | NotifySwitchArgs(InputEvent::nextId(), when, policyFlags, switchValues, switchMask); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 615 | } |
| 616 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 617 | 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] | 618 | std::scoped_lock lock(mLock); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 619 | mNotifyUserActivity.notify_all(); |
| 620 | mUserActivityPokeEvents.push({eventTime, eventType, displayId}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 621 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 622 | |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 623 | bool isStaleEvent(nsecs_t currentTime, nsecs_t eventTime) override { |
| 624 | return std::chrono::nanoseconds(currentTime - eventTime) >= mStaleEventTimeout; |
| 625 | } |
| 626 | |
Siarhei Vishniakou | 2b4782c | 2020-11-07 01:51:18 -0600 | [diff] [blame] | 627 | void onPointerDownOutsideFocus(const sp<IBinder>& newToken) override { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 628 | std::scoped_lock lock(mLock); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 629 | mOnPointerDownToken = newToken; |
| 630 | } |
| 631 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 632 | void setPointerCapture(const PointerCaptureRequest& request) override { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 633 | std::scoped_lock lock(mLock); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 634 | mPointerCaptureRequest = {request}; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 635 | mPointerCaptureChangedCondition.notify_all(); |
| 636 | } |
| 637 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 638 | void notifyDropWindow(const sp<IBinder>& token, float x, float y) override { |
| 639 | std::scoped_lock lock(mLock); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 640 | mNotifyDropWindowWasCalled = true; |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 641 | mDropTargetWindowToken = token; |
| 642 | } |
| 643 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 644 | void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 645 | const std::set<gui::Uid>& uids) override { |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 646 | ASSERT_TRUE(mNotifiedInteractions.emplace(deviceId, uids)); |
| 647 | } |
| 648 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 649 | void assertFilterInputEventWasCalledInternal( |
| 650 | const std::function<void(const InputEvent&)>& verify) { |
Siarhei Vishniakou | cd899e8 | 2020-05-08 09:24:29 -0700 | [diff] [blame] | 651 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | d99e1b6 | 2019-11-26 11:01:06 -0800 | [diff] [blame] | 652 | ASSERT_NE(nullptr, mFilteredEvent) << "Expected filterInputEvent() to have been called."; |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 653 | verify(*mFilteredEvent); |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 654 | mFilteredEvent = nullptr; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 655 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 656 | }; |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 657 | } // namespace |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 658 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 659 | // --- InputDispatcherTest --- |
| 660 | |
| 661 | class InputDispatcherTest : public testing::Test { |
| 662 | protected: |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 663 | std::unique_ptr<FakeInputDispatcherPolicy> mFakePolicy; |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 664 | std::unique_ptr<InputDispatcher> mDispatcher; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 665 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 666 | void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 667 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 668 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy); |
| 669 | |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 670 | mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 671 | // Start InputDispatcher thread |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 672 | ASSERT_EQ(OK, mDispatcher->start()); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 673 | } |
| 674 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 675 | void TearDown() override { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 676 | ASSERT_EQ(OK, mDispatcher->stop()); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 677 | mFakePolicy.reset(); |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 678 | mDispatcher.reset(); |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 679 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 680 | |
| 681 | /** |
| 682 | * Used for debugging when writing the test |
| 683 | */ |
| 684 | void dumpDispatcherState() { |
| 685 | std::string dump; |
| 686 | mDispatcher->dump(dump); |
| 687 | std::stringstream ss(dump); |
| 688 | std::string to; |
| 689 | |
| 690 | while (std::getline(ss, to, '\n')) { |
| 691 | ALOGE("%s", to.c_str()); |
| 692 | } |
| 693 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 694 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 695 | void setFocusedWindow(const sp<WindowInfoHandle>& window) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 696 | FocusRequest request; |
| 697 | request.token = window->getToken(); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 698 | request.windowName = window->getName(); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 699 | request.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 700 | request.displayId = window->getInfo()->displayId; |
| 701 | mDispatcher->setFocusedWindow(request); |
| 702 | } |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 703 | }; |
| 704 | |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 705 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesKeyEvents) { |
| 706 | KeyEvent event; |
| 707 | |
| 708 | // Rejects undefined key actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 709 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 710 | INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 711 | /*action=*/-1, 0, AKEYCODE_A, KEY_A, AMETA_NONE, 0, ARBITRARY_TIME, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 712 | ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 713 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 714 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 715 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 716 | << "Should reject key events with undefined action."; |
| 717 | |
| 718 | // 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] | 719 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
| 720 | 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] | 721 | ARBITRARY_TIME, ARBITRARY_TIME); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 722 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 723 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 724 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 725 | << "Should reject key events with ACTION_MULTIPLE."; |
| 726 | } |
| 727 | |
| 728 | TEST_F(InputDispatcherTest, InjectInputEvent_ValidatesMotionEvents) { |
| 729 | MotionEvent event; |
| 730 | PointerProperties pointerProperties[MAX_POINTERS + 1]; |
| 731 | PointerCoords pointerCoords[MAX_POINTERS + 1]; |
Siarhei Vishniakou | 0174738 | 2022-01-20 13:23:27 -0800 | [diff] [blame] | 732 | for (size_t i = 0; i <= MAX_POINTERS; i++) { |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 733 | pointerProperties[i].clear(); |
| 734 | pointerProperties[i].id = i; |
| 735 | pointerCoords[i].clear(); |
| 736 | } |
| 737 | |
Siarhei Vishniakou | 49e5922 | 2018-12-28 18:17:15 -0800 | [diff] [blame] | 738 | // Some constants commonly used below |
| 739 | constexpr int32_t source = AINPUT_SOURCE_TOUCHSCREEN; |
| 740 | constexpr int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE; |
| 741 | constexpr int32_t metaState = AMETA_NONE; |
| 742 | constexpr MotionClassification classification = MotionClassification::NONE; |
| 743 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 744 | ui::Transform identityTransform; |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 745 | // Rejects undefined motion actions. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 746 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 747 | /*action=*/-1, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 748 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 749 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 750 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 751 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 752 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 753 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 754 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 755 | << "Should reject motion events with undefined action."; |
| 756 | |
| 757 | // Rejects pointer down with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 758 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 759 | POINTER_1_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
| 760 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 761 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 762 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 763 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 764 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 765 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 766 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 767 | << "Should reject motion events with pointer down index too large."; |
| 768 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 769 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 770 | AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 771 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 772 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 773 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 774 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 775 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 776 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 777 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 778 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 779 | << "Should reject motion events with pointer down index too small."; |
| 780 | |
| 781 | // Rejects pointer up with invalid index. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 782 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 783 | POINTER_1_UP, 0, 0, edgeFlags, metaState, 0, classification, identityTransform, |
| 784 | 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 785 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 786 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 787 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 788 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 789 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 790 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 791 | << "Should reject motion events with pointer up index too large."; |
| 792 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 793 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 794 | AMOTION_EVENT_ACTION_POINTER_UP | |
| 795 | (~0U << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT), |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 796 | 0, 0, edgeFlags, metaState, 0, classification, identityTransform, 0, 0, |
| 797 | AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 798 | identityTransform, ARBITRARY_TIME, ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 799 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 800 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 801 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 802 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 803 | << "Should reject motion events with pointer up index too small."; |
| 804 | |
| 805 | // Rejects motion events with invalid number of pointers. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 806 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 807 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 808 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 809 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 810 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 811 | /*pointerCount=*/0, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 812 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 813 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 814 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 815 | << "Should reject motion events with 0 pointers."; |
| 816 | |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 817 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 818 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 819 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 820 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 821 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 822 | /*pointerCount=*/MAX_POINTERS + 1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 823 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 824 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 825 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 826 | << "Should reject motion events with more than MAX_POINTERS pointers."; |
| 827 | |
| 828 | // Rejects motion events with invalid pointer ids. |
| 829 | pointerProperties[0].id = -1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 830 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 831 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 832 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 833 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 834 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 835 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 836 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 837 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 838 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 839 | << "Should reject motion events with pointer ids less than 0."; |
| 840 | |
| 841 | pointerProperties[0].id = MAX_POINTER_ID + 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 842 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 843 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 844 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 845 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 846 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 847 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 848 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 849 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 850 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 851 | << "Should reject motion events with pointer ids greater than MAX_POINTER_ID."; |
| 852 | |
| 853 | // Rejects motion events with duplicate pointer ids. |
| 854 | pointerProperties[0].id = 1; |
| 855 | pointerProperties[1].id = 1; |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 856 | event.initialize(InputEvent::nextId(), DEVICE_ID, source, DISPLAY_ID, INVALID_HMAC, |
| 857 | AMOTION_EVENT_ACTION_DOWN, 0, 0, edgeFlags, metaState, 0, classification, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 858 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 859 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, ARBITRARY_TIME, |
| 860 | ARBITRARY_TIME, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 861 | /*pointerCount=*/2, pointerProperties, pointerCoords); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 862 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 863 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, InputEventInjectionSync::NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 864 | 0ms, 0)) |
Michael Wright | d02c5b6 | 2014-02-10 15:10:22 -0800 | [diff] [blame] | 865 | << "Should reject motion events with duplicate pointer ids."; |
| 866 | } |
| 867 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 868 | /* Test InputDispatcher for notifyConfigurationChanged and notifySwitch events */ |
| 869 | |
| 870 | TEST_F(InputDispatcherTest, NotifyConfigurationChanged_CallsPolicy) { |
| 871 | constexpr nsecs_t eventTime = 20; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 872 | mDispatcher->notifyConfigurationChanged({/*id=*/10, eventTime}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 873 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 874 | |
| 875 | mFakePolicy->assertNotifyConfigurationChangedWasCalled(eventTime); |
| 876 | } |
| 877 | |
| 878 | TEST_F(InputDispatcherTest, NotifySwitch_CallsPolicy) { |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 879 | NotifySwitchArgs args(InputEvent::nextId(), /*eventTime=*/20, /*policyFlags=*/0, |
| 880 | /*switchValues=*/1, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 881 | /*switchMask=*/2); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 882 | mDispatcher->notifySwitch(args); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 883 | |
| 884 | // InputDispatcher adds POLICY_FLAG_TRUSTED because the event went through InputListener |
| 885 | args.policyFlags |= POLICY_FLAG_TRUSTED; |
| 886 | mFakePolicy->assertNotifySwitchWasCalled(args); |
| 887 | } |
| 888 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 889 | namespace { |
| 890 | |
Siarhei Vishniakou | 097c3db | 2020-05-06 14:18:38 -0700 | [diff] [blame] | 891 | static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 500ms; |
Siarhei Vishniakou | 1c494c5 | 2021-08-11 20:25:01 -0700 | [diff] [blame] | 892 | // Default input dispatching timeout if there is no focused application or paused window |
| 893 | // from which to determine an appropriate dispatching timeout. |
| 894 | static const std::chrono::duration DISPATCHING_TIMEOUT = std::chrono::milliseconds( |
| 895 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 896 | android::base::HwTimeoutMultiplier()); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 897 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 898 | class FakeInputReceiver { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 899 | public: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 900 | explicit FakeInputReceiver(std::unique_ptr<InputChannel> clientChannel, const std::string name) |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 901 | : mConsumer(std::move(clientChannel)), mName(name) {} |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 902 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 903 | std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = false) { |
| 904 | auto [consumeSeq, event] = receiveEvent(timeout); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 905 | if (!consumeSeq) { |
| 906 | return nullptr; |
| 907 | } |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 908 | finishEvent(*consumeSeq, handled); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 909 | return std::move(event); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | /** |
| 913 | * Receive an event without acknowledging it. |
| 914 | * Return the sequence number that could later be used to send finished signal. |
| 915 | */ |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 916 | std::pair<std::optional<uint32_t>, std::unique_ptr<InputEvent>> receiveEvent( |
| 917 | std::chrono::milliseconds timeout) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 918 | uint32_t consumeSeq; |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 919 | std::unique_ptr<InputEvent> event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 920 | |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 921 | std::chrono::time_point start = std::chrono::steady_clock::now(); |
| 922 | status_t status = WOULD_BLOCK; |
| 923 | while (status == WOULD_BLOCK) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 924 | InputEvent* rawEventPtr = nullptr; |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 925 | status = mConsumer.consume(&mEventFactory, /*consumeBatches=*/true, -1, &consumeSeq, |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 926 | &rawEventPtr); |
| 927 | event = std::unique_ptr<InputEvent>(rawEventPtr); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 928 | std::chrono::duration elapsed = std::chrono::steady_clock::now() - start; |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 929 | if (elapsed > timeout) { |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 930 | break; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | if (status == WOULD_BLOCK) { |
| 935 | // Just means there's no event available. |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 936 | return std::make_pair(std::nullopt, nullptr); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | if (status != OK) { |
| 940 | ADD_FAILURE() << mName.c_str() << ": consumer consume should return OK."; |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 941 | return std::make_pair(std::nullopt, nullptr); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 942 | } |
| 943 | if (event == nullptr) { |
| 944 | ADD_FAILURE() << "Consumed correctly, but received NULL event from consumer"; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 945 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 946 | return std::make_pair(consumeSeq, std::move(event)); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 947 | } |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 948 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 949 | /** |
| 950 | * To be used together with "receiveEvent" to complete the consumption of an event. |
| 951 | */ |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 952 | void finishEvent(uint32_t consumeSeq, bool handled = true) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 953 | const status_t status = mConsumer.sendFinishedSignal(consumeSeq, handled); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 954 | ASSERT_EQ(OK, status) << mName.c_str() << ": consumer sendFinishedSignal should return OK."; |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 955 | } |
| 956 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 957 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 958 | const status_t status = mConsumer.sendTimeline(inputEventId, timeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 959 | ASSERT_EQ(OK, status); |
| 960 | } |
| 961 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 962 | void consumeEvent(InputEventType expectedEventType, int32_t expectedAction, |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 963 | std::optional<int32_t> expectedDisplayId, |
| 964 | std::optional<int32_t> expectedFlags) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 965 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 08b574f | 2019-11-15 18:05:52 -0800 | [diff] [blame] | 966 | |
| 967 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 968 | << ": consumer should have returned non-NULL event."; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 969 | ASSERT_EQ(expectedEventType, event->getType()) |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 970 | << mName.c_str() << " expected " << ftl::enum_string(expectedEventType) |
| 971 | << " event, got " << *event; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 972 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 973 | if (expectedDisplayId.has_value()) { |
| 974 | EXPECT_EQ(expectedDisplayId, event->getDisplayId()); |
| 975 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 976 | |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 977 | switch (expectedEventType) { |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 978 | case InputEventType::KEY: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 979 | const KeyEvent& keyEvent = static_cast<const KeyEvent&>(*event); |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 980 | ASSERT_THAT(keyEvent, WithKeyAction(expectedAction)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 981 | if (expectedFlags.has_value()) { |
| 982 | EXPECT_EQ(expectedFlags.value(), keyEvent.getFlags()); |
| 983 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 984 | break; |
| 985 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 986 | case InputEventType::MOTION: { |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 987 | const MotionEvent& motionEvent = static_cast<const MotionEvent&>(*event); |
Siarhei Vishniakou | f404321 | 2023-09-18 19:33:03 -0700 | [diff] [blame] | 988 | ASSERT_THAT(motionEvent, WithMotionAction(expectedAction)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 989 | if (expectedFlags.has_value()) { |
| 990 | EXPECT_EQ(expectedFlags.value(), motionEvent.getFlags()); |
| 991 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 992 | break; |
| 993 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 994 | case InputEventType::FOCUS: { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 995 | FAIL() << "Use 'consumeFocusEvent' for FOCUS events"; |
| 996 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 997 | case InputEventType::CAPTURE: { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 998 | FAIL() << "Use 'consumeCaptureEvent' for CAPTURE events"; |
| 999 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1000 | case InputEventType::TOUCH_MODE: { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1001 | FAIL() << "Use 'consumeTouchModeEvent' for TOUCH_MODE events"; |
| 1002 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1003 | case InputEventType::DRAG: { |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1004 | FAIL() << "Use 'consumeDragEvent' for DRAG events"; |
| 1005 | } |
Tiger Huang | 8664f8c | 2018-10-11 19:14:35 +0800 | [diff] [blame] | 1006 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1007 | } |
| 1008 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1009 | std::unique_ptr<MotionEvent> consumeMotion() { |
| 1010 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1011 | |
| 1012 | if (event == nullptr) { |
| 1013 | ADD_FAILURE() << mName << ": expected a MotionEvent, but didn't get one."; |
| 1014 | return nullptr; |
| 1015 | } |
| 1016 | |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1017 | if (event->getType() != InputEventType::MOTION) { |
| 1018 | ADD_FAILURE() << mName << " expected a MotionEvent, got " << *event; |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1019 | return nullptr; |
| 1020 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1021 | return std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event.release())); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1025 | std::unique_ptr<MotionEvent> motionEvent = consumeMotion(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1026 | ASSERT_NE(nullptr, motionEvent) << "Did not get a motion event, but expected " << matcher; |
| 1027 | ASSERT_THAT(*motionEvent, matcher); |
| 1028 | } |
| 1029 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1030 | void consumeFocusEvent(bool hasFocus, bool inTouchMode) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1031 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1032 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1033 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1034 | ASSERT_EQ(InputEventType::FOCUS, event->getType()) |
| 1035 | << "Instead of FocusEvent, got " << *event; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1036 | |
| 1037 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1038 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1039 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1040 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1041 | EXPECT_EQ(hasFocus, focusEvent.getHasFocus()); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1042 | } |
| 1043 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1044 | void consumeCaptureEvent(bool hasCapture) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1045 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1046 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1047 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1048 | ASSERT_EQ(InputEventType::CAPTURE, event->getType()) |
| 1049 | << "Instead of CaptureEvent, got " << *event; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1050 | |
| 1051 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1052 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1053 | |
| 1054 | const auto& captureEvent = static_cast<const CaptureEvent&>(*event); |
| 1055 | EXPECT_EQ(hasCapture, captureEvent.getPointerCaptureEnabled()); |
| 1056 | } |
| 1057 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1058 | void consumeDragEvent(bool isExiting, float x, float y) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1059 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1060 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1061 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1062 | ASSERT_EQ(InputEventType::DRAG, event->getType()) << "Instead of DragEvent, got " << *event; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1063 | |
| 1064 | EXPECT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1065 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1066 | |
| 1067 | const auto& dragEvent = static_cast<const DragEvent&>(*event); |
| 1068 | EXPECT_EQ(isExiting, dragEvent.isExiting()); |
| 1069 | EXPECT_EQ(x, dragEvent.getX()); |
| 1070 | EXPECT_EQ(y, dragEvent.getY()); |
| 1071 | } |
| 1072 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1073 | void consumeTouchModeEvent(bool inTouchMode) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1074 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1075 | ASSERT_NE(nullptr, event) << mName.c_str() |
| 1076 | << ": consumer should have returned non-NULL event."; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1077 | ASSERT_EQ(InputEventType::TOUCH_MODE, event->getType()) |
| 1078 | << "Instead of TouchModeEvent, got " << *event; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1079 | |
| 1080 | ASSERT_EQ(ADISPLAY_ID_NONE, event->getDisplayId()) |
| 1081 | << mName.c_str() << ": event displayId should always be NONE."; |
| 1082 | const auto& touchModeEvent = static_cast<const TouchModeEvent&>(*event); |
| 1083 | EXPECT_EQ(inTouchMode, touchModeEvent.isInTouchMode()); |
| 1084 | } |
| 1085 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1086 | void assertNoEvents() { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1087 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_NO_EVENT_EXPECTED); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1088 | if (event == nullptr) { |
| 1089 | return; |
| 1090 | } |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1091 | if (event->getType() == InputEventType::KEY) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1092 | KeyEvent& keyEvent = static_cast<KeyEvent&>(*event); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1093 | ADD_FAILURE() << "Received key event " << keyEvent; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1094 | } else if (event->getType() == InputEventType::MOTION) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1095 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1096 | ADD_FAILURE() << "Received motion event " << motionEvent; |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1097 | } else if (event->getType() == InputEventType::FOCUS) { |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1098 | FocusEvent& focusEvent = static_cast<FocusEvent&>(*event); |
| 1099 | ADD_FAILURE() << "Received focus event, hasFocus = " |
| 1100 | << (focusEvent.getHasFocus() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1101 | } else if (event->getType() == InputEventType::CAPTURE) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1102 | const auto& captureEvent = static_cast<CaptureEvent&>(*event); |
| 1103 | ADD_FAILURE() << "Received capture event, pointerCaptureEnabled = " |
| 1104 | << (captureEvent.getPointerCaptureEnabled() ? "true" : "false"); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 1105 | } else if (event->getType() == InputEventType::TOUCH_MODE) { |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1106 | const auto& touchModeEvent = static_cast<TouchModeEvent&>(*event); |
| 1107 | ADD_FAILURE() << "Received touch mode event, inTouchMode = " |
| 1108 | << (touchModeEvent.isInTouchMode() ? "true" : "false"); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1109 | } |
| 1110 | FAIL() << mName.c_str() |
| 1111 | << ": should not have received any events, so consume() should return NULL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1112 | } |
| 1113 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1114 | sp<IBinder> getToken() { return mConsumer.getChannel()->getConnectionToken(); } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1115 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1116 | int getChannelFd() { return mConsumer.getChannel()->getFd().get(); } |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1117 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1118 | private: |
| 1119 | InputConsumer mConsumer; |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1120 | DynamicInputEventFactory mEventFactory; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1121 | |
| 1122 | std::string mName; |
| 1123 | }; |
| 1124 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1125 | class FakeWindowHandle : public WindowInfoHandle { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1126 | public: |
| 1127 | static const int32_t WIDTH = 600; |
| 1128 | static const int32_t HEIGHT = 800; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1129 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1130 | FakeWindowHandle(const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 1131 | const std::unique_ptr<InputDispatcher>& dispatcher, const std::string name, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1132 | int32_t displayId, bool createInputChannel = true) |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1133 | : mName(name) { |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1134 | sp<IBinder> token; |
| 1135 | if (createInputChannel) { |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 1136 | base::Result<std::unique_ptr<InputChannel>> channel = |
| 1137 | dispatcher->createInputChannel(name); |
| 1138 | token = (*channel)->getConnectionToken(); |
| 1139 | mInputReceiver = std::make_unique<FakeInputReceiver>(std::move(*channel), name); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | inputApplicationHandle->updateInfo(); |
| 1143 | mInfo.applicationInfo = *inputApplicationHandle->getInfo(); |
| 1144 | |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 1145 | mInfo.token = token; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1146 | mInfo.id = sId++; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1147 | mInfo.name = name; |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1148 | mInfo.dispatchingTimeout = DISPATCHING_TIMEOUT; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1149 | mInfo.alpha = 1.0; |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 1150 | mInfo.frame = Rect(0, 0, WIDTH, HEIGHT); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 1151 | mInfo.transform.set(0, 0); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1152 | mInfo.globalScaleFactor = 1.0; |
| 1153 | mInfo.touchableRegion.clear(); |
| 1154 | mInfo.addTouchableRegion(Rect(0, 0, WIDTH, HEIGHT)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1155 | mInfo.ownerPid = WINDOW_PID; |
| 1156 | mInfo.ownerUid = WINDOW_UID; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1157 | mInfo.displayId = displayId; |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1158 | mInfo.inputConfig = WindowInfo::InputConfig::DEFAULT; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1159 | } |
| 1160 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1161 | sp<FakeWindowHandle> clone(int32_t displayId) { |
| 1162 | sp<FakeWindowHandle> handle = sp<FakeWindowHandle>::make(mInfo.name + "(Mirror)"); |
| 1163 | handle->mInfo = mInfo; |
| 1164 | handle->mInfo.displayId = displayId; |
| 1165 | handle->mInfo.id = sId++; |
| 1166 | handle->mInputReceiver = mInputReceiver; |
| 1167 | return handle; |
| 1168 | } |
| 1169 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1170 | void setTouchable(bool touchable) { |
| 1171 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, !touchable); |
| 1172 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1173 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1174 | void setFocusable(bool focusable) { |
| 1175 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_FOCUSABLE, !focusable); |
| 1176 | } |
| 1177 | |
| 1178 | void setVisible(bool visible) { |
| 1179 | mInfo.setInputConfig(WindowInfo::InputConfig::NOT_VISIBLE, !visible); |
| 1180 | } |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 1181 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1182 | void setDispatchingTimeout(std::chrono::nanoseconds timeout) { |
Siarhei Vishniakou | c1ae556 | 2020-06-30 14:22:57 -0500 | [diff] [blame] | 1183 | mInfo.dispatchingTimeout = timeout; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1186 | void setPaused(bool paused) { |
| 1187 | mInfo.setInputConfig(WindowInfo::InputConfig::PAUSE_DISPATCHING, paused); |
| 1188 | } |
| 1189 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1190 | void setPreventSplitting(bool preventSplitting) { |
| 1191 | mInfo.setInputConfig(WindowInfo::InputConfig::PREVENT_SPLITTING, preventSplitting); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | void setSlippery(bool slippery) { |
| 1195 | mInfo.setInputConfig(WindowInfo::InputConfig::SLIPPERY, slippery); |
| 1196 | } |
| 1197 | |
| 1198 | void setWatchOutsideTouch(bool watchOutside) { |
| 1199 | mInfo.setInputConfig(WindowInfo::InputConfig::WATCH_OUTSIDE_TOUCH, watchOutside); |
| 1200 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1201 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1202 | void setSpy(bool spy) { mInfo.setInputConfig(WindowInfo::InputConfig::SPY, spy); } |
| 1203 | |
| 1204 | void setInterceptsStylus(bool interceptsStylus) { |
| 1205 | mInfo.setInputConfig(WindowInfo::InputConfig::INTERCEPTS_STYLUS, interceptsStylus); |
| 1206 | } |
| 1207 | |
| 1208 | void setDropInput(bool dropInput) { |
| 1209 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT, dropInput); |
| 1210 | } |
| 1211 | |
| 1212 | void setDropInputIfObscured(bool dropInputIfObscured) { |
| 1213 | mInfo.setInputConfig(WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED, dropInputIfObscured); |
| 1214 | } |
| 1215 | |
| 1216 | void setNoInputChannel(bool noInputChannel) { |
| 1217 | mInfo.setInputConfig(WindowInfo::InputConfig::NO_INPUT_CHANNEL, noInputChannel); |
| 1218 | } |
| 1219 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1220 | void setDisableUserActivity(bool disableUserActivity) { |
| 1221 | mInfo.setInputConfig(WindowInfo::InputConfig::DISABLE_USER_ACTIVITY, disableUserActivity); |
| 1222 | } |
| 1223 | |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 1224 | void setGlobalStylusBlocksTouch(bool shouldGlobalStylusBlockTouch) { |
| 1225 | mInfo.setInputConfig(WindowInfo::InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH, |
| 1226 | shouldGlobalStylusBlockTouch); |
| 1227 | } |
| 1228 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1229 | void setAlpha(float alpha) { mInfo.alpha = alpha; } |
| 1230 | |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 1231 | void setTouchOcclusionMode(TouchOcclusionMode mode) { mInfo.touchOcclusionMode = mode; } |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1232 | |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 1233 | void setApplicationToken(sp<IBinder> token) { mInfo.applicationInfo.token = token; } |
| 1234 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1235 | void setFrame(const Rect& frame, const ui::Transform& displayTransform = ui::Transform()) { |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 1236 | mInfo.frame = frame; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1237 | mInfo.touchableRegion.clear(); |
| 1238 | mInfo.addTouchableRegion(frame); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1239 | |
| 1240 | const Rect logicalDisplayFrame = displayTransform.transform(frame); |
| 1241 | ui::Transform translate; |
| 1242 | translate.set(-logicalDisplayFrame.left, -logicalDisplayFrame.top); |
| 1243 | mInfo.transform = translate * displayTransform; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1244 | } |
| 1245 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1246 | void setTouchableRegion(const Region& region) { mInfo.touchableRegion = region; } |
| 1247 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1248 | void setIsWallpaper(bool isWallpaper) { |
| 1249 | mInfo.setInputConfig(WindowInfo::InputConfig::IS_WALLPAPER, isWallpaper); |
| 1250 | } |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1251 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1252 | void setDupTouchToWallpaper(bool hasWallpaper) { |
| 1253 | mInfo.setInputConfig(WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER, hasWallpaper); |
| 1254 | } |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1255 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1256 | void setTrustedOverlay(bool trustedOverlay) { |
| 1257 | mInfo.setInputConfig(WindowInfo::InputConfig::TRUSTED_OVERLAY, trustedOverlay); |
| 1258 | } |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1259 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 1260 | void setWindowTransform(float dsdx, float dtdx, float dtdy, float dsdy) { |
| 1261 | mInfo.transform.set(dsdx, dtdx, dtdy, dsdy); |
| 1262 | } |
| 1263 | |
| 1264 | void setWindowScale(float xScale, float yScale) { setWindowTransform(xScale, 0, 0, yScale); } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1265 | |
yunho.shin | f4a80b8 | 2020-11-16 21:13:57 +0900 | [diff] [blame] | 1266 | void setWindowOffset(float offsetX, float offsetY) { mInfo.transform.set(offsetX, offsetY); } |
| 1267 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1268 | std::unique_ptr<KeyEvent> consumeKey(bool handled = true) { |
| 1269 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED, handled); |
| 1270 | if (event == nullptr) { |
| 1271 | ADD_FAILURE() << "No event"; |
| 1272 | return nullptr; |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1273 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1274 | if (event->getType() != InputEventType::KEY) { |
| 1275 | ADD_FAILURE() << "Instead of key event, got " << event; |
| 1276 | return nullptr; |
| 1277 | } |
| 1278 | return std::unique_ptr<KeyEvent>(static_cast<KeyEvent*>(event.release())); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | void consumeKeyEvent(const ::testing::Matcher<KeyEvent>& matcher) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1282 | std::unique_ptr<KeyEvent> keyEvent = consumeKey(); |
| 1283 | ASSERT_NE(nullptr, keyEvent); |
| 1284 | ASSERT_THAT(*keyEvent, matcher); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1285 | } |
| 1286 | |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1287 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1288 | consumeKeyEvent(AllOf(WithKeyAction(ACTION_DOWN), WithDisplayId(expectedDisplayId), |
| 1289 | WithFlags(expectedFlags))); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1290 | } |
| 1291 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1292 | void consumeKeyUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1293 | consumeKeyEvent(AllOf(WithKeyAction(ACTION_UP), WithDisplayId(expectedDisplayId), |
| 1294 | WithFlags(expectedFlags))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1297 | void consumeMotionCancel(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1298 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1299 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(expectedDisplayId), |
| 1300 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | void consumeMotionMove(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1304 | int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 1305 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), |
| 1306 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | void consumeMotionDown(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1310 | int32_t expectedFlags = 0) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 1311 | consumeAnyMotionDown(expectedDisplayId, expectedFlags); |
| 1312 | } |
| 1313 | |
| 1314 | void consumeAnyMotionDown(std::optional<int32_t> expectedDisplayId = std::nullopt, |
| 1315 | std::optional<int32_t> expectedFlags = std::nullopt) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1316 | consumeMotionEvent( |
| 1317 | AllOf(WithMotionAction(ACTION_DOWN), |
| 1318 | testing::Conditional(expectedDisplayId.has_value(), |
| 1319 | WithDisplayId(*expectedDisplayId), testing::_), |
| 1320 | testing::Conditional(expectedFlags.has_value(), WithFlags(*expectedFlags), |
| 1321 | testing::_))); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1322 | } |
| 1323 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1324 | void consumeMotionPointerDown(int32_t pointerIdx, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1325 | int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1326 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1327 | const int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1328 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1329 | consumeMotionEvent(AllOf(WithMotionAction(action), WithDisplayId(expectedDisplayId), |
| 1330 | WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | void consumeMotionPointerUp(int32_t pointerIdx, int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1334 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1335 | const int32_t action = AMOTION_EVENT_ACTION_POINTER_UP | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1336 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1337 | consumeMotionEvent(AllOf(WithMotionAction(action), WithDisplayId(expectedDisplayId), |
| 1338 | WithFlags(expectedFlags))); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | void consumeMotionUp(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1342 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1343 | consumeMotionEvent(AllOf(WithMotionAction(ACTION_UP), WithDisplayId(expectedDisplayId), |
| 1344 | WithFlags(expectedFlags))); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1347 | void consumeMotionOutside(int32_t expectedDisplayId = ADISPLAY_ID_DEFAULT, |
| 1348 | int32_t expectedFlags = 0) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1349 | consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE), |
| 1350 | WithDisplayId(expectedDisplayId), WithFlags(expectedFlags))); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 1351 | } |
| 1352 | |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1353 | void consumeMotionOutsideWithZeroedCoords() { |
| 1354 | consumeMotionEvent( |
| 1355 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_OUTSIDE), WithRawCoords(0, 0))); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 1356 | } |
| 1357 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1358 | void consumeFocusEvent(bool hasFocus, bool inTouchMode = true) { |
| 1359 | ASSERT_NE(mInputReceiver, nullptr) |
| 1360 | << "Cannot consume events from a window with no receiver"; |
| 1361 | mInputReceiver->consumeFocusEvent(hasFocus, inTouchMode); |
| 1362 | } |
| 1363 | |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1364 | void consumeCaptureEvent(bool hasCapture) { |
| 1365 | ASSERT_NE(mInputReceiver, nullptr) |
| 1366 | << "Cannot consume events from a window with no receiver"; |
| 1367 | mInputReceiver->consumeCaptureEvent(hasCapture); |
| 1368 | } |
| 1369 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1370 | std::unique_ptr<MotionEvent> consumeMotionEvent( |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1371 | const ::testing::Matcher<MotionEvent>& matcher = testing::_) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1372 | std::unique_ptr<InputEvent> event = consume(CONSUME_TIMEOUT_EVENT_EXPECTED); |
| 1373 | if (event == nullptr) { |
| 1374 | ADD_FAILURE() << "No event"; |
| 1375 | return nullptr; |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 1376 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1377 | if (event->getType() != InputEventType::MOTION) { |
| 1378 | ADD_FAILURE() << "Instead of motion event, got " << *event; |
| 1379 | return nullptr; |
| 1380 | } |
| 1381 | std::unique_ptr<MotionEvent> motionEvent = |
| 1382 | std::unique_ptr<MotionEvent>(static_cast<MotionEvent*>(event.release())); |
| 1383 | EXPECT_THAT(*motionEvent, matcher); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1384 | return motionEvent; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1385 | } |
| 1386 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 1387 | void consumeDragEvent(bool isExiting, float x, float y) { |
| 1388 | mInputReceiver->consumeDragEvent(isExiting, x, y); |
| 1389 | } |
| 1390 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 1391 | void consumeTouchModeEvent(bool inTouchMode) { |
| 1392 | ASSERT_NE(mInputReceiver, nullptr) |
| 1393 | << "Cannot consume events from a window with no receiver"; |
| 1394 | mInputReceiver->consumeTouchModeEvent(inTouchMode); |
| 1395 | } |
| 1396 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1397 | std::pair<std::optional<uint32_t>, std::unique_ptr<InputEvent>> receiveEvent() { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1398 | if (mInputReceiver == nullptr) { |
| 1399 | ADD_FAILURE() << "Invalid receive event on window with no receiver"; |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1400 | return std::make_pair(std::nullopt, nullptr); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1401 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1402 | return mInputReceiver->receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | void finishEvent(uint32_t sequenceNum) { |
| 1406 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1407 | mInputReceiver->finishEvent(sequenceNum); |
| 1408 | } |
| 1409 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 1410 | void sendTimeline(int32_t inputEventId, std::array<nsecs_t, GraphicsTimeline::SIZE> timeline) { |
| 1411 | ASSERT_NE(mInputReceiver, nullptr) << "Invalid receive event on window with no receiver"; |
| 1412 | mInputReceiver->sendTimeline(inputEventId, timeline); |
| 1413 | } |
| 1414 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1415 | void assertNoEvents() { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1416 | if (mInputReceiver == nullptr && |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 1417 | mInfo.inputConfig.test(WindowInfo::InputConfig::NO_INPUT_CHANNEL)) { |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 1418 | return; // Can't receive events if the window does not have input channel |
| 1419 | } |
| 1420 | ASSERT_NE(nullptr, mInputReceiver) |
| 1421 | << "Window without InputReceiver must specify feature NO_INPUT_CHANNEL"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1422 | mInputReceiver->assertNoEvents(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1423 | } |
| 1424 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1425 | sp<IBinder> getToken() { return mInfo.token; } |
| 1426 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1427 | const std::string& getName() { return mName; } |
| 1428 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 1429 | void setOwnerInfo(gui::Pid ownerPid, gui::Uid ownerUid) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1430 | mInfo.ownerPid = ownerPid; |
| 1431 | mInfo.ownerUid = ownerUid; |
| 1432 | } |
| 1433 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 1434 | gui::Pid getPid() const { return mInfo.ownerPid; } |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 1435 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1436 | void destroyReceiver() { mInputReceiver = nullptr; } |
| 1437 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 1438 | int getChannelFd() { return mInputReceiver->getChannelFd(); } |
| 1439 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1440 | private: |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1441 | FakeWindowHandle(std::string name) : mName(name){}; |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 1442 | const std::string mName; |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1443 | std::shared_ptr<FakeInputReceiver> mInputReceiver; |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1444 | static std::atomic<int32_t> sId; // each window gets a unique id, like in surfaceflinger |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 1445 | friend class sp<FakeWindowHandle>; |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1446 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1447 | std::unique_ptr<InputEvent> consume(std::chrono::milliseconds timeout, bool handled = true) { |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1448 | if (mInputReceiver == nullptr) { |
| 1449 | LOG(FATAL) << "Cannot consume event from a window with no input event receiver"; |
| 1450 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1451 | std::unique_ptr<InputEvent> event = mInputReceiver->consume(timeout, handled); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1452 | if (event == nullptr) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1453 | ADD_FAILURE() << "Consume failed: no event"; |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1454 | } |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1455 | return event; |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 1456 | } |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 1457 | }; |
| 1458 | |
Siarhei Vishniakou | 540dbae | 2020-05-05 18:17:17 -0700 | [diff] [blame] | 1459 | std::atomic<int32_t> FakeWindowHandle::sId{1}; |
| 1460 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1461 | class FakeMonitorReceiver { |
| 1462 | public: |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1463 | FakeMonitorReceiver(InputDispatcher& dispatcher, const std::string name, int32_t displayId) |
| 1464 | : mInputReceiver(*dispatcher.createInputMonitor(displayId, name, MONITOR_PID), name) {} |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1465 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1466 | sp<IBinder> getToken() { return mInputReceiver.getToken(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1467 | |
| 1468 | void consumeKeyDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1469 | mInputReceiver.consumeEvent(InputEventType::KEY, AKEY_EVENT_ACTION_DOWN, expectedDisplayId, |
| 1470 | expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | std::optional<int32_t> receiveEvent() { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1474 | const auto [sequenceNum, _] = mInputReceiver.receiveEvent(CONSUME_TIMEOUT_EVENT_EXPECTED); |
| 1475 | return sequenceNum; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1478 | void finishEvent(uint32_t consumeSeq) { return mInputReceiver.finishEvent(consumeSeq); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1479 | |
| 1480 | void consumeMotionDown(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1481 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_DOWN, |
| 1482 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | void consumeMotionMove(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1486 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_MOVE, |
| 1487 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | void consumeMotionUp(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1491 | mInputReceiver.consumeEvent(InputEventType::MOTION, AMOTION_EVENT_ACTION_UP, |
| 1492 | expectedDisplayId, expectedFlags); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | void consumeMotionCancel(int32_t expectedDisplayId, int32_t expectedFlags = 0) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1496 | mInputReceiver.consumeMotionEvent( |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1497 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 1498 | WithDisplayId(expectedDisplayId), |
| 1499 | WithFlags(expectedFlags | AMOTION_EVENT_FLAG_CANCELED))); |
| 1500 | } |
| 1501 | |
| 1502 | void consumeMotionPointerDown(int32_t pointerIdx) { |
| 1503 | int32_t action = AMOTION_EVENT_ACTION_POINTER_DOWN | |
| 1504 | (pointerIdx << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1505 | mInputReceiver.consumeEvent(InputEventType::MOTION, action, ADISPLAY_ID_DEFAULT, |
| 1506 | /*expectedFlags=*/0); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | void consumeMotionEvent(const ::testing::Matcher<MotionEvent>& matcher) { |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1510 | mInputReceiver.consumeMotionEvent(matcher); |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 1513 | std::unique_ptr<MotionEvent> consumeMotion() { return mInputReceiver.consumeMotion(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1514 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1515 | void assertNoEvents() { mInputReceiver.assertNoEvents(); } |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1516 | |
| 1517 | private: |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1518 | FakeInputReceiver mInputReceiver; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 1519 | }; |
| 1520 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1521 | static InputEventInjectionResult injectKey( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1522 | InputDispatcher& dispatcher, int32_t action, int32_t repeatCount, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1523 | int32_t displayId = ADISPLAY_ID_NONE, |
| 1524 | InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1525 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1526 | bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1527 | uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1528 | KeyEvent event; |
| 1529 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1530 | |
| 1531 | // Define a valid key down event. |
Garfield Tan | fbe732e | 2020-01-24 11:26:14 -0800 | [diff] [blame] | 1532 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, displayId, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1533 | INVALID_HMAC, action, /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, repeatCount, |
| 1534 | currentTime, currentTime); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1535 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1536 | if (!allowKeyRepeat) { |
| 1537 | policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 1538 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1539 | // Inject event until dispatch out. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1540 | return dispatcher.injectInputEvent(&event, targetUid, syncMode, injectionTimeout, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1541 | } |
| 1542 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1543 | static void assertInjectedKeyTimesOut(InputDispatcher& dispatcher) { |
| 1544 | InputEventInjectionResult result = |
| 1545 | injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
| 1546 | InputEventInjectionSync::WAIT_FOR_RESULT, CONSUME_TIMEOUT_NO_EVENT_EXPECTED); |
| 1547 | if (result != InputEventInjectionResult::TIMED_OUT) { |
| 1548 | FAIL() << "Injection should have timed out, but got " << ftl::enum_string(result); |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | static InputEventInjectionResult injectKeyDown(InputDispatcher& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1553 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1554 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1555 | } |
| 1556 | |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1557 | // Inject a down event that has key repeat disabled. This allows InputDispatcher to idle without |
| 1558 | // sending a subsequent key up. When key repeat is enabled, the dispatcher cannot idle because it |
| 1559 | // has to be woken up to process the repeating key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1560 | static InputEventInjectionResult injectKeyDownNoRepeat(InputDispatcher& dispatcher, |
| 1561 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1562 | return injectKey(dispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, displayId, |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1563 | InputEventInjectionSync::WAIT_FOR_RESULT, INJECT_EVENT_TIMEOUT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1564 | /*allowKeyRepeat=*/false); |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 1565 | } |
| 1566 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1567 | static InputEventInjectionResult injectKeyUp(InputDispatcher& dispatcher, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1568 | int32_t displayId = ADISPLAY_ID_NONE) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1569 | return injectKey(dispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, displayId); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1572 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1573 | InputDispatcher& dispatcher, const MotionEvent& event, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1574 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1575 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1576 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1577 | return dispatcher.injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, |
| 1578 | policyFlags); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 1579 | } |
| 1580 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1581 | static InputEventInjectionResult injectMotionEvent( |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1582 | InputDispatcher& dispatcher, int32_t action, int32_t source, int32_t displayId, |
| 1583 | const PointF& position = {100, 200}, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1584 | const PointF& cursorPosition = {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 1585 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 1586 | std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1587 | InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 1588 | nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 1589 | std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1590 | MotionEventBuilder motionBuilder = |
| 1591 | MotionEventBuilder(action, source) |
| 1592 | .displayId(displayId) |
| 1593 | .eventTime(eventTime) |
| 1594 | .rawXCursorPosition(cursorPosition.x) |
| 1595 | .rawYCursorPosition(cursorPosition.y) |
| 1596 | .pointer( |
| 1597 | PointerBuilder(/*id=*/0, ToolType::FINGER).x(position.x).y(position.y)); |
| 1598 | if (MotionEvent::getActionMasked(action) == ACTION_DOWN) { |
| 1599 | motionBuilder.downTime(eventTime); |
| 1600 | } |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1601 | |
| 1602 | // Inject event until dispatch out. |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 1603 | return injectMotionEvent(dispatcher, motionBuilder.build(), injectionTimeout, injectionMode, |
| 1604 | targetUid, policyFlags); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1605 | } |
| 1606 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1607 | static InputEventInjectionResult injectMotionDown(InputDispatcher& dispatcher, int32_t source, |
| 1608 | int32_t displayId, |
| 1609 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1610 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_DOWN, source, displayId, location); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1611 | } |
| 1612 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1613 | static InputEventInjectionResult injectMotionUp(InputDispatcher& dispatcher, int32_t source, |
| 1614 | int32_t displayId, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1615 | const PointF& location = {100, 200}) { |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 1616 | return injectMotionEvent(dispatcher, AMOTION_EVENT_ACTION_UP, source, displayId, location); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1619 | static NotifyKeyArgs generateKeyArgs(int32_t action, int32_t displayId = ADISPLAY_ID_NONE) { |
| 1620 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1621 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1622 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 1623 | AINPUT_SOURCE_KEYBOARD, displayId, POLICY_FLAG_PASS_TO_USER, action, |
| 1624 | /*flags=*/0, AKEYCODE_A, KEY_A, AMETA_NONE, currentTime); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1625 | |
| 1626 | return args; |
| 1627 | } |
| 1628 | |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1629 | static NotifyKeyArgs generateSystemShortcutArgs(int32_t action, |
| 1630 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1631 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1632 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1633 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 1634 | AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_C, KEY_C, |
| 1635 | AMETA_META_ON, currentTime); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1636 | |
| 1637 | return args; |
| 1638 | } |
| 1639 | |
| 1640 | static NotifyKeyArgs generateAssistantKeyArgs(int32_t action, |
| 1641 | int32_t displayId = ADISPLAY_ID_NONE) { |
| 1642 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1643 | // Define a valid key event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1644 | NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, |
| 1645 | AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_ASSIST, |
| 1646 | KEY_ASSISTANT, AMETA_NONE, currentTime); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 1647 | |
| 1648 | return args; |
| 1649 | } |
| 1650 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1651 | [[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, |
| 1652 | int32_t displayId, |
| 1653 | const std::vector<PointF>& points) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1654 | size_t pointerCount = points.size(); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 1655 | if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_UP) { |
| 1656 | EXPECT_EQ(1U, pointerCount) << "Actions DOWN and UP can only contain a single pointer"; |
| 1657 | } |
| 1658 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1659 | PointerProperties pointerProperties[pointerCount]; |
| 1660 | PointerCoords pointerCoords[pointerCount]; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1661 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1662 | for (size_t i = 0; i < pointerCount; i++) { |
| 1663 | pointerProperties[i].clear(); |
| 1664 | pointerProperties[i].id = i; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1665 | pointerProperties[i].toolType = ToolType::FINGER; |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1666 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1667 | pointerCoords[i].clear(); |
| 1668 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, points[i].x); |
| 1669 | pointerCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, points[i].y); |
| 1670 | } |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1671 | |
| 1672 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1673 | // Define a valid motion event. |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1674 | NotifyMotionArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID, source, |
| 1675 | displayId, POLICY_FLAG_PASS_TO_USER, action, /*actionButton=*/0, |
| 1676 | /*flags=*/0, AMETA_NONE, /*buttonState=*/0, MotionClassification::NONE, |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1677 | AMOTION_EVENT_EDGE_FLAG_NONE, pointerCount, pointerProperties, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1678 | pointerCoords, /*xPrecision=*/0, /*yPrecision=*/0, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 1679 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 1680 | AMOTION_EVENT_INVALID_CURSOR_POSITION, currentTime, /*videoFrames=*/{}); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 1681 | |
| 1682 | return args; |
| 1683 | } |
| 1684 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1685 | static NotifyMotionArgs generateTouchArgs(int32_t action, const std::vector<PointF>& points) { |
| 1686 | return generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, DISPLAY_ID, points); |
| 1687 | } |
| 1688 | |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 1689 | static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source, int32_t displayId) { |
| 1690 | return generateMotionArgs(action, source, displayId, {PointF{100, 200}}); |
| 1691 | } |
| 1692 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 1693 | static NotifyPointerCaptureChangedArgs generatePointerCaptureChangedArgs( |
| 1694 | const PointerCaptureRequest& request) { |
Prabir Pradhan | a8fe7c5 | 2023-12-14 22:07:23 +0000 | [diff] [blame] | 1695 | return NotifyPointerCaptureChangedArgs(InputEvent::nextId(), systemTime(SYSTEM_TIME_MONOTONIC), |
| 1696 | request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 1697 | } |
| 1698 | |
Siarhei Vishniakou | adeb6fa | 2023-05-26 09:11:06 -0700 | [diff] [blame] | 1699 | } // namespace |
| 1700 | |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1701 | /** |
| 1702 | * When a window unexpectedly disposes of its input channel, policy should be notified about the |
| 1703 | * broken channel. |
| 1704 | */ |
| 1705 | TEST_F(InputDispatcherTest, WhenInputChannelBreaks_PolicyIsNotified) { |
| 1706 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1707 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1708 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1709 | "Window that breaks its input channel", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1710 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1711 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7aa3e94 | 2021-11-18 09:49:11 -0800 | [diff] [blame] | 1712 | |
| 1713 | // Window closes its channel, but the window remains. |
| 1714 | window->destroyReceiver(); |
| 1715 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(window->getInfo()->token); |
| 1716 | } |
| 1717 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1718 | TEST_F(InputDispatcherTest, SetInputWindow_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1719 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1720 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1721 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1722 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1723 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1724 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1725 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1726 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1727 | |
| 1728 | // Window should receive motion event. |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 1729 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1730 | } |
| 1731 | |
Siarhei Vishniakou | aeed0da | 2024-01-09 08:57:13 -0800 | [diff] [blame] | 1732 | using InputDispatcherDeathTest = InputDispatcherTest; |
| 1733 | |
| 1734 | /** |
| 1735 | * When 'onWindowInfosChanged' arguments contain a duplicate entry for the same window, dispatcher |
| 1736 | * should crash. |
| 1737 | */ |
| 1738 | TEST_F(InputDispatcherDeathTest, DuplicateWindowInfosAbortDispatcher) { |
| 1739 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
| 1740 | ScopedSilentDeath _silentDeath; |
| 1741 | |
| 1742 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1743 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1744 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 1745 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 1746 | {{*window->getInfo(), *window->getInfo()}, {}, 0, 0}), |
| 1747 | "Incorrect WindowInfosUpdate provided"); |
| 1748 | } |
| 1749 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1750 | TEST_F(InputDispatcherTest, WhenDisplayNotSpecified_InjectMotionToDefaultDisplay) { |
| 1751 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1752 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1753 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1754 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1755 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1756 | // Inject a MotionEvent to an unknown display. |
| 1757 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1758 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_NONE)) |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 1759 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1760 | |
| 1761 | // Window should receive motion event. |
| 1762 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1763 | } |
| 1764 | |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1765 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1766 | * Calling onWindowInfosChanged once should not cause any issues. |
| 1767 | * 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] | 1768 | * called twice. |
| 1769 | */ |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 1770 | TEST_F(InputDispatcherTest, SetInputWindowOnceWithSingleTouchWindow) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1771 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1772 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1773 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1774 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1775 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1776 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1777 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1778 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1779 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1780 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1781 | |
| 1782 | // Window should receive motion event. |
| 1783 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1784 | } |
| 1785 | |
| 1786 | /** |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1787 | * Calling onWindowInfosChanged twice, with the same info, should not cause any issues. |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1788 | */ |
| 1789 | TEST_F(InputDispatcherTest, SetInputWindowTwice_SingleWindowTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1790 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1791 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 1792 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1793 | window->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1794 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1795 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 1796 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1797 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1798 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1799 | {50, 50})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1800 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 1801 | |
| 1802 | // Window should receive motion event. |
| 1803 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 1804 | } |
| 1805 | |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1806 | // The foreground window should receive the first touch down event. |
| 1807 | TEST_F(InputDispatcherTest, SetInputWindow_MultiWindowsTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 1808 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1809 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1810 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 1811 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1812 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1813 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1814 | mDispatcher->onWindowInfosChanged( |
| 1815 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1816 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1817 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 1818 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1819 | |
| 1820 | // 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] | 1821 | windowTop->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 1822 | windowSecond->assertNoEvents(); |
| 1823 | } |
| 1824 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1825 | /** |
| 1826 | * Two windows: A top window, and a wallpaper behind the window. |
| 1827 | * Touch goes to the top window, and then top window disappears. Ensure that wallpaper window |
| 1828 | * gets ACTION_CANCEL. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1829 | * 1. foregroundWindow <-- dup touch to wallpaper |
| 1830 | * 2. wallpaperWindow <-- is wallpaper |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1831 | */ |
| 1832 | TEST_F(InputDispatcherTest, WhenForegroundWindowDisappears_WallpaperTouchIsCanceled) { |
| 1833 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1834 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1835 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1836 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1837 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1838 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1839 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1840 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1841 | mDispatcher->onWindowInfosChanged( |
| 1842 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1843 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1844 | injectMotionEvent(*mDispatcher, |
| 1845 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1846 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(200)) |
| 1847 | .build())) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1848 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1849 | |
| 1850 | // Both foreground window and its wallpaper should receive the touch down |
| 1851 | foregroundWindow->consumeMotionDown(); |
| 1852 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1853 | |
| 1854 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1855 | injectMotionEvent(*mDispatcher, |
| 1856 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 1857 | .pointer(PointerBuilder(0, ToolType::FINGER).x(110).y(200)) |
| 1858 | .build())) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1859 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1860 | |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 1861 | foregroundWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1862 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1863 | |
| 1864 | // Now the foreground window goes away, but the wallpaper stays |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1865 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1866 | foregroundWindow->consumeMotionCancel(); |
| 1867 | // Since the "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 1868 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1869 | } |
| 1870 | |
| 1871 | /** |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1872 | * Two fingers down on the window, and lift off the first finger. |
| 1873 | * Next, cancel the gesture to the window by removing the window. Make sure that the CANCEL event |
| 1874 | * contains a single pointer. |
| 1875 | */ |
| 1876 | TEST_F(InputDispatcherTest, CancelAfterPointer0Up) { |
| 1877 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1878 | sp<FakeWindowHandle> window = |
| 1879 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 1880 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1881 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1882 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1883 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1884 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1885 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1886 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1887 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 1888 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1889 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1890 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1891 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 1892 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 1893 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 1894 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(100)) |
| 1895 | .build()); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1896 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 1897 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 1898 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 1899 | |
| 1900 | // Remove the window. The gesture should be canceled |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1901 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 1902 | const std::map<int32_t, PointF> expectedPointers{{1, PointF{110, 100}}}; |
| 1903 | window->consumeMotionEvent( |
| 1904 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedPointers))); |
| 1905 | } |
| 1906 | |
| 1907 | /** |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1908 | * Same test as WhenForegroundWindowDisappears_WallpaperTouchIsCanceled above, |
| 1909 | * with the following differences: |
| 1910 | * After ACTION_DOWN, Wallpaper window hangs up its channel, which forces the dispatcher to |
| 1911 | * clean up the connection. |
| 1912 | * This later may crash dispatcher during ACTION_CANCEL synthesis, if the dispatcher is not careful. |
| 1913 | * Ensure that there's no crash in the dispatcher. |
| 1914 | */ |
| 1915 | TEST_F(InputDispatcherTest, WhenWallpaperDisappears_NoCrash) { |
| 1916 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 1917 | sp<FakeWindowHandle> foregroundWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1918 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1919 | foregroundWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1920 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1921 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1922 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1923 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1924 | mDispatcher->onWindowInfosChanged( |
| 1925 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1926 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1927 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1928 | {100, 200})) |
| 1929 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1930 | |
| 1931 | // Both foreground window and its wallpaper should receive the touch down |
| 1932 | foregroundWindow->consumeMotionDown(); |
| 1933 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1934 | |
| 1935 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1936 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1937 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 1938 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1939 | |
| 1940 | foregroundWindow->consumeMotionMove(); |
| 1941 | wallpaperWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1942 | |
| 1943 | // Wallpaper closes its channel, but the window remains. |
| 1944 | wallpaperWindow->destroyReceiver(); |
| 1945 | mFakePolicy->assertNotifyInputChannelBrokenWasCalled(wallpaperWindow->getInfo()->token); |
| 1946 | |
| 1947 | // Now the foreground window goes away, but the wallpaper stays, even though its channel |
| 1948 | // is no longer valid. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1949 | mDispatcher->onWindowInfosChanged({{*wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1950 | foregroundWindow->consumeMotionCancel(); |
| 1951 | } |
| 1952 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1953 | class ShouldSplitTouchFixture : public InputDispatcherTest, |
| 1954 | public ::testing::WithParamInterface<bool> {}; |
| 1955 | INSTANTIATE_TEST_SUITE_P(InputDispatcherTest, ShouldSplitTouchFixture, |
| 1956 | ::testing::Values(true, false)); |
Siarhei Vishniakou | 2b03097 | 2021-11-18 10:01:27 -0800 | [diff] [blame] | 1957 | /** |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1958 | * A single window that receives touch (on top), and a wallpaper window underneath it. |
| 1959 | * The top window gets a multitouch gesture. |
| 1960 | * Ensure that wallpaper gets the same gesture. |
| 1961 | */ |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1962 | TEST_P(ShouldSplitTouchFixture, WallpaperWindowReceivesMultiTouch) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1963 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1964 | sp<FakeWindowHandle> foregroundWindow = |
| 1965 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 1966 | foregroundWindow->setDupTouchToWallpaper(true); |
| 1967 | foregroundWindow->setPreventSplitting(GetParam()); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1968 | |
| 1969 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 1970 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 1971 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1972 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 1973 | mDispatcher->onWindowInfosChanged( |
| 1974 | {{*foregroundWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1975 | |
| 1976 | // Touch down on top window |
| 1977 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1978 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1979 | {100, 100})) |
| 1980 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1981 | |
| 1982 | // Both top window and its wallpaper should receive the touch down |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 1983 | foregroundWindow->consumeMotionDown(); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1984 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 1985 | |
| 1986 | // Second finger down on the top window |
| 1987 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 1988 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1989 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 1990 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 1991 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1992 | .build(); |
| 1993 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 1994 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 1995 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 1996 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 1997 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 1998 | foregroundWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
| 1999 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2000 | expectedWallpaperFlags); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2001 | |
| 2002 | const MotionEvent secondFingerUpEvent = |
| 2003 | MotionEventBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 2004 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2005 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2006 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2007 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2008 | .build(); |
| 2009 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2010 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2011 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2012 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2013 | foregroundWindow->consumeMotionPointerUp(0); |
| 2014 | wallpaperWindow->consumeMotionPointerUp(0, ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2015 | |
| 2016 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2017 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 2018 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2019 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2020 | .displayId(ADISPLAY_ID_DEFAULT) |
| 2021 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 2022 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 2023 | .x(100) |
| 2024 | .y(100)) |
| 2025 | .build(), |
| 2026 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT)) |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2027 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2028 | foregroundWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 2029 | wallpaperWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | /** |
| 2033 | * Two windows: a window on the left and window on the right. |
| 2034 | * A third window, wallpaper, is behind both windows, and spans both top windows. |
| 2035 | * The first touch down goes to the left window. A second pointer touches down on the right window. |
| 2036 | * The touch is split, so both left and right windows should receive ACTION_DOWN. |
| 2037 | * The wallpaper will get the full event, so it should receive ACTION_DOWN followed by |
| 2038 | * ACTION_POINTER_DOWN(1). |
| 2039 | */ |
| 2040 | TEST_F(InputDispatcherTest, TwoWindows_SplitWallpaperTouch) { |
| 2041 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2042 | sp<FakeWindowHandle> leftWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2043 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2044 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2045 | leftWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2046 | |
| 2047 | sp<FakeWindowHandle> rightWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2048 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2049 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2050 | rightWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2051 | |
| 2052 | sp<FakeWindowHandle> wallpaperWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 2053 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2054 | wallpaperWindow->setFrame(Rect(0, 0, 400, 200)); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 2055 | wallpaperWindow->setIsWallpaper(true); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2056 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2057 | mDispatcher->onWindowInfosChanged( |
| 2058 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 2059 | {}, |
| 2060 | 0, |
| 2061 | 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2062 | |
| 2063 | // Touch down on left window |
| 2064 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2065 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2066 | {100, 100})) |
| 2067 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2068 | |
| 2069 | // Both foreground window and its wallpaper should receive the touch down |
| 2070 | leftWindow->consumeMotionDown(); |
| 2071 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2072 | |
| 2073 | // Second finger down on the right window |
| 2074 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2075 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2076 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2077 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2078 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2079 | .build(); |
| 2080 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2081 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2082 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 2083 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2084 | |
| 2085 | leftWindow->consumeMotionMove(); |
| 2086 | // Since the touch is split, right window gets ACTION_DOWN |
| 2087 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 2088 | wallpaperWindow->consumeMotionPointerDown(/*pointerIndex=*/1, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2089 | expectedWallpaperFlags); |
| 2090 | |
| 2091 | // Now, leftWindow, which received the first finger, disappears. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2092 | mDispatcher->onWindowInfosChanged( |
| 2093 | {{*rightWindow->getInfo(), *wallpaperWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2094 | leftWindow->consumeMotionCancel(); |
| 2095 | // Since a "parent" window of the wallpaper is gone, wallpaper should receive cancel, too. |
| 2096 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2097 | |
| 2098 | // The pointer that's still down on the right window moves, and goes to the right window only. |
| 2099 | // As far as the dispatcher's concerned though, both pointers are still present. |
| 2100 | const MotionEvent secondFingerMoveEvent = |
| 2101 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2102 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 2103 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(100)) |
| 2104 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(310).y(110)) |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2105 | .build(); |
| 2106 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2107 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 2108 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 2109 | rightWindow->consumeMotionMove(); |
| 2110 | |
| 2111 | leftWindow->assertNoEvents(); |
| 2112 | rightWindow->assertNoEvents(); |
| 2113 | wallpaperWindow->assertNoEvents(); |
| 2114 | } |
| 2115 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2116 | /** |
| 2117 | * Two windows: a window on the left with dup touch to wallpaper and window on the right without it. |
| 2118 | * The touch slips to the right window. so left window and wallpaper should receive ACTION_CANCEL |
| 2119 | * The right window should receive ACTION_DOWN. |
| 2120 | */ |
| 2121 | TEST_F(InputDispatcherTest, WallpaperWindowWhenSlippery) { |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2122 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2123 | sp<FakeWindowHandle> leftWindow = |
| 2124 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2125 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2126 | leftWindow->setDupTouchToWallpaper(true); |
| 2127 | leftWindow->setSlippery(true); |
| 2128 | |
| 2129 | sp<FakeWindowHandle> rightWindow = |
| 2130 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2131 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2132 | |
| 2133 | sp<FakeWindowHandle> wallpaperWindow = |
| 2134 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 2135 | wallpaperWindow->setIsWallpaper(true); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2136 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2137 | mDispatcher->onWindowInfosChanged( |
| 2138 | {{*leftWindow->getInfo(), *rightWindow->getInfo(), *wallpaperWindow->getInfo()}, |
| 2139 | {}, |
| 2140 | 0, |
| 2141 | 0}); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2142 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2143 | // Touch down on left window |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2144 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2145 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2146 | {100, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2147 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2148 | |
| 2149 | // Both foreground window and its wallpaper should receive the touch down |
| 2150 | leftWindow->consumeMotionDown(); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2151 | wallpaperWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 2152 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2153 | // Move to right window, the left window should receive cancel. |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2154 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2155 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2156 | ADISPLAY_ID_DEFAULT, {201, 100})) |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2157 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 2158 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 2159 | leftWindow->consumeMotionCancel(); |
| 2160 | rightWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 2161 | wallpaperWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Arthur Hung | 74c248d | 2022-11-23 07:09:59 +0000 | [diff] [blame] | 2162 | } |
| 2163 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 2164 | /** |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2165 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2166 | * interactive, it might stop sending this flag. |
| 2167 | * In this test, we check that if the policy stops sending this flag mid-gesture, we still ensure |
| 2168 | * to have a consistent input stream. |
| 2169 | * |
| 2170 | * Test procedure: |
| 2171 | * DOWN -> POINTER_DOWN -> (stop sending POLICY_FLAG_PASS_TO_USER) -> CANCEL. |
| 2172 | * DOWN (new gesture). |
| 2173 | * |
| 2174 | * In the bad implementation, we could potentially drop the CANCEL event, and get an inconsistent |
| 2175 | * state in the dispatcher. This would cause the final DOWN event to not be delivered to the app. |
| 2176 | * |
| 2177 | * We technically just need a single window here, but we are using two windows (spy on top and a |
| 2178 | * regular window below) to emulate the actual situation where it happens on the device. |
| 2179 | */ |
| 2180 | TEST_F(InputDispatcherTest, TwoPointerCancelInconsistentPolicy) { |
| 2181 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2182 | sp<FakeWindowHandle> spyWindow = |
| 2183 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2184 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2185 | spyWindow->setTrustedOverlay(true); |
| 2186 | spyWindow->setSpy(true); |
| 2187 | |
| 2188 | sp<FakeWindowHandle> window = |
| 2189 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2190 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2191 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2192 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2193 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2194 | |
| 2195 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2196 | mDispatcher->notifyMotion( |
| 2197 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2198 | .deviceId(touchDeviceId) |
| 2199 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2200 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2201 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2202 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2203 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2204 | .deviceId(touchDeviceId) |
| 2205 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2206 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2207 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2208 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2209 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2210 | spyWindow->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2211 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2212 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 2213 | |
| 2214 | // Cancel the current gesture. Send the cancel without the default policy flags. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2215 | mDispatcher->notifyMotion( |
| 2216 | MotionArgsBuilder(AMOTION_EVENT_ACTION_CANCEL, AINPUT_SOURCE_TOUCHSCREEN) |
| 2217 | .deviceId(touchDeviceId) |
| 2218 | .policyFlags(0) |
| 2219 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2220 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 2221 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2222 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2223 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)); |
| 2224 | |
| 2225 | // We don't need to reset the device to reproduce the issue, but the reset event typically |
| 2226 | // 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] | 2227 | mDispatcher->notifyDeviceReset({/*id=*/1, systemTime(SYSTEM_TIME_MONOTONIC), touchDeviceId}); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2228 | |
| 2229 | // Start new gesture |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2230 | mDispatcher->notifyMotion( |
| 2231 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2232 | .deviceId(touchDeviceId) |
| 2233 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2234 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 2235 | .build()); |
Siarhei Vishniakou | 5bf25d9 | 2023-02-08 15:43:38 -0800 | [diff] [blame] | 2236 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2237 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2238 | |
| 2239 | // No more events |
| 2240 | spyWindow->assertNoEvents(); |
| 2241 | window->assertNoEvents(); |
| 2242 | } |
| 2243 | |
| 2244 | /** |
Linnan Li | 907ae73 | 2023-09-05 17:14:21 +0800 | [diff] [blame] | 2245 | * Same as the above 'TwoPointerCancelInconsistentPolicy' test, but for hovers. |
| 2246 | * The policy typically sets POLICY_FLAG_PASS_TO_USER to the events. But when the display is not |
| 2247 | * interactive, it might stop sending this flag. |
| 2248 | * We've already ensured the consistency of the touch event in this case, and we should also ensure |
| 2249 | * the consistency of the hover event in this case. |
| 2250 | * |
| 2251 | * Test procedure: |
| 2252 | * HOVER_ENTER -> HOVER_MOVE -> (stop sending POLICY_FLAG_PASS_TO_USER) -> HOVER_EXIT |
| 2253 | * HOVER_ENTER -> HOVER_MOVE -> HOVER_EXIT |
| 2254 | * |
| 2255 | * We expect to receive two full streams of hover events. |
| 2256 | */ |
| 2257 | TEST_F(InputDispatcherTest, HoverEventInconsistentPolicy) { |
| 2258 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2259 | |
| 2260 | sp<FakeWindowHandle> window = |
| 2261 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2262 | window->setFrame(Rect(0, 0, 300, 300)); |
| 2263 | |
| 2264 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2265 | |
| 2266 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2267 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2268 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2269 | .build()); |
| 2270 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2271 | |
| 2272 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2273 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2274 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2275 | .build()); |
| 2276 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2277 | |
| 2278 | // Send hover exit without the default policy flags. |
| 2279 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2280 | .policyFlags(0) |
| 2281 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2282 | .build()); |
| 2283 | |
| 2284 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2285 | |
| 2286 | // Send a simple hover event stream, ensure dispatcher not crashed and window can receive |
| 2287 | // right event. |
| 2288 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2289 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2290 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(200).y(201)) |
| 2291 | .build()); |
| 2292 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2293 | |
| 2294 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2295 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2296 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 2297 | .build()); |
| 2298 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2299 | |
| 2300 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2301 | .policyFlags(DEFAULT_POLICY_FLAGS) |
| 2302 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(201).y(202)) |
| 2303 | .build()); |
| 2304 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2305 | } |
| 2306 | |
| 2307 | /** |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2308 | * Two windows: a window on the left and a window on the right. |
| 2309 | * Mouse is hovered from the right window into the left window. |
| 2310 | * Next, we tap on the left window, where the cursor was last seen. |
| 2311 | * The second tap is done onto the right window. |
| 2312 | * The mouse and tap are from two different devices. |
| 2313 | * We technically don't need to set the downtime / eventtime for these events, but setting these |
| 2314 | * explicitly helps during debugging. |
| 2315 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 2316 | * In the buggy implementation, a tap on the right window would cause a crash. |
| 2317 | */ |
| 2318 | TEST_F(InputDispatcherTest, HoverFromLeftToRightAndTap) { |
| 2319 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2320 | sp<FakeWindowHandle> leftWindow = |
| 2321 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2322 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2323 | |
| 2324 | sp<FakeWindowHandle> rightWindow = |
| 2325 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2326 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2327 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2328 | mDispatcher->onWindowInfosChanged( |
| 2329 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2330 | // All times need to start at the current time, otherwise the dispatcher will drop the events as |
| 2331 | // stale. |
| 2332 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2333 | const int32_t mouseDeviceId = 6; |
| 2334 | const int32_t touchDeviceId = 4; |
| 2335 | // Move the cursor from right |
| 2336 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2337 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2338 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2339 | AINPUT_SOURCE_MOUSE) |
| 2340 | .deviceId(mouseDeviceId) |
| 2341 | .downTime(baseTime + 10) |
| 2342 | .eventTime(baseTime + 20) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2343 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2344 | .build())); |
| 2345 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2346 | |
| 2347 | // .. to the left window |
| 2348 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2349 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2350 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 2351 | AINPUT_SOURCE_MOUSE) |
| 2352 | .deviceId(mouseDeviceId) |
| 2353 | .downTime(baseTime + 10) |
| 2354 | .eventTime(baseTime + 30) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2355 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2356 | .build())); |
| 2357 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2358 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 2359 | // Now tap the left window |
| 2360 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2361 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2362 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2363 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2364 | .deviceId(touchDeviceId) |
| 2365 | .downTime(baseTime + 40) |
| 2366 | .eventTime(baseTime + 40) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2367 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2368 | .build())); |
| 2369 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 2370 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2371 | |
| 2372 | // release tap |
| 2373 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2374 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2375 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2376 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2377 | .deviceId(touchDeviceId) |
| 2378 | .downTime(baseTime + 40) |
| 2379 | .eventTime(baseTime + 50) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2380 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2381 | .build())); |
| 2382 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2383 | |
| 2384 | // Tap the window on the right |
| 2385 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2386 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2387 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 2388 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2389 | .deviceId(touchDeviceId) |
| 2390 | .downTime(baseTime + 60) |
| 2391 | .eventTime(baseTime + 60) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2392 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2393 | .build())); |
| 2394 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 2395 | |
| 2396 | // release tap |
| 2397 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2398 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2399 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 2400 | AINPUT_SOURCE_TOUCHSCREEN) |
| 2401 | .deviceId(touchDeviceId) |
| 2402 | .downTime(baseTime + 60) |
| 2403 | .eventTime(baseTime + 70) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 2404 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 2405 | .build())); |
| 2406 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 2407 | |
| 2408 | // No more events |
| 2409 | leftWindow->assertNoEvents(); |
| 2410 | rightWindow->assertNoEvents(); |
| 2411 | } |
| 2412 | |
| 2413 | /** |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2414 | * Start hovering in a window. While this hover is still active, make another window appear on top. |
| 2415 | * The top, obstructing window has no input channel, so it's not supposed to receive input. |
| 2416 | * While the top window is present, the hovering is stopped. |
| 2417 | * Later, hovering gets resumed again. |
| 2418 | * Ensure that new hover gesture is handled correctly. |
| 2419 | * This test reproduces a crash where the HOVER_EXIT event wasn't getting dispatched correctly |
| 2420 | * to the window that's currently being hovered over. |
| 2421 | */ |
| 2422 | TEST_F(InputDispatcherTest, HoverWhileWindowAppears) { |
| 2423 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2424 | sp<FakeWindowHandle> window = |
| 2425 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2426 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2427 | |
| 2428 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2429 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2430 | |
| 2431 | // Start hovering in the window |
| 2432 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2433 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2434 | .build()); |
| 2435 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2436 | |
| 2437 | // Now, an obscuring window appears! |
| 2438 | sp<FakeWindowHandle> obscuringWindow = |
| 2439 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2440 | ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 2441 | /*createInputChannel=*/false); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2442 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2443 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2444 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2445 | obscuringWindow->setNoInputChannel(true); |
| 2446 | obscuringWindow->setFocusable(false); |
| 2447 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2448 | mDispatcher->onWindowInfosChanged( |
| 2449 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2450 | |
| 2451 | // While this new obscuring window is present, the hovering is stopped |
| 2452 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 2453 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2454 | .build()); |
| 2455 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2456 | |
| 2457 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2458 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2459 | |
| 2460 | // And a new hover gesture starts. |
| 2461 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2462 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2463 | .build()); |
| 2464 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2465 | } |
| 2466 | |
| 2467 | /** |
| 2468 | * Same test as 'HoverWhileWindowAppears' above, but here, we also send some HOVER_MOVE events to |
| 2469 | * the obscuring window. |
| 2470 | */ |
| 2471 | TEST_F(InputDispatcherTest, HoverMoveWhileWindowAppears) { |
| 2472 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2473 | sp<FakeWindowHandle> window = |
| 2474 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2475 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2476 | |
| 2477 | // Only a single window is present at first |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2478 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2479 | |
| 2480 | // Start hovering in the window |
| 2481 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2482 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2483 | .build()); |
| 2484 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2485 | |
| 2486 | // Now, an obscuring window appears! |
| 2487 | sp<FakeWindowHandle> obscuringWindow = |
| 2488 | sp<FakeWindowHandle>::make(application, mDispatcher, "Obscuring window", |
| 2489 | ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 2490 | /*createInputChannel=*/false); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2491 | obscuringWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2492 | obscuringWindow->setTouchOcclusionMode(TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 2493 | obscuringWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
| 2494 | obscuringWindow->setNoInputChannel(true); |
| 2495 | obscuringWindow->setFocusable(false); |
| 2496 | obscuringWindow->setAlpha(1.0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2497 | mDispatcher->onWindowInfosChanged( |
| 2498 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2499 | |
| 2500 | // While this new obscuring window is present, the hovering continues. The event can't go to the |
| 2501 | // bottom window due to obstructed touches, so it should generate HOVER_EXIT for that window. |
| 2502 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2503 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2504 | .build()); |
| 2505 | obscuringWindow->assertNoEvents(); |
| 2506 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 2507 | |
| 2508 | // Now the obscuring window goes away. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2509 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 2510 | |
| 2511 | // Hovering continues in the same position. The hovering pointer re-enters the bottom window, |
| 2512 | // so it should generate a HOVER_ENTER |
| 2513 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2514 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 2515 | .build()); |
| 2516 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2517 | |
| 2518 | // Now the MOVE should be getting dispatched normally |
| 2519 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2520 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 2521 | .build()); |
| 2522 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2523 | } |
| 2524 | |
| 2525 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2526 | * Hover mouse over a window, and then send ACTION_SCROLL. Ensure both the hover and the scroll |
| 2527 | * events are delivered to the window. |
| 2528 | */ |
| 2529 | TEST_F(InputDispatcherTest, HoverMoveAndScroll) { |
| 2530 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2531 | sp<FakeWindowHandle> window = |
| 2532 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2533 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2534 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2535 | |
| 2536 | // Start hovering in the window |
| 2537 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2538 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 2539 | .build()); |
| 2540 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 2541 | |
| 2542 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 2543 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2544 | .build()); |
| 2545 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 2546 | |
| 2547 | // Scroll with the mouse |
| 2548 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_SCROLL, AINPUT_SOURCE_MOUSE) |
| 2549 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2550 | .build()); |
| 2551 | window->consumeMotionEvent(WithMotionAction(ACTION_SCROLL)); |
| 2552 | } |
| 2553 | |
| 2554 | using InputDispatcherMultiDeviceTest = InputDispatcherTest; |
| 2555 | |
| 2556 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2557 | * One window. Stylus down on the window. Next, touch from another device goes down. Ensure that |
| 2558 | * touch is dropped, because stylus should be preferred over touch. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2559 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2560 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2561 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2562 | sp<FakeWindowHandle> window = |
| 2563 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2564 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2565 | |
| 2566 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2567 | |
| 2568 | constexpr int32_t touchDeviceId = 4; |
| 2569 | constexpr int32_t stylusDeviceId = 2; |
| 2570 | |
| 2571 | // Stylus down |
| 2572 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2573 | .deviceId(stylusDeviceId) |
| 2574 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2575 | .build()); |
| 2576 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2577 | |
| 2578 | // Touch down |
| 2579 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2580 | .deviceId(touchDeviceId) |
| 2581 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2582 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2583 | |
| 2584 | // Touch move |
| 2585 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2586 | .deviceId(touchDeviceId) |
| 2587 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2588 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2589 | // Touch is ignored because stylus is already down |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2590 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2591 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2592 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2593 | .deviceId(stylusDeviceId) |
| 2594 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2595 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2596 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2597 | WithCoords(101, 111))); |
| 2598 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2599 | window->assertNoEvents(); |
| 2600 | } |
| 2601 | |
| 2602 | /** |
| 2603 | * 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] | 2604 | * 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] | 2605 | * Similar test as above, but with added SPY window. |
| 2606 | */ |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2607 | TEST_F(InputDispatcherMultiDeviceTest, StylusDownWithSpyBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2608 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2609 | sp<FakeWindowHandle> window = |
| 2610 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2611 | sp<FakeWindowHandle> spyWindow = |
| 2612 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 2613 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2614 | spyWindow->setTrustedOverlay(true); |
| 2615 | spyWindow->setSpy(true); |
| 2616 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2617 | |
| 2618 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 2619 | |
| 2620 | constexpr int32_t touchDeviceId = 4; |
| 2621 | constexpr int32_t stylusDeviceId = 2; |
| 2622 | |
| 2623 | // Stylus down |
| 2624 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2625 | .deviceId(stylusDeviceId) |
| 2626 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2627 | .build()); |
| 2628 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2629 | spyWindow->consumeMotionEvent( |
| 2630 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2631 | |
| 2632 | // Touch down |
| 2633 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2634 | .deviceId(touchDeviceId) |
| 2635 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2636 | .build()); |
| 2637 | |
| 2638 | // Touch move |
| 2639 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2640 | .deviceId(touchDeviceId) |
| 2641 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2642 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2643 | |
| 2644 | // Touch is ignored because stylus is already down |
| 2645 | |
| 2646 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2647 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2648 | .deviceId(stylusDeviceId) |
| 2649 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2650 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2651 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2652 | WithCoords(101, 111))); |
| 2653 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2654 | WithCoords(101, 111))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2655 | |
| 2656 | window->assertNoEvents(); |
| 2657 | spyWindow->assertNoEvents(); |
| 2658 | } |
| 2659 | |
| 2660 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2661 | * 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] | 2662 | * touch is dropped, because stylus hover takes precedence. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2663 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2664 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverBlocksTouchDown) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2665 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2666 | sp<FakeWindowHandle> window = |
| 2667 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2668 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2669 | |
| 2670 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2671 | |
| 2672 | constexpr int32_t touchDeviceId = 4; |
| 2673 | constexpr int32_t stylusDeviceId = 2; |
| 2674 | |
| 2675 | // Stylus down on the window |
| 2676 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2677 | .deviceId(stylusDeviceId) |
| 2678 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2679 | .build()); |
| 2680 | window->consumeMotionEvent( |
| 2681 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2682 | |
| 2683 | // Touch down on window |
| 2684 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2685 | .deviceId(touchDeviceId) |
| 2686 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2687 | .build()); |
| 2688 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2689 | .deviceId(touchDeviceId) |
| 2690 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2691 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2692 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2693 | // Touch is ignored because stylus is hovering |
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 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2696 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2697 | .deviceId(stylusDeviceId) |
| 2698 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2699 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2700 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2701 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2702 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2703 | // and subsequent touches continue to be ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2704 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2705 | .deviceId(touchDeviceId) |
| 2706 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2707 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2708 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2709 | } |
| 2710 | |
| 2711 | /** |
| 2712 | * 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] | 2713 | * Ensure that touch is canceled, because stylus hover should take precedence. |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2714 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2715 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusHover) { |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2716 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2717 | sp<FakeWindowHandle> window = |
| 2718 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2719 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2720 | |
| 2721 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2722 | |
| 2723 | constexpr int32_t touchDeviceId = 4; |
| 2724 | constexpr int32_t stylusDeviceId = 2; |
| 2725 | |
| 2726 | // Touch down on window |
| 2727 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2728 | .deviceId(touchDeviceId) |
| 2729 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2730 | .build()); |
| 2731 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2732 | .deviceId(touchDeviceId) |
| 2733 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2734 | .build()); |
| 2735 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2736 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2737 | |
| 2738 | // Stylus hover on the window |
| 2739 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2740 | .deviceId(stylusDeviceId) |
| 2741 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2742 | .build()); |
| 2743 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2744 | .deviceId(stylusDeviceId) |
| 2745 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2746 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2747 | // Stylus hover movement causes touch to be canceled |
| 2748 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 2749 | WithCoords(141, 146))); |
| 2750 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 2751 | WithDeviceId(stylusDeviceId), WithCoords(100, 110))); |
| 2752 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), |
| 2753 | WithDeviceId(stylusDeviceId), WithCoords(101, 111))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2754 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2755 | // Subsequent touch movements are ignored |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2756 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2757 | .deviceId(touchDeviceId) |
| 2758 | .pointer(PointerBuilder(0, ToolType::FINGER).x(142).y(147)) |
| 2759 | .build()); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 2760 | |
| 2761 | window->assertNoEvents(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2762 | } |
| 2763 | |
| 2764 | /** |
| 2765 | * One window. Stylus down on the window. Then, stylus from another device goes down. Ensure that |
| 2766 | * the latest stylus takes over. That is, old stylus should be canceled and the new stylus should |
| 2767 | * become active. |
| 2768 | */ |
| 2769 | TEST_F(InputDispatcherMultiDeviceTest, LatestStylusWins) { |
| 2770 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2771 | sp<FakeWindowHandle> window = |
| 2772 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2773 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2774 | |
| 2775 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2776 | |
| 2777 | constexpr int32_t stylusDeviceId1 = 3; |
| 2778 | constexpr int32_t stylusDeviceId2 = 5; |
| 2779 | |
| 2780 | // Touch down on window |
| 2781 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2782 | .deviceId(stylusDeviceId1) |
| 2783 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(99).y(100)) |
| 2784 | .build()); |
| 2785 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2786 | .deviceId(stylusDeviceId1) |
| 2787 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(101)) |
| 2788 | .build()); |
| 2789 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId1))); |
| 2790 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId1))); |
| 2791 | |
| 2792 | // Second stylus down |
| 2793 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2794 | .deviceId(stylusDeviceId2) |
| 2795 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(9).y(10)) |
| 2796 | .build()); |
| 2797 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2798 | .deviceId(stylusDeviceId2) |
| 2799 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(11)) |
| 2800 | .build()); |
| 2801 | |
| 2802 | // First stylus is canceled, second one takes over. |
| 2803 | window->consumeMotionEvent( |
| 2804 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId1))); |
| 2805 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId2))); |
| 2806 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId2))); |
| 2807 | |
| 2808 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2809 | .deviceId(stylusDeviceId1) |
| 2810 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(102)) |
| 2811 | .build()); |
| 2812 | // Subsequent stylus movements are delivered correctly |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2813 | window->assertNoEvents(); |
| 2814 | } |
| 2815 | |
| 2816 | /** |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2817 | * One window. Touch down on the window. Then, stylus down on the window from another device. |
| 2818 | * Ensure that is canceled, because stylus down should be preferred over touch. |
| 2819 | */ |
| 2820 | TEST_F(InputDispatcherMultiDeviceTest, TouchIsCanceledByStylusDown) { |
| 2821 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2822 | sp<FakeWindowHandle> window = |
| 2823 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 2824 | window->setFrame(Rect(0, 0, 200, 200)); |
| 2825 | |
| 2826 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 2827 | |
| 2828 | constexpr int32_t touchDeviceId = 4; |
| 2829 | constexpr int32_t stylusDeviceId = 2; |
| 2830 | |
| 2831 | // Touch down on window |
| 2832 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2833 | .deviceId(touchDeviceId) |
| 2834 | .pointer(PointerBuilder(0, ToolType::FINGER).x(140).y(145)) |
| 2835 | .build()); |
| 2836 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 2837 | .deviceId(touchDeviceId) |
| 2838 | .pointer(PointerBuilder(0, ToolType::FINGER).x(141).y(146)) |
| 2839 | .build()); |
| 2840 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2841 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 2842 | |
| 2843 | // Stylus down on the window |
| 2844 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 2845 | .deviceId(stylusDeviceId) |
| 2846 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(110)) |
| 2847 | .build()); |
| 2848 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 2849 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 2850 | |
| 2851 | // Subsequent stylus movements are delivered correctly |
| 2852 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 2853 | .deviceId(stylusDeviceId) |
| 2854 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(101).y(111)) |
| 2855 | .build()); |
| 2856 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId), |
| 2857 | WithCoords(101, 111))); |
| 2858 | } |
| 2859 | |
| 2860 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2861 | * Two windows: a window on the left and a window on the right. |
| 2862 | * Mouse is clicked on the left window and remains down. Touch is touched on the right and remains |
| 2863 | * down. Then, on the left window, also place second touch pointer down. |
| 2864 | * This test tries to reproduce a crash. |
| 2865 | * In the buggy implementation, second pointer down on the left window would cause a crash. |
| 2866 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2867 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceSplitTouch) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2868 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2869 | sp<FakeWindowHandle> leftWindow = |
| 2870 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2871 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2872 | |
| 2873 | sp<FakeWindowHandle> rightWindow = |
| 2874 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2875 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2876 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 2877 | mDispatcher->onWindowInfosChanged( |
| 2878 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2879 | |
| 2880 | const int32_t touchDeviceId = 4; |
| 2881 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2882 | |
| 2883 | // Start hovering over the left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2884 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2885 | .deviceId(mouseDeviceId) |
| 2886 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2887 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2888 | leftWindow->consumeMotionEvent( |
| 2889 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2890 | |
| 2891 | // Mouse down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2892 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 2893 | .deviceId(mouseDeviceId) |
| 2894 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2895 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2896 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2897 | |
| 2898 | leftWindow->consumeMotionEvent( |
| 2899 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 2900 | leftWindow->consumeMotionEvent( |
| 2901 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 2902 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2903 | mDispatcher->notifyMotion( |
| 2904 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 2905 | .deviceId(mouseDeviceId) |
| 2906 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2907 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 2908 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 2909 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2910 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 2911 | |
| 2912 | // First touch pointer down on right window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2913 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2914 | .deviceId(touchDeviceId) |
| 2915 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2916 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2917 | leftWindow->assertNoEvents(); |
| 2918 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2919 | rightWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 2920 | |
| 2921 | // Second touch pointer down on left window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 2922 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 2923 | .deviceId(touchDeviceId) |
| 2924 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 2925 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 2926 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2927 | // Since this is now a new splittable pointer going down on the left window, and it's coming |
| 2928 | // from a different device, the current gesture in the left window (pointer down) should first |
| 2929 | // be canceled. |
| 2930 | leftWindow->consumeMotionEvent( |
| 2931 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 2932 | leftWindow->consumeMotionEvent( |
| 2933 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 2934 | // This MOVE event is not necessary (doesn't carry any new information), but it's there in the |
| 2935 | // current implementation. |
| 2936 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{100, 100}}}; |
| 2937 | rightWindow->consumeMotionEvent( |
| 2938 | AllOf(WithMotionAction(ACTION_MOVE), WithPointers(expectedPointers))); |
| 2939 | |
| 2940 | leftWindow->assertNoEvents(); |
| 2941 | rightWindow->assertNoEvents(); |
| 2942 | } |
| 2943 | |
| 2944 | /** |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2945 | * Two windows: a window on the left and a window on the right. |
| 2946 | * Mouse is hovered on the left window and stylus is hovered on the right window. |
| 2947 | */ |
| 2948 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHover) { |
| 2949 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 2950 | sp<FakeWindowHandle> leftWindow = |
| 2951 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 2952 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 2953 | |
| 2954 | sp<FakeWindowHandle> rightWindow = |
| 2955 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 2956 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 2957 | |
| 2958 | mDispatcher->onWindowInfosChanged( |
| 2959 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 2960 | |
| 2961 | const int32_t stylusDeviceId = 3; |
| 2962 | const int32_t mouseDeviceId = 6; |
| 2963 | |
| 2964 | // Start hovering over the left window |
| 2965 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 2966 | .deviceId(mouseDeviceId) |
| 2967 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 2968 | .build()); |
| 2969 | leftWindow->consumeMotionEvent( |
| 2970 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 2971 | |
| 2972 | // Stylus hovered on right window |
| 2973 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 2974 | .deviceId(stylusDeviceId) |
| 2975 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(300).y(100)) |
| 2976 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2977 | rightWindow->consumeMotionEvent( |
| 2978 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 2979 | |
| 2980 | // Subsequent HOVER_MOVE events are dispatched correctly. |
| 2981 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 2982 | .deviceId(mouseDeviceId) |
| 2983 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(120)) |
| 2984 | .build()); |
| 2985 | leftWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2986 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(mouseDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2987 | |
| 2988 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 2989 | .deviceId(stylusDeviceId) |
| 2990 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(310).y(110)) |
| 2991 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2992 | rightWindow->consumeMotionEvent( |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 2993 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 2994 | |
| 2995 | leftWindow->assertNoEvents(); |
| 2996 | rightWindow->assertNoEvents(); |
| 2997 | } |
| 2998 | |
| 2999 | /** |
| 3000 | * Three windows: a window on the left and a window on the right. |
| 3001 | * And a spy window that's positioned above all of them. |
| 3002 | * Stylus down on the left window and remains down. Touch goes down on the right and remains down. |
| 3003 | * Check the stream that's received by the spy. |
| 3004 | */ |
| 3005 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceWithSpy) { |
| 3006 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3007 | |
| 3008 | sp<FakeWindowHandle> spyWindow = |
| 3009 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3010 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 3011 | spyWindow->setTrustedOverlay(true); |
| 3012 | spyWindow->setSpy(true); |
| 3013 | |
| 3014 | sp<FakeWindowHandle> leftWindow = |
| 3015 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 3016 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3017 | |
| 3018 | sp<FakeWindowHandle> rightWindow = |
| 3019 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 3020 | |
| 3021 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3022 | |
| 3023 | mDispatcher->onWindowInfosChanged( |
| 3024 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3025 | |
| 3026 | const int32_t stylusDeviceId = 1; |
| 3027 | const int32_t touchDeviceId = 2; |
| 3028 | |
| 3029 | // Stylus down on the left window |
| 3030 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3031 | .deviceId(stylusDeviceId) |
| 3032 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 3033 | .build()); |
| 3034 | leftWindow->consumeMotionEvent( |
| 3035 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 3036 | spyWindow->consumeMotionEvent( |
| 3037 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 3038 | |
| 3039 | // Touch down on the right window |
| 3040 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3041 | .deviceId(touchDeviceId) |
| 3042 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3043 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3044 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3045 | rightWindow->consumeMotionEvent( |
| 3046 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3047 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3048 | // Spy window does not receive touch events, because stylus events take precedence, and it |
| 3049 | // already has an active stylus gesture. |
| 3050 | |
| 3051 | // 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] | 3052 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 3053 | .deviceId(stylusDeviceId) |
| 3054 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 3055 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3056 | leftWindow->consumeMotionEvent( |
| 3057 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 3058 | spyWindow->consumeMotionEvent( |
| 3059 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3060 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3061 | // Further MOVE events keep going to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3062 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3063 | .deviceId(touchDeviceId) |
| 3064 | .pointer(PointerBuilder(0, ToolType::FINGER).x(310).y(110)) |
| 3065 | .build()); |
| 3066 | rightWindow->consumeMotionEvent( |
| 3067 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3068 | |
| 3069 | spyWindow->assertNoEvents(); |
| 3070 | leftWindow->assertNoEvents(); |
| 3071 | rightWindow->assertNoEvents(); |
| 3072 | } |
| 3073 | |
| 3074 | /** |
| 3075 | * Three windows: a window on the left, a window on the right, and a spy window positioned above |
| 3076 | * both. |
| 3077 | * Check hover in left window and touch down in the right window. |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3078 | * 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] | 3079 | * At the same time, left and right should be getting independent streams of hovering and touch, |
| 3080 | * respectively. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3081 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3082 | TEST_F(InputDispatcherMultiDeviceTest, MultiDeviceHoverBlocksTouchWithSpy) { |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3083 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3084 | |
| 3085 | sp<FakeWindowHandle> spyWindow = |
| 3086 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3087 | spyWindow->setFrame(Rect(0, 0, 400, 400)); |
| 3088 | spyWindow->setTrustedOverlay(true); |
| 3089 | spyWindow->setSpy(true); |
| 3090 | |
| 3091 | sp<FakeWindowHandle> leftWindow = |
| 3092 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 3093 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3094 | |
| 3095 | sp<FakeWindowHandle> rightWindow = |
| 3096 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 3097 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3098 | |
| 3099 | mDispatcher->onWindowInfosChanged( |
| 3100 | {{*spyWindow->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 3101 | |
| 3102 | const int32_t stylusDeviceId = 1; |
| 3103 | const int32_t touchDeviceId = 2; |
| 3104 | |
| 3105 | // Stylus hover on the left window |
| 3106 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3107 | .deviceId(stylusDeviceId) |
| 3108 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(100).y(100)) |
| 3109 | .build()); |
| 3110 | leftWindow->consumeMotionEvent( |
| 3111 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3112 | spyWindow->consumeMotionEvent( |
| 3113 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3114 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3115 | // Touch down on the right window. Spy doesn't receive this touch because it already has |
| 3116 | // stylus hovering there. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3117 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3118 | .deviceId(touchDeviceId) |
| 3119 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3120 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3121 | leftWindow->assertNoEvents(); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3122 | spyWindow->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3123 | rightWindow->consumeMotionEvent( |
| 3124 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3125 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3126 | // 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] | 3127 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3128 | .deviceId(stylusDeviceId) |
| 3129 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(110).y(110)) |
| 3130 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3131 | leftWindow->consumeMotionEvent( |
| 3132 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3133 | spyWindow->consumeMotionEvent( |
| 3134 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3135 | |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3136 | // Touch movements continue. They should be delivered to the right window only |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3137 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3138 | .deviceId(touchDeviceId) |
| 3139 | .pointer(PointerBuilder(0, ToolType::FINGER).x(301).y(101)) |
| 3140 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3141 | rightWindow->consumeMotionEvent( |
| 3142 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(touchDeviceId))); |
| 3143 | |
| 3144 | spyWindow->assertNoEvents(); |
| 3145 | leftWindow->assertNoEvents(); |
| 3146 | rightWindow->assertNoEvents(); |
| 3147 | } |
| 3148 | |
| 3149 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3150 | * On a single window, use two different devices: mouse and touch. |
| 3151 | * Touch happens first, with two pointers going down, and then the first pointer leaving. |
| 3152 | * Mouse is clicked next, which causes the touch stream to be aborted with ACTION_CANCEL. |
| 3153 | * Finally, a second touch pointer goes down again. Ensure the second touch pointer is ignored, |
| 3154 | * because the mouse is currently down, and a POINTER_DOWN event from the touchscreen does not |
| 3155 | * represent a new gesture. |
| 3156 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3157 | TEST_F(InputDispatcherMultiDeviceTest, MixedTouchAndMouseWithPointerDown) { |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3158 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3159 | sp<FakeWindowHandle> window = |
| 3160 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3161 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3162 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3163 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3164 | |
| 3165 | const int32_t touchDeviceId = 4; |
| 3166 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3167 | |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 3168 | // First touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3169 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3170 | .deviceId(touchDeviceId) |
| 3171 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3172 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3173 | // Second touch pointer down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3174 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3175 | .deviceId(touchDeviceId) |
| 3176 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3177 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3178 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3179 | // First touch pointer lifts. The second one remains down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3180 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3181 | .deviceId(touchDeviceId) |
| 3182 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3183 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3184 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3185 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3186 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3187 | window->consumeMotionEvent(WithMotionAction(POINTER_0_UP)); |
| 3188 | |
| 3189 | // Mouse down. The touch should be canceled |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3190 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3191 | .deviceId(mouseDeviceId) |
| 3192 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3193 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3194 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3195 | |
| 3196 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
Siarhei Vishniakou | 82dc042 | 2023-02-17 23:12:52 -0800 | [diff] [blame] | 3197 | WithPointerCount(1u))); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3198 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3199 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3200 | mDispatcher->notifyMotion( |
| 3201 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3202 | .deviceId(mouseDeviceId) |
| 3203 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3204 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3205 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(320).y(100)) |
| 3206 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3207 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3208 | |
| 3209 | // Second touch pointer down. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3210 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3211 | .deviceId(touchDeviceId) |
| 3212 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3213 | .pointer(PointerBuilder(1, ToolType::FINGER).x(350).y(100)) |
| 3214 | .build()); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3215 | // Since we already canceled this touch gesture, it will be ignored until a completely new |
| 3216 | // gesture is started. This is easier to implement than trying to keep track of the new pointer |
| 3217 | // and generating an ACTION_DOWN instead of ACTION_POINTER_DOWN. |
| 3218 | // However, mouse movements should continue to work. |
| 3219 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3220 | .deviceId(mouseDeviceId) |
| 3221 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3222 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(330).y(110)) |
| 3223 | .build()); |
| 3224 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(mouseDeviceId))); |
| 3225 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3226 | window->assertNoEvents(); |
| 3227 | } |
| 3228 | |
| 3229 | /** |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3230 | * Inject a touch down and then send a new event via 'notifyMotion'. Ensure the new event cancels |
| 3231 | * the injected event. |
| 3232 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3233 | TEST_F(InputDispatcherMultiDeviceTest, UnfinishedInjectedEvent) { |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3234 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3235 | sp<FakeWindowHandle> window = |
| 3236 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3237 | window->setFrame(Rect(0, 0, 400, 400)); |
| 3238 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3239 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3240 | |
| 3241 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3242 | // Pretend a test injects an ACTION_DOWN mouse event, but forgets to lift up the touch after |
| 3243 | // completion. |
| 3244 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3245 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3246 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3247 | .deviceId(ReservedInputDeviceId::VIRTUAL_KEYBOARD_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3248 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3249 | .build())); |
| 3250 | window->consumeMotionEvent( |
| 3251 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3252 | |
| 3253 | // Now a real touch comes. Rather than crashing or dropping the real event, the injected pointer |
| 3254 | // should be canceled and the new gesture should take over. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3255 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3256 | .deviceId(touchDeviceId) |
| 3257 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3258 | .build()); |
Siarhei Vishniakou | 56e7909 | 2023-02-21 19:13:16 -0800 | [diff] [blame] | 3259 | |
| 3260 | window->consumeMotionEvent( |
| 3261 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(VIRTUAL_KEYBOARD_ID))); |
| 3262 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3263 | } |
| 3264 | |
| 3265 | /** |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3266 | * This test is similar to the test above, but the sequence of injected events is different. |
| 3267 | * |
| 3268 | * Two windows: a window on the left and a window on the right. |
| 3269 | * Mouse is hovered over the left window. |
| 3270 | * Next, we tap on the left window, where the cursor was last seen. |
| 3271 | * |
| 3272 | * After that, we inject one finger down onto the right window, and then a second finger down onto |
| 3273 | * the left window. |
| 3274 | * The touch is split, so this last gesture should cause 2 ACTION_DOWN events, one in the right |
| 3275 | * window (first), and then another on the left window (second). |
| 3276 | * This test reproduces a crash where there is a mismatch between the downTime and eventTime. |
| 3277 | * In the buggy implementation, second finger down on the left window would cause a crash. |
| 3278 | */ |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3279 | TEST_F(InputDispatcherMultiDeviceTest, HoverTapAndSplitTouch) { |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3280 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3281 | sp<FakeWindowHandle> leftWindow = |
| 3282 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 3283 | leftWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3284 | |
| 3285 | sp<FakeWindowHandle> rightWindow = |
| 3286 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 3287 | rightWindow->setFrame(Rect(200, 0, 400, 200)); |
| 3288 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3289 | mDispatcher->onWindowInfosChanged( |
| 3290 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3291 | |
| 3292 | const int32_t mouseDeviceId = 6; |
| 3293 | const int32_t touchDeviceId = 4; |
| 3294 | // Hover over the left window. Keep the cursor there. |
| 3295 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3296 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3297 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 3298 | AINPUT_SOURCE_MOUSE) |
| 3299 | .deviceId(mouseDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3300 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3301 | .build())); |
| 3302 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 3303 | |
| 3304 | // Tap on left window |
| 3305 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3306 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3307 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3308 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3309 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3310 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3311 | .build())); |
| 3312 | |
| 3313 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3314 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3315 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3316 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3317 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3318 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3319 | .build())); |
| 3320 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3321 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3322 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_UP)); |
| 3323 | |
| 3324 | // First finger down on right window |
| 3325 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3326 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3327 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 3328 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3329 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3330 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3331 | .build())); |
| 3332 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3333 | |
| 3334 | // Second finger down on the left window |
| 3335 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3336 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3337 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3338 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3339 | .pointer(PointerBuilder(0, ToolType::FINGER).x(300).y(100)) |
| 3340 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 3341 | .build())); |
| 3342 | leftWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_DOWN)); |
| 3343 | rightWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_MOVE)); |
| 3344 | |
| 3345 | // No more events |
| 3346 | leftWindow->assertNoEvents(); |
| 3347 | rightWindow->assertNoEvents(); |
| 3348 | } |
| 3349 | |
| 3350 | /** |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3351 | * Start hovering with a stylus device, and then tap with a touch device. Ensure no crash occurs. |
| 3352 | * While the touch is down, new hover events from the stylus device should be ignored. After the |
| 3353 | * touch is gone, stylus hovering should start working again. |
| 3354 | */ |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3355 | TEST_F(InputDispatcherMultiDeviceTest, StylusHoverIgnoresTouchTap) { |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3356 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3357 | sp<FakeWindowHandle> window = |
| 3358 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3359 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3360 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3361 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3362 | |
| 3363 | const int32_t stylusDeviceId = 5; |
| 3364 | const int32_t touchDeviceId = 4; |
| 3365 | // Start hovering with stylus |
| 3366 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3367 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3368 | MotionEventBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3369 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3370 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3371 | .build())); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3372 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3373 | |
| 3374 | // Finger down on the window |
| 3375 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3376 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 3377 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3378 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3379 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3380 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3381 | // The touch device should be ignored! |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3382 | |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 3383 | // Continue hovering with stylus. |
| 3384 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3385 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3386 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3387 | AINPUT_SOURCE_STYLUS) |
| 3388 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3389 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(60).y(60)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3390 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3391 | // Hovers continue to work |
| 3392 | window->consumeMotionEvent( |
| 3393 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3394 | |
| 3395 | // Lift up the finger |
| 3396 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3397 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3398 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 3399 | AINPUT_SOURCE_TOUCHSCREEN) |
| 3400 | .deviceId(touchDeviceId) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3401 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3402 | .build())); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3403 | |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3404 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3405 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3406 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3407 | AINPUT_SOURCE_STYLUS) |
| 3408 | .deviceId(stylusDeviceId) |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 3409 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(70).y(70)) |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3410 | .build())); |
Siarhei Vishniakou | 3ad54f5 | 2023-11-02 16:54:40 -0700 | [diff] [blame] | 3411 | window->consumeMotionEvent( |
| 3412 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 2e3e443 | 2023-02-09 18:34:11 -0800 | [diff] [blame] | 3413 | window->assertNoEvents(); |
| 3414 | } |
| 3415 | |
| 3416 | /** |
Siarhei Vishniakou | f77f60a | 2023-10-23 17:26:05 -0700 | [diff] [blame] | 3417 | * If stylus is down anywhere on the screen, then touches should not be delivered to windows that |
| 3418 | * have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 3419 | * |
| 3420 | * Two windows: one on the left and one on the right. |
| 3421 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 3422 | * Stylus down on the left window, and then touch down on the right window. |
| 3423 | * Check that the right window doesn't get touches while the stylus is down on the left window. |
| 3424 | */ |
| 3425 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusDownBlocksTouch) { |
| 3426 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3427 | sp<FakeWindowHandle> leftWindow = |
| 3428 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
| 3429 | ADISPLAY_ID_DEFAULT); |
| 3430 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3431 | |
| 3432 | sp<FakeWindowHandle> sbtRightWindow = |
| 3433 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3434 | "Stylus blocks touch (right) window", ADISPLAY_ID_DEFAULT); |
| 3435 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3436 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 3437 | |
| 3438 | mDispatcher->onWindowInfosChanged( |
| 3439 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 3440 | |
| 3441 | const int32_t stylusDeviceId = 5; |
| 3442 | const int32_t touchDeviceId = 4; |
| 3443 | |
| 3444 | // Stylus down in the left window |
| 3445 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3446 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 3447 | .deviceId(stylusDeviceId) |
| 3448 | .build()); |
| 3449 | leftWindow->consumeMotionEvent( |
| 3450 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 3451 | |
| 3452 | // Finger tap on the right window |
| 3453 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3454 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3455 | .deviceId(touchDeviceId) |
| 3456 | .build()); |
| 3457 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3458 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3459 | .deviceId(touchDeviceId) |
| 3460 | .build()); |
| 3461 | |
| 3462 | // The touch should be blocked, because stylus is down somewhere else on screen! |
| 3463 | sbtRightWindow->assertNoEvents(); |
| 3464 | |
| 3465 | // Continue stylus motion, and ensure it's not impacted. |
| 3466 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 3467 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3468 | .deviceId(stylusDeviceId) |
| 3469 | .build()); |
| 3470 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3471 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3472 | .deviceId(stylusDeviceId) |
| 3473 | .build()); |
| 3474 | leftWindow->consumeMotionEvent( |
| 3475 | AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
| 3476 | leftWindow->consumeMotionEvent( |
| 3477 | AllOf(WithMotionAction(ACTION_UP), WithDeviceId(stylusDeviceId))); |
| 3478 | |
| 3479 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3480 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3481 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3482 | .deviceId(touchDeviceId) |
| 3483 | .build()); |
| 3484 | sbtRightWindow->consumeMotionEvent( |
| 3485 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3486 | } |
| 3487 | |
| 3488 | /** |
| 3489 | * If stylus is hovering anywhere on the screen, then touches should not be delivered to windows |
| 3490 | * that have InputConfig::GLOBAL_STYLUS_BLOCKS_TOUCH. |
| 3491 | * |
| 3492 | * Two windows: one on the left and one on the right. |
| 3493 | * The window on the right has GLOBAL_STYLUS_BLOCKS_TOUCH config. |
| 3494 | * Stylus hover on the left window, and then touch down on the right window. |
| 3495 | * Check that the right window doesn't get touches while the stylus is hovering on the left window. |
| 3496 | */ |
| 3497 | TEST_F(InputDispatcherMultiDeviceTest, GlobalStylusHoverBlocksTouch) { |
| 3498 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3499 | sp<FakeWindowHandle> leftWindow = |
| 3500 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left window", |
| 3501 | ADISPLAY_ID_DEFAULT); |
| 3502 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 3503 | |
| 3504 | sp<FakeWindowHandle> sbtRightWindow = |
| 3505 | sp<FakeWindowHandle>::make(application, mDispatcher, |
| 3506 | "Stylus blocks touch (right) window", ADISPLAY_ID_DEFAULT); |
| 3507 | sbtRightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 3508 | sbtRightWindow->setGlobalStylusBlocksTouch(true); |
| 3509 | |
| 3510 | mDispatcher->onWindowInfosChanged( |
| 3511 | {{*leftWindow->getInfo(), *sbtRightWindow->getInfo()}, {}, 0, 0}); |
| 3512 | |
| 3513 | const int32_t stylusDeviceId = 5; |
| 3514 | const int32_t touchDeviceId = 4; |
| 3515 | |
| 3516 | // Stylus hover in the left window |
| 3517 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3518 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(52)) |
| 3519 | .deviceId(stylusDeviceId) |
| 3520 | .build()); |
| 3521 | leftWindow->consumeMotionEvent( |
| 3522 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(stylusDeviceId))); |
| 3523 | |
| 3524 | // Finger tap on the right window |
| 3525 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3526 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3527 | .deviceId(touchDeviceId) |
| 3528 | .build()); |
| 3529 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 3530 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(151)) |
| 3531 | .deviceId(touchDeviceId) |
| 3532 | .build()); |
| 3533 | |
| 3534 | // The touch should be blocked, because stylus is hovering somewhere else on screen! |
| 3535 | sbtRightWindow->assertNoEvents(); |
| 3536 | |
| 3537 | // Continue stylus motion, and ensure it's not impacted. |
| 3538 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 3539 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3540 | .deviceId(stylusDeviceId) |
| 3541 | .build()); |
| 3542 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3543 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(53)) |
| 3544 | .deviceId(stylusDeviceId) |
| 3545 | .build()); |
| 3546 | leftWindow->consumeMotionEvent( |
| 3547 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithDeviceId(stylusDeviceId))); |
| 3548 | leftWindow->consumeMotionEvent( |
| 3549 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(stylusDeviceId))); |
| 3550 | |
| 3551 | // Now that the stylus gesture is done, touches should be getting delivered correctly. |
| 3552 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3553 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(153)) |
| 3554 | .deviceId(touchDeviceId) |
| 3555 | .build()); |
| 3556 | sbtRightWindow->consumeMotionEvent( |
| 3557 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 3558 | } |
| 3559 | |
| 3560 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3561 | * A spy window above a window with no input channel. |
| 3562 | * Start hovering with a stylus device, and then tap with it. |
| 3563 | * Ensure spy window receives the entire sequence. |
| 3564 | */ |
| 3565 | TEST_F(InputDispatcherTest, StylusHoverAndDownNoInputChannel) { |
| 3566 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3567 | sp<FakeWindowHandle> spyWindow = |
| 3568 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3569 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3570 | spyWindow->setTrustedOverlay(true); |
| 3571 | spyWindow->setSpy(true); |
| 3572 | sp<FakeWindowHandle> window = |
| 3573 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3574 | window->setNoInputChannel(true); |
| 3575 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3576 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3577 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3578 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3579 | // Start hovering with stylus |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3580 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3581 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3582 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3583 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3584 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3585 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3586 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3587 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3588 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3589 | |
| 3590 | // Stylus touches down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3591 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 3592 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3593 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3594 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3595 | |
| 3596 | // Stylus goes up |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3597 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 3598 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3599 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3600 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 3601 | |
| 3602 | // Again hover |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3603 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3604 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3605 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3606 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3607 | // Stop hovering |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3608 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3609 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3610 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3611 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3612 | |
| 3613 | // No more events |
| 3614 | spyWindow->assertNoEvents(); |
| 3615 | window->assertNoEvents(); |
| 3616 | } |
| 3617 | |
| 3618 | /** |
Siarhei Vishniakou | 6b71b63 | 2023-10-27 21:34:46 -0700 | [diff] [blame] | 3619 | * A stale stylus HOVER_EXIT event is injected. Since it's a stale event, it should generally be |
| 3620 | * rejected. But since we already have an ongoing gesture, this event should be processed. |
| 3621 | * This prevents inconsistent events being handled inside the dispatcher. |
| 3622 | */ |
| 3623 | TEST_F(InputDispatcherTest, StaleStylusHoverGestureIsComplete) { |
| 3624 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3625 | |
| 3626 | sp<FakeWindowHandle> window = |
| 3627 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3628 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3629 | |
| 3630 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 3631 | |
| 3632 | // Start hovering with stylus |
| 3633 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3634 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3635 | .build()); |
| 3636 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3637 | |
| 3638 | NotifyMotionArgs hoverExit = MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 3639 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 3640 | .build(); |
| 3641 | // Make this 'hoverExit' event stale |
| 3642 | mFakePolicy->setStaleEventTimeout(100ms); |
| 3643 | std::this_thread::sleep_for(100ms); |
| 3644 | |
| 3645 | // It shouldn't be dropped by the dispatcher, even though it's stale. |
| 3646 | mDispatcher->notifyMotion(hoverExit); |
| 3647 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3648 | |
| 3649 | // Stylus starts hovering again! There should be no crash. |
| 3650 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 3651 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(51)) |
| 3652 | .build()); |
| 3653 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 3654 | } |
| 3655 | |
| 3656 | /** |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3657 | * Start hovering with a mouse, and then tap with a touch device. Pilfer the touch stream. |
| 3658 | * Next, click with the mouse device. Both windows (spy and regular) should receive the new mouse |
| 3659 | * ACTION_DOWN event because that's a new gesture, and pilfering should no longer be active. |
| 3660 | * While the mouse is down, new move events from the touch device should be ignored. |
| 3661 | */ |
| 3662 | TEST_F(InputDispatcherTest, TouchPilferAndMouseMove) { |
| 3663 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3664 | sp<FakeWindowHandle> spyWindow = |
| 3665 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 3666 | spyWindow->setFrame(Rect(0, 0, 200, 200)); |
| 3667 | spyWindow->setTrustedOverlay(true); |
| 3668 | spyWindow->setSpy(true); |
| 3669 | sp<FakeWindowHandle> window = |
| 3670 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3671 | window->setFrame(Rect(0, 0, 200, 200)); |
| 3672 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3673 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3674 | |
| 3675 | const int32_t mouseDeviceId = 7; |
| 3676 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3677 | |
| 3678 | // Hover a bit with mouse first |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3679 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 3680 | .deviceId(mouseDeviceId) |
| 3681 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3682 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3683 | spyWindow->consumeMotionEvent( |
| 3684 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3685 | window->consumeMotionEvent( |
| 3686 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 3687 | |
| 3688 | // Start touching |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3689 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3690 | .deviceId(touchDeviceId) |
| 3691 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 3692 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3693 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3694 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3695 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3696 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3697 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3698 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3699 | .deviceId(touchDeviceId) |
| 3700 | .pointer(PointerBuilder(0, ToolType::FINGER).x(55).y(55)) |
| 3701 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3702 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3703 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3704 | |
| 3705 | // Pilfer the stream |
| 3706 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 3707 | window->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 3708 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3709 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3710 | .deviceId(touchDeviceId) |
| 3711 | .pointer(PointerBuilder(0, ToolType::FINGER).x(60).y(60)) |
| 3712 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3713 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3714 | |
| 3715 | // Mouse down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3716 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3717 | .deviceId(mouseDeviceId) |
| 3718 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3719 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3720 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3721 | |
| 3722 | spyWindow->consumeMotionEvent( |
| 3723 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 3724 | spyWindow->consumeMotionEvent( |
| 3725 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3726 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3727 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3728 | mDispatcher->notifyMotion( |
| 3729 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3730 | .deviceId(mouseDeviceId) |
| 3731 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3732 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3733 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
| 3734 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3735 | spyWindow->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3736 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 3737 | |
| 3738 | // Mouse move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3739 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 3740 | .deviceId(mouseDeviceId) |
| 3741 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3742 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
| 3743 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3744 | spyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3745 | window->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 3746 | |
| 3747 | // Touch move! |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3748 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 3749 | .deviceId(touchDeviceId) |
| 3750 | .pointer(PointerBuilder(0, ToolType::FINGER).x(65).y(65)) |
| 3751 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3752 | |
| 3753 | // No more events |
| 3754 | spyWindow->assertNoEvents(); |
| 3755 | window->assertNoEvents(); |
| 3756 | } |
| 3757 | |
| 3758 | /** |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3759 | * On the display, have a single window, and also an area where there's no window. |
| 3760 | * First pointer touches the "no window" area of the screen. Second pointer touches the window. |
| 3761 | * Make sure that the window receives the second pointer, and first pointer is simply ignored. |
| 3762 | */ |
| 3763 | TEST_F(InputDispatcherTest, SplitWorksWhenEmptyAreaIsTouched) { |
| 3764 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3765 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3766 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3767 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3768 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3769 | |
| 3770 | // Touch down on the empty space |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3771 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{-1, -1}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3772 | |
| 3773 | mDispatcher->waitForIdle(); |
| 3774 | window->assertNoEvents(); |
| 3775 | |
| 3776 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3777 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{-1, -1}, {10, 10}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3778 | mDispatcher->waitForIdle(); |
| 3779 | window->consumeMotionDown(); |
| 3780 | } |
| 3781 | |
| 3782 | /** |
| 3783 | * Same test as above, but instead of touching the empty space, the first touch goes to |
| 3784 | * non-touchable window. |
| 3785 | */ |
| 3786 | TEST_F(InputDispatcherTest, SplitWorksWhenNonTouchableWindowIsTouched) { |
| 3787 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3788 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3789 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3790 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3791 | window1->setTouchable(false); |
| 3792 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3793 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3794 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3795 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3796 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3797 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3798 | // Touch down on the non-touchable window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3799 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3800 | |
| 3801 | mDispatcher->waitForIdle(); |
| 3802 | window1->assertNoEvents(); |
| 3803 | window2->assertNoEvents(); |
| 3804 | |
| 3805 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3806 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 3807 | mDispatcher->waitForIdle(); |
| 3808 | window2->consumeMotionDown(); |
| 3809 | } |
| 3810 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3811 | /** |
| 3812 | * When splitting touch events the downTime should be adjusted such that the downTime corresponds |
| 3813 | * to the event time of the first ACTION_DOWN sent to the particular window. |
| 3814 | */ |
| 3815 | TEST_F(InputDispatcherTest, SplitTouchesSendCorrectActionDownTime) { |
| 3816 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3817 | sp<FakeWindowHandle> window1 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3818 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window1", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3819 | window1->setTouchableRegion(Region{{0, 0, 100, 100}}); |
| 3820 | sp<FakeWindowHandle> window2 = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3821 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window2", DISPLAY_ID); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3822 | window2->setTouchableRegion(Region{{100, 0, 200, 100}}); |
| 3823 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3824 | mDispatcher->onWindowInfosChanged({{*window1->getInfo(), *window2->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3825 | |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3826 | // Touch down on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3827 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_DOWN, {{50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3828 | |
| 3829 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3830 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 3831 | std::unique_ptr<MotionEvent> motionEvent1 = window1->consumeMotionEvent(); |
| 3832 | ASSERT_NE(nullptr, motionEvent1); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3833 | window2->assertNoEvents(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 3834 | nsecs_t downTimeForWindow1 = motionEvent1->getDownTime(); |
| 3835 | ASSERT_EQ(motionEvent1->getDownTime(), motionEvent1->getEventTime()); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3836 | |
| 3837 | // Now touch down on the window with another pointer |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3838 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_1_DOWN, {{50, 50}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3839 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 3840 | std::unique_ptr<MotionEvent> motionEvent2 = window2->consumeMotionEvent(); |
| 3841 | ASSERT_NE(nullptr, motionEvent2); |
| 3842 | nsecs_t downTimeForWindow2 = motionEvent2->getDownTime(); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3843 | ASSERT_NE(downTimeForWindow1, downTimeForWindow2); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 3844 | ASSERT_EQ(motionEvent2->getDownTime(), motionEvent2->getEventTime()); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3845 | |
| 3846 | // Now move the pointer on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3847 | mDispatcher->notifyMotion(generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{50, 50}, {151, 51}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3848 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3849 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3850 | |
| 3851 | // Now add new touch down on the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3852 | mDispatcher->notifyMotion(generateTouchArgs(POINTER_2_DOWN, {{50, 50}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3853 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3854 | window2->consumeMotionEvent(WithDownTime(downTimeForWindow2)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3855 | |
| 3856 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 3857 | window1->consumeMotionMove(); |
| 3858 | window1->assertNoEvents(); |
| 3859 | |
| 3860 | // Now move the pointer on the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3861 | mDispatcher->notifyMotion( |
| 3862 | generateTouchArgs(AMOTION_EVENT_ACTION_MOVE, {{51, 51}, {151, 51}, {150, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3863 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3864 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3865 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3866 | mDispatcher->notifyMotion( |
| 3867 | generateTouchArgs(POINTER_3_DOWN, {{51, 51}, {151, 51}, {150, 50}, {50, 50}})); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3868 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | e9349e7 | 2022-12-02 11:39:20 -0800 | [diff] [blame] | 3869 | window1->consumeMotionEvent(WithDownTime(downTimeForWindow1)); |
Vaibhav Devmurari | 882bd9b | 2022-06-23 14:54:54 +0000 | [diff] [blame] | 3870 | } |
| 3871 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3872 | TEST_F(InputDispatcherTest, HoverMoveEnterMouseClickAndHoverMoveExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 3873 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3874 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3875 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3876 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3877 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 3878 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3879 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3880 | |
| 3881 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 3882 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3883 | mDispatcher->onWindowInfosChanged( |
| 3884 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3885 | |
| 3886 | // 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] | 3887 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3888 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3889 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3890 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3891 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3892 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3893 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3894 | |
| 3895 | // Move cursor into left window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3896 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3897 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3898 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3899 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3900 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3901 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3902 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 3903 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3904 | |
| 3905 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3906 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3907 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3908 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3909 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3910 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3911 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3912 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 3913 | windowLeft->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3914 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3915 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3916 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3917 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 3918 | AINPUT_SOURCE_MOUSE) |
| 3919 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3920 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3921 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3922 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3923 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3924 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3925 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3926 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3927 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 3928 | AINPUT_SOURCE_MOUSE) |
| 3929 | .buttonState(0) |
| 3930 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3931 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3932 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3933 | windowLeft->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3934 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3935 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3936 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3937 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 3938 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3939 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3940 | .build())); |
| 3941 | windowLeft->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 3942 | |
| 3943 | // Move mouse cursor back to right window |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 3944 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 3945 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3946 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 3947 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 3948 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(900).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 3949 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3950 | windowRight->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 3951 | |
| 3952 | // No more events |
| 3953 | windowLeft->assertNoEvents(); |
| 3954 | windowRight->assertNoEvents(); |
| 3955 | } |
| 3956 | |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3957 | /** |
| 3958 | * Put two fingers down (and don't release them) and click the mouse button. |
| 3959 | * The clicking of mouse is a new ACTION_DOWN event. Since it's from a different device, the |
| 3960 | * currently active gesture should be canceled, and the new one should proceed. |
| 3961 | */ |
| 3962 | TEST_F(InputDispatcherTest, TwoPointersDownMouseClick) { |
| 3963 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 3964 | sp<FakeWindowHandle> window = |
| 3965 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 3966 | window->setFrame(Rect(0, 0, 600, 800)); |
| 3967 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 3968 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3969 | |
| 3970 | const int32_t touchDeviceId = 4; |
| 3971 | const int32_t mouseDeviceId = 6; |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3972 | |
| 3973 | // Two pointers down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3974 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3975 | .deviceId(touchDeviceId) |
| 3976 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3977 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3978 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3979 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 3980 | .deviceId(touchDeviceId) |
| 3981 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 3982 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 3983 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3984 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 3985 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 3986 | |
| 3987 | // Inject a series of mouse events for a mouse click |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3988 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 3989 | .deviceId(mouseDeviceId) |
| 3990 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 3991 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 3992 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 3993 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId), |
| 3994 | WithPointerCount(2u))); |
| 3995 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(mouseDeviceId))); |
| 3996 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 3997 | mDispatcher->notifyMotion( |
| 3998 | MotionArgsBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, AINPUT_SOURCE_MOUSE) |
| 3999 | .deviceId(mouseDeviceId) |
| 4000 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4001 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4002 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4003 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4004 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
| 4005 | |
| 4006 | // Try to send more touch events while the mouse is down. Since it's a continuation of an |
| 4007 | // already canceled gesture, it should be ignored. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4008 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 4009 | .deviceId(touchDeviceId) |
| 4010 | .pointer(PointerBuilder(0, ToolType::FINGER).x(101).y(101)) |
| 4011 | .pointer(PointerBuilder(1, ToolType::FINGER).x(121).y(121)) |
| 4012 | .build()); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4013 | window->assertNoEvents(); |
| 4014 | } |
| 4015 | |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4016 | TEST_F(InputDispatcherTest, HoverWithSpyWindows) { |
| 4017 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4018 | |
| 4019 | sp<FakeWindowHandle> spyWindow = |
| 4020 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 4021 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4022 | spyWindow->setTrustedOverlay(true); |
| 4023 | spyWindow->setSpy(true); |
| 4024 | sp<FakeWindowHandle> window = |
| 4025 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4026 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4027 | |
| 4028 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4029 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4030 | |
| 4031 | // Send mouse cursor to the window |
| 4032 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4033 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4034 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4035 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4036 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4037 | .build())); |
| 4038 | |
| 4039 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4040 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4041 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4042 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4043 | |
| 4044 | window->assertNoEvents(); |
| 4045 | spyWindow->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4046 | } |
| 4047 | |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4048 | TEST_F(InputDispatcherTest, MouseAndTouchWithSpyWindows) { |
| 4049 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4050 | |
| 4051 | sp<FakeWindowHandle> spyWindow = |
| 4052 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 4053 | spyWindow->setFrame(Rect(0, 0, 600, 800)); |
| 4054 | spyWindow->setTrustedOverlay(true); |
| 4055 | spyWindow->setSpy(true); |
| 4056 | sp<FakeWindowHandle> window = |
| 4057 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4058 | window->setFrame(Rect(0, 0, 600, 800)); |
| 4059 | |
| 4060 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4061 | mDispatcher->onWindowInfosChanged({{*spyWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4062 | |
| 4063 | // Send mouse cursor to the window |
| 4064 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4065 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4066 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4067 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4068 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(100)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4069 | .build())); |
| 4070 | |
| 4071 | // Move mouse cursor |
| 4072 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4073 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4074 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4075 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4076 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(110).y(110)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4077 | .build())); |
| 4078 | |
| 4079 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4080 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4081 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4082 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4083 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4084 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4085 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4086 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4087 | // Touch down on the window |
| 4088 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4089 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4090 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 4091 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4092 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4093 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4094 | .build())); |
| 4095 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4096 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4097 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4098 | WithSource(AINPUT_SOURCE_MOUSE))); |
| 4099 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4100 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4101 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4102 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4103 | |
| 4104 | // pilfer the motion, retaining the gesture on the spy window. |
| 4105 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindow->getToken())); |
| 4106 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL), |
| 4107 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4108 | |
| 4109 | // Touch UP on the window |
| 4110 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4111 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4112 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 4113 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4114 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4115 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(200)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4116 | .build())); |
| 4117 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4118 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4119 | |
| 4120 | // Previously, a touch was pilfered. However, that gesture was just finished. Now, we are going |
| 4121 | // to send a new gesture. It should again go to both windows (spy and the window below), just |
| 4122 | // like the first gesture did, before pilfering. The window configuration has not changed. |
| 4123 | |
| 4124 | // One more tap - DOWN |
| 4125 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4126 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4127 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 4128 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4129 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4130 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4131 | .build())); |
| 4132 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4133 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4134 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4135 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4136 | |
| 4137 | // Touch UP on the window |
| 4138 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4139 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4140 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 4141 | AINPUT_SOURCE_TOUCHSCREEN) |
| 4142 | .deviceId(SECOND_DEVICE_ID) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4143 | .pointer(PointerBuilder(0, ToolType::FINGER).x(250).y(250)) |
Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 4144 | .build())); |
| 4145 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4146 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4147 | spyWindow->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4148 | WithSource(AINPUT_SOURCE_TOUCHSCREEN))); |
| 4149 | |
| 4150 | window->assertNoEvents(); |
| 4151 | spyWindow->assertNoEvents(); |
| 4152 | } |
| 4153 | |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4154 | // This test is different from the test above that HOVER_ENTER and HOVER_EXIT events are injected |
| 4155 | // directly in this test. |
| 4156 | TEST_F(InputDispatcherTest, HoverEnterMouseClickAndHoverExit) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4157 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4158 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4159 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4160 | window->setFrame(Rect(0, 0, 1200, 800)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4161 | |
| 4162 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4163 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4164 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4165 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4166 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4167 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4168 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4169 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4170 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4171 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4172 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4173 | // Inject a series of mouse events for a mouse click |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4174 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4175 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4176 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4177 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4178 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4179 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4180 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4181 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4182 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4183 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4184 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4185 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_PRESS, |
| 4186 | AINPUT_SOURCE_MOUSE) |
| 4187 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 4188 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4189 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4190 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4191 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4192 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4193 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4194 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4195 | MotionEventBuilder(AMOTION_EVENT_ACTION_BUTTON_RELEASE, |
| 4196 | AINPUT_SOURCE_MOUSE) |
| 4197 | .buttonState(0) |
| 4198 | .actionButton(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4199 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4200 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4201 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE)); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4202 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4203 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4204 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4205 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4206 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4207 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4208 | .build())); |
| 4209 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 4210 | |
Siarhei Vishniakou | a235c04 | 2023-05-02 09:59:09 -0700 | [diff] [blame] | 4211 | // We already canceled the hovering implicitly by injecting the "DOWN" event without lifting the |
| 4212 | // hover first. Therefore, injection of HOVER_EXIT is inconsistent, and should fail. |
| 4213 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4214 | injectMotionEvent(*mDispatcher, |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4215 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_EXIT, |
| 4216 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4217 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4218 | .build())); |
Siarhei Vishniakou | f372b81 | 2023-02-14 18:06:51 -0800 | [diff] [blame] | 4219 | window->assertNoEvents(); |
Garfield Tan | df26e86 | 2020-07-01 20:18:19 -0700 | [diff] [blame] | 4220 | } |
| 4221 | |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4222 | /** |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4223 | * Hover over a window, and then remove that window. Make sure that HOVER_EXIT for that event |
| 4224 | * is generated. |
| 4225 | */ |
| 4226 | TEST_F(InputDispatcherTest, HoverExitIsSentToRemovedWindow) { |
| 4227 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4228 | sp<FakeWindowHandle> window = |
| 4229 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4230 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 4231 | |
| 4232 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4233 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4234 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4235 | |
| 4236 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4237 | injectMotionEvent(*mDispatcher, |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4238 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, |
| 4239 | AINPUT_SOURCE_MOUSE) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4240 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4241 | .build())); |
| 4242 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4243 | |
| 4244 | // Remove the window, but keep the channel. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4245 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4246 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)); |
| 4247 | } |
| 4248 | |
| 4249 | /** |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4250 | * Test that invalid HOVER events sent by accessibility do not cause a fatal crash. |
| 4251 | */ |
Ameer Armaly | cff4fa5 | 2023-10-04 23:45:11 +0000 | [diff] [blame] | 4252 | TEST_F_WITH_FLAGS(InputDispatcherTest, InvalidA11yHoverStreamDoesNotCrash, |
| 4253 | REQUIRES_FLAGS_DISABLED(ACONFIG_FLAG(com::android::input::flags, |
| 4254 | a11y_crash_on_inconsistent_event_stream))) { |
Daniel Norman | 7487dfa | 2023-08-02 16:39:45 -0700 | [diff] [blame] | 4255 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4256 | sp<FakeWindowHandle> window = |
| 4257 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4258 | window->setFrame(Rect(0, 0, 1200, 800)); |
| 4259 | |
| 4260 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4261 | |
| 4262 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 4263 | |
| 4264 | MotionEventBuilder hoverEnterBuilder = |
| 4265 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4266 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(400)) |
| 4267 | .addFlag(AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 4268 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4269 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 4270 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4271 | injectMotionEvent(*mDispatcher, hoverEnterBuilder.build())); |
| 4272 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4273 | window->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
| 4274 | } |
| 4275 | |
| 4276 | /** |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4277 | * If mouse is hovering when the touch goes down, the hovering should be stopped via HOVER_EXIT. |
| 4278 | */ |
| 4279 | TEST_F(InputDispatcherTest, TouchDownAfterMouseHover) { |
| 4280 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4281 | sp<FakeWindowHandle> window = |
| 4282 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4283 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4284 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4285 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4286 | |
| 4287 | const int32_t mouseDeviceId = 7; |
| 4288 | const int32_t touchDeviceId = 4; |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4289 | |
| 4290 | // Start hovering with the mouse |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4291 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 4292 | .deviceId(mouseDeviceId) |
| 4293 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(10)) |
| 4294 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4295 | window->consumeMotionEvent( |
| 4296 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), WithDeviceId(mouseDeviceId))); |
| 4297 | |
| 4298 | // Touch goes down |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4299 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4300 | .deviceId(touchDeviceId) |
| 4301 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4302 | .build()); |
Siarhei Vishniakou | 4e1ffa5 | 2023-02-21 11:50:34 -0800 | [diff] [blame] | 4303 | |
| 4304 | window->consumeMotionEvent( |
| 4305 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithDeviceId(mouseDeviceId))); |
| 4306 | window->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
| 4307 | } |
| 4308 | |
| 4309 | /** |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4310 | * Inject a mouse hover event followed by a tap from touchscreen. |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4311 | * The tap causes a HOVER_EXIT event to be generated because the current event |
| 4312 | * stream's source has been switched. |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4313 | */ |
| 4314 | TEST_F(InputDispatcherTest, MouseHoverAndTouchTap) { |
| 4315 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4316 | sp<FakeWindowHandle> window = |
| 4317 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4318 | window->setFrame(Rect(0, 0, 100, 100)); |
| 4319 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4320 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4321 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 4322 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(50).y(50)) |
| 4323 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4324 | ASSERT_NO_FATAL_FAILURE( |
| 4325 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER), |
| 4326 | WithSource(AINPUT_SOURCE_MOUSE)))); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4327 | |
| 4328 | // Tap on the window |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4329 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4330 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4331 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4332 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 4333 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT), |
| 4334 | WithSource(AINPUT_SOURCE_MOUSE)))); |
| 4335 | |
| 4336 | ASSERT_NO_FATAL_FAILURE( |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4337 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), |
| 4338 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4339 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4340 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4341 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 4342 | .build()); |
Siarhei Vishniakou | 0b0374d | 2022-11-17 17:40:53 -0800 | [diff] [blame] | 4343 | ASSERT_NO_FATAL_FAILURE( |
| 4344 | window->consumeMotionEvent(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), |
| 4345 | WithSource(AINPUT_SOURCE_TOUCHSCREEN)))); |
| 4346 | } |
| 4347 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4348 | TEST_F(InputDispatcherTest, HoverEnterMoveRemoveWindowsInSecondDisplay) { |
| 4349 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4350 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 4351 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 4352 | ADISPLAY_ID_DEFAULT); |
| 4353 | windowDefaultDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 4354 | sp<FakeWindowHandle> windowSecondDisplay = |
| 4355 | sp<FakeWindowHandle>::make(application, mDispatcher, "SecondDisplay", |
| 4356 | SECOND_DISPLAY_ID); |
| 4357 | windowSecondDisplay->setFrame(Rect(0, 0, 600, 800)); |
| 4358 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4359 | mDispatcher->onWindowInfosChanged( |
| 4360 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4361 | |
| 4362 | // Set cursor position in window in default display and check that hover enter and move |
| 4363 | // events are generated. |
| 4364 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4365 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4366 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4367 | AINPUT_SOURCE_MOUSE) |
| 4368 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4369 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(300).y(600)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4370 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4371 | windowDefaultDisplay->consumeMotionEvent(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4372 | |
| 4373 | // Remove all windows in secondary display and check that no event happens on window in |
| 4374 | // primary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4375 | mDispatcher->onWindowInfosChanged({{*windowDefaultDisplay->getInfo()}, {}, 0, 0}); |
| 4376 | |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4377 | windowDefaultDisplay->assertNoEvents(); |
| 4378 | |
| 4379 | // Move cursor position in window in default display and check that only hover move |
| 4380 | // event is generated and not hover enter event. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4381 | mDispatcher->onWindowInfosChanged( |
| 4382 | {{*windowDefaultDisplay->getInfo(), *windowSecondDisplay->getInfo()}, {}, 0, 0}); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4383 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4384 | injectMotionEvent(*mDispatcher, |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4385 | MotionEventBuilder(AMOTION_EVENT_ACTION_HOVER_MOVE, |
| 4386 | AINPUT_SOURCE_MOUSE) |
| 4387 | .displayId(ADISPLAY_ID_DEFAULT) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4388 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(400).y(700)) |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4389 | .build())); |
Siarhei Vishniakou | 5cee1e3 | 2022-11-29 12:35:39 -0800 | [diff] [blame] | 4390 | windowDefaultDisplay->consumeMotionEvent( |
| 4391 | AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE), |
| 4392 | WithSource(AINPUT_SOURCE_MOUSE))); |
Tommy Nordgren | dae9dfc | 2022-10-13 11:25:57 +0200 | [diff] [blame] | 4393 | windowDefaultDisplay->assertNoEvents(); |
| 4394 | } |
| 4395 | |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4396 | TEST_F(InputDispatcherTest, DispatchMouseEventsUnderCursor) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4397 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4398 | |
| 4399 | sp<FakeWindowHandle> windowLeft = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4400 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4401 | windowLeft->setFrame(Rect(0, 0, 600, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4402 | sp<FakeWindowHandle> windowRight = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4403 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4404 | windowRight->setFrame(Rect(600, 0, 1200, 800)); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4405 | |
| 4406 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 4407 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4408 | mDispatcher->onWindowInfosChanged( |
| 4409 | {{*windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4410 | |
| 4411 | // Inject an event with coordinate in the area of right window, with mouse cursor in the area of |
| 4412 | // left window. This event should be dispatched to the left window. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 4413 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4414 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 4415 | ADISPLAY_ID_DEFAULT, {610, 400}, {599, 400})); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 4416 | windowLeft->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 4417 | windowRight->assertNoEvents(); |
| 4418 | } |
| 4419 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4420 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsKeyStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4421 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4422 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4423 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 4424 | window->setFocusable(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4425 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4426 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 4427 | setFocusedWindow(window); |
| 4428 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 4429 | window->consumeFocusEvent(true); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4430 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4431 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4432 | |
| 4433 | // Window should receive key down event. |
| 4434 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4435 | |
| 4436 | // When device reset happens, that key stream should be terminated with FLAG_CANCELED |
| 4437 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4438 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 4439 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT, AKEY_EVENT_FLAG_CANCELED); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4440 | } |
| 4441 | |
| 4442 | TEST_F(InputDispatcherTest, NotifyDeviceReset_CancelsMotionStream) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 4443 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4444 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4445 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4446 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4447 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4448 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4449 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4450 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4451 | |
| 4452 | // Window should receive motion down event. |
| 4453 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4454 | |
| 4455 | // When device reset happens, that motion stream should be terminated with ACTION_CANCEL |
| 4456 | // on the app side. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4457 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 4458 | window->consumeMotionEvent( |
| 4459 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 4460 | } |
| 4461 | |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 4462 | TEST_F(InputDispatcherTest, NotifyDeviceResetCancelsHoveringStream) { |
| 4463 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4464 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4465 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4466 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4467 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 4468 | |
| 4469 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 4470 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(10).y(10)) |
| 4471 | .build()); |
| 4472 | |
| 4473 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 4474 | |
| 4475 | // When device reset happens, that hover stream should be terminated with ACTION_HOVER_EXIT |
| 4476 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
| 4477 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 4478 | |
| 4479 | // After the device has been reset, a new hovering stream can be sent to the window |
| 4480 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 4481 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(15).y(15)) |
| 4482 | .build()); |
| 4483 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 4484 | } |
| 4485 | |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4486 | TEST_F(InputDispatcherTest, InterceptKeyByPolicy) { |
| 4487 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4488 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4489 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4490 | window->setFocusable(true); |
| 4491 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4492 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4493 | setFocusedWindow(window); |
| 4494 | |
| 4495 | window->consumeFocusEvent(true); |
| 4496 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4497 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4498 | const std::chrono::milliseconds interceptKeyTimeout = 50ms; |
| 4499 | const nsecs_t injectTime = keyArgs.eventTime; |
| 4500 | mFakePolicy->setInterceptKeyTimeout(interceptKeyTimeout); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4501 | mDispatcher->notifyKey(keyArgs); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4502 | // The dispatching time should be always greater than or equal to intercept key timeout. |
| 4503 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4504 | ASSERT_TRUE((systemTime(SYSTEM_TIME_MONOTONIC) - injectTime) >= |
| 4505 | std::chrono::nanoseconds(interceptKeyTimeout).count()); |
| 4506 | } |
| 4507 | |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 4508 | /** |
| 4509 | * Keys with ACTION_UP are delivered immediately, even if a long 'intercept key timeout' is set. |
| 4510 | */ |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4511 | TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) { |
| 4512 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4513 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4514 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4515 | window->setFocusable(true); |
| 4516 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4517 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4518 | setFocusedWindow(window); |
| 4519 | |
| 4520 | window->consumeFocusEvent(true); |
| 4521 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4522 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4523 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f54d2ab | 2023-06-09 13:23:53 -0700 | [diff] [blame] | 4524 | |
| 4525 | // Set a value that's significantly larger than the default consumption timeout. If the |
| 4526 | // implementation is correct, the actual value doesn't matter; it won't slow down the test. |
| 4527 | mFakePolicy->setInterceptKeyTimeout(600ms); |
| 4528 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
| 4529 | // Window should receive key event immediately when same key up. |
Arthur Hung | 2ee6d0b | 2022-03-03 20:19:38 +0800 | [diff] [blame] | 4530 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 4531 | } |
| 4532 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4533 | /** |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4534 | * Two windows. First is a regular window. Second does not overlap with the first, and has |
| 4535 | * WATCH_OUTSIDE_TOUCH. |
| 4536 | * Both windows are owned by the same UID. |
| 4537 | * Tap first window. Make sure that the second window receives ACTION_OUTSIDE with correct, non-zero |
| 4538 | * coordinates. The coordinates are not zeroed out because both windows are owned by the same UID. |
| 4539 | */ |
| 4540 | TEST_F(InputDispatcherTest, ActionOutsideForOwnedWindowHasValidCoordinates) { |
| 4541 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4542 | sp<FakeWindowHandle> window = |
| 4543 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4544 | window->setFrame(Rect{0, 0, 100, 100}); |
| 4545 | |
| 4546 | sp<FakeWindowHandle> outsideWindow = |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 4547 | sp<FakeWindowHandle>::make(application, mDispatcher, "Outside Window", |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4548 | ADISPLAY_ID_DEFAULT); |
| 4549 | outsideWindow->setFrame(Rect{100, 100, 200, 200}); |
| 4550 | outsideWindow->setWatchOutsideTouch(true); |
| 4551 | // 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] | 4552 | mDispatcher->onWindowInfosChanged({{*outsideWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4553 | |
| 4554 | // Tap on first window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4555 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4556 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4557 | {PointF{50, 50}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4558 | window->consumeMotionDown(); |
| 4559 | // The coordinates of the tap in 'outsideWindow' are relative to its top left corner. |
| 4560 | // Therefore, we should offset them by (100, 100) relative to the screen's top left corner. |
| 4561 | outsideWindow->consumeMotionEvent( |
| 4562 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithCoords(-50, -50))); |
Prabir Pradhan | 502a725 | 2023-12-01 16:11:24 +0000 | [diff] [blame] | 4563 | |
| 4564 | // Ensure outsideWindow doesn't get any more events for the gesture. |
| 4565 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 4566 | ADISPLAY_ID_DEFAULT, {PointF{51, 51}})); |
| 4567 | window->consumeMotionMove(); |
| 4568 | outsideWindow->assertNoEvents(); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4569 | } |
| 4570 | |
| 4571 | /** |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4572 | * This test documents the behavior of WATCH_OUTSIDE_TOUCH. The window will get ACTION_OUTSIDE when |
| 4573 | * a another pointer causes ACTION_DOWN to be sent to another window for the first time. Only one |
| 4574 | * ACTION_OUTSIDE event is sent per gesture. |
| 4575 | */ |
| 4576 | TEST_F(InputDispatcherTest, ActionOutsideSentOnlyWhenAWindowIsTouched) { |
| 4577 | // There are three windows that do not overlap. `window` wants to WATCH_OUTSIDE_TOUCH. |
| 4578 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4579 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4580 | "First Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4581 | window->setWatchOutsideTouch(true); |
| 4582 | window->setFrame(Rect{0, 0, 100, 100}); |
| 4583 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4584 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 4585 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4586 | secondWindow->setFrame(Rect{100, 100, 200, 200}); |
| 4587 | sp<FakeWindowHandle> thirdWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4588 | sp<FakeWindowHandle>::make(application, mDispatcher, "Third Window", |
| 4589 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4590 | thirdWindow->setFrame(Rect{200, 200, 300, 300}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4591 | mDispatcher->onWindowInfosChanged( |
| 4592 | {{*window->getInfo(), *secondWindow->getInfo(), *thirdWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4593 | |
| 4594 | // First pointer lands outside all windows. `window` does not get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4595 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 4596 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4597 | {PointF{-10, -10}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4598 | window->assertNoEvents(); |
| 4599 | secondWindow->assertNoEvents(); |
| 4600 | |
| 4601 | // The second pointer lands inside `secondWindow`, which should receive a DOWN event. |
| 4602 | // Now, `window` should get ACTION_OUTSIDE. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4603 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 4604 | ADISPLAY_ID_DEFAULT, |
| 4605 | {PointF{-10, -10}, PointF{105, 105}})); |
Siarhei Vishniakou | 487c49b | 2022-12-02 15:48:57 -0800 | [diff] [blame] | 4606 | const std::map<int32_t, PointF> expectedPointers{{0, PointF{-10, -10}}, {1, PointF{105, 105}}}; |
| 4607 | window->consumeMotionEvent( |
| 4608 | AllOf(WithMotionAction(ACTION_OUTSIDE), WithPointers(expectedPointers))); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4609 | secondWindow->consumeMotionDown(); |
| 4610 | thirdWindow->assertNoEvents(); |
| 4611 | |
| 4612 | // The third pointer lands inside `thirdWindow`, which should receive a DOWN event. There is |
| 4613 | // 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] | 4614 | mDispatcher->notifyMotion( |
| 4615 | generateMotionArgs(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4616 | {PointF{-10, -10}, PointF{105, 105}, PointF{205, 205}})); |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4617 | window->assertNoEvents(); |
| 4618 | secondWindow->consumeMotionMove(); |
| 4619 | thirdWindow->consumeMotionDown(); |
| 4620 | } |
| 4621 | |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4622 | TEST_F(InputDispatcherTest, OnWindowInfosChanged_RemoveAllWindowsOnDisplay) { |
| 4623 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 4624 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4625 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4626 | window->setFocusable(true); |
| 4627 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4628 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4629 | setFocusedWindow(window); |
| 4630 | |
| 4631 | window->consumeFocusEvent(true); |
| 4632 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4633 | const NotifyKeyArgs keyDown = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
| 4634 | const NotifyKeyArgs keyUp = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
| 4635 | mDispatcher->notifyKey(keyDown); |
| 4636 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4637 | |
| 4638 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 4639 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 4640 | |
| 4641 | // 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] | 4642 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4643 | |
| 4644 | window->consumeFocusEvent(false); |
| 4645 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 4646 | mDispatcher->notifyKey(keyDown); |
| 4647 | mDispatcher->notifyKey(keyUp); |
Prabir Pradhan | 814fe08 | 2022-07-22 20:22:18 +0000 | [diff] [blame] | 4648 | window->assertNoEvents(); |
| 4649 | } |
| 4650 | |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4651 | TEST_F(InputDispatcherTest, NonSplitTouchableWindowReceivesMultiTouch) { |
| 4652 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4653 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 4654 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 4655 | // Ensure window is non-split and have some transform. |
| 4656 | window->setPreventSplitting(true); |
| 4657 | window->setWindowOffset(20, 40); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4658 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4659 | |
| 4660 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4661 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4662 | {50, 50})) |
| 4663 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4664 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 4665 | |
| 4666 | const MotionEvent secondFingerDownEvent = |
| 4667 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4668 | .displayId(ADISPLAY_ID_DEFAULT) |
| 4669 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 4670 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 4671 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(-30).y(-50)) |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4672 | .build(); |
| 4673 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 4674 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4675 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 4676 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4677 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 4678 | std::unique_ptr<MotionEvent> event = window->consumeMotionEvent(); |
| 4679 | ASSERT_NE(nullptr, event); |
| 4680 | EXPECT_EQ(POINTER_1_DOWN, event->getAction()); |
| 4681 | EXPECT_EQ(70, event->getX(0)); // 50 + 20 |
| 4682 | EXPECT_EQ(90, event->getY(0)); // 50 + 40 |
| 4683 | EXPECT_EQ(-10, event->getX(1)); // -30 + 20 |
| 4684 | EXPECT_EQ(-10, event->getY(1)); // -50 + 40 |
Arthur Hung | 9648374 | 2022-11-15 03:30:48 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
Siarhei Vishniakou | 25537f8 | 2023-07-18 14:35:47 -0700 | [diff] [blame] | 4687 | /** |
| 4688 | * Two windows: a splittable and a non-splittable. |
| 4689 | * The non-splittable window shouldn't receive any "incomplete" gestures. |
| 4690 | * Send the first pointer to the splittable window, and then touch the non-splittable window. |
| 4691 | * The second pointer should be dropped because the initial window is splittable, so it won't get |
| 4692 | * any pointers outside of it, and the second window is non-splittable, so it shouldn't get any |
| 4693 | * "incomplete" gestures. |
| 4694 | */ |
| 4695 | TEST_F(InputDispatcherTest, SplittableAndNonSplittableWindows) { |
| 4696 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4697 | sp<FakeWindowHandle> leftWindow = |
| 4698 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left splittable Window", |
| 4699 | ADISPLAY_ID_DEFAULT); |
| 4700 | leftWindow->setPreventSplitting(false); |
| 4701 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 4702 | sp<FakeWindowHandle> rightWindow = |
| 4703 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right non-splittable Window", |
| 4704 | ADISPLAY_ID_DEFAULT); |
| 4705 | rightWindow->setPreventSplitting(true); |
| 4706 | rightWindow->setFrame(Rect(100, 100, 200, 200)); |
| 4707 | mDispatcher->onWindowInfosChanged( |
| 4708 | {{*leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 4709 | |
| 4710 | // Touch down on left, splittable window |
| 4711 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4712 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 4713 | .build()); |
| 4714 | leftWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4715 | |
| 4716 | mDispatcher->notifyMotion( |
| 4717 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4718 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 4719 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
| 4720 | .build()); |
| 4721 | leftWindow->assertNoEvents(); |
| 4722 | rightWindow->assertNoEvents(); |
| 4723 | } |
| 4724 | |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4725 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeOnlySentToTrustedOverlays) { |
| 4726 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4727 | sp<FakeWindowHandle> window = |
| 4728 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4729 | window->setFrame(Rect(0, 0, 400, 400)); |
| 4730 | sp<FakeWindowHandle> trustedOverlay = |
| 4731 | sp<FakeWindowHandle>::make(application, mDispatcher, "Trusted Overlay", |
| 4732 | ADISPLAY_ID_DEFAULT); |
| 4733 | trustedOverlay->setSpy(true); |
| 4734 | trustedOverlay->setTrustedOverlay(true); |
| 4735 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4736 | mDispatcher->onWindowInfosChanged({{*trustedOverlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4737 | |
| 4738 | // Start a three-finger touchpad swipe |
| 4739 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4740 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4741 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4742 | .build()); |
| 4743 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 4744 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4745 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4746 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4747 | .build()); |
| 4748 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 4749 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4750 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4751 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 4752 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4753 | .build()); |
| 4754 | |
| 4755 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4756 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4757 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_DOWN)); |
| 4758 | |
| 4759 | // Move the swipe a bit |
| 4760 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4761 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4762 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4763 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4764 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4765 | .build()); |
| 4766 | |
| 4767 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 4768 | |
| 4769 | // End the swipe |
| 4770 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 4771 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4772 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4773 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4774 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4775 | .build()); |
| 4776 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 4777 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4778 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4779 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4780 | .build()); |
| 4781 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4782 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4783 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4784 | .build()); |
| 4785 | |
| 4786 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_2_UP)); |
| 4787 | trustedOverlay->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 4788 | trustedOverlay->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 4789 | |
| 4790 | window->assertNoEvents(); |
| 4791 | } |
| 4792 | |
| 4793 | TEST_F(InputDispatcherTest, TouchpadThreeFingerSwipeNotSentToSingleWindow) { |
| 4794 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4795 | sp<FakeWindowHandle> window = |
| 4796 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4797 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 4798 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | b166c00 | 2023-05-09 13:06:05 +0000 | [diff] [blame] | 4799 | |
| 4800 | // Start a three-finger touchpad swipe |
| 4801 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 4802 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4803 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4804 | .build()); |
| 4805 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_MOUSE) |
| 4806 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4807 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4808 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4809 | .build()); |
| 4810 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_DOWN, AINPUT_SOURCE_MOUSE) |
| 4811 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(100)) |
| 4812 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(100)) |
| 4813 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(100)) |
| 4814 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4815 | .build()); |
| 4816 | |
| 4817 | // Move the swipe a bit |
| 4818 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 4819 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4820 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4821 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4822 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4823 | .build()); |
| 4824 | |
| 4825 | // End the swipe |
| 4826 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_2_UP, AINPUT_SOURCE_MOUSE) |
| 4827 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4828 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4829 | .pointer(PointerBuilder(2, ToolType::FINGER).x(300).y(105)) |
| 4830 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4831 | .build()); |
| 4832 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_MOUSE) |
| 4833 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4834 | .pointer(PointerBuilder(1, ToolType::FINGER).x(250).y(105)) |
| 4835 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4836 | .build()); |
| 4837 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 4838 | .pointer(PointerBuilder(0, ToolType::FINGER).x(200).y(105)) |
| 4839 | .classification(MotionClassification::MULTI_FINGER_SWIPE) |
| 4840 | .build()); |
| 4841 | |
| 4842 | window->assertNoEvents(); |
| 4843 | } |
| 4844 | |
Prabir Pradhan | b60b1dc | 2022-03-15 14:02:35 +0000 | [diff] [blame] | 4845 | /** |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4846 | * Send a two-pointer gesture to a single window. The window's orientation changes in response to |
| 4847 | * the first pointer. |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4848 | * 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] | 4849 | */ |
| 4850 | TEST_F(InputDispatcherTest, MultiplePointersWithRotatingWindow) { |
| 4851 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4852 | sp<FakeWindowHandle> window = |
| 4853 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 4854 | window->setFrame(Rect(0, 0, 400, 400)); |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4855 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4856 | |
| 4857 | const nsecs_t baseTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 4858 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4859 | .downTime(baseTime + 10) |
| 4860 | .eventTime(baseTime + 10) |
| 4861 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4862 | .build()); |
| 4863 | |
| 4864 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 4865 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4866 | // Change the transform so that the orientation is now different from original. |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4867 | window->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4868 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4869 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4870 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4871 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4872 | .downTime(baseTime + 10) |
| 4873 | .eventTime(baseTime + 30) |
| 4874 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4875 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 4876 | .build()); |
| 4877 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4878 | window->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 4879 | |
| 4880 | // 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] | 4881 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4882 | .downTime(baseTime + 10) |
| 4883 | .eventTime(baseTime + 40) |
| 4884 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4885 | .pointer(PointerBuilder(1, ToolType::FINGER).x(200).y(200)) |
| 4886 | .build()); |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4887 | |
| 4888 | window->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 4889 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4890 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 4891 | .downTime(baseTime + 10) |
| 4892 | .eventTime(baseTime + 50) |
| 4893 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
| 4894 | .build()); |
| 4895 | |
Prabir Pradhan | 69d00bf | 2023-06-23 19:55:18 +0000 | [diff] [blame] | 4896 | window->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 4897 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4898 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 4899 | .downTime(baseTime + 60) |
| 4900 | .eventTime(baseTime + 60) |
| 4901 | .pointer(PointerBuilder(0, ToolType::FINGER).x(40).y(40)) |
| 4902 | .build()); |
| 4903 | |
Siarhei Vishniakou | 700424c | 2023-07-18 17:18:42 -0700 | [diff] [blame] | 4904 | window->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 4905 | } |
| 4906 | |
| 4907 | /** |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 4908 | * When there are multiple screens, such as screen projection to TV or screen recording, if the |
| 4909 | * cancel event occurs, the coordinates of the cancel event should be sent to the target screen, and |
| 4910 | * its coordinates should be converted by the transform of the windows of target screen. |
| 4911 | */ |
| 4912 | TEST_F(InputDispatcherTest, WhenMultiDisplayWindowSameToken_DispatchCancelToTargetDisplay) { |
| 4913 | // This case will create a window and a spy window on the default display and mirror |
| 4914 | // window on the second display. cancel event is sent through spy window pilferPointers |
| 4915 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 4916 | |
| 4917 | sp<FakeWindowHandle> spyWindowDefaultDisplay = |
| 4918 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
| 4919 | spyWindowDefaultDisplay->setTrustedOverlay(true); |
| 4920 | spyWindowDefaultDisplay->setSpy(true); |
| 4921 | |
| 4922 | sp<FakeWindowHandle> windowDefaultDisplay = |
| 4923 | sp<FakeWindowHandle>::make(application, mDispatcher, "DefaultDisplay", |
| 4924 | ADISPLAY_ID_DEFAULT); |
| 4925 | windowDefaultDisplay->setWindowTransform(1, 0, 0, 1); |
| 4926 | |
| 4927 | sp<FakeWindowHandle> windowSecondDisplay = windowDefaultDisplay->clone(SECOND_DISPLAY_ID); |
| 4928 | windowSecondDisplay->setWindowTransform(2, 0, 0, 2); |
| 4929 | |
| 4930 | // Add the windows to the dispatcher |
| 4931 | mDispatcher->onWindowInfosChanged( |
| 4932 | {{*spyWindowDefaultDisplay->getInfo(), *windowDefaultDisplay->getInfo(), |
| 4933 | *windowSecondDisplay->getInfo()}, |
| 4934 | {}, |
| 4935 | 0, |
| 4936 | 0}); |
| 4937 | |
| 4938 | // Send down to ADISPLAY_ID_DEFAULT |
| 4939 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 4940 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 4941 | {100, 100})) |
| 4942 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 4943 | |
| 4944 | spyWindowDefaultDisplay->consumeMotionDown(); |
| 4945 | windowDefaultDisplay->consumeMotionDown(); |
| 4946 | |
| 4947 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spyWindowDefaultDisplay->getToken())); |
| 4948 | |
| 4949 | // windowDefaultDisplay gets cancel |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 4950 | std::unique_ptr<MotionEvent> event = windowDefaultDisplay->consumeMotionEvent(); |
| 4951 | ASSERT_NE(nullptr, event); |
| 4952 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, event->getAction()); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 4953 | |
| 4954 | // The cancel event is sent to windowDefaultDisplay of the ADISPLAY_ID_DEFAULT display, so the |
| 4955 | // coordinates of the cancel are converted by windowDefaultDisplay's transform, the x and y |
| 4956 | // coordinates are both 100, otherwise if the cancel event is sent to windowSecondDisplay of |
| 4957 | // SECOND_DISPLAY_ID, the x and y coordinates are 200 |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 4958 | EXPECT_EQ(100, event->getX(0)); |
| 4959 | EXPECT_EQ(100, event->getY(0)); |
Hu Guo | 771a769 | 2023-09-17 20:51:08 +0800 | [diff] [blame] | 4960 | } |
| 4961 | |
| 4962 | /** |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4963 | * Ensure the correct coordinate spaces are used by InputDispatcher. |
| 4964 | * |
| 4965 | * InputDispatcher works in the display space, so its coordinate system is relative to the display |
| 4966 | * panel. Windows get events in the window space, and get raw coordinates in the logical display |
| 4967 | * space. |
| 4968 | */ |
| 4969 | class InputDispatcherDisplayProjectionTest : public InputDispatcherTest { |
| 4970 | public: |
| 4971 | void SetUp() override { |
| 4972 | InputDispatcherTest::SetUp(); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4973 | removeAllWindowsAndDisplays(); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4974 | } |
| 4975 | |
| 4976 | void addDisplayInfo(int displayId, const ui::Transform& transform) { |
| 4977 | gui::DisplayInfo info; |
| 4978 | info.displayId = displayId; |
| 4979 | info.transform = transform; |
| 4980 | mDisplayInfos.push_back(std::move(info)); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4981 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4982 | } |
| 4983 | |
| 4984 | void addWindow(const sp<WindowInfoHandle>& windowHandle) { |
| 4985 | mWindowInfos.push_back(*windowHandle->getInfo()); |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 4986 | mDispatcher->onWindowInfosChanged({mWindowInfos, mDisplayInfos, 0, 0}); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4987 | } |
| 4988 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 4989 | void removeAllWindowsAndDisplays() { |
| 4990 | mDisplayInfos.clear(); |
| 4991 | mWindowInfos.clear(); |
| 4992 | } |
| 4993 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 4994 | // Set up a test scenario where the display has a scaled projection and there are two windows |
| 4995 | // on the display. |
| 4996 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupScaledDisplayScenario() { |
| 4997 | // The display has a projection that has a scale factor of 2 and 4 in the x and y directions |
| 4998 | // respectively. |
| 4999 | ui::Transform displayTransform; |
| 5000 | displayTransform.set(2, 0, 0, 4); |
| 5001 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 5002 | |
| 5003 | std::shared_ptr<FakeApplicationHandle> application = |
| 5004 | std::make_shared<FakeApplicationHandle>(); |
| 5005 | |
| 5006 | // Add two windows to the display. Their frames are represented in the display space. |
| 5007 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5008 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5009 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5010 | firstWindow->setFrame(Rect(0, 0, 100, 200), displayTransform); |
| 5011 | addWindow(firstWindow); |
| 5012 | |
| 5013 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5014 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5015 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5016 | secondWindow->setFrame(Rect(100, 200, 200, 400), displayTransform); |
| 5017 | addWindow(secondWindow); |
| 5018 | return {std::move(firstWindow), std::move(secondWindow)}; |
| 5019 | } |
| 5020 | |
| 5021 | private: |
| 5022 | std::vector<gui::DisplayInfo> mDisplayInfos; |
| 5023 | std::vector<gui::WindowInfo> mWindowInfos; |
| 5024 | }; |
| 5025 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5026 | TEST_F(InputDispatcherDisplayProjectionTest, HitTestCoordinateSpaceConsistency) { |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5027 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5028 | // 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] | 5029 | // selected so that if the hit test was performed with the point and the bounds being in |
| 5030 | // different coordinate spaces, the event would end up in the incorrect window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5031 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5032 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5033 | {PointF{75, 55}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5034 | |
| 5035 | firstWindow->consumeMotionDown(); |
| 5036 | secondWindow->assertNoEvents(); |
| 5037 | } |
| 5038 | |
| 5039 | // Ensure that when a MotionEvent is injected through the InputDispatcher::injectInputEvent() API, |
| 5040 | // the event should be treated as being in the logical display space. |
| 5041 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionInLogicalDisplaySpace) { |
| 5042 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5043 | // Send down to the first window. The point is represented in the logical display space. The |
| 5044 | // point is selected so that if the hit test was done in logical display space, then it would |
| 5045 | // end up in the incorrect window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5046 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5047 | PointF{75 * 2, 55 * 4}); |
| 5048 | |
| 5049 | firstWindow->consumeMotionDown(); |
| 5050 | secondWindow->assertNoEvents(); |
| 5051 | } |
| 5052 | |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5053 | // Ensure that when a MotionEvent that has a custom transform is injected, the post-transformed |
| 5054 | // event should be treated as being in the logical display space. |
| 5055 | TEST_F(InputDispatcherDisplayProjectionTest, InjectionWithTransformInLogicalDisplaySpace) { |
| 5056 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5057 | |
| 5058 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5059 | ui::Transform injectedEventTransform; |
| 5060 | injectedEventTransform.set(matrix); |
| 5061 | const vec2 expectedPoint{75, 55}; // The injected point in the logical display space. |
| 5062 | const vec2 untransformedPoint = injectedEventTransform.inverse().transform(expectedPoint); |
| 5063 | |
| 5064 | MotionEvent event = MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 5065 | .displayId(ADISPLAY_ID_DEFAULT) |
| 5066 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 5067 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER) |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5068 | .x(untransformedPoint.x) |
| 5069 | .y(untransformedPoint.y)) |
| 5070 | .build(); |
| 5071 | event.transform(matrix); |
| 5072 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5073 | injectMotionEvent(*mDispatcher, event, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | daa2f14 | 2021-12-10 09:30:08 +0000 | [diff] [blame] | 5074 | InputEventInjectionSync::WAIT_FOR_RESULT); |
| 5075 | |
| 5076 | firstWindow->consumeMotionDown(); |
| 5077 | secondWindow->assertNoEvents(); |
| 5078 | } |
| 5079 | |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5080 | TEST_F(InputDispatcherDisplayProjectionTest, WindowGetsEventsInCorrectCoordinateSpace) { |
| 5081 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5082 | |
| 5083 | // Send down to the second window. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5084 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5085 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5086 | {PointF{150, 220}})); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5087 | |
| 5088 | firstWindow->assertNoEvents(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 5089 | std::unique_ptr<MotionEvent> event = secondWindow->consumeMotionEvent(); |
| 5090 | ASSERT_NE(nullptr, event); |
| 5091 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, event->getAction()); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5092 | |
| 5093 | // 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] | 5094 | EXPECT_EQ(300, event->getRawX(0)); |
| 5095 | EXPECT_EQ(880, event->getRawY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5096 | |
| 5097 | // Ensure that the x and y values are in the window's coordinate space. |
| 5098 | // The left-top of the second window is at (100, 200) in display space, which is (200, 800) in |
| 5099 | // 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] | 5100 | EXPECT_EQ(100, event->getX(0)); |
| 5101 | EXPECT_EQ(80, event->getY(0)); |
Prabir Pradhan | c44ce4d | 2021-10-05 05:26:29 -0700 | [diff] [blame] | 5102 | } |
| 5103 | |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5104 | TEST_F(InputDispatcherDisplayProjectionTest, CancelMotionWithCorrectCoordinates) { |
| 5105 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5106 | // The monitor will always receive events in the logical display's coordinate space, because |
| 5107 | // it does not have a window. |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 5108 | FakeMonitorReceiver monitor{*mDispatcher, "Monitor", ADISPLAY_ID_DEFAULT}; |
Prabir Pradhan | 112b1ad | 2023-09-21 09:53:53 +0000 | [diff] [blame] | 5109 | |
| 5110 | // Send down to the first window. |
| 5111 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5112 | ADISPLAY_ID_DEFAULT, {PointF{50, 100}})); |
| 5113 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5114 | monitor.consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5115 | |
| 5116 | // Second pointer goes down on second window. |
| 5117 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5118 | ADISPLAY_ID_DEFAULT, |
| 5119 | {PointF{50, 100}, PointF{150, 220}})); |
| 5120 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80))); |
| 5121 | const std::map<int32_t, PointF> expectedMonitorPointers{{0, PointF{100, 400}}, |
| 5122 | {1, PointF{300, 880}}}; |
| 5123 | monitor.consumeMotionEvent( |
| 5124 | AllOf(WithMotionAction(POINTER_1_DOWN), WithPointers(expectedMonitorPointers))); |
| 5125 | |
| 5126 | mDispatcher->cancelCurrentTouch(); |
| 5127 | |
| 5128 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 5129 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 80))); |
| 5130 | monitor.consumeMotionEvent( |
| 5131 | AllOf(WithMotionAction(ACTION_CANCEL), WithPointers(expectedMonitorPointers))); |
| 5132 | } |
| 5133 | |
Prabir Pradhan | 1c29a09 | 2023-09-21 10:29:29 +0000 | [diff] [blame] | 5134 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeDownWithCorrectCoordinates) { |
| 5135 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5136 | |
| 5137 | // Send down to the first window. |
| 5138 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5139 | ADISPLAY_ID_DEFAULT, {PointF{50, 100}})); |
| 5140 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 400))); |
| 5141 | |
| 5142 | // The pointer is transferred to the second window, and the second window receives it in the |
| 5143 | // correct coordinate space. |
| 5144 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 5145 | firstWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_CANCEL), WithCoords(100, 400))); |
| 5146 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithCoords(-100, -400))); |
| 5147 | } |
| 5148 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5149 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverEnterExitWithCorrectCoordinates) { |
| 5150 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5151 | |
| 5152 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 5153 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 5154 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5155 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5156 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5157 | |
| 5158 | // Touch down at the same location and ensure a hover exit is synthesized. |
| 5159 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
| 5160 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5161 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5162 | WithRawCoords(300, 880))); |
| 5163 | secondWindow->consumeMotionEvent( |
| 5164 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5165 | secondWindow->assertNoEvents(); |
| 5166 | firstWindow->assertNoEvents(); |
| 5167 | } |
| 5168 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5169 | // Same as above, but while the window is being mirrored. |
| 5170 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 5171 | SynthesizeHoverEnterExitWithCorrectCoordinatesWhenMirrored) { |
| 5172 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5173 | |
| 5174 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5175 | ui::Transform secondDisplayTransform; |
| 5176 | secondDisplayTransform.set(matrix); |
| 5177 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 5178 | |
| 5179 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 5180 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 5181 | addWindow(secondWindowClone); |
| 5182 | |
| 5183 | // Send hover move to the second window, and ensure it shows up as hover enter. |
| 5184 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 5185 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5186 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5187 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5188 | |
| 5189 | // Touch down at the same location and ensure a hover exit is synthesized for the correct |
| 5190 | // display. |
| 5191 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_STYLUS, |
| 5192 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5193 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5194 | WithRawCoords(300, 880))); |
| 5195 | secondWindow->consumeMotionEvent( |
| 5196 | AllOf(WithMotionAction(ACTION_DOWN), WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5197 | secondWindow->assertNoEvents(); |
| 5198 | firstWindow->assertNoEvents(); |
| 5199 | } |
| 5200 | |
Prabir Pradhan | 0dfcac7 | 2023-10-05 20:04:21 +0000 | [diff] [blame] | 5201 | TEST_F(InputDispatcherDisplayProjectionTest, SynthesizeHoverCancelationWithCorrectCoordinates) { |
| 5202 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5203 | |
| 5204 | // Send hover enter to second window |
| 5205 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
| 5206 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5207 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5208 | WithCoords(100, 80), WithRawCoords(300, 880))); |
| 5209 | |
| 5210 | mDispatcher->cancelCurrentTouch(); |
| 5211 | |
| 5212 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5213 | WithRawCoords(300, 880))); |
| 5214 | secondWindow->assertNoEvents(); |
| 5215 | firstWindow->assertNoEvents(); |
| 5216 | } |
| 5217 | |
Prabir Pradhan | 453ae73 | 2023-10-13 14:30:14 +0000 | [diff] [blame] | 5218 | // Same as above, but while the window is being mirrored. |
Prabir Pradhan | 1646338 | 2023-10-12 23:03:19 +0000 | [diff] [blame] | 5219 | TEST_F(InputDispatcherDisplayProjectionTest, |
| 5220 | SynthesizeHoverCancelationWithCorrectCoordinatesWhenMirrored) { |
| 5221 | auto [firstWindow, secondWindow] = setupScaledDisplayScenario(); |
| 5222 | |
| 5223 | const std::array<float, 9> matrix = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0.0, 0.0, 1.0}; |
| 5224 | ui::Transform secondDisplayTransform; |
| 5225 | secondDisplayTransform.set(matrix); |
| 5226 | addDisplayInfo(SECOND_DISPLAY_ID, secondDisplayTransform); |
| 5227 | |
| 5228 | sp<FakeWindowHandle> secondWindowClone = secondWindow->clone(SECOND_DISPLAY_ID); |
| 5229 | secondWindowClone->setWindowTransform(1.1, 2.2, 3.3, 4.4); |
| 5230 | addWindow(secondWindowClone); |
| 5231 | |
| 5232 | // Send hover enter to second window |
| 5233 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS, |
| 5234 | ADISPLAY_ID_DEFAULT, {PointF{150, 220}})); |
| 5235 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 5236 | WithCoords(100, 80), WithRawCoords(300, 880), |
| 5237 | WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 5238 | |
| 5239 | mDispatcher->cancelCurrentTouch(); |
| 5240 | |
| 5241 | // Ensure the cancelation happens with the correct displayId and the correct coordinates. |
| 5242 | secondWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_EXIT), WithCoords(100, 80), |
| 5243 | WithRawCoords(300, 880), |
| 5244 | WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 5245 | secondWindow->assertNoEvents(); |
| 5246 | firstWindow->assertNoEvents(); |
| 5247 | } |
| 5248 | |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5249 | /** Ensure consistent behavior of InputDispatcher in all orientations. */ |
| 5250 | class InputDispatcherDisplayOrientationFixture |
| 5251 | : public InputDispatcherDisplayProjectionTest, |
| 5252 | public ::testing::WithParamInterface<ui::Rotation> {}; |
| 5253 | |
| 5254 | // This test verifies the touchable region of a window for all rotations of the display by tapping |
| 5255 | // in different locations on the display, specifically points close to the four corners of a |
| 5256 | // window. |
| 5257 | TEST_P(InputDispatcherDisplayOrientationFixture, HitTestInDifferentOrientations) { |
| 5258 | constexpr static int32_t displayWidth = 400; |
| 5259 | constexpr static int32_t displayHeight = 800; |
| 5260 | |
| 5261 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5262 | |
| 5263 | const auto rotation = GetParam(); |
| 5264 | |
| 5265 | // Set up the display with the specified rotation. |
| 5266 | const bool isRotated = rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270; |
| 5267 | const int32_t logicalDisplayWidth = isRotated ? displayHeight : displayWidth; |
| 5268 | const int32_t logicalDisplayHeight = isRotated ? displayWidth : displayHeight; |
| 5269 | const ui::Transform displayTransform(ui::Transform::toRotationFlags(rotation), |
| 5270 | logicalDisplayWidth, logicalDisplayHeight); |
| 5271 | addDisplayInfo(ADISPLAY_ID_DEFAULT, displayTransform); |
| 5272 | |
| 5273 | // Create a window with its bounds determined in the logical display. |
| 5274 | const Rect frameInLogicalDisplay(100, 100, 200, 300); |
| 5275 | const Rect frameInDisplay = displayTransform.inverse().transform(frameInLogicalDisplay); |
| 5276 | sp<FakeWindowHandle> window = |
| 5277 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 5278 | window->setFrame(frameInDisplay, displayTransform); |
| 5279 | addWindow(window); |
| 5280 | |
| 5281 | // The following points in logical display space should be inside the window. |
| 5282 | static const std::array<vec2, 4> insidePoints{ |
| 5283 | {{100, 100}, {199.99, 100}, {100, 299.99}, {199.99, 299.99}}}; |
| 5284 | for (const auto pointInsideWindow : insidePoints) { |
| 5285 | const vec2 p = displayTransform.inverse().transform(pointInsideWindow); |
| 5286 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5287 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5288 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5289 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5290 | window->consumeMotionDown(); |
| 5291 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5292 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 5293 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5294 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5295 | window->consumeMotionUp(); |
| 5296 | } |
| 5297 | |
| 5298 | // The following points in logical display space should be outside the window. |
| 5299 | static const std::array<vec2, 5> outsidePoints{ |
| 5300 | {{200, 100}, {100, 300}, {200, 300}, {100, 99.99}, {99.99, 100}}}; |
| 5301 | for (const auto pointOutsideWindow : outsidePoints) { |
| 5302 | const vec2 p = displayTransform.inverse().transform(pointOutsideWindow); |
| 5303 | const PointF pointInDisplaySpace{p.x, p.y}; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5304 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5305 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5306 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5307 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5308 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, |
| 5309 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5310 | {pointInDisplaySpace})); |
Prabir Pradhan | 33e3baa | 2022-12-06 20:30:22 +0000 | [diff] [blame] | 5311 | } |
| 5312 | window->assertNoEvents(); |
| 5313 | } |
| 5314 | |
| 5315 | // Run the precision tests for all rotations. |
| 5316 | INSTANTIATE_TEST_SUITE_P(InputDispatcherDisplayOrientationTests, |
| 5317 | InputDispatcherDisplayOrientationFixture, |
| 5318 | ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, |
| 5319 | ui::ROTATION_270), |
| 5320 | [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) { |
| 5321 | return ftl::enum_string(testParamInfo.param); |
| 5322 | }); |
| 5323 | |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5324 | using TransferFunction = std::function<bool(const std::unique_ptr<InputDispatcher>& dispatcher, |
| 5325 | sp<IBinder>, sp<IBinder>)>; |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5326 | |
| 5327 | class TransferTouchFixture : public InputDispatcherTest, |
| 5328 | public ::testing::WithParamInterface<TransferFunction> {}; |
| 5329 | |
| 5330 | TEST_P(TransferTouchFixture, TransferTouch_OnePointer) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5331 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5332 | |
| 5333 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5334 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5335 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5336 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5337 | firstWindow->setDupTouchToWallpaper(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5338 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5339 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5340 | ADISPLAY_ID_DEFAULT); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5341 | sp<FakeWindowHandle> wallpaper = |
| 5342 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper", ADISPLAY_ID_DEFAULT); |
| 5343 | wallpaper->setIsWallpaper(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5344 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5345 | mDispatcher->onWindowInfosChanged( |
| 5346 | {{*firstWindow->getInfo(), *secondWindow->getInfo(), *wallpaper->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5347 | |
| 5348 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5349 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5350 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5351 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5352 | // Only the first window should get the down event |
| 5353 | firstWindow->consumeMotionDown(); |
| 5354 | secondWindow->assertNoEvents(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5355 | wallpaper->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5356 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5357 | // Transfer touch to the second window |
| 5358 | TransferFunction f = GetParam(); |
| 5359 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5360 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5361 | // The first window gets cancel and the second gets down |
| 5362 | firstWindow->consumeMotionCancel(); |
| 5363 | secondWindow->consumeMotionDown(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5364 | wallpaper->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5365 | |
| 5366 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5367 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5368 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5369 | // The first window gets no events and the second gets up |
| 5370 | firstWindow->assertNoEvents(); |
| 5371 | secondWindow->consumeMotionUp(); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5372 | wallpaper->assertNoEvents(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5373 | } |
| 5374 | |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5375 | /** |
| 5376 | * When 'transferTouch' API is invoked, dispatcher needs to find the "best" window to take touch |
| 5377 | * from. When we have spy windows, there are several windows to choose from: either spy, or the |
| 5378 | * 'real' (non-spy) window. Always prefer the 'real' window because that's what would be most |
| 5379 | * natural to the user. |
| 5380 | * In this test, we are sending a pointer to both spy window and first window. We then try to |
| 5381 | * transfer touch to the second window. The dispatcher should identify the first window as the |
| 5382 | * one that should lose the gesture, and therefore the action should be to move the gesture from |
| 5383 | * the first window to the second. |
| 5384 | * The main goal here is to test the behaviour of 'transferTouch' API, but it's still valid to test |
| 5385 | * the other API, as well. |
| 5386 | */ |
| 5387 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWindowsWithSpy) { |
| 5388 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5389 | |
| 5390 | // Create a couple of windows + a spy window |
| 5391 | sp<FakeWindowHandle> spyWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5392 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5393 | spyWindow->setTrustedOverlay(true); |
| 5394 | spyWindow->setSpy(true); |
| 5395 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5396 | sp<FakeWindowHandle>::make(application, mDispatcher, "First", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5397 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5398 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5399 | |
| 5400 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5401 | mDispatcher->onWindowInfosChanged( |
| 5402 | {{*spyWindow->getInfo(), *firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5403 | |
| 5404 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5405 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5406 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5407 | // Only the first window and spy should get the down event |
| 5408 | spyWindow->consumeMotionDown(); |
| 5409 | firstWindow->consumeMotionDown(); |
| 5410 | |
| 5411 | // Transfer touch to the second window. Non-spy window should be preferred over the spy window |
| 5412 | // if f === 'transferTouch'. |
| 5413 | TransferFunction f = GetParam(); |
| 5414 | const bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5415 | ASSERT_TRUE(success); |
| 5416 | // The first window gets cancel and the second gets down |
| 5417 | firstWindow->consumeMotionCancel(); |
| 5418 | secondWindow->consumeMotionDown(); |
| 5419 | |
| 5420 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5421 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5422 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5423 | // The first window gets no events and the second+spy get up |
| 5424 | firstWindow->assertNoEvents(); |
| 5425 | spyWindow->consumeMotionUp(); |
| 5426 | secondWindow->consumeMotionUp(); |
| 5427 | } |
| 5428 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5429 | TEST_P(TransferTouchFixture, TransferTouch_TwoPointersNonSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5430 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5431 | |
| 5432 | PointF touchPoint = {10, 10}; |
| 5433 | |
| 5434 | // Create a couple of windows |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5435 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5436 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5437 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5438 | firstWindow->setPreventSplitting(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5439 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5440 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5441 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 5442 | secondWindow->setPreventSplitting(true); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5443 | |
| 5444 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5445 | mDispatcher->onWindowInfosChanged( |
| 5446 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5447 | |
| 5448 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5449 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5450 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5451 | {touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5452 | // Only the first window should get the down event |
| 5453 | firstWindow->consumeMotionDown(); |
| 5454 | secondWindow->assertNoEvents(); |
| 5455 | |
| 5456 | // Send pointer down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5457 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5458 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5459 | // Only the first window should get the pointer down event |
| 5460 | firstWindow->consumeMotionPointerDown(1); |
| 5461 | secondWindow->assertNoEvents(); |
| 5462 | |
| 5463 | // Transfer touch focus to the second window |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5464 | TransferFunction f = GetParam(); |
| 5465 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5466 | ASSERT_TRUE(success); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5467 | // The first window gets cancel and the second gets down and pointer down |
| 5468 | firstWindow->consumeMotionCancel(); |
| 5469 | secondWindow->consumeMotionDown(); |
| 5470 | secondWindow->consumeMotionPointerDown(1); |
| 5471 | |
| 5472 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5473 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5474 | ADISPLAY_ID_DEFAULT, {touchPoint, touchPoint})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5475 | // The first window gets nothing and the second gets pointer up |
| 5476 | firstWindow->assertNoEvents(); |
| 5477 | secondWindow->consumeMotionPointerUp(1); |
| 5478 | |
| 5479 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5480 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5481 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5482 | // The first window gets nothing and the second gets up |
| 5483 | firstWindow->assertNoEvents(); |
| 5484 | secondWindow->consumeMotionUp(); |
| 5485 | } |
| 5486 | |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5487 | TEST_P(TransferTouchFixture, TransferTouch_MultipleWallpapers) { |
| 5488 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5489 | |
| 5490 | // Create a couple of windows |
| 5491 | sp<FakeWindowHandle> firstWindow = |
| 5492 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5493 | ADISPLAY_ID_DEFAULT); |
| 5494 | firstWindow->setDupTouchToWallpaper(true); |
| 5495 | sp<FakeWindowHandle> secondWindow = |
| 5496 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5497 | ADISPLAY_ID_DEFAULT); |
| 5498 | secondWindow->setDupTouchToWallpaper(true); |
| 5499 | |
| 5500 | sp<FakeWindowHandle> wallpaper1 = |
| 5501 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper1", ADISPLAY_ID_DEFAULT); |
| 5502 | wallpaper1->setIsWallpaper(true); |
| 5503 | |
| 5504 | sp<FakeWindowHandle> wallpaper2 = |
| 5505 | sp<FakeWindowHandle>::make(application, mDispatcher, "Wallpaper2", ADISPLAY_ID_DEFAULT); |
| 5506 | wallpaper2->setIsWallpaper(true); |
| 5507 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5508 | mDispatcher->onWindowInfosChanged({{*firstWindow->getInfo(), *wallpaper1->getInfo(), |
| 5509 | *secondWindow->getInfo(), *wallpaper2->getInfo()}, |
| 5510 | {}, |
| 5511 | 0, |
| 5512 | 0}); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5513 | |
| 5514 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5515 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5516 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5517 | |
| 5518 | // Only the first window should get the down event |
| 5519 | firstWindow->consumeMotionDown(); |
| 5520 | secondWindow->assertNoEvents(); |
| 5521 | wallpaper1->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5522 | wallpaper2->assertNoEvents(); |
| 5523 | |
| 5524 | // Transfer touch focus to the second window |
| 5525 | TransferFunction f = GetParam(); |
| 5526 | bool success = f(mDispatcher, firstWindow->getToken(), secondWindow->getToken()); |
| 5527 | ASSERT_TRUE(success); |
| 5528 | |
| 5529 | // The first window gets cancel and the second gets down |
| 5530 | firstWindow->consumeMotionCancel(); |
| 5531 | secondWindow->consumeMotionDown(); |
| 5532 | wallpaper1->consumeMotionCancel(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5533 | wallpaper2->consumeMotionDown(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5534 | |
| 5535 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5536 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5537 | ADISPLAY_ID_DEFAULT)); |
Arthur Hung | c539dbb | 2022-12-08 07:45:36 +0000 | [diff] [blame] | 5538 | // The first window gets no events and the second gets up |
| 5539 | firstWindow->assertNoEvents(); |
| 5540 | secondWindow->consumeMotionUp(); |
| 5541 | wallpaper1->assertNoEvents(); |
| 5542 | wallpaper2->consumeMotionUp(ADISPLAY_ID_DEFAULT, expectedWallpaperFlags); |
| 5543 | } |
| 5544 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5545 | // For the cases of single pointer touch and two pointers non-split touch, the api's |
| 5546 | // 'transferTouch' and 'transferTouchFocus' are equivalent in behaviour. They only differ |
| 5547 | // for the case where there are multiple pointers split across several windows. |
| 5548 | INSTANTIATE_TEST_SUITE_P(TransferFunctionTests, TransferTouchFixture, |
| 5549 | ::testing::Values( |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5550 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 5551 | sp<IBinder> /*ignored*/, sp<IBinder> destChannelToken) { |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5552 | return dispatcher->transferTouch(destChannelToken, |
| 5553 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5554 | }, |
Siarhei Vishniakou | 1805009 | 2021-09-01 13:32:49 -0700 | [diff] [blame] | 5555 | [&](const std::unique_ptr<InputDispatcher>& dispatcher, |
| 5556 | sp<IBinder> from, sp<IBinder> to) { |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5557 | return dispatcher->transferTouchFocus(from, to, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 5558 | /*isDragAndDrop=*/false); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5559 | })); |
| 5560 | |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5561 | TEST_F(InputDispatcherTest, TransferTouchFocus_TwoPointersSplitTouch) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5562 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5563 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5564 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5565 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5566 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5567 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5568 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 5569 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5570 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5571 | ADISPLAY_ID_DEFAULT); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5572 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5573 | |
| 5574 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5575 | mDispatcher->onWindowInfosChanged( |
| 5576 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5577 | |
| 5578 | PointF pointInFirst = {300, 200}; |
| 5579 | PointF pointInSecond = {300, 600}; |
| 5580 | |
| 5581 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5582 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5583 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5584 | {pointInFirst})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5585 | // Only the first window should get the down event |
| 5586 | firstWindow->consumeMotionDown(); |
| 5587 | secondWindow->assertNoEvents(); |
| 5588 | |
| 5589 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5590 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5591 | ADISPLAY_ID_DEFAULT, |
| 5592 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5593 | // The first window gets a move and the second a down |
| 5594 | firstWindow->consumeMotionMove(); |
| 5595 | secondWindow->consumeMotionDown(); |
| 5596 | |
| 5597 | // Transfer touch focus to the second window |
| 5598 | mDispatcher->transferTouchFocus(firstWindow->getToken(), secondWindow->getToken()); |
| 5599 | // The first window gets cancel and the new gets pointer down (it already saw down) |
| 5600 | firstWindow->consumeMotionCancel(); |
| 5601 | secondWindow->consumeMotionPointerDown(1); |
| 5602 | |
| 5603 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5604 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5605 | ADISPLAY_ID_DEFAULT, |
| 5606 | {pointInFirst, pointInSecond})); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5607 | // The first window gets nothing and the second gets pointer up |
| 5608 | firstWindow->assertNoEvents(); |
| 5609 | secondWindow->consumeMotionPointerUp(1); |
| 5610 | |
| 5611 | // Send up event to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5612 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5613 | ADISPLAY_ID_DEFAULT)); |
Svet Ganov | 5d3bc37 | 2020-01-26 23:11:07 -0800 | [diff] [blame] | 5614 | // The first window gets nothing and the second gets up |
| 5615 | firstWindow->assertNoEvents(); |
| 5616 | secondWindow->consumeMotionUp(); |
| 5617 | } |
| 5618 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5619 | // Same as TransferTouchFocus_TwoPointersSplitTouch, but using 'transferTouch' api. |
| 5620 | // Unlike 'transferTouchFocus', calling 'transferTouch' when there are two windows receiving |
| 5621 | // touch is not supported, so the touch should continue on those windows and the transferred-to |
| 5622 | // window should get nothing. |
| 5623 | TEST_F(InputDispatcherTest, TransferTouch_TwoPointersSplitTouch) { |
| 5624 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5625 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5626 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5627 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5628 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5629 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5630 | |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5631 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5632 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5633 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5634 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5635 | |
| 5636 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5637 | mDispatcher->onWindowInfosChanged( |
| 5638 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5639 | |
| 5640 | PointF pointInFirst = {300, 200}; |
| 5641 | PointF pointInSecond = {300, 600}; |
| 5642 | |
| 5643 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5644 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5645 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5646 | {pointInFirst})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5647 | // Only the first window should get the down event |
| 5648 | firstWindow->consumeMotionDown(); |
| 5649 | secondWindow->assertNoEvents(); |
| 5650 | |
| 5651 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5652 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5653 | ADISPLAY_ID_DEFAULT, |
| 5654 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5655 | // The first window gets a move and the second a down |
| 5656 | firstWindow->consumeMotionMove(); |
| 5657 | secondWindow->consumeMotionDown(); |
| 5658 | |
| 5659 | // Transfer touch focus to the second window |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5660 | const bool transferred = |
| 5661 | mDispatcher->transferTouch(secondWindow->getToken(), ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5662 | // The 'transferTouch' call should not succeed, because there are 2 touched windows |
| 5663 | ASSERT_FALSE(transferred); |
| 5664 | firstWindow->assertNoEvents(); |
| 5665 | secondWindow->assertNoEvents(); |
| 5666 | |
| 5667 | // The rest of the dispatch should proceed as normal |
| 5668 | // Send pointer up to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5669 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5670 | ADISPLAY_ID_DEFAULT, |
| 5671 | {pointInFirst, pointInSecond})); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5672 | // The first window gets MOVE and the second gets pointer up |
| 5673 | firstWindow->consumeMotionMove(); |
| 5674 | secondWindow->consumeMotionUp(); |
| 5675 | |
| 5676 | // Send up event to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5677 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 5678 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d0c6bc8 | 2021-03-13 03:14:52 +0000 | [diff] [blame] | 5679 | // The first window gets nothing and the second gets up |
| 5680 | firstWindow->consumeMotionUp(); |
| 5681 | secondWindow->assertNoEvents(); |
| 5682 | } |
| 5683 | |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5684 | // This case will create two windows and one mirrored window on the default display and mirror |
| 5685 | // two windows on the second display. It will test if 'transferTouchFocus' works fine if we put |
| 5686 | // the windows info of second display before default display. |
| 5687 | TEST_F(InputDispatcherTest, TransferTouchFocus_CloneSurface) { |
| 5688 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5689 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5690 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5691 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5692 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5693 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5694 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5695 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5696 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5697 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5698 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5699 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5700 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5701 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5702 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5703 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5704 | |
| 5705 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5706 | mDispatcher->onWindowInfosChanged( |
| 5707 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 5708 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 5709 | *secondWindowInPrimary->getInfo()}, |
| 5710 | {}, |
| 5711 | 0, |
| 5712 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5713 | |
| 5714 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5715 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5716 | {50, 50})) |
| 5717 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5718 | |
| 5719 | // Window should receive motion event. |
| 5720 | firstWindowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5721 | |
| 5722 | // Transfer touch focus |
| 5723 | ASSERT_TRUE(mDispatcher->transferTouchFocus(firstWindowInPrimary->getToken(), |
| 5724 | secondWindowInPrimary->getToken())); |
| 5725 | // The first window gets cancel. |
| 5726 | firstWindowInPrimary->consumeMotionCancel(); |
| 5727 | secondWindowInPrimary->consumeMotionDown(); |
| 5728 | |
| 5729 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5730 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5731 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 5732 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5733 | firstWindowInPrimary->assertNoEvents(); |
| 5734 | secondWindowInPrimary->consumeMotionMove(); |
| 5735 | |
| 5736 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5737 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5738 | {150, 50})) |
| 5739 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5740 | firstWindowInPrimary->assertNoEvents(); |
| 5741 | secondWindowInPrimary->consumeMotionUp(); |
| 5742 | } |
| 5743 | |
| 5744 | // Same as TransferTouchFocus_CloneSurface, but this touch on the secondary display and use |
| 5745 | // 'transferTouch' api. |
| 5746 | TEST_F(InputDispatcherTest, TransferTouch_CloneSurface) { |
| 5747 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5748 | sp<FakeWindowHandle> firstWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5749 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5750 | firstWindowInPrimary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5751 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5752 | sp<FakeWindowHandle>::make(application, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5753 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5754 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5755 | sp<FakeWindowHandle> mirrorWindowInPrimary = firstWindowInPrimary->clone(ADISPLAY_ID_DEFAULT); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5756 | mirrorWindowInPrimary->setFrame(Rect(0, 100, 100, 200)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5757 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5758 | sp<FakeWindowHandle> firstWindowInSecondary = firstWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5759 | firstWindowInSecondary->setFrame(Rect(0, 0, 100, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5760 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 5761 | sp<FakeWindowHandle> secondWindowInSecondary = secondWindowInPrimary->clone(SECOND_DISPLAY_ID); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5762 | secondWindowInPrimary->setFrame(Rect(100, 0, 200, 100)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5763 | |
| 5764 | // Update window info, let it find window handle of second display first. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5765 | mDispatcher->onWindowInfosChanged( |
| 5766 | {{*firstWindowInSecondary->getInfo(), *secondWindowInSecondary->getInfo(), |
| 5767 | *mirrorWindowInPrimary->getInfo(), *firstWindowInPrimary->getInfo(), |
| 5768 | *secondWindowInPrimary->getInfo()}, |
| 5769 | {}, |
| 5770 | 0, |
| 5771 | 0}); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5772 | |
| 5773 | // Touch on second display. |
| 5774 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5775 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 5776 | {50, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5777 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5778 | |
| 5779 | // Window should receive motion event. |
| 5780 | firstWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 5781 | |
| 5782 | // Transfer touch focus |
Siarhei Vishniakou | 7ae7afd | 2022-03-31 15:26:13 -0700 | [diff] [blame] | 5783 | ASSERT_TRUE(mDispatcher->transferTouch(secondWindowInSecondary->getToken(), SECOND_DISPLAY_ID)); |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5784 | |
| 5785 | // The first window gets cancel. |
| 5786 | firstWindowInPrimary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 5787 | secondWindowInPrimary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 5788 | |
| 5789 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5790 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5791 | SECOND_DISPLAY_ID, {150, 50})) |
| 5792 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5793 | firstWindowInPrimary->assertNoEvents(); |
| 5794 | secondWindowInPrimary->consumeMotionMove(SECOND_DISPLAY_ID); |
| 5795 | |
| 5796 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5797 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {150, 50})) |
Arthur Hung | abbb9d8 | 2021-09-01 14:52:30 +0000 | [diff] [blame] | 5798 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5799 | firstWindowInPrimary->assertNoEvents(); |
| 5800 | secondWindowInPrimary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 5801 | } |
| 5802 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5803 | TEST_F(InputDispatcherTest, FocusedWindow_ReceivesFocusEventAndKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5804 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5805 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5806 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5807 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 5808 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5809 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 5810 | setFocusedWindow(window); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5811 | |
| 5812 | window->consumeFocusEvent(true); |
| 5813 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5814 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5815 | |
| 5816 | // Window should receive key down event. |
| 5817 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5818 | |
| 5819 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5820 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5821 | mFakePolicy->assertUserActivityPoked(); |
| 5822 | } |
| 5823 | |
| 5824 | TEST_F(InputDispatcherTest, FocusedWindow_DisableUserActivity) { |
| 5825 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5826 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5827 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5828 | |
| 5829 | window->setDisableUserActivity(true); |
| 5830 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5831 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5832 | setFocusedWindow(window); |
| 5833 | |
| 5834 | window->consumeFocusEvent(true); |
| 5835 | |
| 5836 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5837 | |
| 5838 | // Window should receive key down event. |
| 5839 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 5840 | |
| 5841 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5842 | mDispatcher->waitForIdle(); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5843 | mFakePolicy->assertUserActivityNotPoked(); |
| 5844 | } |
| 5845 | |
| 5846 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) { |
| 5847 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5848 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5849 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5850 | |
| 5851 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5852 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5853 | setFocusedWindow(window); |
| 5854 | |
| 5855 | window->consumeFocusEvent(true); |
| 5856 | |
| 5857 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5858 | mDispatcher->waitForIdle(); |
| 5859 | |
| 5860 | // System key is not passed down |
| 5861 | window->assertNoEvents(); |
| 5862 | |
| 5863 | // Should have poked user activity |
| 5864 | mFakePolicy->assertUserActivityPoked(); |
| 5865 | } |
| 5866 | |
| 5867 | TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) { |
| 5868 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5869 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5870 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5871 | |
| 5872 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5873 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5874 | setFocusedWindow(window); |
| 5875 | |
| 5876 | window->consumeFocusEvent(true); |
| 5877 | |
| 5878 | mDispatcher->notifyKey(generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5879 | mDispatcher->waitForIdle(); |
| 5880 | |
| 5881 | // System key is not passed down |
| 5882 | window->assertNoEvents(); |
| 5883 | |
| 5884 | // Should have poked user activity |
| 5885 | mFakePolicy->assertUserActivityPoked(); |
| 5886 | } |
| 5887 | |
| 5888 | TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) { |
| 5889 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5890 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5891 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5892 | |
| 5893 | window->setDisableUserActivity(true); |
| 5894 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5895 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Josep del Rio | b398162 | 2023-04-18 15:49:45 +0000 | [diff] [blame] | 5896 | setFocusedWindow(window); |
| 5897 | |
| 5898 | window->consumeFocusEvent(true); |
| 5899 | |
| 5900 | mDispatcher->notifyKey(generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
| 5901 | mDispatcher->waitForIdle(); |
| 5902 | |
| 5903 | // System key is not passed down |
| 5904 | window->assertNoEvents(); |
| 5905 | |
| 5906 | // Should have poked user activity |
| 5907 | mFakePolicy->assertUserActivityPoked(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5908 | } |
| 5909 | |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5910 | TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) { |
| 5911 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5912 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5913 | "Fake Window", ADISPLAY_ID_DEFAULT); |
| 5914 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5915 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5916 | |
| 5917 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 5918 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5919 | ADISPLAY_ID_DEFAULT, {100, 100})) |
| 5920 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 5921 | |
| 5922 | window->consumeMotionEvent( |
| 5923 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 5924 | |
| 5925 | // Should have poked user activity |
Siarhei Vishniakou | 4fd8673 | 2023-05-24 17:33:01 +0000 | [diff] [blame] | 5926 | mDispatcher->waitForIdle(); |
Siarhei Vishniakou | 90ee478 | 2023-05-08 11:57:24 -0700 | [diff] [blame] | 5927 | mFakePolicy->assertUserActivityPoked(); |
| 5928 | } |
| 5929 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5930 | TEST_F(InputDispatcherTest, UnfocusedWindow_DoesNotReceiveFocusEventOrKeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5931 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5932 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5933 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5934 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5935 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5936 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5937 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5938 | mDispatcher->waitForIdle(); |
| 5939 | |
| 5940 | window->assertNoEvents(); |
| 5941 | } |
| 5942 | |
| 5943 | // If a window is touchable, but does not have focus, it should receive motion events, but not keys |
| 5944 | TEST_F(InputDispatcherTest, UnfocusedWindow_ReceivesMotionsButNotKeys) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 5945 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5946 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 5947 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5948 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5949 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5950 | |
| 5951 | // Send key |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5952 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5953 | // Send motion |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5954 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5955 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 5956 | |
| 5957 | // Window should receive only the motion event |
| 5958 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 5959 | window->assertNoEvents(); // Key event or focus event will not be received |
| 5960 | } |
| 5961 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5962 | TEST_F(InputDispatcherTest, PointerCancel_SendCancelWhenSplitTouch) { |
| 5963 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 5964 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5965 | sp<FakeWindowHandle> firstWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5966 | sp<FakeWindowHandle>::make(application, mDispatcher, "First Window", |
| 5967 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5968 | firstWindow->setFrame(Rect(0, 0, 600, 400)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5969 | |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5970 | sp<FakeWindowHandle> secondWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 5971 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second Window", |
| 5972 | ADISPLAY_ID_DEFAULT); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5973 | secondWindow->setFrame(Rect(0, 400, 600, 800)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5974 | |
| 5975 | // Add the windows to the dispatcher |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 5976 | mDispatcher->onWindowInfosChanged( |
| 5977 | {{*firstWindow->getInfo(), *secondWindow->getInfo()}, {}, 0, 0}); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5978 | |
| 5979 | PointF pointInFirst = {300, 200}; |
| 5980 | PointF pointInSecond = {300, 600}; |
| 5981 | |
| 5982 | // Send down to the first window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5983 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 5984 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 5985 | {pointInFirst})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5986 | // Only the first window should get the down event |
| 5987 | firstWindow->consumeMotionDown(); |
| 5988 | secondWindow->assertNoEvents(); |
| 5989 | |
| 5990 | // Send down to the second window |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 5991 | mDispatcher->notifyMotion(generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 5992 | ADISPLAY_ID_DEFAULT, |
| 5993 | {pointInFirst, pointInSecond})); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 5994 | // The first window gets a move and the second a down |
| 5995 | firstWindow->consumeMotionMove(); |
| 5996 | secondWindow->consumeMotionDown(); |
| 5997 | |
| 5998 | // Send pointer cancel to the second window |
| 5999 | NotifyMotionArgs pointerUpMotionArgs = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 6000 | generateMotionArgs(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6001 | {pointInFirst, pointInSecond}); |
| 6002 | pointerUpMotionArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6003 | mDispatcher->notifyMotion(pointerUpMotionArgs); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6004 | // The first window gets move and the second gets cancel. |
| 6005 | firstWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 6006 | secondWindow->consumeMotionCancel(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 6007 | |
| 6008 | // Send up event. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6009 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6010 | ADISPLAY_ID_DEFAULT)); |
arthurhung | ea3f4fc | 2020-12-21 23:18:53 +0800 | [diff] [blame] | 6011 | // The first window gets up and the second gets nothing. |
| 6012 | firstWindow->consumeMotionUp(); |
| 6013 | secondWindow->assertNoEvents(); |
| 6014 | } |
| 6015 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6016 | TEST_F(InputDispatcherTest, SendTimeline_DoesNotCrashDispatcher) { |
| 6017 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6018 | |
| 6019 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6020 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6021 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6022 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 6023 | graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME] = 2; |
| 6024 | graphicsTimeline[GraphicsTimeline::PRESENT_TIME] = 3; |
| 6025 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6026 | window->sendTimeline(/*inputEventId=*/1, graphicsTimeline); |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 6027 | window->assertNoEvents(); |
| 6028 | mDispatcher->waitForIdle(); |
| 6029 | } |
| 6030 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6031 | using InputDispatcherMonitorTest = InputDispatcherTest; |
| 6032 | |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6033 | /** |
| 6034 | * Two entities that receive touch: A window, and a global monitor. |
| 6035 | * The touch goes to the window, and then the window disappears. |
| 6036 | * The monitor does not get cancel right away. But if more events come in, the touch gets canceled |
| 6037 | * for the monitor, as well. |
| 6038 | * 1. foregroundWindow |
| 6039 | * 2. monitor <-- global monitor (doesn't observe z order, receives all events) |
| 6040 | */ |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6041 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsCanceledWhenForegroundWindowDisappears) { |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6042 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6043 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6044 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6045 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6046 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6047 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6048 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6049 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6050 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6051 | {100, 200})) |
| 6052 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6053 | |
| 6054 | // Both the foreground window and the global monitor should receive the touch down |
| 6055 | window->consumeMotionDown(); |
| 6056 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6057 | |
| 6058 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6059 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6060 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 6061 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6062 | |
| 6063 | window->consumeMotionMove(); |
| 6064 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6065 | |
| 6066 | // Now the foreground window goes away |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6067 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6068 | window->consumeMotionCancel(); |
| 6069 | monitor.assertNoEvents(); // Global monitor does not get a cancel yet |
| 6070 | |
| 6071 | // If more events come in, there will be no more foreground window to send them to. This will |
| 6072 | // cause a cancel for the monitor, as well. |
| 6073 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6074 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | ca20550 | 2021-07-16 21:31:58 +0000 | [diff] [blame] | 6075 | ADISPLAY_ID_DEFAULT, {120, 200})) |
| 6076 | << "Injection should fail because the window was removed"; |
| 6077 | window->assertNoEvents(); |
| 6078 | // Global monitor now gets the cancel |
| 6079 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 6080 | } |
| 6081 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6082 | TEST_F(InputDispatcherMonitorTest, ReceivesMotionEvents) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6083 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6084 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6085 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6086 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6087 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6088 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6089 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6090 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6091 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6092 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6093 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6094 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6095 | } |
| 6096 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6097 | TEST_F(InputDispatcherMonitorTest, MonitorCannotPilferPointers) { |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6098 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6099 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6100 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6101 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6102 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6103 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6104 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6105 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6106 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6107 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 6108 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6109 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6110 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6111 | // Pilfer pointers from the monitor. |
| 6112 | // This should not do anything and the window should continue to receive events. |
| 6113 | EXPECT_NE(OK, mDispatcher->pilferPointers(monitor.getToken())); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6114 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6115 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6116 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6117 | ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6118 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6119 | |
| 6120 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6121 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
Michael Wright | 3a240c4 | 2019-12-10 20:53:41 +0000 | [diff] [blame] | 6122 | } |
| 6123 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6124 | TEST_F(InputDispatcherMonitorTest, NoWindowTransform) { |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6125 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6126 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6127 | "Fake Window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6128 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6129 | window->setWindowOffset(20, 40); |
| 6130 | window->setWindowTransform(0, 1, -1, 0); |
| 6131 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6132 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6133 | |
| 6134 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6135 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6136 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 6137 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6138 | std::unique_ptr<MotionEvent> event = monitor.consumeMotion(); |
| 6139 | ASSERT_NE(nullptr, event); |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 6140 | // Even though window has transform, gesture monitor must not. |
| 6141 | ASSERT_EQ(ui::Transform(), event->getTransform()); |
| 6142 | } |
| 6143 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6144 | TEST_F(InputDispatcherMonitorTest, InjectionFailsWithNoWindow) { |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6145 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 6146 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6147 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6148 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6149 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 6150 | << "Injection should fail if there is a monitor, but no touchable window"; |
| 6151 | monitor.assertNoEvents(); |
Arthur Hung | b3307ee | 2021-10-14 10:57:37 +0000 | [diff] [blame] | 6152 | } |
| 6153 | |
Linnan Li | d815095 | 2024-01-26 18:07:17 +0000 | [diff] [blame^] | 6154 | /** |
| 6155 | * Two displays |
| 6156 | * The first monitor has a foreground window, a monitor |
| 6157 | * The second window has only one monitor. |
| 6158 | * We first inject a Down event into the first display, this injection should succeed and both |
| 6159 | * the foreground window and monitor should receive a down event, then inject a Down event into |
| 6160 | * the second display as well, this injection should fail, at this point, the first display |
| 6161 | * window and monitor should not receive a cancel or any other event. |
| 6162 | * Continue to inject Move and UP events to the first display, the events should be received |
| 6163 | * normally by the foreground window and monitor. |
| 6164 | */ |
| 6165 | TEST_F(InputDispatcherMonitorTest, MonitorTouchIsNotCanceledWhenAnotherEmptyDisplayReceiveEvents) { |
| 6166 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6167 | sp<FakeWindowHandle> window = |
| 6168 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 6169 | |
| 6170 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6171 | FakeMonitorReceiver secondMonitor = FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
| 6172 | |
| 6173 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 6174 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6175 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6176 | {100, 200})) |
| 6177 | << "The down event injected into the first display should succeed"; |
| 6178 | |
| 6179 | window->consumeMotionDown(); |
| 6180 | monitor.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6181 | secondMonitor.assertNoEvents(); |
| 6182 | |
| 6183 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6184 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 6185 | {100, 200})) |
| 6186 | << "The down event injected into the second display should failed"; |
| 6187 | // Foreground window and monitor on the first display should not receive any events. |
| 6188 | window->assertNoEvents(); |
| 6189 | monitor.assertNoEvents(); |
| 6190 | secondMonitor.assertNoEvents(); |
| 6191 | |
| 6192 | // Continue to inject event to first display. |
| 6193 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6194 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6195 | ADISPLAY_ID_DEFAULT, {110, 220})) |
| 6196 | << "The move event injected into the first display should succeed"; |
| 6197 | |
| 6198 | window->consumeMotionMove(); |
| 6199 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6200 | secondMonitor.assertNoEvents(); |
| 6201 | |
| 6202 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6203 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6204 | {110, 220})) |
| 6205 | << "The up event injected into the first display should succeed"; |
| 6206 | |
| 6207 | window->consumeMotionUp(); |
| 6208 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 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); |
| 6247 | secondWindow->assertNoEvents(); |
| 6248 | secondMonitor.assertNoEvents(); |
| 6249 | |
| 6250 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6251 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, |
| 6252 | {100, 200})) |
| 6253 | << "The down event injected into the second display should succeed"; |
| 6254 | |
| 6255 | window->assertNoEvents(); |
| 6256 | monitor.assertNoEvents(); |
| 6257 | secondWindow->consumeMotionDown(SECOND_DISPLAY_ID); |
| 6258 | secondMonitor.consumeMotionDown(SECOND_DISPLAY_ID); |
| 6259 | |
| 6260 | // Now second window is gone away. |
| 6261 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
| 6262 | |
| 6263 | // The gone window should receive a cancel, and the monitor on the second display should not |
| 6264 | // receive any events. |
| 6265 | window->assertNoEvents(); |
| 6266 | monitor.assertNoEvents(); |
| 6267 | secondWindow->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 6268 | secondMonitor.assertNoEvents(); |
| 6269 | |
| 6270 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6271 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6272 | SECOND_DISPLAY_ID, {110, 220})) |
| 6273 | << "The move event injected into the second display should failed"; |
| 6274 | // Now the monitor on the second display should receive a cancel event. |
| 6275 | secondMonitor.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 6276 | // Other windows and monitors should not receive any events. |
| 6277 | window->assertNoEvents(); |
| 6278 | monitor.assertNoEvents(); |
| 6279 | secondWindow->assertNoEvents(); |
| 6280 | |
| 6281 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6282 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6283 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 6284 | << "The move event injected into the first display should succeed"; |
| 6285 | |
| 6286 | window->consumeMotionMove(); |
| 6287 | monitor.consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 6288 | secondWindow->assertNoEvents(); |
| 6289 | secondMonitor.assertNoEvents(); |
| 6290 | |
| 6291 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6292 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID, {110, |
| 6293 | 220})) |
| 6294 | << "The up event injected into the second display should failed"; |
| 6295 | |
| 6296 | window->assertNoEvents(); |
| 6297 | monitor.assertNoEvents(); |
| 6298 | secondWindow->assertNoEvents(); |
| 6299 | secondMonitor.assertNoEvents(); |
| 6300 | |
| 6301 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6302 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6303 | {110, 220})) |
| 6304 | << "The up event injected into the first display should succeed"; |
| 6305 | |
| 6306 | window->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6307 | monitor.consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 6308 | window->assertNoEvents(); |
| 6309 | monitor.assertNoEvents(); |
| 6310 | } |
| 6311 | |
| 6312 | /** |
| 6313 | * One display with transform |
| 6314 | * There is a foreground window and a monitor on the display |
| 6315 | * Inject down event and move event sequentially, the foreground window and monitor can receive down |
| 6316 | * event and move event, then let the foreground window go away, the foreground window receives |
| 6317 | * cancel event, inject move event again, the monitor receives cancel event, all the events received |
| 6318 | * by the monitor should be with the same transform as the display |
| 6319 | */ |
| 6320 | TEST_F(InputDispatcherMonitorTest, MonitorTouchCancelEventWithDisplayTransform) { |
| 6321 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6322 | sp<FakeWindowHandle> window = |
| 6323 | sp<FakeWindowHandle>::make(application, mDispatcher, "Foreground", ADISPLAY_ID_DEFAULT); |
| 6324 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
| 6325 | |
| 6326 | ui::Transform transform; |
| 6327 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 6328 | |
| 6329 | gui::DisplayInfo displayInfo; |
| 6330 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 6331 | displayInfo.transform = transform; |
| 6332 | |
| 6333 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
| 6334 | |
| 6335 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6336 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6337 | {100, 200})) |
| 6338 | << "The down event injected should succeed"; |
| 6339 | |
| 6340 | window->consumeMotionDown(); |
| 6341 | std::unique_ptr<MotionEvent> downMotionEvent = monitor.consumeMotion(); |
| 6342 | EXPECT_EQ(transform, downMotionEvent->getTransform()); |
| 6343 | EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, downMotionEvent->getAction()); |
| 6344 | |
| 6345 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6346 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6347 | ADISPLAY_ID_DEFAULT, {110, 220})) |
| 6348 | << "The move event injected should succeed"; |
| 6349 | |
| 6350 | window->consumeMotionMove(); |
| 6351 | std::unique_ptr<MotionEvent> moveMotionEvent = monitor.consumeMotion(); |
| 6352 | EXPECT_EQ(transform, moveMotionEvent->getTransform()); |
| 6353 | EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, moveMotionEvent->getAction()); |
| 6354 | |
| 6355 | // Let foreground window gone |
| 6356 | mDispatcher->onWindowInfosChanged({{}, {displayInfo}, 0, 0}); |
| 6357 | |
| 6358 | // Foreground window should receive a cancel event, but monitor not. |
| 6359 | window->consumeMotionCancel(); |
| 6360 | monitor.assertNoEvents(); |
| 6361 | |
| 6362 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6363 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 6364 | ADISPLAY_ID_DEFAULT, {110, 220})) |
| 6365 | << "The move event injected should failed"; |
| 6366 | // Now foreground should not receive any events, but monitor should receive a cancel event |
| 6367 | // with transform that same as display's display. |
| 6368 | window->assertNoEvents(); |
| 6369 | std::unique_ptr<MotionEvent> cancelMotionEvent = monitor.consumeMotion(); |
| 6370 | EXPECT_EQ(transform, cancelMotionEvent->getTransform()); |
| 6371 | EXPECT_EQ(ADISPLAY_ID_DEFAULT, cancelMotionEvent->getDisplayId()); |
| 6372 | EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, cancelMotionEvent->getAction()); |
| 6373 | |
| 6374 | // Other event inject to this display should fail. |
| 6375 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
| 6376 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 6377 | ADISPLAY_ID_DEFAULT, {110, 220})) |
| 6378 | << "The up event injected should failed"; |
| 6379 | window->assertNoEvents(); |
| 6380 | monitor.assertNoEvents(); |
| 6381 | } |
| 6382 | |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6383 | TEST_F(InputDispatcherTest, TestMoveEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6384 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6385 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6386 | "Fake Window", ADISPLAY_ID_DEFAULT); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6387 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6388 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6389 | |
| 6390 | NotifyMotionArgs motionArgs = |
| 6391 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6392 | ADISPLAY_ID_DEFAULT); |
| 6393 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6394 | mDispatcher->notifyMotion(motionArgs); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6395 | // Window should receive motion down event. |
| 6396 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 6397 | |
| 6398 | motionArgs.action = AMOTION_EVENT_ACTION_MOVE; |
Garfield Tan | c51d1ba | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 6399 | motionArgs.id += 1; |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6400 | motionArgs.eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 6401 | motionArgs.pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 6402 | motionArgs.pointerCoords[0].getX() - 10); |
| 6403 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6404 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 6405 | window->consumeMotionMove(ADISPLAY_ID_DEFAULT, /*expectedFlags=*/0); |
chaviw | 81e2bb9 | 2019-12-18 15:03:51 -0800 | [diff] [blame] | 6406 | } |
| 6407 | |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6408 | /** |
| 6409 | * Dispatcher has touch mode enabled by default. Typically, the policy overrides that value to |
| 6410 | * the device default right away. In the test scenario, we check both the default value, |
| 6411 | * and the action of enabling / disabling. |
| 6412 | */ |
| 6413 | TEST_F(InputDispatcherTest, TouchModeState_IsSentToApps) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6414 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6415 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6416 | "Test window", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6417 | const WindowInfo& windowInfo = *window->getInfo(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6418 | |
| 6419 | // Set focused application. |
| 6420 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6421 | window->setFocusable(true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6422 | |
| 6423 | SCOPED_TRACE("Check default value of touch mode"); |
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=*/true); |
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=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6432 | |
| 6433 | SCOPED_TRACE("Disable touch mode"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6434 | mDispatcher->setInTouchMode(false, 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(false); |
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=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6441 | |
| 6442 | SCOPED_TRACE("Remove the window to trigger focus loss"); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6443 | window->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6444 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6445 | window->consumeFocusEvent(/*hasFocus=*/false, /*inTouchMode=*/false); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6446 | |
| 6447 | SCOPED_TRACE("Enable touch mode again"); |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 6448 | mDispatcher->setInTouchMode(true, windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6449 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 6450 | window->consumeTouchModeEvent(true); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6451 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6452 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6453 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6454 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 6455 | |
| 6456 | window->assertNoEvents(); |
| 6457 | } |
| 6458 | |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6459 | TEST_F(InputDispatcherTest, VerifyInputEvent_KeyEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6460 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6461 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6462 | "Test window", ADISPLAY_ID_DEFAULT); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6463 | |
| 6464 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 6465 | window->setFocusable(true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6466 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6467 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6468 | setFocusedWindow(window); |
| 6469 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 6470 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6471 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6472 | const NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
| 6473 | mDispatcher->notifyKey(keyArgs); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6474 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6475 | std::unique_ptr<KeyEvent> event = window->consumeKey(); |
| 6476 | ASSERT_NE(event, nullptr); |
| 6477 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6478 | ASSERT_NE(verified, nullptr); |
| 6479 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::KEY); |
| 6480 | |
| 6481 | ASSERT_EQ(keyArgs.eventTime, verified->eventTimeNanos); |
| 6482 | ASSERT_EQ(keyArgs.deviceId, verified->deviceId); |
| 6483 | ASSERT_EQ(keyArgs.source, verified->source); |
| 6484 | ASSERT_EQ(keyArgs.displayId, verified->displayId); |
| 6485 | |
| 6486 | const VerifiedKeyEvent& verifiedKey = static_cast<const VerifiedKeyEvent&>(*verified); |
| 6487 | |
| 6488 | ASSERT_EQ(keyArgs.action, verifiedKey.action); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6489 | ASSERT_EQ(keyArgs.flags & VERIFIED_KEY_EVENT_FLAGS, verifiedKey.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6490 | ASSERT_EQ(keyArgs.downTime, verifiedKey.downTimeNanos); |
Gang Wang | e908789 | 2020-01-07 12:17:14 -0500 | [diff] [blame] | 6491 | ASSERT_EQ(keyArgs.keyCode, verifiedKey.keyCode); |
| 6492 | ASSERT_EQ(keyArgs.scanCode, verifiedKey.scanCode); |
| 6493 | ASSERT_EQ(keyArgs.metaState, verifiedKey.metaState); |
| 6494 | ASSERT_EQ(0, verifiedKey.repeatCount); |
| 6495 | } |
| 6496 | |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6497 | TEST_F(InputDispatcherTest, VerifyInputEvent_MotionEvent) { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 6498 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6499 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 6500 | "Test window", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6501 | |
| 6502 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6503 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 6504 | ui::Transform transform; |
| 6505 | transform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 6506 | |
| 6507 | gui::DisplayInfo displayInfo; |
| 6508 | displayInfo.displayId = ADISPLAY_ID_DEFAULT; |
| 6509 | displayInfo.transform = transform; |
| 6510 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 6511 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {displayInfo}, 0, 0}); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6512 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6513 | const NotifyMotionArgs motionArgs = |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6514 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 6515 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6516 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6517 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 6518 | std::unique_ptr<MotionEvent> event = window->consumeMotionEvent(); |
| 6519 | ASSERT_NE(nullptr, event); |
| 6520 | std::unique_ptr<VerifiedInputEvent> verified = mDispatcher->verifyInputEvent(*event); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6521 | ASSERT_NE(verified, nullptr); |
| 6522 | ASSERT_EQ(verified->type, VerifiedInputEvent::Type::MOTION); |
| 6523 | |
| 6524 | EXPECT_EQ(motionArgs.eventTime, verified->eventTimeNanos); |
| 6525 | EXPECT_EQ(motionArgs.deviceId, verified->deviceId); |
| 6526 | EXPECT_EQ(motionArgs.source, verified->source); |
| 6527 | EXPECT_EQ(motionArgs.displayId, verified->displayId); |
| 6528 | |
| 6529 | const VerifiedMotionEvent& verifiedMotion = static_cast<const VerifiedMotionEvent&>(*verified); |
| 6530 | |
Prabir Pradhan | b5cb957 | 2021-09-24 06:35:16 -0700 | [diff] [blame] | 6531 | const vec2 rawXY = |
| 6532 | MotionEvent::calculateTransformedXY(motionArgs.source, transform, |
| 6533 | motionArgs.pointerCoords[0].getXYValue()); |
| 6534 | EXPECT_EQ(rawXY.x, verifiedMotion.rawX); |
| 6535 | EXPECT_EQ(rawXY.y, verifiedMotion.rawY); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6536 | EXPECT_EQ(motionArgs.action & AMOTION_EVENT_ACTION_MASK, verifiedMotion.actionMasked); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6537 | EXPECT_EQ(motionArgs.flags & VERIFIED_MOTION_EVENT_FLAGS, verifiedMotion.flags); |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 6538 | EXPECT_EQ(motionArgs.downTime, verifiedMotion.downTimeNanos); |
Siarhei Vishniakou | 47040bf | 2020-02-28 15:03:13 -0800 | [diff] [blame] | 6539 | EXPECT_EQ(motionArgs.metaState, verifiedMotion.metaState); |
| 6540 | EXPECT_EQ(motionArgs.buttonState, verifiedMotion.buttonState); |
| 6541 | } |
| 6542 | |
chaviw | 09c8d2d | 2020-08-24 15:48:26 -0700 | [diff] [blame] | 6543 | /** |
| 6544 | * Ensure that separate calls to sign the same data are generating the same key. |
| 6545 | * We avoid asserting against INVALID_HMAC. Since the key is random, there is a non-zero chance |
| 6546 | * that a specific key and data combination would produce INVALID_HMAC, which would cause flaky |
| 6547 | * tests. |
| 6548 | */ |
| 6549 | TEST_F(InputDispatcherTest, GeneratedHmac_IsConsistent) { |
| 6550 | KeyEvent event = getTestKeyEvent(); |
| 6551 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 6552 | |
| 6553 | std::array<uint8_t, 32> hmac1 = mDispatcher->sign(verifiedEvent); |
| 6554 | std::array<uint8_t, 32> hmac2 = mDispatcher->sign(verifiedEvent); |
| 6555 | ASSERT_EQ(hmac1, hmac2); |
| 6556 | } |
| 6557 | |
| 6558 | /** |
| 6559 | * Ensure that changes in VerifiedKeyEvent produce a different hmac. |
| 6560 | */ |
| 6561 | TEST_F(InputDispatcherTest, GeneratedHmac_ChangesWhenFieldsChange) { |
| 6562 | KeyEvent event = getTestKeyEvent(); |
| 6563 | VerifiedKeyEvent verifiedEvent = verifiedKeyEventFromKeyEvent(event); |
| 6564 | std::array<uint8_t, 32> initialHmac = mDispatcher->sign(verifiedEvent); |
| 6565 | |
| 6566 | verifiedEvent.deviceId += 1; |
| 6567 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6568 | |
| 6569 | verifiedEvent.source += 1; |
| 6570 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6571 | |
| 6572 | verifiedEvent.eventTimeNanos += 1; |
| 6573 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6574 | |
| 6575 | verifiedEvent.displayId += 1; |
| 6576 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6577 | |
| 6578 | verifiedEvent.action += 1; |
| 6579 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6580 | |
| 6581 | verifiedEvent.downTimeNanos += 1; |
| 6582 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6583 | |
| 6584 | verifiedEvent.flags += 1; |
| 6585 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6586 | |
| 6587 | verifiedEvent.keyCode += 1; |
| 6588 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6589 | |
| 6590 | verifiedEvent.scanCode += 1; |
| 6591 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6592 | |
| 6593 | verifiedEvent.metaState += 1; |
| 6594 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6595 | |
| 6596 | verifiedEvent.repeatCount += 1; |
| 6597 | ASSERT_NE(initialHmac, mDispatcher->sign(verifiedEvent)); |
| 6598 | } |
| 6599 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6600 | TEST_F(InputDispatcherTest, SetFocusedWindow) { |
| 6601 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6602 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6603 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6604 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6605 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6606 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6607 | |
| 6608 | // Top window is also focusable but is not granted focus. |
| 6609 | windowTop->setFocusable(true); |
| 6610 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6611 | mDispatcher->onWindowInfosChanged( |
| 6612 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6613 | setFocusedWindow(windowSecond); |
| 6614 | |
| 6615 | windowSecond->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6616 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6617 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6618 | |
| 6619 | // Focused window should receive event. |
| 6620 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6621 | windowTop->assertNoEvents(); |
| 6622 | } |
| 6623 | |
| 6624 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestInvalidChannel) { |
| 6625 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6626 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6627 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6628 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6629 | |
| 6630 | window->setFocusable(true); |
| 6631 | // Release channel for window is no longer valid. |
| 6632 | window->releaseChannel(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6633 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6634 | setFocusedWindow(window); |
| 6635 | |
| 6636 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6637 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6638 | |
| 6639 | // window channel is invalid, so it should not receive any input event. |
| 6640 | window->assertNoEvents(); |
| 6641 | } |
| 6642 | |
| 6643 | TEST_F(InputDispatcherTest, SetFocusedWindow_DropRequestNoFocusableWindow) { |
| 6644 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6645 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6646 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6647 | window->setFocusable(false); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6648 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6649 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6650 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6651 | setFocusedWindow(window); |
| 6652 | |
| 6653 | // Test inject a key down, should timeout. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6654 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6655 | |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 6656 | // window is not focusable, so it should not receive any input event. |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6657 | window->assertNoEvents(); |
| 6658 | } |
| 6659 | |
| 6660 | TEST_F(InputDispatcherTest, SetFocusedWindow_CheckFocusedToken) { |
| 6661 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6662 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6663 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6664 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6665 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6666 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6667 | |
| 6668 | windowTop->setFocusable(true); |
| 6669 | windowSecond->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6670 | mDispatcher->onWindowInfosChanged( |
| 6671 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6672 | setFocusedWindow(windowTop); |
| 6673 | windowTop->consumeFocusEvent(true); |
| 6674 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6675 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6676 | mDispatcher->onWindowInfosChanged( |
| 6677 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6678 | windowSecond->consumeFocusEvent(true); |
| 6679 | windowTop->consumeFocusEvent(false); |
| 6680 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6681 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6682 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6683 | |
| 6684 | // Focused window should receive event. |
| 6685 | windowSecond->consumeKeyDown(ADISPLAY_ID_NONE); |
| 6686 | } |
| 6687 | |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6688 | TEST_F(InputDispatcherTest, SetFocusedWindow_TransferFocusTokenNotFocusable) { |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6689 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6690 | sp<FakeWindowHandle> windowTop = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6691 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6692 | sp<FakeWindowHandle> windowSecond = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6693 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6694 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6695 | |
| 6696 | windowTop->setFocusable(true); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6697 | windowSecond->setFocusable(false); |
| 6698 | windowTop->editInfo()->focusTransferTarget = windowSecond->getToken(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6699 | mDispatcher->onWindowInfosChanged( |
| 6700 | {{*windowTop->getInfo(), *windowSecond->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6701 | setFocusedWindow(windowTop); |
| 6702 | windowTop->consumeFocusEvent(true); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6703 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6704 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6705 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6706 | |
| 6707 | // Event should be dropped. |
Chavi Weingarten | 847e851 | 2023-03-29 00:26:09 +0000 | [diff] [blame] | 6708 | windowTop->consumeKeyDown(ADISPLAY_ID_NONE); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6709 | windowSecond->assertNoEvents(); |
| 6710 | } |
| 6711 | |
| 6712 | TEST_F(InputDispatcherTest, SetFocusedWindow_DeferInvisibleWindow) { |
| 6713 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6714 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6715 | sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6716 | sp<FakeWindowHandle> previousFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6717 | sp<FakeWindowHandle>::make(application, mDispatcher, "previousFocusedWindow", |
| 6718 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6719 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6720 | |
| 6721 | window->setFocusable(true); |
| 6722 | previousFocusedWindow->setFocusable(true); |
| 6723 | window->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6724 | mDispatcher->onWindowInfosChanged( |
| 6725 | {{*window->getInfo(), *previousFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6726 | setFocusedWindow(previousFocusedWindow); |
| 6727 | previousFocusedWindow->consumeFocusEvent(true); |
| 6728 | |
| 6729 | // Requesting focus on invisible window takes focus from currently focused window. |
| 6730 | setFocusedWindow(window); |
| 6731 | previousFocusedWindow->consumeFocusEvent(false); |
| 6732 | |
| 6733 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 6734 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 6735 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 6736 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6737 | |
| 6738 | // Window does not get focus event or key down. |
| 6739 | window->assertNoEvents(); |
| 6740 | |
| 6741 | // Window becomes visible. |
| 6742 | window->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6743 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 6744 | |
| 6745 | // Window receives focus event. |
| 6746 | window->consumeFocusEvent(true); |
| 6747 | // Focused window receives key down. |
| 6748 | window->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 6749 | } |
| 6750 | |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6751 | TEST_F(InputDispatcherTest, DisplayRemoved) { |
| 6752 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6753 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6754 | sp<FakeWindowHandle>::make(application, mDispatcher, "window", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6755 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6756 | |
| 6757 | // window is granted focus. |
| 6758 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6759 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 599f141 | 2021-06-21 10:39:58 -0700 | [diff] [blame] | 6760 | setFocusedWindow(window); |
| 6761 | window->consumeFocusEvent(true); |
| 6762 | |
| 6763 | // When a display is removed window loses focus. |
| 6764 | mDispatcher->displayRemoved(ADISPLAY_ID_DEFAULT); |
| 6765 | window->consumeFocusEvent(false); |
| 6766 | } |
| 6767 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6768 | /** |
| 6769 | * Launch two windows, with different owners. One window (slipperyExitWindow) has Flag::SLIPPERY, |
| 6770 | * and overlaps the other window, slipperyEnterWindow. The window 'slipperyExitWindow' is on top |
| 6771 | * of the 'slipperyEnterWindow'. |
| 6772 | * |
| 6773 | * Inject touch down into the top window. Upon receipt of the DOWN event, move the window in such |
| 6774 | * a way so that the touched location is no longer covered by the top window. |
| 6775 | * |
| 6776 | * Next, inject a MOVE event. Because the top window already moved earlier, this event is now |
| 6777 | * positioned over the bottom (slipperyEnterWindow) only. And because the top window had |
| 6778 | * Flag::SLIPPERY, this will cause the top window to lose the touch event (it will receive |
| 6779 | * ACTION_CANCEL instead), and the bottom window will receive a newly generated gesture (starting |
| 6780 | * with ACTION_DOWN). |
| 6781 | * Thus, the touch has been transferred from the top window into the bottom window, because the top |
| 6782 | * window moved itself away from the touched location and had Flag::SLIPPERY. |
| 6783 | * |
| 6784 | * Even though the top window moved away from the touched location, it is still obscuring the bottom |
| 6785 | * window. It's just not obscuring it at the touched location. That means, FLAG_WINDOW_IS_PARTIALLY_ |
| 6786 | * OBSCURED should be set for the MotionEvent that reaches the bottom window. |
| 6787 | * |
| 6788 | * In this test, we ensure that the event received by the bottom window has |
| 6789 | * FLAG_WINDOW_IS_PARTIALLY_OBSCURED. |
| 6790 | */ |
| 6791 | TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6792 | constexpr gui::Pid SLIPPERY_PID{WINDOW_PID.val() + 1}; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6793 | constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1}; |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6794 | |
| 6795 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6796 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 6797 | |
| 6798 | sp<FakeWindowHandle> slipperyExitWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6799 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 6800 | slipperyExitWindow->setSlippery(true); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6801 | // Make sure this one overlaps the bottom window |
| 6802 | slipperyExitWindow->setFrame(Rect(25, 25, 75, 75)); |
| 6803 | // Change the owner uid/pid of the window so that it is considered to be occluding the bottom |
| 6804 | // one. Windows with the same owner are not considered to be occluding each other. |
| 6805 | slipperyExitWindow->setOwnerInfo(SLIPPERY_PID, SLIPPERY_UID); |
| 6806 | |
| 6807 | sp<FakeWindowHandle> slipperyEnterWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 6808 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6809 | slipperyExitWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6810 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6811 | mDispatcher->onWindowInfosChanged( |
| 6812 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6813 | |
| 6814 | // Use notifyMotion instead of injecting to avoid dealing with injection permissions |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6815 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 6816 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6817 | {{50, 50}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6818 | slipperyExitWindow->consumeMotionDown(); |
| 6819 | slipperyExitWindow->setFrame(Rect(70, 70, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6820 | mDispatcher->onWindowInfosChanged( |
| 6821 | {{*slipperyExitWindow->getInfo(), *slipperyEnterWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6822 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 6823 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_MOVE, |
| 6824 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 6825 | {{51, 51}})); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 6826 | |
| 6827 | slipperyExitWindow->consumeMotionCancel(); |
| 6828 | |
| 6829 | slipperyEnterWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT, |
| 6830 | AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 6831 | } |
| 6832 | |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 6833 | /** |
| 6834 | * Two windows, one on the left and another on the right. The left window is slippery. The right |
| 6835 | * window isn't eligible to receive touch because it specifies InputConfig::DROP_INPUT. When the |
| 6836 | * touch moves from the left window into the right window, the gesture should continue to go to the |
| 6837 | * left window. Touch shouldn't slip because the right window can't receive touches. This test |
| 6838 | * reproduces a crash. |
| 6839 | */ |
| 6840 | TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { |
| 6841 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6842 | |
| 6843 | sp<FakeWindowHandle> leftSlipperyWindow = |
| 6844 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 6845 | leftSlipperyWindow->setSlippery(true); |
| 6846 | leftSlipperyWindow->setFrame(Rect(0, 0, 100, 100)); |
| 6847 | |
| 6848 | sp<FakeWindowHandle> rightDropTouchesWindow = |
| 6849 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 6850 | rightDropTouchesWindow->setFrame(Rect(100, 0, 200, 100)); |
| 6851 | rightDropTouchesWindow->setDropInput(true); |
| 6852 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6853 | mDispatcher->onWindowInfosChanged( |
| 6854 | {{*leftSlipperyWindow->getInfo(), *rightDropTouchesWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | afa08cc | 2023-05-08 22:35:50 -0700 | [diff] [blame] | 6855 | |
| 6856 | // Start touch in the left window |
| 6857 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6858 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6859 | .build()); |
| 6860 | leftSlipperyWindow->consumeMotionDown(); |
| 6861 | |
| 6862 | // And move it into the right window |
| 6863 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6864 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 6865 | .build()); |
| 6866 | |
| 6867 | // Since the right window isn't eligible to receive input, touch does not slip. |
| 6868 | // The left window continues to receive the gesture. |
| 6869 | leftSlipperyWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 6870 | rightDropTouchesWindow->assertNoEvents(); |
| 6871 | } |
| 6872 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 6873 | /** |
| 6874 | * A single window is on screen first. Touch is injected into that window. Next, a second window |
| 6875 | * appears. Since the first window is slippery, touch will move from the first window to the second. |
| 6876 | */ |
| 6877 | TEST_F(InputDispatcherTest, InjectedTouchSlips) { |
| 6878 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6879 | sp<FakeWindowHandle> originalWindow = |
| 6880 | sp<FakeWindowHandle>::make(application, mDispatcher, "Original", ADISPLAY_ID_DEFAULT); |
| 6881 | originalWindow->setFrame(Rect(0, 0, 200, 200)); |
| 6882 | originalWindow->setSlippery(true); |
| 6883 | |
| 6884 | sp<FakeWindowHandle> appearingWindow = |
| 6885 | sp<FakeWindowHandle>::make(application, mDispatcher, "Appearing", ADISPLAY_ID_DEFAULT); |
| 6886 | appearingWindow->setFrame(Rect(0, 0, 200, 200)); |
| 6887 | |
| 6888 | mDispatcher->onWindowInfosChanged({{*originalWindow->getInfo()}, {}, 0, 0}); |
| 6889 | |
| 6890 | // Touch down on the original window |
| 6891 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6892 | injectMotionEvent(*mDispatcher, |
| 6893 | MotionEventBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6894 | .pointer(PointerBuilder(1, ToolType::FINGER).x(100).y(100)) |
| 6895 | .build())); |
| 6896 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 6897 | |
| 6898 | // Now, a new window appears. This could be, for example, a notification shade that appears |
| 6899 | // after user starts to drag down on the launcher window. |
| 6900 | mDispatcher->onWindowInfosChanged( |
| 6901 | {{*appearingWindow->getInfo(), *originalWindow->getInfo()}, {}, 0, 0}); |
| 6902 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6903 | injectMotionEvent(*mDispatcher, |
| 6904 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6905 | .pointer(PointerBuilder(1, ToolType::FINGER).x(110).y(110)) |
| 6906 | .build())); |
| 6907 | originalWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 6908 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 6909 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 6910 | injectMotionEvent(*mDispatcher, |
| 6911 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6912 | .pointer(PointerBuilder(1, ToolType::FINGER).x(120).y(120)) |
| 6913 | .build())); |
| 6914 | appearingWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 6915 | |
| 6916 | originalWindow->assertNoEvents(); |
| 6917 | appearingWindow->assertNoEvents(); |
| 6918 | } |
| 6919 | |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6920 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6921 | using Uid = gui::Uid; |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6922 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 6923 | |
| 6924 | sp<FakeWindowHandle> leftWindow = |
| 6925 | sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); |
| 6926 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6927 | leftWindow->setOwnerInfo(gui::Pid{1}, Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6928 | |
| 6929 | sp<FakeWindowHandle> rightSpy = |
| 6930 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT); |
| 6931 | rightSpy->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6932 | rightSpy->setOwnerInfo(gui::Pid{2}, Uid{102}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6933 | rightSpy->setSpy(true); |
| 6934 | rightSpy->setTrustedOverlay(true); |
| 6935 | |
| 6936 | sp<FakeWindowHandle> rightWindow = |
| 6937 | sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); |
| 6938 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 6939 | rightWindow->setOwnerInfo(gui::Pid{3}, Uid{103}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6940 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 6941 | mDispatcher->onWindowInfosChanged( |
| 6942 | {{*rightSpy->getInfo(), *rightWindow->getInfo(), *leftWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6943 | |
| 6944 | // Touch in the left window |
| 6945 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6946 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6947 | .build()); |
| 6948 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown()); |
| 6949 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6950 | ASSERT_NO_FATAL_FAILURE( |
| 6951 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6952 | |
| 6953 | // Touch another finger over the right windows |
| 6954 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 6955 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6956 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6957 | .build()); |
| 6958 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionDown()); |
| 6959 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionDown()); |
| 6960 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove()); |
| 6961 | mDispatcher->waitForIdle(); |
| 6962 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6963 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, |
| 6964 | {Uid{101}, Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6965 | |
| 6966 | // Release finger over left window. The UP actions are not treated as device interaction. |
| 6967 | // The windows that did not receive the UP pointer will receive MOVE events, but since this |
| 6968 | // is part of the UP action, we do not treat this as device interaction. |
| 6969 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_0_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6970 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 6971 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6972 | .build()); |
| 6973 | ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionUp()); |
| 6974 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 6975 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 6976 | mDispatcher->waitForIdle(); |
| 6977 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 6978 | |
| 6979 | // Move remaining finger |
| 6980 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 6981 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6982 | .build()); |
| 6983 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionMove()); |
| 6984 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); |
| 6985 | mDispatcher->waitForIdle(); |
| 6986 | ASSERT_NO_FATAL_FAILURE( |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 6987 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 6988 | |
| 6989 | // Release all fingers |
| 6990 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 6991 | .pointer(PointerBuilder(1, ToolType::FINGER).x(150).y(50)) |
| 6992 | .build()); |
| 6993 | ASSERT_NO_FATAL_FAILURE(rightSpy->consumeMotionUp()); |
| 6994 | ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionUp()); |
| 6995 | mDispatcher->waitForIdle(); |
| 6996 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 6997 | } |
| 6998 | |
| 6999 | TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) { |
| 7000 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7001 | |
| 7002 | sp<FakeWindowHandle> window = |
| 7003 | sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 7004 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 7005 | window->setOwnerInfo(gui::Pid{1}, gui::Uid{101}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7006 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7007 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7008 | setFocusedWindow(window); |
| 7009 | ASSERT_NO_FATAL_FAILURE(window->consumeFocusEvent(true)); |
| 7010 | |
| 7011 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build()); |
| 7012 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT)); |
| 7013 | mDispatcher->waitForIdle(); |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 7014 | ASSERT_NO_FATAL_FAILURE( |
| 7015 | mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}})); |
Prabir Pradhan | 8ede1d1 | 2023-05-08 19:37:44 +0000 | [diff] [blame] | 7016 | |
| 7017 | // The UP actions are not treated as device interaction. |
| 7018 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build()); |
| 7019 | ASSERT_NO_FATAL_FAILURE(window->consumeKeyUp(ADISPLAY_ID_DEFAULT)); |
| 7020 | mDispatcher->waitForIdle(); |
| 7021 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasNotCalled()); |
| 7022 | } |
| 7023 | |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7024 | TEST_F(InputDispatcherTest, HoverEnterExitSynthesisUsesNewEventId) { |
| 7025 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 7026 | |
| 7027 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 7028 | ADISPLAY_ID_DEFAULT); |
| 7029 | left->setFrame(Rect(0, 0, 100, 100)); |
| 7030 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 7031 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 7032 | right->setFrame(Rect(100, 0, 200, 100)); |
| 7033 | sp<FakeWindowHandle> spy = |
| 7034 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT); |
| 7035 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 7036 | spy->setTrustedOverlay(true); |
| 7037 | spy->setSpy(true); |
| 7038 | |
| 7039 | mDispatcher->onWindowInfosChanged( |
| 7040 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 7041 | |
| 7042 | // Send hover move to the left window, and ensure hover enter is synthesized with a new eventId. |
| 7043 | NotifyMotionArgs notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, |
| 7044 | ADISPLAY_ID_DEFAULT, {PointF{50, 50}}); |
| 7045 | mDispatcher->notifyMotion(notifyArgs); |
| 7046 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7047 | std::unique_ptr<MotionEvent> leftEnter = left->consumeMotionEvent( |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7048 | AllOf(WithMotionAction(ACTION_HOVER_ENTER), 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, leftEnter); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7051 | spy->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(leftEnter->getId())), |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7054 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 7055 | |
| 7056 | // Send move to the right window, and ensure hover exit and enter are synthesized with new ids. |
| 7057 | notifyArgs = generateMotionArgs(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT, |
| 7058 | {PointF{150, 50}}); |
| 7059 | mDispatcher->notifyMotion(notifyArgs); |
| 7060 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7061 | std::unique_ptr<MotionEvent> leftExit = left->consumeMotionEvent( |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7062 | AllOf(WithMotionAction(ACTION_HOVER_EXIT), Not(WithEventId(notifyArgs.id)), |
| 7063 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7064 | ASSERT_NE(nullptr, leftExit); |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7065 | right->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER), |
| 7066 | Not(WithEventId(notifyArgs.id)), |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7067 | Not(WithEventId(leftExit->getId())), |
Prabir Pradhan | 5893d36 | 2023-11-17 04:30:40 +0000 | [diff] [blame] | 7068 | WithEventIdSource(IdGenerator::Source::INPUT_DISPATCHER))); |
| 7069 | |
| 7070 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithEventId(notifyArgs.id))); |
| 7071 | } |
| 7072 | |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 7073 | class InputDispatcherFallbackKeyTest : public InputDispatcherTest { |
| 7074 | protected: |
| 7075 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 7076 | sp<FakeWindowHandle> mWindow; |
| 7077 | |
| 7078 | virtual void SetUp() override { |
| 7079 | InputDispatcherTest::SetUp(); |
| 7080 | |
| 7081 | mApp = std::make_shared<FakeApplicationHandle>(); |
| 7082 | |
| 7083 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); |
| 7084 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
| 7085 | |
| 7086 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 7087 | setFocusedWindow(mWindow); |
| 7088 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeFocusEvent(/*hasFocus=*/true)); |
| 7089 | } |
| 7090 | |
| 7091 | void setFallback(int32_t keycode) { |
| 7092 | mFakePolicy->setUnhandledKeyHandler([keycode](const KeyEvent& event) { |
| 7093 | return KeyEventBuilder(event).keyCode(keycode).build(); |
| 7094 | }); |
| 7095 | } |
| 7096 | |
| 7097 | void consumeKey(bool handled, const ::testing::Matcher<KeyEvent>& matcher) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7098 | std::unique_ptr<KeyEvent> event = mWindow->consumeKey(handled); |
| 7099 | ASSERT_NE(nullptr, event); |
| 7100 | ASSERT_THAT(*event, matcher); |
Prabir Pradhan | b0dad3a | 2023-11-02 20:52:47 +0000 | [diff] [blame] | 7101 | } |
| 7102 | }; |
| 7103 | |
| 7104 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotNotifiedForHandledKey) { |
| 7105 | mDispatcher->notifyKey( |
| 7106 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7107 | consumeKey(/*handled=*/true, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 7108 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7109 | } |
| 7110 | |
| 7111 | TEST_F(InputDispatcherFallbackKeyTest, PolicyNotifiedForUnhandledKey) { |
| 7112 | mDispatcher->notifyKey( |
| 7113 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7114 | consumeKey(/*handled=*/false, AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A))); |
| 7115 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7116 | } |
| 7117 | |
| 7118 | TEST_F(InputDispatcherFallbackKeyTest, NoFallbackRequestedByPolicy) { |
| 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 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7126 | |
| 7127 | // Since the policy did not request any fallback to be generated, ensure there are no events. |
| 7128 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7129 | } |
| 7130 | |
| 7131 | TEST_F(InputDispatcherFallbackKeyTest, FallbackDispatchForUnhandledKey) { |
| 7132 | setFallback(AKEYCODE_B); |
| 7133 | mDispatcher->notifyKey( |
| 7134 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7135 | |
| 7136 | // Do not handle this key event. |
| 7137 | consumeKey(/*handled=*/false, |
| 7138 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7139 | |
| 7140 | // Since the key was not handled, ensure the fallback event was dispatched instead. |
| 7141 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7142 | consumeKey(/*handled=*/true, |
| 7143 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7144 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7145 | |
| 7146 | // Release the original key, and ensure the fallback key is also released. |
| 7147 | mDispatcher->notifyKey( |
| 7148 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7149 | consumeKey(/*handled=*/false, |
| 7150 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7151 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7152 | consumeKey(/*handled=*/true, |
| 7153 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7154 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7155 | |
| 7156 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7157 | mWindow->assertNoEvents(); |
| 7158 | } |
| 7159 | |
| 7160 | TEST_F(InputDispatcherFallbackKeyTest, AppHandlesPreviouslyUnhandledKey) { |
| 7161 | setFallback(AKEYCODE_B); |
| 7162 | mDispatcher->notifyKey( |
| 7163 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7164 | |
| 7165 | // Do not handle this key event, but handle the fallback. |
| 7166 | consumeKey(/*handled=*/false, |
| 7167 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7168 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7169 | consumeKey(/*handled=*/true, |
| 7170 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7171 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7172 | |
| 7173 | // Release the original key, and ensure the fallback key is also released. |
| 7174 | mDispatcher->notifyKey( |
| 7175 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7176 | // But this time, the app handles the original key. |
| 7177 | consumeKey(/*handled=*/true, |
| 7178 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7179 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7180 | // Ensure the fallback key is canceled. |
| 7181 | consumeKey(/*handled=*/true, |
| 7182 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7183 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 7184 | |
| 7185 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7186 | mWindow->assertNoEvents(); |
| 7187 | } |
| 7188 | |
| 7189 | TEST_F(InputDispatcherFallbackKeyTest, AppDoesNotHandleFallback) { |
| 7190 | setFallback(AKEYCODE_B); |
| 7191 | mDispatcher->notifyKey( |
| 7192 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7193 | |
| 7194 | // Do not handle this key event. |
| 7195 | consumeKey(/*handled=*/false, |
| 7196 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7197 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7198 | // App does not handle the fallback either, so ensure another fallback is not generated. |
| 7199 | setFallback(AKEYCODE_C); |
| 7200 | consumeKey(/*handled=*/false, |
| 7201 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7202 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7203 | |
| 7204 | // Release the original key, and ensure the fallback key is also released. |
| 7205 | setFallback(AKEYCODE_B); |
| 7206 | mDispatcher->notifyKey( |
| 7207 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7208 | consumeKey(/*handled=*/false, |
| 7209 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7210 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7211 | consumeKey(/*handled=*/false, |
| 7212 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7213 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7214 | |
| 7215 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7216 | mWindow->assertNoEvents(); |
| 7217 | } |
| 7218 | |
| 7219 | TEST_F(InputDispatcherFallbackKeyTest, InconsistentPolicyCancelsFallback) { |
| 7220 | setFallback(AKEYCODE_B); |
| 7221 | mDispatcher->notifyKey( |
| 7222 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7223 | |
| 7224 | // Do not handle this key event, so fallback is generated. |
| 7225 | consumeKey(/*handled=*/false, |
| 7226 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7227 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7228 | consumeKey(/*handled=*/true, |
| 7229 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7230 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7231 | |
| 7232 | // Release the original key, but assume the policy is misbehaving and it |
| 7233 | // generates an inconsistent fallback to the one from the DOWN event. |
| 7234 | setFallback(AKEYCODE_C); |
| 7235 | mDispatcher->notifyKey( |
| 7236 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7237 | consumeKey(/*handled=*/false, |
| 7238 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7239 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7240 | // Ensure the fallback key reported before as DOWN is canceled due to the inconsistency. |
| 7241 | consumeKey(/*handled=*/true, |
| 7242 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7243 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 7244 | |
| 7245 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7246 | mWindow->assertNoEvents(); |
| 7247 | } |
| 7248 | |
| 7249 | TEST_F(InputDispatcherFallbackKeyTest, CanceledKeyCancelsFallback) { |
| 7250 | setFallback(AKEYCODE_B); |
| 7251 | mDispatcher->notifyKey( |
| 7252 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7253 | |
| 7254 | // Do not handle this key event, so fallback is generated. |
| 7255 | consumeKey(/*handled=*/false, |
| 7256 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7257 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7258 | consumeKey(/*handled=*/true, |
| 7259 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7260 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7261 | |
| 7262 | // The original key is canceled. |
| 7263 | mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
| 7264 | .keyCode(AKEYCODE_A) |
| 7265 | .addFlag(AKEY_EVENT_FLAG_CANCELED) |
| 7266 | .build()); |
| 7267 | consumeKey(/*handled=*/false, |
| 7268 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), |
| 7269 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 7270 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7271 | // Ensure the fallback key is also canceled due to the original key being canceled. |
| 7272 | consumeKey(/*handled=*/true, |
| 7273 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_B), |
| 7274 | WithFlags(AKEY_EVENT_FLAG_FALLBACK | AKEY_EVENT_FLAG_CANCELED))); |
| 7275 | |
| 7276 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyNotReported()); |
| 7277 | mWindow->assertNoEvents(); |
| 7278 | } |
| 7279 | |
Prabir Pradhan | b13da8f | 2024-01-09 23:10:13 +0000 | [diff] [blame] | 7280 | TEST_F(InputDispatcherFallbackKeyTest, WindowRemovedDuringPolicyCall) { |
| 7281 | setFallback(AKEYCODE_B); |
| 7282 | mDispatcher->notifyKey( |
| 7283 | KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7284 | |
| 7285 | // Do not handle this key event. |
| 7286 | consumeKey(/*handled=*/false, |
| 7287 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7288 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7289 | consumeKey(/*handled=*/true, |
| 7290 | AllOf(WithKeyAction(ACTION_DOWN), WithKeyCode(AKEYCODE_B), |
| 7291 | WithFlags(AKEY_EVENT_FLAG_FALLBACK))); |
| 7292 | |
| 7293 | mFakePolicy->setUnhandledKeyHandler([&](const KeyEvent& event) { |
| 7294 | // When the unhandled key is reported to the policy next, remove the input channel. |
| 7295 | mDispatcher->removeInputChannel(mWindow->getToken()); |
| 7296 | return KeyEventBuilder(event).keyCode(AKEYCODE_B).build(); |
| 7297 | }); |
| 7298 | // Release the original key, and let the app now handle the previously unhandled key. |
| 7299 | // This should result in the previously generated fallback key to be cancelled. |
| 7300 | // Since the policy was notified of the unhandled DOWN event earlier, it will also be notified |
| 7301 | // of the UP event for consistency. The Dispatcher calls into the policy from its own thread |
| 7302 | // without holding the lock, because it need to synchronously fetch the fallback key. While in |
| 7303 | // the policy call, we will now remove the input channel. Once the policy call returns, the |
| 7304 | // Dispatcher will no longer have a channel to send cancellation events to. Ensure this does |
| 7305 | // not cause any crashes. |
| 7306 | mDispatcher->notifyKey( |
| 7307 | KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build()); |
| 7308 | consumeKey(/*handled=*/true, |
| 7309 | AllOf(WithKeyAction(ACTION_UP), WithKeyCode(AKEYCODE_A), WithFlags(0))); |
| 7310 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertUnhandledKeyReported(AKEYCODE_A)); |
| 7311 | } |
| 7312 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7313 | class InputDispatcherKeyRepeatTest : public InputDispatcherTest { |
| 7314 | protected: |
Siarhei Vishniakou | fa2a049 | 2023-11-14 13:13:18 -0800 | [diff] [blame] | 7315 | static constexpr std::chrono::nanoseconds KEY_REPEAT_TIMEOUT = 40ms; |
| 7316 | static constexpr std::chrono::nanoseconds KEY_REPEAT_DELAY = 40ms; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7317 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7318 | std::shared_ptr<FakeApplicationHandle> mApp; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7319 | sp<FakeWindowHandle> mWindow; |
| 7320 | |
| 7321 | virtual void SetUp() override { |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 7322 | mFakePolicy = std::make_unique<FakeInputDispatcherPolicy>(); |
Prabir Pradhan | a41d244 | 2023-04-20 21:30:40 +0000 | [diff] [blame] | 7323 | mDispatcher = std::make_unique<InputDispatcher>(*mFakePolicy); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7324 | mDispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false); |
Nergi Rahardi | 730cf3c | 2023-04-13 12:41:17 +0900 | [diff] [blame] | 7325 | mDispatcher->setKeyRepeatConfiguration(KEY_REPEAT_TIMEOUT, KEY_REPEAT_DELAY); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7326 | ASSERT_EQ(OK, mDispatcher->start()); |
| 7327 | |
| 7328 | setUpWindow(); |
| 7329 | } |
| 7330 | |
| 7331 | void setUpWindow() { |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7332 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7333 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "Fake Window", ADISPLAY_ID_DEFAULT); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7334 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7335 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7336 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7337 | setFocusedWindow(mWindow); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7338 | mWindow->consumeFocusEvent(true); |
| 7339 | } |
| 7340 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7341 | void sendAndConsumeKeyDown(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7342 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7343 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7344 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Otherwise it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7345 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7346 | |
| 7347 | // Window should receive key down event. |
| 7348 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7349 | } |
| 7350 | |
| 7351 | void expectKeyRepeatOnce(int32_t repeatCount) { |
| 7352 | SCOPED_TRACE(StringPrintf("Checking event with repeat count %" PRId32, repeatCount)); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7353 | mWindow->consumeKeyEvent( |
| 7354 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithRepeatCount(repeatCount))); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7355 | } |
| 7356 | |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7357 | void sendAndConsumeKeyUp(int32_t deviceId) { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7358 | NotifyKeyArgs keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7359 | keyArgs.deviceId = deviceId; |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7360 | keyArgs.policyFlags |= POLICY_FLAG_TRUSTED; // Unless it won't generate repeat event |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7361 | mDispatcher->notifyKey(keyArgs); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7362 | |
| 7363 | // Window should receive key down event. |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7364 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7365 | /*expectedFlags=*/0); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7366 | } |
| 7367 | }; |
| 7368 | |
| 7369 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeat) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7370 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7371 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 7372 | expectKeyRepeatOnce(repeatCount); |
| 7373 | } |
| 7374 | } |
| 7375 | |
| 7376 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_ReceivesKeyRepeatFromTwoDevices) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7377 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7378 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 7379 | expectKeyRepeatOnce(repeatCount); |
| 7380 | } |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7381 | sendAndConsumeKeyDown(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7382 | /* repeatCount will start from 1 for deviceId 2 */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7383 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
| 7384 | expectKeyRepeatOnce(repeatCount); |
| 7385 | } |
| 7386 | } |
| 7387 | |
| 7388 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7389 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 7390 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 7391 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7392 | mWindow->assertNoEvents(); |
| 7393 | } |
| 7394 | |
| 7395 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatAfterStaleDeviceKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7396 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 7397 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 7398 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 7399 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7400 | // Stale key up from device 1. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7401 | sendAndConsumeKeyUp(/*deviceId=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7402 | // Device 2 is still down, keep repeating |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7403 | expectKeyRepeatOnce(/*repeatCount=*/2); |
| 7404 | expectKeyRepeatOnce(/*repeatCount=*/3); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7405 | // Device 2 key up |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7406 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7407 | mWindow->assertNoEvents(); |
| 7408 | } |
| 7409 | |
| 7410 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_KeyRepeatStopsAfterRepeatingKeyUp) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7411 | sendAndConsumeKeyDown(/*deviceId=*/1); |
| 7412 | expectKeyRepeatOnce(/*repeatCount=*/1); |
| 7413 | sendAndConsumeKeyDown(/*deviceId=*/2); |
| 7414 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7415 | // 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] | 7416 | sendAndConsumeKeyUp(/*deviceId=*/2); |
Chris Ye | 2ad9539 | 2020-09-01 13:44:44 -0700 | [diff] [blame] | 7417 | // Device 1 still holds key down, but the repeating was already stopped |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7418 | mWindow->assertNoEvents(); |
| 7419 | } |
| 7420 | |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 7421 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_StopsKeyRepeatAfterDisableInputDevice) { |
| 7422 | sendAndConsumeKeyDown(DEVICE_ID); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7423 | expectKeyRepeatOnce(/*repeatCount=*/1); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7424 | mDispatcher->notifyDeviceReset({/*id=*/10, /*eventTime=*/20, DEVICE_ID}); |
liushenxiang | 4223291 | 2021-05-21 20:24:09 +0800 | [diff] [blame] | 7425 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT, |
| 7426 | AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_LONG_PRESS); |
| 7427 | mWindow->assertNoEvents(); |
| 7428 | } |
| 7429 | |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7430 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseEventIdFromInputDispatcher) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 7431 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7432 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7433 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7434 | std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey(); |
| 7435 | ASSERT_NE(nullptr, repeatEvent); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7436 | EXPECT_EQ(IdGenerator::Source::INPUT_DISPATCHER, |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7437 | IdGenerator::getSource(repeatEvent->getId())); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7438 | } |
| 7439 | } |
| 7440 | |
| 7441 | TEST_F(InputDispatcherKeyRepeatTest, FocusedWindow_RepeatKeyEventsUseUniqueEventId) { |
Michael Wright | e1cbbd6 | 2023-02-22 19:32:13 +0000 | [diff] [blame] | 7442 | GTEST_SKIP() << "Flaky test (b/270393106)"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7443 | sendAndConsumeKeyDown(/*deviceId=*/1); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7444 | |
| 7445 | std::unordered_set<int32_t> idSet; |
| 7446 | for (int32_t repeatCount = 1; repeatCount <= 10; ++repeatCount) { |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 7447 | std::unique_ptr<KeyEvent> repeatEvent = mWindow->consumeKey(); |
| 7448 | ASSERT_NE(nullptr, repeatEvent); |
| 7449 | int32_t id = repeatEvent->getId(); |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 7450 | EXPECT_EQ(idSet.end(), idSet.find(id)); |
| 7451 | idSet.insert(id); |
| 7452 | } |
| 7453 | } |
| 7454 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7455 | /* Test InputDispatcher for MultiDisplay */ |
| 7456 | class InputDispatcherFocusOnTwoDisplaysTest : public InputDispatcherTest { |
| 7457 | public: |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7458 | virtual void SetUp() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7459 | InputDispatcherTest::SetUp(); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7460 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7461 | application1 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7462 | windowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7463 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7464 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7465 | // Set focus window for primary display, but focused display would be second one. |
| 7466 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application1); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7467 | windowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7468 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
| 7469 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7470 | setFocusedWindow(windowInPrimary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7471 | windowInPrimary->consumeFocusEvent(true); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7472 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7473 | application2 = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7474 | windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7475 | sp<FakeWindowHandle>::make(application2, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7476 | // Set focus to second display window. |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7477 | // Set focus display to second one. |
| 7478 | mDispatcher->setFocusedDisplay(SECOND_DISPLAY_ID); |
| 7479 | // Set focus window for second display. |
| 7480 | mDispatcher->setFocusedApplication(SECOND_DISPLAY_ID, application2); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 7481 | windowInSecondary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7482 | mDispatcher->onWindowInfosChanged( |
| 7483 | {{*windowInPrimary->getInfo(), *windowInSecondary->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7484 | setFocusedWindow(windowInSecondary); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7485 | windowInSecondary->consumeFocusEvent(true); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7486 | } |
| 7487 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 7488 | virtual void TearDown() override { |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7489 | InputDispatcherTest::TearDown(); |
| 7490 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7491 | application1.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7492 | windowInPrimary.clear(); |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7493 | application2.reset(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7494 | windowInSecondary.clear(); |
| 7495 | } |
| 7496 | |
| 7497 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7498 | std::shared_ptr<FakeApplicationHandle> application1; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7499 | sp<FakeWindowHandle> windowInPrimary; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 7500 | std::shared_ptr<FakeApplicationHandle> application2; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7501 | sp<FakeWindowHandle> windowInSecondary; |
| 7502 | }; |
| 7503 | |
| 7504 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayTouch) { |
| 7505 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7506 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7507 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7508 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7509 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7510 | windowInSecondary->assertNoEvents(); |
| 7511 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7512 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7513 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7514 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7515 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7516 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7517 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7518 | } |
| 7519 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7520 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, SetInputWindow_MultiDisplayFocus) { |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 7521 | // Test inject a key down with display id specified. |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 7522 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7523 | injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7524 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7525 | windowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Tiger Huang | 721e26f | 2018-07-24 22:26:19 +0800 | [diff] [blame] | 7526 | windowInSecondary->assertNoEvents(); |
| 7527 | |
| 7528 | // Test inject a key down without display id specified. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7529 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7530 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7531 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7532 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7533 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 7534 | // Remove all windows in secondary display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7535 | mDispatcher->onWindowInfosChanged({{*windowInPrimary->getInfo()}, {}, 0, 0}); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7536 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7537 | // Old focus should receive a cancel event. |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 7538 | windowInSecondary->consumeKeyUp(ADISPLAY_ID_NONE, AKEY_EVENT_FLAG_CANCELED); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7539 | |
| 7540 | // Test inject a key down, should timeout because of no target window. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7541 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7542 | windowInPrimary->assertNoEvents(); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 7543 | windowInSecondary->consumeFocusEvent(false); |
Arthur Hung | b92218b | 2018-08-14 12:00:21 +0800 | [diff] [blame] | 7544 | windowInSecondary->assertNoEvents(); |
| 7545 | } |
| 7546 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7547 | // Test per-display input monitors for motion event. |
| 7548 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorMotionEvent_MultiDisplay) { |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7549 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7550 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7551 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7552 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7553 | |
| 7554 | // Test touch down on primary display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7555 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7556 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7557 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7558 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7559 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7560 | windowInSecondary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7561 | monitorInSecondary.assertNoEvents(); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7562 | |
| 7563 | // Test touch down on second display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7564 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7565 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7566 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7567 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7568 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7569 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7570 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7571 | |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 7572 | // Lift up the touch from the second display |
| 7573 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7574 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Siarhei Vishniakou | 92c8fd5 | 2023-01-29 14:57:43 -0800 | [diff] [blame] | 7575 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7576 | windowInSecondary->consumeMotionUp(SECOND_DISPLAY_ID); |
| 7577 | monitorInSecondary.consumeMotionUp(SECOND_DISPLAY_ID); |
| 7578 | |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7579 | // Test inject a non-pointer motion event. |
| 7580 | // If specific a display, it will dispatch to the focused window of particular display, |
| 7581 | // or it will dispatch to the focused window of focused display. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7582 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7583 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_NONE)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7584 | << "Inject motion 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->consumeMotionDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7588 | monitorInSecondary.consumeMotionDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7589 | } |
| 7590 | |
| 7591 | // Test per-display input monitors for key event. |
| 7592 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, MonitorKeyEvent_MultiDisplay) { |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7593 | // Input monitor per display. |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7594 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7595 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7596 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7597 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7598 | |
| 7599 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7600 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7601 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7602 | windowInPrimary->assertNoEvents(); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7603 | monitorInPrimary.assertNoEvents(); |
Siarhei Vishniakou | c5ca85c | 2019-11-15 17:20:00 -0800 | [diff] [blame] | 7604 | windowInSecondary->consumeKeyDown(ADISPLAY_ID_NONE); |
chaviw | d1c2318 | 2019-12-20 18:44:56 -0800 | [diff] [blame] | 7605 | monitorInSecondary.consumeKeyDown(ADISPLAY_ID_NONE); |
Arthur Hung | 2fbf37f | 2018-09-13 18:16:41 +0800 | [diff] [blame] | 7606 | } |
| 7607 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7608 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CanFocusWindowOnUnfocusedDisplay) { |
| 7609 | sp<FakeWindowHandle> secondWindowInPrimary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7610 | sp<FakeWindowHandle>::make(application1, mDispatcher, "D_1_W2", ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7611 | secondWindowInPrimary->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7612 | mDispatcher->onWindowInfosChanged( |
| 7613 | {{*windowInPrimary->getInfo(), *secondWindowInPrimary->getInfo(), |
| 7614 | *windowInSecondary->getInfo()}, |
| 7615 | {}, |
| 7616 | 0, |
| 7617 | 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7618 | setFocusedWindow(secondWindowInPrimary); |
| 7619 | windowInPrimary->consumeFocusEvent(false); |
| 7620 | secondWindowInPrimary->consumeFocusEvent(true); |
| 7621 | |
| 7622 | // Test inject a key down. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7623 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 7624 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 7625 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 7626 | windowInPrimary->assertNoEvents(); |
| 7627 | windowInSecondary->assertNoEvents(); |
| 7628 | secondWindowInPrimary->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 7629 | } |
| 7630 | |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7631 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, CancelTouch_MultiDisplay) { |
| 7632 | FakeMonitorReceiver monitorInPrimary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7633 | FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7634 | FakeMonitorReceiver monitorInSecondary = |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 7635 | FakeMonitorReceiver(*mDispatcher, "M_2", SECOND_DISPLAY_ID); |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7636 | |
| 7637 | // Test touch down on primary display. |
| 7638 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7639 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7640 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7641 | windowInPrimary->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7642 | monitorInPrimary.consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 7643 | |
| 7644 | // Test touch down on second display. |
| 7645 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7646 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, SECOND_DISPLAY_ID)) |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7647 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 7648 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID); |
| 7649 | monitorInSecondary.consumeMotionDown(SECOND_DISPLAY_ID); |
| 7650 | |
| 7651 | // Trigger cancel touch. |
| 7652 | mDispatcher->cancelCurrentTouch(); |
| 7653 | windowInPrimary->consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 7654 | monitorInPrimary.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 7655 | windowInSecondary->consumeMotionCancel(SECOND_DISPLAY_ID); |
| 7656 | monitorInSecondary.consumeMotionCancel(SECOND_DISPLAY_ID); |
| 7657 | |
| 7658 | // Test inject a move motion event, no window/monitor should receive the event. |
| 7659 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7660 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7661 | ADISPLAY_ID_DEFAULT, {110, 200})) |
| 7662 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 7663 | windowInPrimary->assertNoEvents(); |
| 7664 | monitorInPrimary.assertNoEvents(); |
| 7665 | |
| 7666 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7667 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | dfd528e | 2021-12-08 13:23:04 +0000 | [diff] [blame] | 7668 | SECOND_DISPLAY_ID, {110, 200})) |
| 7669 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
| 7670 | windowInSecondary->assertNoEvents(); |
| 7671 | monitorInSecondary.assertNoEvents(); |
| 7672 | } |
| 7673 | |
Hu Guo | cb134f1 | 2023-12-23 13:42:44 +0000 | [diff] [blame] | 7674 | /** |
| 7675 | * Send a key to the primary display and to the secondary display. |
| 7676 | * Then cause the key on the primary display to be canceled by sending in a stale key. |
| 7677 | * Ensure that the key on the primary display is canceled, and that the key on the secondary display |
| 7678 | * does not get canceled. |
| 7679 | */ |
| 7680 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture) { |
| 7681 | // Send a key down on primary display |
| 7682 | mDispatcher->notifyKey( |
| 7683 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 7684 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7685 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 7686 | .build()); |
| 7687 | windowInPrimary->consumeKeyEvent( |
| 7688 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 7689 | windowInSecondary->assertNoEvents(); |
| 7690 | |
| 7691 | // Send a key down on second display |
| 7692 | mDispatcher->notifyKey( |
| 7693 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 7694 | .displayId(SECOND_DISPLAY_ID) |
| 7695 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 7696 | .build()); |
| 7697 | windowInSecondary->consumeKeyEvent( |
| 7698 | AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID))); |
| 7699 | windowInPrimary->assertNoEvents(); |
| 7700 | |
| 7701 | // Send a valid key up event on primary display that will be dropped because it is stale |
| 7702 | NotifyKeyArgs staleKeyUp = |
| 7703 | KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD) |
| 7704 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7705 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 7706 | .build(); |
| 7707 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms; |
| 7708 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
| 7709 | std::this_thread::sleep_for(STALE_EVENT_TIMEOUT); |
| 7710 | mDispatcher->notifyKey(staleKeyUp); |
| 7711 | |
| 7712 | // Only the key gesture corresponding to the dropped event should receive the cancel event. |
| 7713 | // Therefore, windowInPrimary should get the cancel event and windowInSecondary should not |
| 7714 | // receive any events. |
| 7715 | windowInPrimary->consumeKeyEvent(AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), |
| 7716 | WithDisplayId(ADISPLAY_ID_DEFAULT), |
| 7717 | WithFlags(AKEY_EVENT_FLAG_CANCELED))); |
| 7718 | windowInSecondary->assertNoEvents(); |
| 7719 | } |
| 7720 | |
| 7721 | /** |
| 7722 | * Similar to 'WhenDropKeyEvent_OnlyCancelCorrespondingKeyGesture' but for motion events. |
| 7723 | */ |
| 7724 | TEST_F(InputDispatcherFocusOnTwoDisplaysTest, WhenDropMotionEvent_OnlyCancelCorrespondingGesture) { |
| 7725 | // Send touch down on primary display. |
| 7726 | mDispatcher->notifyMotion( |
| 7727 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7728 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 7729 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7730 | .build()); |
| 7731 | windowInPrimary->consumeMotionEvent( |
| 7732 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
| 7733 | windowInSecondary->assertNoEvents(); |
| 7734 | |
| 7735 | // Send touch down on second display. |
| 7736 | mDispatcher->notifyMotion( |
| 7737 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 7738 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 7739 | .displayId(SECOND_DISPLAY_ID) |
| 7740 | .build()); |
| 7741 | windowInPrimary->assertNoEvents(); |
| 7742 | windowInSecondary->consumeMotionEvent( |
| 7743 | AllOf(WithMotionAction(ACTION_DOWN), WithDisplayId(SECOND_DISPLAY_ID))); |
| 7744 | |
| 7745 | // inject a valid MotionEvent on primary display that will be stale when it arrives. |
| 7746 | NotifyMotionArgs staleMotionUp = |
| 7747 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 7748 | .displayId(ADISPLAY_ID_DEFAULT) |
| 7749 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 7750 | .build(); |
| 7751 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 10ms; |
| 7752 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
| 7753 | std::this_thread::sleep_for(STALE_EVENT_TIMEOUT); |
| 7754 | mDispatcher->notifyMotion(staleMotionUp); |
| 7755 | |
| 7756 | // For stale motion events, we let the gesture to complete. This behaviour is different from key |
| 7757 | // events, where we would cancel the current keys instead. |
| 7758 | windowInPrimary->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 7759 | windowInSecondary->assertNoEvents(); |
| 7760 | } |
| 7761 | |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7762 | class InputFilterTest : public InputDispatcherTest { |
| 7763 | protected: |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7764 | void testNotifyMotion(int32_t displayId, bool expectToBeFiltered, |
| 7765 | const ui::Transform& transform = ui::Transform()) { |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7766 | NotifyMotionArgs motionArgs; |
| 7767 | |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7768 | motionArgs = |
| 7769 | generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7770 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 7771 | motionArgs = |
| 7772 | generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, displayId); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7773 | mDispatcher->notifyMotion(motionArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7774 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7775 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 3218fc0 | 2023-06-15 20:41:02 -0700 | [diff] [blame] | 7776 | const auto xy = transform.transform(motionArgs.pointerCoords[0].getXYValue()); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7777 | mFakePolicy->assertFilterInputEventWasCalled(motionArgs, xy); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7778 | } else { |
| 7779 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 7780 | } |
| 7781 | } |
| 7782 | |
| 7783 | void testNotifyKey(bool expectToBeFiltered) { |
| 7784 | NotifyKeyArgs keyArgs; |
| 7785 | |
| 7786 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_DOWN); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7787 | mDispatcher->notifyKey(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7788 | keyArgs = generateKeyArgs(AKEY_EVENT_ACTION_UP); |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 7789 | mDispatcher->notifyKey(keyArgs); |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 7790 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7791 | |
| 7792 | if (expectToBeFiltered) { |
Siarhei Vishniakou | 8935a80 | 2019-11-15 16:41:44 -0800 | [diff] [blame] | 7793 | mFakePolicy->assertFilterInputEventWasCalled(keyArgs); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7794 | } else { |
| 7795 | mFakePolicy->assertFilterInputEventWasNotCalled(); |
| 7796 | } |
| 7797 | } |
| 7798 | }; |
| 7799 | |
| 7800 | // Test InputFilter for MotionEvent |
| 7801 | TEST_F(InputFilterTest, MotionEvent_InputFilter) { |
| 7802 | // 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] | 7803 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false); |
| 7804 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7805 | |
| 7806 | // Enable InputFilter |
| 7807 | mDispatcher->setInputFilterEnabled(true); |
| 7808 | // 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] | 7809 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true); |
| 7810 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7811 | |
| 7812 | // Disable InputFilter |
| 7813 | mDispatcher->setInputFilterEnabled(false); |
| 7814 | // 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] | 7815 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/false); |
| 7816 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7817 | } |
| 7818 | |
| 7819 | // Test InputFilter for KeyEvent |
| 7820 | TEST_F(InputFilterTest, KeyEvent_InputFilter) { |
| 7821 | // 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] | 7822 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7823 | |
| 7824 | // Enable InputFilter |
| 7825 | mDispatcher->setInputFilterEnabled(true); |
| 7826 | // Send a key event, and check if it is filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7827 | testNotifyKey(/*expectToBeFiltered=*/true); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7828 | |
| 7829 | // Disable InputFilter |
| 7830 | mDispatcher->setInputFilterEnabled(false); |
| 7831 | // Send a key event, and check if it isn't filtered. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7832 | testNotifyKey(/*expectToBeFiltered=*/false); |
Jackal Guo | f969668 | 2018-10-05 12:23:23 +0800 | [diff] [blame] | 7833 | } |
| 7834 | |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7835 | // Ensure that MotionEvents sent to the InputFilter through InputListener are converted to the |
| 7836 | // logical display coordinate space. |
| 7837 | TEST_F(InputFilterTest, MotionEvent_UsesLogicalDisplayCoordinates_notifyMotion) { |
| 7838 | ui::Transform firstDisplayTransform; |
| 7839 | firstDisplayTransform.set({1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 0, 0, 1}); |
| 7840 | ui::Transform secondDisplayTransform; |
| 7841 | secondDisplayTransform.set({-6.6, -5.5, -4.4, -3.3, -2.2, -1.1, 0, 0, 1}); |
| 7842 | |
| 7843 | std::vector<gui::DisplayInfo> displayInfos(2); |
| 7844 | displayInfos[0].displayId = ADISPLAY_ID_DEFAULT; |
| 7845 | displayInfos[0].transform = firstDisplayTransform; |
| 7846 | displayInfos[1].displayId = SECOND_DISPLAY_ID; |
| 7847 | displayInfos[1].transform = secondDisplayTransform; |
| 7848 | |
Patrick Williams | d828f30 | 2023-04-28 17:52:08 -0500 | [diff] [blame] | 7849 | mDispatcher->onWindowInfosChanged({{}, displayInfos, 0, 0}); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7850 | |
| 7851 | // Enable InputFilter |
| 7852 | mDispatcher->setInputFilterEnabled(true); |
| 7853 | |
| 7854 | // Ensure the correct transforms are used for the displays. |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7855 | testNotifyMotion(ADISPLAY_ID_DEFAULT, /*expectToBeFiltered=*/true, firstDisplayTransform); |
| 7856 | testNotifyMotion(SECOND_DISPLAY_ID, /*expectToBeFiltered=*/true, secondDisplayTransform); |
Prabir Pradhan | 81420cc | 2021-09-06 10:28:50 -0700 | [diff] [blame] | 7857 | } |
| 7858 | |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7859 | class InputFilterInjectionPolicyTest : public InputDispatcherTest { |
| 7860 | protected: |
| 7861 | virtual void SetUp() override { |
| 7862 | InputDispatcherTest::SetUp(); |
| 7863 | |
| 7864 | /** |
| 7865 | * We don't need to enable input filter to test the injected event policy, but we enabled it |
| 7866 | * here to make the tests more realistic, since this policy only matters when inputfilter is |
| 7867 | * on. |
| 7868 | */ |
| 7869 | mDispatcher->setInputFilterEnabled(true); |
| 7870 | |
| 7871 | std::shared_ptr<InputApplicationHandle> application = |
| 7872 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 7873 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Test Window", |
| 7874 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7875 | |
| 7876 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7877 | mWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 7878 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7879 | setFocusedWindow(mWindow); |
| 7880 | mWindow->consumeFocusEvent(true); |
| 7881 | } |
| 7882 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7883 | void testInjectedKey(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 7884 | int32_t flags) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7885 | KeyEvent event; |
| 7886 | |
| 7887 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 7888 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_KEYBOARD, |
| 7889 | ADISPLAY_ID_NONE, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7890 | KEY_A, AMETA_NONE, /*repeatCount=*/0, eventTime, eventTime); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7891 | const int32_t additionalPolicyFlags = |
| 7892 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_DISABLE_KEY_REPEAT; |
| 7893 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7894 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 7895 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7896 | policyFlags | additionalPolicyFlags)); |
| 7897 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7898 | mWindow->consumeKeyEvent(AllOf(WithDeviceId(resolvedDeviceId), WithFlags(flags))); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7899 | } |
| 7900 | |
| 7901 | void testInjectedMotion(int32_t policyFlags, int32_t injectedDeviceId, int32_t resolvedDeviceId, |
| 7902 | int32_t flags) { |
| 7903 | MotionEvent event; |
| 7904 | PointerProperties pointerProperties[1]; |
| 7905 | PointerCoords pointerCoords[1]; |
| 7906 | pointerProperties[0].clear(); |
| 7907 | pointerProperties[0].id = 0; |
| 7908 | pointerCoords[0].clear(); |
| 7909 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, 300); |
| 7910 | pointerCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, 400); |
| 7911 | |
| 7912 | ui::Transform identityTransform; |
| 7913 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 7914 | event.initialize(InputEvent::nextId(), injectedDeviceId, AINPUT_SOURCE_TOUCHSCREEN, |
| 7915 | DISPLAY_ID, INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0, 0, |
| 7916 | AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0, MotionClassification::NONE, |
| 7917 | identityTransform, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 7918 | AMOTION_EVENT_INVALID_CURSOR_POSITION, identityTransform, eventTime, |
Evan Rosky | 0957669 | 2021-07-01 12:22:09 -0700 | [diff] [blame] | 7919 | eventTime, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 7920 | /*pointerCount=*/1, pointerProperties, pointerCoords); |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7921 | |
| 7922 | const int32_t additionalPolicyFlags = POLICY_FLAG_PASS_TO_USER; |
| 7923 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7924 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 4648fea | 2023-06-27 01:00:12 +0000 | [diff] [blame] | 7925 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7926 | policyFlags | additionalPolicyFlags)); |
| 7927 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 7928 | mWindow->consumeMotionEvent(AllOf(WithFlags(flags), WithDeviceId(resolvedDeviceId))); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7929 | } |
| 7930 | |
| 7931 | private: |
| 7932 | sp<FakeWindowHandle> mWindow; |
| 7933 | }; |
| 7934 | |
| 7935 | TEST_F(InputFilterInjectionPolicyTest, TrustedFilteredEvents_KeepOriginalDeviceId) { |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7936 | // Must have POLICY_FLAG_FILTERED here to indicate that the event has gone through the input |
| 7937 | // filter. Without it, the event will no different from a regularly injected event, and the |
| 7938 | // injected device id will be overwritten. |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7939 | testInjectedKey(POLICY_FLAG_FILTERED, /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
| 7940 | /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7941 | } |
| 7942 | |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7943 | TEST_F(InputFilterInjectionPolicyTest, KeyEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7944 | testInjectedKey(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7945 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7946 | AKEY_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
| 7947 | } |
| 7948 | |
| 7949 | TEST_F(InputFilterInjectionPolicyTest, |
| 7950 | MotionEventsInjectedFromAccessibility_HaveAccessibilityFlag) { |
| 7951 | testInjectedMotion(POLICY_FLAG_FILTERED | POLICY_FLAG_INJECTED_FROM_ACCESSIBILITY, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7952 | /*injectedDeviceId=*/3, /*resolvedDeviceId=*/3, |
Siarhei Vishniakou | f00a4ec | 2021-06-16 03:55:32 +0000 | [diff] [blame] | 7953 | AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7954 | } |
| 7955 | |
| 7956 | TEST_F(InputFilterInjectionPolicyTest, RegularInjectedEvents_ReceiveVirtualDeviceId) { |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 7957 | testInjectedKey(/*policyFlags=*/0, /*injectedDeviceId=*/3, |
| 7958 | /*resolvedDeviceId=*/VIRTUAL_KEYBOARD_ID, /*flags=*/0); |
Siarhei Vishniakou | 5d552c4 | 2021-05-21 05:02:22 +0000 | [diff] [blame] | 7959 | } |
| 7960 | |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 7961 | class InputDispatcherUserActivityPokeTests : public InputDispatcherTest { |
| 7962 | protected: |
| 7963 | virtual void SetUp() override { |
| 7964 | InputDispatcherTest::SetUp(); |
| 7965 | |
| 7966 | std::shared_ptr<FakeApplicationHandle> application = |
| 7967 | std::make_shared<FakeApplicationHandle>(); |
| 7968 | application->setDispatchingTimeout(100ms); |
| 7969 | mWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "TestWindow", |
| 7970 | ADISPLAY_ID_DEFAULT); |
Yeabkal Wubshit | 222d83d | 2024-01-24 18:00:09 +0000 | [diff] [blame] | 7971 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
Yeabkal Wubshit | b8aadfa | 2024-01-17 17:03:42 -0800 | [diff] [blame] | 7972 | mWindow->setDispatchingTimeout(100ms); |
| 7973 | mWindow->setFocusable(true); |
| 7974 | |
| 7975 | // Set focused application. |
| 7976 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 7977 | |
| 7978 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
| 7979 | setFocusedWindow(mWindow); |
| 7980 | mWindow->consumeFocusEvent(true); |
| 7981 | } |
| 7982 | |
| 7983 | void notifyAndConsumeMotion(int32_t action, uint32_t source, int32_t displayId, |
| 7984 | nsecs_t eventTime) { |
| 7985 | mDispatcher->notifyMotion(MotionArgsBuilder(action, source) |
| 7986 | .displayId(displayId) |
| 7987 | .eventTime(eventTime) |
| 7988 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 7989 | .build()); |
| 7990 | mWindow->consumeMotionEvent(WithMotionAction(action)); |
| 7991 | } |
| 7992 | |
| 7993 | private: |
| 7994 | sp<FakeWindowHandle> mWindow; |
| 7995 | }; |
| 7996 | |
| 7997 | TEST_F_WITH_FLAGS( |
| 7998 | InputDispatcherUserActivityPokeTests, MinPokeTimeObserved, |
| 7999 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 8000 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 8001 | mDispatcher->setMinTimeBetweenUserActivityPokes(50ms); |
| 8002 | |
| 8003 | // First event of type TOUCH. Should poke. |
| 8004 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8005 | milliseconds_to_nanoseconds(50)); |
| 8006 | mFakePolicy->assertUserActivityPoked( |
| 8007 | {{milliseconds_to_nanoseconds(50), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 8008 | |
| 8009 | // 80ns > 50ns has passed since previous TOUCH event. Should poke. |
| 8010 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8011 | milliseconds_to_nanoseconds(130)); |
| 8012 | mFakePolicy->assertUserActivityPoked( |
| 8013 | {{milliseconds_to_nanoseconds(130), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 8014 | |
| 8015 | // First event of type OTHER. Should poke (despite being within 50ns of previous TOUCH event). |
| 8016 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT, |
| 8017 | milliseconds_to_nanoseconds(135)); |
| 8018 | mFakePolicy->assertUserActivityPoked( |
| 8019 | {{milliseconds_to_nanoseconds(135), USER_ACTIVITY_EVENT_OTHER, ADISPLAY_ID_DEFAULT}}); |
| 8020 | |
| 8021 | // Within 50ns of previous TOUCH event. Should NOT poke. |
| 8022 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8023 | milliseconds_to_nanoseconds(140)); |
| 8024 | mFakePolicy->assertUserActivityNotPoked(); |
| 8025 | |
| 8026 | // Within 50ns of previous OTHER event. Should NOT poke. |
| 8027 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT, |
| 8028 | milliseconds_to_nanoseconds(150)); |
| 8029 | mFakePolicy->assertUserActivityNotPoked(); |
| 8030 | |
| 8031 | // Within 50ns of previous TOUCH event (which was at time 130). Should NOT poke. |
| 8032 | // Note that STYLUS is mapped to TOUCH user activity, since it's a pointer-type source. |
| 8033 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT, |
| 8034 | milliseconds_to_nanoseconds(160)); |
| 8035 | mFakePolicy->assertUserActivityNotPoked(); |
| 8036 | |
| 8037 | // 65ns > 50ns has passed since previous OTHER event. Should poke. |
| 8038 | notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER, ADISPLAY_ID_DEFAULT, |
| 8039 | milliseconds_to_nanoseconds(200)); |
| 8040 | mFakePolicy->assertUserActivityPoked( |
| 8041 | {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_OTHER, ADISPLAY_ID_DEFAULT}}); |
| 8042 | |
| 8043 | // 170ns > 50ns has passed since previous TOUCH event. Should poke. |
| 8044 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_STYLUS, ADISPLAY_ID_DEFAULT, |
| 8045 | milliseconds_to_nanoseconds(300)); |
| 8046 | mFakePolicy->assertUserActivityPoked( |
| 8047 | {{milliseconds_to_nanoseconds(300), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 8048 | |
| 8049 | // Assert that there's no more user activity poke event. |
| 8050 | mFakePolicy->assertUserActivityNotPoked(); |
| 8051 | } |
| 8052 | |
| 8053 | TEST_F_WITH_FLAGS( |
| 8054 | InputDispatcherUserActivityPokeTests, DefaultMinPokeTimeOf100MsUsed, |
| 8055 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 8056 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 8057 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8058 | milliseconds_to_nanoseconds(200)); |
| 8059 | mFakePolicy->assertUserActivityPoked( |
| 8060 | {{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 8061 | |
| 8062 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8063 | milliseconds_to_nanoseconds(280)); |
| 8064 | mFakePolicy->assertUserActivityNotPoked(); |
| 8065 | |
| 8066 | notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 8067 | milliseconds_to_nanoseconds(340)); |
| 8068 | mFakePolicy->assertUserActivityPoked( |
| 8069 | {{milliseconds_to_nanoseconds(340), USER_ACTIVITY_EVENT_TOUCH, ADISPLAY_ID_DEFAULT}}); |
| 8070 | } |
| 8071 | |
| 8072 | TEST_F_WITH_FLAGS( |
| 8073 | InputDispatcherUserActivityPokeTests, ZeroMinPokeTimeDisablesRateLimiting, |
| 8074 | REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags, |
| 8075 | rate_limit_user_activity_poke_in_dispatcher))) { |
| 8076 | mDispatcher->setMinTimeBetweenUserActivityPokes(0ms); |
| 8077 | |
| 8078 | notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 20); |
| 8079 | mFakePolicy->assertUserActivityPoked(); |
| 8080 | |
| 8081 | notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, 30); |
| 8082 | mFakePolicy->assertUserActivityPoked(); |
| 8083 | } |
| 8084 | |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8085 | class InputDispatcherOnPointerDownOutsideFocus : public InputDispatcherTest { |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 8086 | virtual void SetUp() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8087 | InputDispatcherTest::SetUp(); |
| 8088 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8089 | std::shared_ptr<FakeApplicationHandle> application = |
| 8090 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8091 | mUnfocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8092 | sp<FakeWindowHandle>::make(application, mDispatcher, "Top", ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8093 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8094 | |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8095 | mFocusedWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8096 | sp<FakeWindowHandle>::make(application, mDispatcher, "Second", ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8097 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8098 | |
| 8099 | // Set focused application. |
| 8100 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8101 | mFocusedWindow->setFocusable(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8102 | |
| 8103 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8104 | mDispatcher->onWindowInfosChanged( |
| 8105 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8106 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8107 | mFocusedWindow->consumeFocusEvent(true); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8108 | } |
| 8109 | |
Prabir Pradhan | 3608aad | 2019-10-02 17:08:26 -0700 | [diff] [blame] | 8110 | virtual void TearDown() override { |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8111 | InputDispatcherTest::TearDown(); |
| 8112 | |
| 8113 | mUnfocusedWindow.clear(); |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8114 | mFocusedWindow.clear(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8115 | } |
| 8116 | |
| 8117 | protected: |
| 8118 | sp<FakeWindowHandle> mUnfocusedWindow; |
Siarhei Vishniakou | b9b1535 | 2019-11-26 13:19:26 -0800 | [diff] [blame] | 8119 | sp<FakeWindowHandle> mFocusedWindow; |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 8120 | static constexpr PointF FOCUSED_WINDOW_TOUCH_POINT = {60, 60}; |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8121 | }; |
| 8122 | |
| 8123 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 8124 | // DOWN on the window that doesn't have focus. Ensure the window that didn't have focus received |
| 8125 | // the onPointerDownOutsideFocus callback. |
| 8126 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_Success) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8127 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8128 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 8129 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8130 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 8131 | mUnfocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8132 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8133 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8134 | mFakePolicy->assertOnPointerDownEquals(mUnfocusedWindow->getToken()); |
| 8135 | } |
| 8136 | |
| 8137 | // Have two windows, one with focus. Inject MotionEvent with source TRACKBALL and action |
| 8138 | // DOWN on the window that doesn't have focus. Ensure no window received the |
| 8139 | // onPointerDownOutsideFocus callback. |
| 8140 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonPointerSource) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8141 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8142 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TRACKBALL, ADISPLAY_ID_DEFAULT, |
| 8143 | {20, 20})) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8144 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 8145 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8146 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8147 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8148 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8149 | } |
| 8150 | |
| 8151 | // Have two windows, one with focus. Inject KeyEvent with action DOWN on the window that doesn't |
| 8152 | // have focus. Ensure no window received the onPointerDownOutsideFocus callback. |
| 8153 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_NonMotionFailure) { |
Prabir Pradhan | 93f342c | 2021-03-11 15:05:30 -0800 | [diff] [blame] | 8154 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8155 | injectKeyDownNoRepeat(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8156 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 8157 | mFocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8158 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8159 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8160 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8161 | } |
| 8162 | |
| 8163 | // Have two windows, one with focus. Inject MotionEvent with source TOUCHSCREEN and action |
| 8164 | // DOWN on the window that already has focus. Ensure no window received the |
| 8165 | // onPointerDownOutsideFocus callback. |
Siarhei Vishniakou | 870ecec | 2020-12-09 08:07:46 -1000 | [diff] [blame] | 8166 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, OnPointerDownOutsideFocus_OnAlreadyFocusedWindow) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8167 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8168 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | fb9fcda | 2020-05-04 14:59:19 -0700 | [diff] [blame] | 8169 | FOCUSED_WINDOW_TOUCH_POINT)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8170 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Siarhei Vishniakou | 03aee2a | 2020-04-13 20:44:54 -0700 | [diff] [blame] | 8171 | mFocusedWindow->consumeMotionDown(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8172 | |
Siarhei Vishniakou | 2bfa905 | 2019-11-21 18:10:54 -0800 | [diff] [blame] | 8173 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8174 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
chaviw | fd6d351 | 2019-03-25 13:23:49 -0700 | [diff] [blame] | 8175 | } |
| 8176 | |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 8177 | // Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag |
| 8178 | // NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback. |
| 8179 | TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) { |
| 8180 | const MotionEvent event = |
| 8181 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE) |
| 8182 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 8183 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(20).y(20)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 8184 | .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) |
| 8185 | .build(); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8186 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(*mDispatcher, event)) |
Prabir Pradhan | 47cf0a0 | 2021-03-11 20:30:57 -0800 | [diff] [blame] | 8187 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 8188 | mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE); |
| 8189 | |
| 8190 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8191 | mFakePolicy->assertOnPointerDownWasNotCalled(); |
| 8192 | // Ensure that the unfocused window did not receive any FOCUS events. |
| 8193 | mUnfocusedWindow->assertNoEvents(); |
| 8194 | } |
| 8195 | |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8196 | // These tests ensures we can send touch events to a single client when there are multiple input |
| 8197 | // windows that point to the same client token. |
| 8198 | class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest { |
| 8199 | virtual void SetUp() override { |
| 8200 | InputDispatcherTest::SetUp(); |
| 8201 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8202 | std::shared_ptr<FakeApplicationHandle> application = |
| 8203 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8204 | mWindow1 = sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window 1", |
| 8205 | ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8206 | mWindow1->setFrame(Rect(0, 0, 100, 100)); |
| 8207 | |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 8208 | mWindow2 = mWindow1->clone(ADISPLAY_ID_DEFAULT); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8209 | mWindow2->setFrame(Rect(100, 100, 200, 200)); |
| 8210 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8211 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8212 | } |
| 8213 | |
| 8214 | protected: |
| 8215 | sp<FakeWindowHandle> mWindow1; |
| 8216 | sp<FakeWindowHandle> mWindow2; |
| 8217 | |
| 8218 | // 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] | 8219 | static PointF getPointInWindow(const WindowInfo* windowInfo, const PointF& point) { |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 8220 | vec2 vals = windowInfo->transform.transform(point.x, point.y); |
| 8221 | return {vals.x, vals.y}; |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8222 | } |
| 8223 | |
| 8224 | void consumeMotionEvent(const sp<FakeWindowHandle>& window, int32_t expectedAction, |
| 8225 | const std::vector<PointF>& points) { |
Siarhei Vishniakou | f1035d4 | 2019-09-20 16:32:01 +0100 | [diff] [blame] | 8226 | const std::string name = window->getName(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8227 | std::unique_ptr<MotionEvent> motionEvent = |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 8228 | window->consumeMotionEvent(WithMotionAction(expectedAction)); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8229 | ASSERT_NE(nullptr, motionEvent); |
| 8230 | ASSERT_EQ(points.size(), motionEvent->getPointerCount()); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8231 | |
| 8232 | for (size_t i = 0; i < points.size(); i++) { |
| 8233 | float expectedX = points[i].x; |
| 8234 | float expectedY = points[i].y; |
| 8235 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8236 | EXPECT_EQ(expectedX, motionEvent->getX(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8237 | << "expected " << expectedX << " for x[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8238 | << ", got " << motionEvent->getX(i); |
| 8239 | EXPECT_EQ(expectedY, motionEvent->getY(i)) |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8240 | << "expected " << expectedY << " for y[" << i << "] coord of " << name.c_str() |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8241 | << ", got " << motionEvent->getY(i); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8242 | } |
| 8243 | } |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8244 | |
Siarhei Vishniakou | f355bf9 | 2021-12-09 10:43:21 -0800 | [diff] [blame] | 8245 | void touchAndAssertPositions(int32_t action, const std::vector<PointF>& touchedPoints, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8246 | std::vector<PointF> expectedPoints) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 8247 | mDispatcher->notifyMotion(generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN, |
| 8248 | ADISPLAY_ID_DEFAULT, touchedPoints)); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8249 | |
| 8250 | // Always consume from window1 since it's the window that has the InputReceiver |
| 8251 | consumeMotionEvent(mWindow1, action, expectedPoints); |
| 8252 | } |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8253 | }; |
| 8254 | |
| 8255 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchSameScale) { |
| 8256 | // Touch Window 1 |
| 8257 | PointF touchedPoint = {10, 10}; |
| 8258 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8259 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8260 | |
| 8261 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8262 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8263 | |
| 8264 | // Touch Window 2 |
| 8265 | touchedPoint = {150, 150}; |
| 8266 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8267 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8268 | } |
| 8269 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8270 | TEST_F(InputDispatcherMultiWindowSameTokenTests, SingleTouchDifferentTransform) { |
| 8271 | // Set scale value for window2 |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8272 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8273 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8274 | |
| 8275 | // Touch Window 1 |
| 8276 | PointF touchedPoint = {10, 10}; |
| 8277 | PointF expectedPoint = getPointInWindow(mWindow1->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8278 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8279 | // Release touch on Window 1 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8280 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8281 | |
| 8282 | // Touch Window 2 |
| 8283 | touchedPoint = {150, 150}; |
| 8284 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8285 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
| 8286 | touchAndAssertPositions(AMOTION_EVENT_ACTION_UP, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8287 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8288 | // Update the transform so rotation is set |
| 8289 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8290 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8291 | expectedPoint = getPointInWindow(mWindow2->getInfo(), touchedPoint); |
| 8292 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, {touchedPoint}, {expectedPoint}); |
chaviw | af87b3e | 2019-10-01 16:59:28 -0700 | [diff] [blame] | 8293 | } |
| 8294 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8295 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8296 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8297 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8298 | |
| 8299 | // Touch Window 1 |
| 8300 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 8301 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8302 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8303 | |
| 8304 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8305 | touchedPoints.push_back(PointF{150, 150}); |
| 8306 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8307 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8308 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8309 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8310 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8311 | expectedPoints.pop_back(); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8312 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8313 | // Update the transform so rotation is set for Window 2 |
| 8314 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8315 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8316 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8317 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8318 | } |
| 8319 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8320 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleTouchMoveDifferentTransform) { |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8321 | mWindow2->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8322 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8323 | |
| 8324 | // Touch Window 1 |
| 8325 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 8326 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8327 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8328 | |
| 8329 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8330 | touchedPoints.push_back(PointF{150, 150}); |
| 8331 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8332 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8333 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [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 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8340 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8341 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8342 | // Release Window 2 |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8343 | touchAndAssertPositions(POINTER_1_UP, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8344 | expectedPoints.pop_back(); |
| 8345 | |
| 8346 | // Touch Window 2 |
| 8347 | mWindow2->setWindowTransform(0, -1, 1, 0); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8348 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8349 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8350 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8351 | |
| 8352 | // Move both windows |
| 8353 | touchedPoints = {{20, 20}, {175, 175}}; |
| 8354 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 8355 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 8356 | |
| 8357 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8358 | } |
| 8359 | |
| 8360 | TEST_F(InputDispatcherMultiWindowSameTokenTests, MultipleWindowsFirstTouchWithScale) { |
| 8361 | mWindow1->setWindowScale(0.5f, 0.5f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8362 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8363 | |
| 8364 | // Touch Window 1 |
| 8365 | std::vector<PointF> touchedPoints = {PointF{10, 10}}; |
| 8366 | std::vector<PointF> expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0])}; |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8367 | touchAndAssertPositions(AMOTION_EVENT_ACTION_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8368 | |
| 8369 | // Touch Window 2 |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8370 | touchedPoints.push_back(PointF{150, 150}); |
| 8371 | expectedPoints.push_back(getPointInWindow(mWindow2->getInfo(), touchedPoints[1])); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8372 | |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 8373 | touchAndAssertPositions(POINTER_1_DOWN, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8374 | |
| 8375 | // Move both windows |
| 8376 | touchedPoints = {{20, 20}, {175, 175}}; |
| 8377 | expectedPoints = {getPointInWindow(mWindow1->getInfo(), touchedPoints[0]), |
| 8378 | getPointInWindow(mWindow2->getInfo(), touchedPoints[1])}; |
| 8379 | |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 8380 | touchAndAssertPositions(AMOTION_EVENT_ACTION_MOVE, touchedPoints, expectedPoints); |
Chavi Weingarten | 65f98b8 | 2020-01-16 18:56:50 +0000 | [diff] [blame] | 8381 | } |
| 8382 | |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 8383 | /** |
| 8384 | * When one of the windows is slippery, the touch should not slip into the other window with the |
| 8385 | * same input channel. |
| 8386 | */ |
| 8387 | TEST_F(InputDispatcherMultiWindowSameTokenTests, TouchDoesNotSlipEvenIfSlippery) { |
| 8388 | mWindow1->setSlippery(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8389 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 0f6558d | 2023-04-21 12:05:13 -0700 | [diff] [blame] | 8390 | |
| 8391 | // Touch down in window 1 |
| 8392 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
| 8393 | ADISPLAY_ID_DEFAULT, {{50, 50}})); |
| 8394 | consumeMotionEvent(mWindow1, ACTION_DOWN, {{50, 50}}); |
| 8395 | |
| 8396 | // Move touch to be above window 2. Even though window 1 is slippery, touch should not slip. |
| 8397 | // That means the gesture should continue normally, without any ACTION_CANCEL or ACTION_DOWN |
| 8398 | // getting generated. |
| 8399 | mDispatcher->notifyMotion(generateMotionArgs(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
| 8400 | ADISPLAY_ID_DEFAULT, {{150, 150}})); |
| 8401 | |
| 8402 | consumeMotionEvent(mWindow1, ACTION_MOVE, {{150, 150}}); |
| 8403 | } |
| 8404 | |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8405 | /** |
| 8406 | * When hover starts in one window and continues into the other, there should be a HOVER_EXIT and |
| 8407 | * a HOVER_ENTER generated, even if the windows have the same token. This is because the new window |
| 8408 | * that the pointer is hovering over may have a different transform. |
| 8409 | */ |
| 8410 | TEST_F(InputDispatcherMultiWindowSameTokenTests, HoverIntoClone) { |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8411 | mDispatcher->onWindowInfosChanged({{*mWindow1->getInfo(), *mWindow2->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8412 | |
| 8413 | // Start hover in window 1 |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 8414 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_TOUCHSCREEN) |
| 8415 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 8416 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8417 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 8418 | {getPointInWindow(mWindow1->getInfo(), PointF{50, 50})}); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8419 | // Move hover to window 2. |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 8420 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8421 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 8422 | .build()); |
Siarhei Vishniakou | d5876ba | 2023-05-15 17:58:34 -0700 | [diff] [blame] | 8423 | consumeMotionEvent(mWindow1, ACTION_HOVER_EXIT, {{50, 50}}); |
| 8424 | consumeMotionEvent(mWindow1, ACTION_HOVER_ENTER, |
| 8425 | {getPointInWindow(mWindow2->getInfo(), PointF{150, 150})}); |
| 8426 | } |
| 8427 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8428 | class InputDispatcherSingleWindowAnr : public InputDispatcherTest { |
| 8429 | virtual void SetUp() override { |
| 8430 | InputDispatcherTest::SetUp(); |
| 8431 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8432 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8433 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8434 | mWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "TestWindow", |
| 8435 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8436 | mWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8437 | mWindow->setDispatchingTimeout(100ms); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8438 | mWindow->setFocusable(true); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8439 | |
| 8440 | // Set focused application. |
| 8441 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 8442 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8443 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8444 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8445 | mWindow->consumeFocusEvent(true); |
| 8446 | } |
| 8447 | |
| 8448 | virtual void TearDown() override { |
| 8449 | InputDispatcherTest::TearDown(); |
| 8450 | mWindow.clear(); |
| 8451 | } |
| 8452 | |
| 8453 | protected: |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8454 | static constexpr std::chrono::duration SPY_TIMEOUT = 200ms; |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8455 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8456 | sp<FakeWindowHandle> mWindow; |
| 8457 | static constexpr PointF WINDOW_LOCATION = {20, 20}; |
| 8458 | |
| 8459 | void tapOnWindow() { |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 8460 | const auto touchingPointer = PointerBuilder(/*id=*/0, ToolType::FINGER) |
| 8461 | .x(WINDOW_LOCATION.x) |
| 8462 | .y(WINDOW_LOCATION.y); |
| 8463 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8464 | .pointer(touchingPointer) |
| 8465 | .build()); |
| 8466 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8467 | .pointer(touchingPointer) |
| 8468 | .build()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8469 | } |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8470 | |
| 8471 | sp<FakeWindowHandle> addSpyWindow() { |
| 8472 | sp<FakeWindowHandle> spy = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8473 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Spy", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8474 | spy->setTrustedOverlay(true); |
| 8475 | spy->setFocusable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 8476 | spy->setSpy(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8477 | spy->setDispatchingTimeout(SPY_TIMEOUT); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8478 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *mWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8479 | return spy; |
| 8480 | } |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8481 | }; |
| 8482 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8483 | // Send a tap and respond, which should not cause an ANR. |
| 8484 | TEST_F(InputDispatcherSingleWindowAnr, WhenTouchIsConsumed_NoAnr) { |
| 8485 | tapOnWindow(); |
| 8486 | mWindow->consumeMotionDown(); |
| 8487 | mWindow->consumeMotionUp(); |
| 8488 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8489 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8490 | } |
| 8491 | |
| 8492 | // Send a regular key and respond, which should not cause an ANR. |
| 8493 | TEST_F(InputDispatcherSingleWindowAnr, WhenKeyIsConsumed_NoAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8494 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8495 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 8496 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8497 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8498 | } |
| 8499 | |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 8500 | TEST_F(InputDispatcherSingleWindowAnr, WhenFocusedApplicationChanges_NoAnr) { |
| 8501 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8502 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 8503 | mWindow->consumeFocusEvent(false); |
| 8504 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8505 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8506 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 8507 | InputEventInjectionSync::NONE, CONSUME_TIMEOUT_EVENT_EXPECTED, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8508 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8509 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | e41c451 | 2020-09-08 19:35:58 -0500 | [diff] [blame] | 8510 | // Key will not go to window because we have no focused window. |
| 8511 | // The 'no focused window' ANR timer should start instead. |
| 8512 | |
| 8513 | // Now, the focused application goes away. |
| 8514 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, nullptr); |
| 8515 | // The key should get dropped and there should be no ANR. |
| 8516 | |
| 8517 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8518 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8519 | } |
| 8520 | |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8521 | // 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] | 8522 | // When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 8523 | // So InputDispatcher will enqueue ACTION_CANCEL event as well. |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8524 | TEST_F(InputDispatcherSingleWindowAnr, OnPointerDown_BasicAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8525 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8526 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8527 | WINDOW_LOCATION)); |
| 8528 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8529 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8530 | ASSERT_TRUE(sequenceNum); |
| 8531 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8532 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8533 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8534 | mWindow->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8535 | mWindow->consumeMotionEvent( |
| 8536 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8537 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8538 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8539 | } |
| 8540 | |
| 8541 | // Send a key to the app and have the app not respond right away. |
| 8542 | TEST_F(InputDispatcherSingleWindowAnr, OnKeyDown_BasicAnr) { |
| 8543 | // Inject a key, and don't respond - expect that ANR is called. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8544 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDownNoRepeat(*mDispatcher)); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8545 | const auto [sequenceNum, _] = mWindow->receiveEvent(); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8546 | ASSERT_TRUE(sequenceNum); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8547 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8548 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | 4cb50ca | 2020-05-26 21:43:02 -0700 | [diff] [blame] | 8549 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8550 | } |
| 8551 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8552 | // We have a focused application, but no focused window |
| 8553 | TEST_F(InputDispatcherSingleWindowAnr, FocusedApplication_NoFocusedWindow) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8554 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8555 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8556 | mWindow->consumeFocusEvent(false); |
| 8557 | |
| 8558 | // taps on the window work as normal |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8559 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8560 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8561 | WINDOW_LOCATION)); |
| 8562 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 8563 | mDispatcher->waitForIdle(); |
| 8564 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8565 | |
| 8566 | // Once a focused event arrives, we get an ANR for this application |
| 8567 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 8568 | // injection times out (instead of failing). |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8569 | const InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8570 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8571 | InputEventInjectionSync::WAIT_FOR_RESULT, 50ms, /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8572 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8573 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 8574 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8575 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8576 | } |
| 8577 | |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8578 | /** |
| 8579 | * Make sure the stale key is dropped before causing an ANR. So even if there's no focused window, |
| 8580 | * there will not be an ANR. |
| 8581 | */ |
| 8582 | TEST_F(InputDispatcherSingleWindowAnr, StaleKeyEventDoesNotAnr) { |
| 8583 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8584 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8585 | mWindow->consumeFocusEvent(false); |
| 8586 | |
| 8587 | KeyEvent event; |
Siarhei Vishniakou | a733311 | 2023-10-27 13:33:29 -0700 | [diff] [blame] | 8588 | static constexpr std::chrono::duration STALE_EVENT_TIMEOUT = 1000ms; |
| 8589 | mFakePolicy->setStaleEventTimeout(STALE_EVENT_TIMEOUT); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8590 | const nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC) - |
| 8591 | std::chrono::nanoseconds(STALE_EVENT_TIMEOUT).count(); |
| 8592 | |
| 8593 | // Define a valid key down event that is stale (too old). |
| 8594 | event.initialize(InputEvent::nextId(), DEVICE_ID, AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_NONE, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 8595 | INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, /*flags=*/0, AKEYCODE_A, KEY_A, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8596 | AMETA_NONE, /*repeatCount=*/1, eventTime, eventTime); |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8597 | |
| 8598 | const int32_t policyFlags = POLICY_FLAG_FILTERED | POLICY_FLAG_PASS_TO_USER; |
| 8599 | |
| 8600 | InputEventInjectionResult result = |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 8601 | mDispatcher->injectInputEvent(&event, /*targetUid=*/{}, |
Siarhei Vishniakou | 289e924 | 2022-02-15 14:50:16 -0800 | [diff] [blame] | 8602 | InputEventInjectionSync::WAIT_FOR_RESULT, |
| 8603 | INJECT_EVENT_TIMEOUT, policyFlags); |
| 8604 | ASSERT_EQ(InputEventInjectionResult::FAILED, result) |
| 8605 | << "Injection should fail because the event is stale"; |
| 8606 | |
| 8607 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8608 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8609 | mWindow->assertNoEvents(); |
| 8610 | } |
| 8611 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8612 | // We have a focused application, but no focused window |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8613 | // Make sure that we don't notify policy twice about the same ANR. |
| 8614 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) { |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 8615 | const std::chrono::duration appTimeout = 400ms; |
| 8616 | mApplication->setDispatchingTimeout(appTimeout); |
| 8617 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 8618 | |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8619 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8620 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8621 | mWindow->consumeFocusEvent(false); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8622 | |
| 8623 | // Once a focused event arrives, we get an ANR for this application |
| 8624 | // We specify the injection timeout to be smaller than the application timeout, to ensure that |
| 8625 | // injection times out (instead of failing). |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 8626 | const std::chrono::duration eventInjectionTimeout = 100ms; |
| 8627 | ASSERT_LT(eventInjectionTimeout, appTimeout); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8628 | const InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8629 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | 06405fc | 2023-09-22 13:40:51 -0700 | [diff] [blame] | 8630 | InputEventInjectionSync::WAIT_FOR_RESULT, eventInjectionTimeout, |
| 8631 | /*allowKeyRepeat=*/false); |
| 8632 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result) |
| 8633 | << "result=" << ftl::enum_string(result); |
| 8634 | // We already waited for 'eventInjectionTimeout`, because the countdown started when the event |
| 8635 | // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait. |
| 8636 | std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout; |
| 8637 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8638 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 8639 | std::this_thread::sleep_for(appTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8640 | // ANR should not be raised again. It is up to policy to do that if it desires. |
| 8641 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8642 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8643 | // If we now get a focused window, the ANR should stop, but the policy handles that via |
| 8644 | // '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] | 8645 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8646 | } |
| 8647 | |
| 8648 | // We have a focused application, but no focused window |
| 8649 | TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DropsFocusedEvents) { |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8650 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8651 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8652 | mWindow->consumeFocusEvent(false); |
| 8653 | |
| 8654 | // Once a focused event arrives, we get an ANR for this application |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8655 | ASSERT_NO_FATAL_FAILURE(assertInjectedKeyTimesOut(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8656 | |
Vishnu Nair | e4df875 | 2022-09-08 09:17:55 -0700 | [diff] [blame] | 8657 | const std::chrono::duration timeout = mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 8658 | mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(timeout, mApplication); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8659 | |
| 8660 | // Future focused events get dropped right away |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8661 | ASSERT_EQ(InputEventInjectionResult::FAILED, injectKeyDown(*mDispatcher)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8662 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8663 | mWindow->assertNoEvents(); |
| 8664 | } |
| 8665 | |
| 8666 | /** |
| 8667 | * Ensure that the implementation is valid. Since we are using multiset to keep track of the |
| 8668 | * ANR timeouts, we are allowing entries with identical timestamps in the same connection. |
| 8669 | * If we process 1 of the events, but ANR on the second event with the same timestamp, |
| 8670 | * the ANR mechanism should still work. |
| 8671 | * |
| 8672 | * In this test, we are injecting DOWN and UP events with the same timestamps, and acknowledging the |
| 8673 | * DOWN event, while not responding on the second one. |
| 8674 | */ |
| 8675 | TEST_F(InputDispatcherSingleWindowAnr, Anr_HandlesEventsWithIdenticalTimestamps) { |
| 8676 | nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8677 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8678 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 8679 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 8680 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8681 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8682 | |
| 8683 | // Now send ACTION_UP, with identical timestamp |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8684 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8685 | ADISPLAY_ID_DEFAULT, WINDOW_LOCATION, |
| 8686 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 8687 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8688 | 500ms, InputEventInjectionSync::WAIT_FOR_RESULT, currentTime); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8689 | |
| 8690 | // We have now sent down and up. Let's consume first event and then ANR on the second. |
| 8691 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8692 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8693 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8694 | } |
| 8695 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8696 | // A spy window can receive an ANR |
| 8697 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowAnr) { |
| 8698 | sp<FakeWindowHandle> spy = addSpyWindow(); |
| 8699 | |
| 8700 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8701 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8702 | WINDOW_LOCATION)); |
| 8703 | mWindow->consumeMotionDown(); |
| 8704 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8705 | const auto [sequenceNum, _] = spy->receiveEvent(); // ACTION_DOWN |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8706 | ASSERT_TRUE(sequenceNum); |
| 8707 | const std::chrono::duration timeout = spy->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8708 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, spy); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8709 | |
| 8710 | spy->finishEvent(*sequenceNum); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8711 | spy->consumeMotionEvent( |
| 8712 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8713 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8714 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(spy->getToken(), mWindow->getPid()); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8715 | } |
| 8716 | |
| 8717 | // 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] | 8718 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8719 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnKey) { |
| 8720 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8721 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8722 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8723 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8724 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8725 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8726 | |
| 8727 | // Stuck on the ACTION_UP |
| 8728 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8729 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8730 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8731 | // 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] | 8732 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8733 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8734 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8735 | |
| 8736 | mWindow->consumeKeyUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 8737 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8738 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8739 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8740 | spy->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8741 | } |
| 8742 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8743 | // 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] | 8744 | // new motion events |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8745 | TEST_F(InputDispatcherSingleWindowAnr, SpyWindowReceivesEventsDuringAppAnrOnMotion) { |
| 8746 | sp<FakeWindowHandle> spy = addSpyWindow(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8747 | |
| 8748 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8749 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8750 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8751 | |
| 8752 | mWindow->consumeMotionDown(); |
| 8753 | // Stuck on the ACTION_UP |
| 8754 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8755 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8756 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8757 | // 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] | 8758 | tapOnWindow(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8759 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8760 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8761 | |
| 8762 | mWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); // still the previous motion |
| 8763 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8764 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8765 | mWindow->assertNoEvents(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8766 | spy->assertNoEvents(); |
| 8767 | } |
| 8768 | |
| 8769 | TEST_F(InputDispatcherSingleWindowAnr, UnresponsiveMonitorAnr) { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8770 | mDispatcher->setMonitorDispatchingTimeoutForTest(SPY_TIMEOUT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8771 | |
Prabir Pradhan | fb54907 | 2023-10-05 19:17:36 +0000 | [diff] [blame] | 8772 | FakeMonitorReceiver monitor = FakeMonitorReceiver(*mDispatcher, "M_1", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8773 | |
| 8774 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8775 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8776 | WINDOW_LOCATION)); |
| 8777 | |
| 8778 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 8779 | const std::optional<uint32_t> consumeSeq = monitor.receiveEvent(); |
| 8780 | ASSERT_TRUE(consumeSeq); |
| 8781 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8782 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(SPY_TIMEOUT, monitor.getToken(), |
| 8783 | MONITOR_PID); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 8784 | |
| 8785 | monitor.finishEvent(*consumeSeq); |
| 8786 | monitor.consumeMotionCancel(ADISPLAY_ID_DEFAULT); |
| 8787 | |
| 8788 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8789 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(monitor.getToken(), MONITOR_PID); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8790 | } |
| 8791 | |
| 8792 | // If a window is unresponsive, then you get anr. if the window later catches up and starts to |
| 8793 | // process events, you don't get an anr. When the window later becomes unresponsive again, you |
| 8794 | // get an ANR again. |
| 8795 | // 1. tap -> block on ACTION_UP -> receive ANR |
| 8796 | // 2. consume all pending events (= queue becomes healthy again) |
| 8797 | // 3. tap again -> block on ACTION_UP again -> receive ANR second time |
| 8798 | TEST_F(InputDispatcherSingleWindowAnr, SameWindow_CanReceiveAnrTwice) { |
| 8799 | tapOnWindow(); |
| 8800 | |
| 8801 | mWindow->consumeMotionDown(); |
| 8802 | // Block on ACTION_UP |
| 8803 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8804 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8805 | mWindow->consumeMotionUp(); // Now the connection should be healthy again |
| 8806 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8807 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8808 | mWindow->assertNoEvents(); |
| 8809 | |
| 8810 | tapOnWindow(); |
| 8811 | mWindow->consumeMotionDown(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8812 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8813 | mWindow->consumeMotionUp(); |
| 8814 | |
| 8815 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8816 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8817 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8818 | mWindow->assertNoEvents(); |
| 8819 | } |
| 8820 | |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8821 | // If a connection remains unresponsive for a while, make sure policy is only notified once about |
| 8822 | // it. |
| 8823 | TEST_F(InputDispatcherSingleWindowAnr, Policy_DoesNotGetDuplicateAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8824 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8825 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8826 | WINDOW_LOCATION)); |
| 8827 | |
| 8828 | const std::chrono::duration windowTimeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8829 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(windowTimeout, mWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8830 | std::this_thread::sleep_for(windowTimeout); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8831 | // 'notifyConnectionUnresponsive' should only be called once per connection |
| 8832 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 8833 | // 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] | 8834 | mWindow->consumeMotionDown(); |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 8835 | mWindow->consumeMotionEvent( |
| 8836 | AllOf(WithMotionAction(ACTION_CANCEL), WithDisplayId(ADISPLAY_ID_DEFAULT))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8837 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8838 | mDispatcher->waitForIdle(); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 8839 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 8840 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8841 | } |
| 8842 | |
| 8843 | /** |
| 8844 | * 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] | 8845 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8846 | * |
| 8847 | * Warning!!! |
| 8848 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 8849 | * and the injection timeout that we specify when injecting the key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8850 | * We must have the injection timeout (100ms) be smaller than |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8851 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 8852 | * |
| 8853 | * If that value changes, this test should also change. |
| 8854 | */ |
| 8855 | TEST_F(InputDispatcherSingleWindowAnr, Key_StaysPendingWhileMotionIsProcessed) { |
| 8856 | 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] | 8857 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8858 | |
| 8859 | tapOnWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8860 | const auto& [downSequenceNum, downEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8861 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8862 | const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8863 | ASSERT_TRUE(upSequenceNum); |
| 8864 | // Don't finish the events yet, and send a key |
| 8865 | // Injection will "succeed" because we will eventually give up and send the key to the focused |
| 8866 | // window even if motions are still being processed. But because the injection timeout is short, |
| 8867 | // we will receive INJECTION_TIMED_OUT as the result. |
| 8868 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8869 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8870 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 8871 | InputEventInjectionSync::WAIT_FOR_RESULT, 100ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 8872 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8873 | // Key will not be sent to the window, yet, because the window is still processing events |
| 8874 | // 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] | 8875 | // Make sure that `assertNoEvents` doesn't wait too long, because it could cause an ANR. |
| 8876 | // Rely here on the fact that it uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED under the hood. |
| 8877 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 8878 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8879 | |
| 8880 | std::this_thread::sleep_for(500ms); |
| 8881 | // if we wait long enough though, dispatcher will give up, and still send the key |
| 8882 | // to the focused window, even though we have not yet finished the motion event |
| 8883 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 8884 | mWindow->finishEvent(*downSequenceNum); |
| 8885 | mWindow->finishEvent(*upSequenceNum); |
| 8886 | } |
| 8887 | |
| 8888 | /** |
| 8889 | * If a window is processing a motion event, and then a key event comes in, the key event should |
| 8890 | * not go to the focused window until the motion is processed. |
| 8891 | * If then a new motion comes in, then the pending key event should be going to the currently |
| 8892 | * focused window right away. |
| 8893 | */ |
| 8894 | TEST_F(InputDispatcherSingleWindowAnr, |
| 8895 | PendingKey_IsDroppedWhileMotionIsProcessedAndNewTouchComesIn) { |
| 8896 | 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] | 8897 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8898 | |
| 8899 | tapOnWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8900 | const auto& [downSequenceNum, _] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8901 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8902 | const auto& [upSequenceNum, upEvent] = mWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8903 | ASSERT_TRUE(upSequenceNum); |
| 8904 | // Don't finish the events yet, and send a key |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 8905 | mDispatcher->notifyKey( |
| 8906 | KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD) |
| 8907 | .policyFlags(DEFAULT_POLICY_FLAGS | POLICY_FLAG_DISABLE_KEY_REPEAT) |
| 8908 | .build()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8909 | // At this point, key is still pending, and should not be sent to the application yet. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 8910 | // Make sure the `assertNoEvents` check doesn't take too long. It uses |
| 8911 | // CONSUME_TIMEOUT_NO_EVENT_EXPECTED under the hood. |
| 8912 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 8913 | mWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8914 | |
| 8915 | // Now tap down again. It should cause the pending key to go to the focused window right away. |
| 8916 | tapOnWindow(); |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 8917 | mWindow->consumeKeyEvent(WithKeyAction(AKEY_EVENT_ACTION_DOWN)); // it doesn't matter that we |
| 8918 | // haven't ack'd the other events yet. We can finish events in any order. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8919 | mWindow->finishEvent(*downSequenceNum); // first tap's ACTION_DOWN |
| 8920 | mWindow->finishEvent(*upSequenceNum); // first tap's ACTION_UP |
Siarhei Vishniakou | 67bf216 | 2023-11-16 13:29:50 -0800 | [diff] [blame] | 8921 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 8922 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8923 | mWindow->assertNoEvents(); |
| 8924 | } |
| 8925 | |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 8926 | /** |
| 8927 | * Send an event to the app and have the app not respond right away. |
| 8928 | * When ANR is raised, policy will tell the dispatcher to cancel the events for that window. |
| 8929 | * So InputDispatcher will enqueue ACTION_CANCEL event as well. |
| 8930 | * At some point, the window becomes responsive again. |
| 8931 | * Ensure that subsequent events get dropped, and the next gesture is delivered. |
| 8932 | */ |
| 8933 | TEST_F(InputDispatcherSingleWindowAnr, TwoGesturesWithAnr) { |
| 8934 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8935 | .pointer(PointerBuilder(0, ToolType::FINGER).x(10).y(10)) |
| 8936 | .build()); |
| 8937 | |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 8938 | const auto [sequenceNum, _] = mWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | adb9fc9 | 2023-05-26 10:46:09 -0700 | [diff] [blame] | 8939 | ASSERT_TRUE(sequenceNum); |
| 8940 | const std::chrono::duration timeout = mWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
| 8941 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mWindow); |
| 8942 | |
| 8943 | mWindow->finishEvent(*sequenceNum); |
| 8944 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
| 8945 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 8946 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mWindow->getToken(), mWindow->getPid()); |
| 8947 | |
| 8948 | // Now that the window is responsive, let's continue the gesture. |
| 8949 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 8950 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8951 | .build()); |
| 8952 | |
| 8953 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8954 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8955 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 8956 | .build()); |
| 8957 | |
| 8958 | mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8959 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8960 | .pointer(PointerBuilder(1, ToolType::FINGER).x(3).y(3)) |
| 8961 | .build()); |
| 8962 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 8963 | .pointer(PointerBuilder(0, ToolType::FINGER).x(11).y(11)) |
| 8964 | .build()); |
| 8965 | // We already canceled this pointer, so the window shouldn't get any new events. |
| 8966 | mWindow->assertNoEvents(); |
| 8967 | |
| 8968 | // Start another one. |
| 8969 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 8970 | .pointer(PointerBuilder(0, ToolType::FINGER).x(15).y(15)) |
| 8971 | .build()); |
| 8972 | mWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 8973 | } |
| 8974 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8975 | class InputDispatcherMultiWindowAnr : public InputDispatcherTest { |
| 8976 | virtual void SetUp() override { |
| 8977 | InputDispatcherTest::SetUp(); |
| 8978 | |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 8979 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8980 | mApplication->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8981 | mUnfocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Unfocused", |
| 8982 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8983 | mUnfocusedWindow->setFrame(Rect(0, 0, 30, 30)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8984 | // 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] | 8985 | mUnfocusedWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8986 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 8987 | mFocusedWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Focused", |
| 8988 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 8989 | mFocusedWindow->setDispatchingTimeout(100ms); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8990 | mFocusedWindow->setFrame(Rect(50, 50, 100, 100)); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8991 | |
| 8992 | // Set focused application. |
| 8993 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 8994 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 8995 | |
| 8996 | // Expect one focus window exist in display. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 8997 | mDispatcher->onWindowInfosChanged( |
| 8998 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 8999 | setFocusedWindow(mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9000 | mFocusedWindow->consumeFocusEvent(true); |
| 9001 | } |
| 9002 | |
| 9003 | virtual void TearDown() override { |
| 9004 | InputDispatcherTest::TearDown(); |
| 9005 | |
| 9006 | mUnfocusedWindow.clear(); |
| 9007 | mFocusedWindow.clear(); |
| 9008 | } |
| 9009 | |
| 9010 | protected: |
Chris Ye | a209fde | 2020-07-22 13:54:51 -0700 | [diff] [blame] | 9011 | std::shared_ptr<FakeApplicationHandle> mApplication; |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9012 | sp<FakeWindowHandle> mUnfocusedWindow; |
| 9013 | sp<FakeWindowHandle> mFocusedWindow; |
| 9014 | static constexpr PointF UNFOCUSED_WINDOW_LOCATION = {20, 20}; |
| 9015 | static constexpr PointF FOCUSED_WINDOW_LOCATION = {75, 75}; |
| 9016 | static constexpr PointF LOCATION_OUTSIDE_ALL_WINDOWS = {40, 40}; |
| 9017 | |
| 9018 | void tapOnFocusedWindow() { tap(FOCUSED_WINDOW_LOCATION); } |
| 9019 | |
| 9020 | void tapOnUnfocusedWindow() { tap(UNFOCUSED_WINDOW_LOCATION); } |
| 9021 | |
| 9022 | private: |
| 9023 | void tap(const PointF& location) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9024 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9025 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9026 | location)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9027 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9028 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9029 | location)); |
| 9030 | } |
| 9031 | }; |
| 9032 | |
| 9033 | // If we have 2 windows that are both unresponsive, the one with the shortest timeout |
| 9034 | // should be ANR'd first. |
| 9035 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsive) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9036 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 9037 | injectMotionEvent(*mDispatcher, |
| 9038 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 9039 | AINPUT_SOURCE_TOUCHSCREEN) |
| 9040 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 9041 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 9042 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 9043 | .build())); |
| 9044 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 9045 | injectMotionEvent(*mDispatcher, |
| 9046 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, |
| 9047 | AINPUT_SOURCE_TOUCHSCREEN) |
| 9048 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 9049 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 9050 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 9051 | .build())); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9052 | mFocusedWindow->consumeMotionDown(); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 9053 | mFocusedWindow->consumeMotionUp(); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 9054 | mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9055 | // We consumed all events, so no ANR |
| 9056 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9057 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9058 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9059 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 9060 | injectMotionEvent(*mDispatcher, |
| 9061 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 9062 | AINPUT_SOURCE_TOUCHSCREEN) |
| 9063 | .pointer(PointerBuilder(0, ToolType::FINGER) |
| 9064 | .x(FOCUSED_WINDOW_LOCATION.x) |
| 9065 | .y(FOCUSED_WINDOW_LOCATION.y)) |
| 9066 | .build())); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9067 | const auto [unfocusedSequenceNum, _] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9068 | ASSERT_TRUE(unfocusedSequenceNum); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9069 | |
| 9070 | const std::chrono::duration timeout = |
| 9071 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9072 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 9073 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9074 | mUnfocusedWindow->finishEvent(*unfocusedSequenceNum); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9075 | mFocusedWindow->consumeMotionDown(); |
| 9076 | // This cancel is generated because the connection was unresponsive |
| 9077 | mFocusedWindow->consumeMotionCancel(); |
| 9078 | mFocusedWindow->assertNoEvents(); |
| 9079 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9080 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9081 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 9082 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9083 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9084 | } |
| 9085 | |
| 9086 | // If we have 2 windows with identical timeouts that are both unresponsive, |
| 9087 | // it doesn't matter which order they should have ANR. |
| 9088 | // But we should receive ANR for both. |
| 9089 | TEST_F(InputDispatcherMultiWindowAnr, TwoWindows_BothUnresponsiveWithSameTimeout) { |
| 9090 | // Set the timeout for unfocused window to match the focused window |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9091 | mUnfocusedWindow->setDispatchingTimeout( |
| 9092 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9093 | mDispatcher->onWindowInfosChanged( |
| 9094 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9095 | |
| 9096 | tapOnFocusedWindow(); |
| 9097 | // 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] | 9098 | // 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] | 9099 | std::array<sp<IBinder>, 2> anrConnectionTokens = {mFakePolicy->getUnresponsiveWindowToken( |
| 9100 | mFocusedWindow->getDispatchingTimeout( |
| 9101 | DISPATCHING_TIMEOUT)), |
| 9102 | mFakePolicy->getUnresponsiveWindowToken(0ms)}; |
| 9103 | |
| 9104 | ASSERT_THAT(anrConnectionTokens, |
| 9105 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 9106 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9107 | |
| 9108 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9109 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9110 | |
| 9111 | mFocusedWindow->consumeMotionDown(); |
| 9112 | mFocusedWindow->consumeMotionUp(); |
| 9113 | mUnfocusedWindow->consumeMotionOutside(); |
| 9114 | |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9115 | std::array<sp<IBinder>, 2> responsiveTokens = {mFakePolicy->getResponsiveWindowToken(), |
| 9116 | mFakePolicy->getResponsiveWindowToken()}; |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9117 | |
| 9118 | // Both applications should be marked as responsive, in any order |
Siarhei Vishniakou | f83c693 | 2023-07-07 17:48:10 -0700 | [diff] [blame] | 9119 | ASSERT_THAT(responsiveTokens, |
| 9120 | ::testing::UnorderedElementsAre(testing::Eq(mFocusedWindow->getToken()), |
| 9121 | testing::Eq(mUnfocusedWindow->getToken()))); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9122 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9123 | } |
| 9124 | |
| 9125 | // If a window is already not responding, the second tap on the same window should be ignored. |
| 9126 | // We should also log an error to account for the dropped event (not tested here). |
| 9127 | // At the same time, FLAG_WATCH_OUTSIDE_TOUCH targets should not receive any events. |
| 9128 | TEST_F(InputDispatcherMultiWindowAnr, DuringAnr_SecondTapIsIgnored) { |
| 9129 | tapOnFocusedWindow(); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 9130 | mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9131 | // Receive the events, but don't respond |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9132 | const auto [downEventSequenceNum, downEvent] = mFocusedWindow->receiveEvent(); // ACTION_DOWN |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9133 | ASSERT_TRUE(downEventSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9134 | const auto [upEventSequenceNum, upEvent] = mFocusedWindow->receiveEvent(); // ACTION_UP |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9135 | ASSERT_TRUE(upEventSequenceNum); |
| 9136 | const std::chrono::duration timeout = |
| 9137 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9138 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9139 | |
| 9140 | // Tap once again |
| 9141 | // 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] | 9142 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9143 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9144 | FOCUSED_WINDOW_LOCATION)); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9145 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9146 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9147 | FOCUSED_WINDOW_LOCATION)); |
| 9148 | // Unfocused window does not receive ACTION_OUTSIDE because the tapped window is not a |
| 9149 | // valid touch target |
| 9150 | mUnfocusedWindow->assertNoEvents(); |
| 9151 | |
| 9152 | // Consume the first tap |
| 9153 | mFocusedWindow->finishEvent(*downEventSequenceNum); |
| 9154 | mFocusedWindow->finishEvent(*upEventSequenceNum); |
| 9155 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9156 | // The second tap did not go to the focused window |
| 9157 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9158 | // Since all events are finished, connection should be deemed healthy again |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9159 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 9160 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9161 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9162 | } |
| 9163 | |
| 9164 | // If you tap outside of all windows, there will not be ANR |
| 9165 | TEST_F(InputDispatcherMultiWindowAnr, TapOutsideAllWindows_DoesNotAnr) { |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9166 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9167 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9168 | LOCATION_OUTSIDE_ALL_WINDOWS)); |
| 9169 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9170 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9171 | } |
| 9172 | |
| 9173 | // Since the focused window is paused, tapping on it should not produce any events |
| 9174 | TEST_F(InputDispatcherMultiWindowAnr, Window_CanBePaused) { |
| 9175 | mFocusedWindow->setPaused(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9176 | mDispatcher->onWindowInfosChanged( |
| 9177 | {{*mUnfocusedWindow->getInfo(), *mFocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9178 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9179 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9180 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9181 | FOCUSED_WINDOW_LOCATION)); |
| 9182 | |
| 9183 | std::this_thread::sleep_for(mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT)); |
| 9184 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9185 | // Should not ANR because the window is paused, and touches shouldn't go to it |
| 9186 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
| 9187 | |
| 9188 | mFocusedWindow->assertNoEvents(); |
| 9189 | mUnfocusedWindow->assertNoEvents(); |
| 9190 | } |
| 9191 | |
| 9192 | /** |
| 9193 | * 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] | 9194 | * not get delivered to the focused window until the motion is processed. |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9195 | * If a different window becomes focused at this time, the key should go to that window instead. |
| 9196 | * |
| 9197 | * Warning!!! |
| 9198 | * This test depends on the value of android::inputdispatcher::KEY_WAITING_FOR_MOTION_TIMEOUT |
| 9199 | * and the injection timeout that we specify when injecting the key. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9200 | * We must have the injection timeout (100ms) be smaller than |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9201 | * KEY_WAITING_FOR_MOTION_TIMEOUT (currently 500ms). |
| 9202 | * |
| 9203 | * If that value changes, this test should also change. |
| 9204 | */ |
| 9205 | TEST_F(InputDispatcherMultiWindowAnr, PendingKey_GoesToNewlyFocusedWindow) { |
| 9206 | // Set a long ANR timeout to prevent it from triggering |
| 9207 | mFocusedWindow->setDispatchingTimeout(2s); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9208 | mDispatcher->onWindowInfosChanged( |
| 9209 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9210 | |
| 9211 | tapOnUnfocusedWindow(); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9212 | const auto [downSequenceNum, downEvent] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9213 | ASSERT_TRUE(downSequenceNum); |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9214 | const auto [upSequenceNum, upEvent] = mUnfocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9215 | ASSERT_TRUE(upSequenceNum); |
| 9216 | // Don't finish the events yet, and send a key |
| 9217 | // Injection will succeed because we will eventually give up and send the key to the focused |
| 9218 | // window even if motions are still being processed. |
| 9219 | |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9220 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9221 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 9222 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9223 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9224 | // 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] | 9225 | // and the key remains pending, waiting for the touch events to be processed. |
| 9226 | // Make sure `assertNoEvents` doesn't take too long. It uses CONSUME_TIMEOUT_NO_EVENT_EXPECTED |
| 9227 | // under the hood. |
| 9228 | static_assert(CONSUME_TIMEOUT_NO_EVENT_EXPECTED < 100ms); |
| 9229 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9230 | |
| 9231 | // 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] | 9232 | mFocusedWindow->setFocusable(false); |
| 9233 | mUnfocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9234 | mDispatcher->onWindowInfosChanged( |
| 9235 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9236 | setFocusedWindow(mUnfocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9237 | |
| 9238 | // Focus events should precede the key events |
| 9239 | mUnfocusedWindow->consumeFocusEvent(true); |
| 9240 | mFocusedWindow->consumeFocusEvent(false); |
| 9241 | |
| 9242 | // Finish the tap events, which should unblock dispatcher |
| 9243 | mUnfocusedWindow->finishEvent(*downSequenceNum); |
| 9244 | mUnfocusedWindow->finishEvent(*upSequenceNum); |
| 9245 | |
| 9246 | // Now that all queues are cleared and no backlog in the connections, the key event |
| 9247 | // can finally go to the newly focused "mUnfocusedWindow". |
| 9248 | mUnfocusedWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 9249 | mFocusedWindow->assertNoEvents(); |
| 9250 | mUnfocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9251 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9252 | } |
| 9253 | |
| 9254 | // When the touch stream is split across 2 windows, and one of them does not respond, |
| 9255 | // then ANR should be raised and the touch should be canceled for the unresponsive window. |
| 9256 | // The other window should not be affected by that. |
| 9257 | TEST_F(InputDispatcherMultiWindowAnr, SplitTouch_SingleWindowAnr) { |
| 9258 | // Touch Window 1 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9259 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9260 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9261 | {FOCUSED_WINDOW_LOCATION})); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 9262 | mUnfocusedWindow->consumeMotionOutside(ADISPLAY_ID_DEFAULT, /*flags=*/0); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9263 | |
| 9264 | // Touch Window 2 |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9265 | mDispatcher->notifyMotion( |
| 9266 | generateMotionArgs(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9267 | {FOCUSED_WINDOW_LOCATION, UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9268 | |
| 9269 | const std::chrono::duration timeout = |
| 9270 | mFocusedWindow->getDispatchingTimeout(DISPATCHING_TIMEOUT); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9271 | mFakePolicy->assertNotifyWindowUnresponsiveWasCalled(timeout, mFocusedWindow); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9272 | |
| 9273 | mUnfocusedWindow->consumeMotionDown(); |
| 9274 | mFocusedWindow->consumeMotionDown(); |
| 9275 | // Focused window may or may not receive ACTION_MOVE |
| 9276 | // But it should definitely receive ACTION_CANCEL due to the ANR |
Siarhei Vishniakou | d3061ab | 2023-12-18 20:41:08 -0800 | [diff] [blame] | 9277 | const auto [moveOrCancelSequenceNum, event] = mFocusedWindow->receiveEvent(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9278 | ASSERT_TRUE(moveOrCancelSequenceNum); |
| 9279 | mFocusedWindow->finishEvent(*moveOrCancelSequenceNum); |
| 9280 | ASSERT_NE(nullptr, event); |
Siarhei Vishniakou | 63b6361 | 2023-04-12 11:00:23 -0700 | [diff] [blame] | 9281 | ASSERT_EQ(event->getType(), InputEventType::MOTION); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9282 | MotionEvent& motionEvent = static_cast<MotionEvent&>(*event); |
| 9283 | if (motionEvent.getAction() == AMOTION_EVENT_ACTION_MOVE) { |
| 9284 | mFocusedWindow->consumeMotionCancel(); |
| 9285 | } else { |
| 9286 | ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionEvent.getAction()); |
| 9287 | } |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9288 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
Prabir Pradhan | edd9640 | 2022-02-15 01:46:16 -0800 | [diff] [blame] | 9289 | mFakePolicy->assertNotifyWindowResponsiveWasCalled(mFocusedWindow->getToken(), |
| 9290 | mFocusedWindow->getPid()); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9291 | |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9292 | mUnfocusedWindow->assertNoEvents(); |
| 9293 | mFocusedWindow->assertNoEvents(); |
Siarhei Vishniakou | 234129c | 2020-10-22 22:28:12 -0500 | [diff] [blame] | 9294 | mFakePolicy->assertNotifyAnrWasNotCalled(); |
Siarhei Vishniakou | d44dddf | 2020-03-25 16:16:40 -0700 | [diff] [blame] | 9295 | } |
| 9296 | |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9297 | /** |
| 9298 | * If we have no focused window, and a key comes in, we start the ANR timer. |
| 9299 | * The focused application should add a focused window before the timer runs out to prevent ANR. |
| 9300 | * |
| 9301 | * If the user touches another application during this time, the key should be dropped. |
| 9302 | * Next, if a new focused window comes in, without toggling the focused application, |
| 9303 | * then no ANR should occur. |
| 9304 | * |
| 9305 | * Normally, we would expect the new focused window to be accompanied by 'setFocusedApplication', |
| 9306 | * but in some cases the policy may not update the focused application. |
| 9307 | */ |
| 9308 | TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) { |
| 9309 | std::shared_ptr<FakeApplicationHandle> focusedApplication = |
| 9310 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | c033dfb | 2023-10-03 10:45:16 -0700 | [diff] [blame] | 9311 | focusedApplication->setDispatchingTimeout(300ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9312 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication); |
| 9313 | // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused. |
| 9314 | mFocusedWindow->setFocusable(false); |
| 9315 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9316 | mDispatcher->onWindowInfosChanged( |
| 9317 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9318 | mFocusedWindow->consumeFocusEvent(false); |
| 9319 | |
| 9320 | // Send a key. The ANR timer should start because there is no focused window. |
| 9321 | // 'focusedApplication' will get blamed if this timer completes. |
| 9322 | // 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] | 9323 | InputEventInjectionResult result = |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9324 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 9325 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 9326 | /*allowKeyRepeat=*/false); |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9327 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9328 | |
| 9329 | // Wait until dispatcher starts the "no focused window" timer. If we don't wait here, |
| 9330 | // then the injected touches won't cause the focused event to get dropped. |
| 9331 | // The dispatcher only checks for whether the queue should be pruned upon queueing. |
| 9332 | // If we inject the touch right away and the ANR timer hasn't started, the touch event would |
| 9333 | // simply be added to the queue without 'shouldPruneInboundQueueLocked' returning 'true'. |
| 9334 | // For this test, it means that the key would get delivered to the window once it becomes |
| 9335 | // focused. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9336 | std::this_thread::sleep_for(100ms); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9337 | |
| 9338 | // Touch unfocused window. This should force the pending key to get dropped. |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9339 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9340 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9341 | {UNFOCUSED_WINDOW_LOCATION})); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9342 | |
| 9343 | // We do not consume the motion right away, because that would require dispatcher to first |
| 9344 | // process (== drop) the key event, and by that time, ANR will be raised. |
| 9345 | // Set the focused window first. |
| 9346 | mFocusedWindow->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9347 | mDispatcher->onWindowInfosChanged( |
| 9348 | {{*mFocusedWindow->getInfo(), *mUnfocusedWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | f56b269 | 2020-09-08 19:43:33 -0500 | [diff] [blame] | 9349 | setFocusedWindow(mFocusedWindow); |
| 9350 | mFocusedWindow->consumeFocusEvent(true); |
| 9351 | // We do not call "setFocusedApplication" here, even though the newly focused window belongs |
| 9352 | // to another application. This could be a bug / behaviour in the policy. |
| 9353 | |
| 9354 | mUnfocusedWindow->consumeMotionDown(); |
| 9355 | |
| 9356 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 9357 | // Should not ANR because we actually have a focused window. It was just added too slowly. |
| 9358 | ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyAnrWasNotCalled()); |
| 9359 | } |
| 9360 | |
Siarhei Vishniakou | 99e407b | 2023-12-26 18:09:32 -0800 | [diff] [blame] | 9361 | /** |
| 9362 | * If we are pruning input queue, we should never drop pointer events. Otherwise, we risk having |
| 9363 | * an inconsistent event stream inside the dispatcher. In this test, we make sure that the |
| 9364 | * dispatcher doesn't prune pointer events incorrectly. |
| 9365 | * |
| 9366 | * This test reproduces a crash in InputDispatcher. |
| 9367 | * To reproduce the crash, we need to simulate the conditions for "pruning input queue" to occur. |
| 9368 | * |
| 9369 | * Keep the currently focused application (mApplication), and have no focused window. |
| 9370 | * We set up two additional windows: |
| 9371 | * 1) The navigation bar window. This simulates the system "NavigationBar", which is used in the |
| 9372 | * 3-button navigation mode. This window injects a BACK button when it's touched. 2) The application |
| 9373 | * window. This window is not focusable, but is touchable. |
| 9374 | * |
| 9375 | * We first touch the navigation bar, which causes it to inject a key. Since there's no focused |
| 9376 | * window, the dispatcher doesn't process this key, and all other events inside dispatcher are now |
| 9377 | * blocked. The dispatcher is waiting for 'mApplication' to add a focused window. |
| 9378 | * |
| 9379 | * Now, we touch "Another window". This window is owned by a different application than |
| 9380 | * 'mApplication'. This causes the dispatcher to stop waiting for 'mApplication' to add a focused |
| 9381 | * window. Now, the "pruning input queue" behaviour should kick in, and the dispatcher should start |
| 9382 | * dropping the events from its queue. Ensure that no crash occurs. |
| 9383 | * |
| 9384 | * In this test, we are setting long timeouts to prevent ANRs and events dropped due to being stale. |
| 9385 | * This does not affect the test running time. |
| 9386 | */ |
| 9387 | TEST_F(InputDispatcherMultiWindowAnr, PruningInputQueueShouldNotDropPointerEvents) { |
| 9388 | std::shared_ptr<FakeApplicationHandle> systemUiApplication = |
| 9389 | std::make_shared<FakeApplicationHandle>(); |
| 9390 | systemUiApplication->setDispatchingTimeout(3000ms); |
| 9391 | mFakePolicy->setStaleEventTimeout(3000ms); |
| 9392 | sp<FakeWindowHandle> navigationBar = |
| 9393 | sp<FakeWindowHandle>::make(systemUiApplication, mDispatcher, "NavigationBar", |
| 9394 | ADISPLAY_ID_DEFAULT); |
| 9395 | navigationBar->setFocusable(false); |
| 9396 | navigationBar->setWatchOutsideTouch(true); |
| 9397 | navigationBar->setFrame(Rect(0, 0, 100, 100)); |
| 9398 | |
| 9399 | mApplication->setDispatchingTimeout(3000ms); |
| 9400 | // 'mApplication' is already focused, but we call it again here to make it explicit. |
| 9401 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication); |
| 9402 | |
| 9403 | std::shared_ptr<FakeApplicationHandle> anotherApplication = |
| 9404 | std::make_shared<FakeApplicationHandle>(); |
| 9405 | sp<FakeWindowHandle> appWindow = |
| 9406 | sp<FakeWindowHandle>::make(anotherApplication, mDispatcher, "Another window", |
| 9407 | ADISPLAY_ID_DEFAULT); |
| 9408 | appWindow->setFocusable(false); |
| 9409 | appWindow->setFrame(Rect(100, 100, 200, 200)); |
| 9410 | |
| 9411 | mDispatcher->onWindowInfosChanged( |
| 9412 | {{*navigationBar->getInfo(), *appWindow->getInfo()}, {}, 0, 0}); |
| 9413 | // 'mFocusedWindow' is no longer in the dispatcher window list, and therefore loses focus |
| 9414 | mFocusedWindow->consumeFocusEvent(false); |
| 9415 | |
| 9416 | // Touch down the navigation bar. It consumes the touch and injects a key into the dispatcher |
| 9417 | // in response. |
| 9418 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9419 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 9420 | .build()); |
| 9421 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 9422 | |
| 9423 | // Key will not be sent anywhere because we have no focused window. It will remain pending. |
| 9424 | // Pretend we are injecting KEYCODE_BACK, but it doesn't actually matter what key it is. |
| 9425 | InputEventInjectionResult result = |
| 9426 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 9427 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms, |
| 9428 | /*allowKeyRepeat=*/false); |
| 9429 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
| 9430 | |
| 9431 | // Finish the gesture - lift up finger and inject ACTION_UP key event |
| 9432 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 9433 | .pointer(PointerBuilder(0, ToolType::FINGER).x(50).y(50)) |
| 9434 | .build()); |
| 9435 | result = injectKey(*mDispatcher, AKEY_EVENT_ACTION_UP, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT, |
| 9436 | InputEventInjectionSync::NONE, /*injectionTimeout=*/100ms, |
| 9437 | /*allowKeyRepeat=*/false); |
| 9438 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, result); |
| 9439 | // The key that was injected is blocking the dispatcher, so the navigation bar shouldn't be |
| 9440 | // getting any events yet. |
| 9441 | navigationBar->assertNoEvents(); |
| 9442 | |
| 9443 | // Now touch "Another window". This touch is going to a different application than the one we |
| 9444 | // are waiting for (which is 'mApplication'). |
| 9445 | // This should cause the dispatcher to drop the pending focus-dispatched events (like the key |
| 9446 | // trying to be injected) and to continue processing the rest of the events in the original |
| 9447 | // order. |
| 9448 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 9449 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(150)) |
| 9450 | .build()); |
| 9451 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_UP)); |
| 9452 | navigationBar->consumeMotionEvent(WithMotionAction(ACTION_OUTSIDE)); |
| 9453 | appWindow->consumeMotionEvent(WithMotionAction(ACTION_DOWN)); |
| 9454 | |
| 9455 | appWindow->assertNoEvents(); |
| 9456 | navigationBar->assertNoEvents(); |
| 9457 | } |
| 9458 | |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9459 | // These tests ensure we cannot send touch events to a window that's positioned behind a window |
| 9460 | // that has feature NO_INPUT_CHANNEL. |
| 9461 | // Layout: |
| 9462 | // Top (closest to user) |
| 9463 | // mNoInputWindow (above all windows) |
| 9464 | // mBottomWindow |
| 9465 | // Bottom (furthest from user) |
| 9466 | class InputDispatcherMultiWindowOcclusionTests : public InputDispatcherTest { |
| 9467 | virtual void SetUp() override { |
| 9468 | InputDispatcherTest::SetUp(); |
| 9469 | |
| 9470 | mApplication = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9471 | mNoInputWindow = |
| 9472 | sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 9473 | "Window without input channel", ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 9474 | /*createInputChannel=*/false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9475 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9476 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
| 9477 | // It's perfectly valid for this window to not have an associated input channel |
| 9478 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9479 | mBottomWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, "Bottom window", |
| 9480 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9481 | mBottomWindow->setFrame(Rect(0, 0, 100, 100)); |
| 9482 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9483 | mDispatcher->onWindowInfosChanged( |
| 9484 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9485 | } |
| 9486 | |
| 9487 | protected: |
| 9488 | std::shared_ptr<FakeApplicationHandle> mApplication; |
| 9489 | sp<FakeWindowHandle> mNoInputWindow; |
| 9490 | sp<FakeWindowHandle> mBottomWindow; |
| 9491 | }; |
| 9492 | |
| 9493 | TEST_F(InputDispatcherMultiWindowOcclusionTests, NoInputChannelFeature_DropsTouches) { |
| 9494 | PointF touchedPoint = {10, 10}; |
| 9495 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9496 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9497 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9498 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9499 | |
| 9500 | mNoInputWindow->assertNoEvents(); |
| 9501 | // Even though the window 'mNoInputWindow' positioned above 'mBottomWindow' does not have |
| 9502 | // an input channel, it is not marked as FLAG_NOT_TOUCHABLE, |
| 9503 | // and therefore should prevent mBottomWindow from receiving touches |
| 9504 | mBottomWindow->assertNoEvents(); |
| 9505 | } |
| 9506 | |
| 9507 | /** |
| 9508 | * If a window has feature NO_INPUT_CHANNEL, and somehow (by mistake) still has an input channel, |
| 9509 | * ensure that this window does not receive any touches, and blocks touches to windows underneath. |
| 9510 | */ |
| 9511 | TEST_F(InputDispatcherMultiWindowOcclusionTests, |
| 9512 | NoInputChannelFeature_DropsTouchesWithValidChannel) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9513 | mNoInputWindow = sp<FakeWindowHandle>::make(mApplication, mDispatcher, |
| 9514 | "Window with input channel and NO_INPUT_CHANNEL", |
| 9515 | ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9516 | |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 9517 | mNoInputWindow->setNoInputChannel(true); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9518 | mNoInputWindow->setFrame(Rect(0, 0, 100, 100)); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9519 | mDispatcher->onWindowInfosChanged( |
| 9520 | {{*mNoInputWindow->getInfo(), *mBottomWindow->getInfo()}, {}, 0, 0}); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9521 | |
| 9522 | PointF touchedPoint = {10, 10}; |
| 9523 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9524 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9525 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9526 | {touchedPoint})); |
Siarhei Vishniakou | a2862a0 | 2020-07-20 16:36:46 -0500 | [diff] [blame] | 9527 | |
| 9528 | mNoInputWindow->assertNoEvents(); |
| 9529 | mBottomWindow->assertNoEvents(); |
| 9530 | } |
| 9531 | |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9532 | class InputDispatcherMirrorWindowFocusTests : public InputDispatcherTest { |
| 9533 | protected: |
| 9534 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 9535 | sp<FakeWindowHandle> mWindow; |
| 9536 | sp<FakeWindowHandle> mMirror; |
| 9537 | |
| 9538 | virtual void SetUp() override { |
| 9539 | InputDispatcherTest::SetUp(); |
| 9540 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9541 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | e7cc69c | 2024-01-05 21:35:28 +0000 | [diff] [blame] | 9542 | mMirror = mWindow->clone(ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9543 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
| 9544 | mWindow->setFocusable(true); |
| 9545 | mMirror->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9546 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9547 | } |
| 9548 | }; |
| 9549 | |
| 9550 | TEST_F(InputDispatcherMirrorWindowFocusTests, CanGetFocus) { |
| 9551 | // Request focus on a mirrored window |
| 9552 | setFocusedWindow(mMirror); |
| 9553 | |
| 9554 | // window gets focused |
| 9555 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9556 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9557 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9558 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
| 9559 | } |
| 9560 | |
| 9561 | // A focused & mirrored window remains focused only if the window and its mirror are both |
| 9562 | // focusable. |
| 9563 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAllWindowsFocusable) { |
| 9564 | setFocusedWindow(mMirror); |
| 9565 | |
| 9566 | // window gets focused |
| 9567 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9568 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9569 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9570 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9571 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9572 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9573 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9574 | |
| 9575 | mMirror->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9576 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9577 | |
| 9578 | // window loses focus since one of the windows associated with the token in not focusable |
| 9579 | mWindow->consumeFocusEvent(false); |
| 9580 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9581 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9582 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9583 | mWindow->assertNoEvents(); |
| 9584 | } |
| 9585 | |
| 9586 | // A focused & mirrored window remains focused until the window and its mirror both become |
| 9587 | // invisible. |
| 9588 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedIfAnyWindowVisible) { |
| 9589 | setFocusedWindow(mMirror); |
| 9590 | |
| 9591 | // window gets focused |
| 9592 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9593 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9594 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9595 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9596 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9597 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9598 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9599 | |
| 9600 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9601 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9602 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9603 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9604 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9605 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9606 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9607 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9608 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9609 | |
| 9610 | mWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9611 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9612 | |
| 9613 | // window loses focus only after all windows associated with the token become invisible. |
| 9614 | mWindow->consumeFocusEvent(false); |
| 9615 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9616 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9617 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9618 | mWindow->assertNoEvents(); |
| 9619 | } |
| 9620 | |
| 9621 | // A focused & mirrored window remains focused until both windows are removed. |
| 9622 | TEST_F(InputDispatcherMirrorWindowFocusTests, FocusedWhileWindowsAlive) { |
| 9623 | setFocusedWindow(mMirror); |
| 9624 | |
| 9625 | // window gets focused |
| 9626 | mWindow->consumeFocusEvent(true); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9627 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9628 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9629 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9630 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9631 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9632 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9633 | |
| 9634 | // single window is removed but the window token remains focused |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9635 | mDispatcher->onWindowInfosChanged({{*mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9636 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9637 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9638 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9639 | mWindow->consumeKeyDown(ADISPLAY_ID_NONE); |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9640 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9641 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9642 | mWindow->consumeKeyUp(ADISPLAY_ID_NONE); |
| 9643 | |
| 9644 | // Both windows are removed |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9645 | mDispatcher->onWindowInfosChanged({{}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9646 | mWindow->consumeFocusEvent(false); |
| 9647 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9648 | ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, injectKeyDown(*mDispatcher)) |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9649 | << "Inject key event should return InputEventInjectionResult::TIMED_OUT"; |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9650 | mWindow->assertNoEvents(); |
| 9651 | } |
| 9652 | |
| 9653 | // Focus request can be pending until one window becomes visible. |
| 9654 | TEST_F(InputDispatcherMirrorWindowFocusTests, DeferFocusWhenInvisible) { |
| 9655 | // Request focus on an invisible mirror. |
| 9656 | mWindow->setVisible(false); |
| 9657 | mMirror->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9658 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9659 | setFocusedWindow(mMirror); |
| 9660 | |
| 9661 | // Injected key goes to pending queue. |
Siarhei Vishniakou | ae6229e | 2019-12-30 16:23:19 -0800 | [diff] [blame] | 9662 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 9663 | injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, |
| 9664 | ADISPLAY_ID_DEFAULT, InputEventInjectionSync::NONE)); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9665 | |
| 9666 | mMirror->setVisible(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9667 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mMirror->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 958da93 | 2020-08-21 17:12:37 -0700 | [diff] [blame] | 9668 | |
| 9669 | // window gets focused |
| 9670 | mWindow->consumeFocusEvent(true); |
| 9671 | // window gets the pending key event |
| 9672 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 9673 | } |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9674 | |
| 9675 | class InputDispatcherPointerCaptureTests : public InputDispatcherTest { |
| 9676 | protected: |
| 9677 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 9678 | sp<FakeWindowHandle> mWindow; |
| 9679 | sp<FakeWindowHandle> mSecondWindow; |
| 9680 | |
| 9681 | void SetUp() override { |
| 9682 | InputDispatcherTest::SetUp(); |
| 9683 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9684 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9685 | mWindow->setFocusable(true); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9686 | mSecondWindow = |
| 9687 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9688 | mSecondWindow->setFocusable(true); |
| 9689 | |
| 9690 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9691 | mDispatcher->onWindowInfosChanged( |
| 9692 | {{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9693 | |
| 9694 | setFocusedWindow(mWindow); |
| 9695 | mWindow->consumeFocusEvent(true); |
| 9696 | } |
| 9697 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9698 | void notifyPointerCaptureChanged(const PointerCaptureRequest& request) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9699 | mDispatcher->notifyPointerCaptureChanged(generatePointerCaptureChangedArgs(request)); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9700 | } |
| 9701 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9702 | PointerCaptureRequest requestAndVerifyPointerCapture(const sp<FakeWindowHandle>& window, |
| 9703 | bool enabled) { |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9704 | mDispatcher->requestPointerCapture(window->getToken(), enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9705 | auto request = mFakePolicy->assertSetPointerCaptureCalled(enabled); |
| 9706 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9707 | window->consumeCaptureEvent(enabled); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9708 | return request; |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9709 | } |
| 9710 | }; |
| 9711 | |
| 9712 | TEST_F(InputDispatcherPointerCaptureTests, EnablePointerCaptureWhenFocused) { |
| 9713 | // Ensure that capture cannot be obtained for unfocused windows. |
| 9714 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
| 9715 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 9716 | mSecondWindow->assertNoEvents(); |
| 9717 | |
| 9718 | // Ensure that capture can be enabled from the focus window. |
| 9719 | requestAndVerifyPointerCapture(mWindow, true); |
| 9720 | |
| 9721 | // Ensure that capture cannot be disabled from a window that does not have capture. |
| 9722 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), false); |
| 9723 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 9724 | |
| 9725 | // Ensure that capture can be disabled from the window with capture. |
| 9726 | requestAndVerifyPointerCapture(mWindow, false); |
| 9727 | } |
| 9728 | |
| 9729 | TEST_F(InputDispatcherPointerCaptureTests, DisablesPointerCaptureAfterWindowLosesFocus) { |
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 | setFocusedWindow(mSecondWindow); |
| 9733 | |
| 9734 | // Ensure that the capture disabled event was sent first. |
| 9735 | mWindow->consumeCaptureEvent(false); |
| 9736 | mWindow->consumeFocusEvent(false); |
| 9737 | mSecondWindow->consumeFocusEvent(true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9738 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9739 | |
| 9740 | // 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] | 9741 | notifyPointerCaptureChanged({}); |
| 9742 | notifyPointerCaptureChanged(request); |
| 9743 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9744 | mWindow->assertNoEvents(); |
| 9745 | mSecondWindow->assertNoEvents(); |
| 9746 | mFakePolicy->assertSetPointerCaptureNotCalled(); |
| 9747 | } |
| 9748 | |
| 9749 | TEST_F(InputDispatcherPointerCaptureTests, UnexpectedStateChangeDisablesPointerCapture) { |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9750 | auto request = requestAndVerifyPointerCapture(mWindow, true); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9751 | |
| 9752 | // InputReader unexpectedly disables and enables pointer capture. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9753 | notifyPointerCaptureChanged({}); |
| 9754 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9755 | |
| 9756 | // Ensure that Pointer Capture is disabled. |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9757 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 9998771 | 2020-11-10 18:43:05 -0800 | [diff] [blame] | 9758 | mWindow->consumeCaptureEvent(false); |
| 9759 | mWindow->assertNoEvents(); |
| 9760 | } |
| 9761 | |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9762 | TEST_F(InputDispatcherPointerCaptureTests, OutOfOrderRequests) { |
| 9763 | requestAndVerifyPointerCapture(mWindow, true); |
| 9764 | |
| 9765 | // The first window loses focus. |
| 9766 | setFocusedWindow(mSecondWindow); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9767 | mFakePolicy->assertSetPointerCaptureCalled(false); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9768 | mWindow->consumeCaptureEvent(false); |
| 9769 | |
| 9770 | // Request Pointer Capture from the second window before the notification from InputReader |
| 9771 | // arrives. |
| 9772 | mDispatcher->requestPointerCapture(mSecondWindow->getToken(), true); |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9773 | auto request = mFakePolicy->assertSetPointerCaptureCalled(true); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9774 | |
| 9775 | // InputReader notifies Pointer Capture was disabled (because of the focus change). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9776 | notifyPointerCaptureChanged({}); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9777 | |
| 9778 | // InputReader notifies Pointer Capture was enabled (because of mSecondWindow's request). |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9779 | notifyPointerCaptureChanged(request); |
Prabir Pradhan | 167e6d9 | 2021-02-04 16:18:17 -0800 | [diff] [blame] | 9780 | |
| 9781 | mSecondWindow->consumeFocusEvent(true); |
| 9782 | mSecondWindow->consumeCaptureEvent(true); |
| 9783 | } |
| 9784 | |
Prabir Pradhan | 5cc1a69 | 2021-08-06 14:01:18 +0000 | [diff] [blame] | 9785 | TEST_F(InputDispatcherPointerCaptureTests, EnableRequestFollowsSequenceNumbers) { |
| 9786 | // App repeatedly enables and disables capture. |
| 9787 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 9788 | auto firstRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 9789 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 9790 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 9791 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 9792 | auto secondRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 9793 | |
| 9794 | // InputReader notifies that PointerCapture has been enabled for the first request. Since the |
| 9795 | // first request is now stale, this should do nothing. |
| 9796 | notifyPointerCaptureChanged(firstRequest); |
| 9797 | mWindow->assertNoEvents(); |
| 9798 | |
| 9799 | // InputReader notifies that the second request was enabled. |
| 9800 | notifyPointerCaptureChanged(secondRequest); |
| 9801 | mWindow->consumeCaptureEvent(true); |
| 9802 | } |
| 9803 | |
Prabir Pradhan | 7092e26 | 2022-05-03 16:51:09 +0000 | [diff] [blame] | 9804 | TEST_F(InputDispatcherPointerCaptureTests, RapidToggleRequests) { |
| 9805 | requestAndVerifyPointerCapture(mWindow, true); |
| 9806 | |
| 9807 | // App toggles pointer capture off and on. |
| 9808 | mDispatcher->requestPointerCapture(mWindow->getToken(), false); |
| 9809 | mFakePolicy->assertSetPointerCaptureCalled(false); |
| 9810 | |
| 9811 | mDispatcher->requestPointerCapture(mWindow->getToken(), true); |
| 9812 | auto enableRequest = mFakePolicy->assertSetPointerCaptureCalled(true); |
| 9813 | |
| 9814 | // InputReader notifies that the latest "enable" request was processed, while skipping over the |
| 9815 | // preceding "disable" request. |
| 9816 | notifyPointerCaptureChanged(enableRequest); |
| 9817 | |
| 9818 | // Since pointer capture was never disabled during the rapid toggle, the window does not receive |
| 9819 | // any notifications. |
| 9820 | mWindow->assertNoEvents(); |
| 9821 | } |
| 9822 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 9823 | /** |
| 9824 | * One window. Hover mouse in the window, and then start capture. Make sure that the relative |
| 9825 | * mouse movements don't affect the previous mouse hovering state. |
| 9826 | * When pointer capture is enabled, the incoming events are always ACTION_MOVE (there are no |
| 9827 | * HOVER_MOVE events). |
| 9828 | */ |
| 9829 | TEST_F(InputDispatcherPointerCaptureTests, MouseHoverAndPointerCapture) { |
| 9830 | // Mouse hover on the window |
| 9831 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 9832 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 9833 | .build()); |
| 9834 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 9835 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(100).y(110)) |
| 9836 | .build()); |
| 9837 | |
| 9838 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_ENTER))); |
| 9839 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_HOVER_MOVE))); |
| 9840 | |
| 9841 | // Start pointer capture |
| 9842 | requestAndVerifyPointerCapture(mWindow, true); |
| 9843 | |
| 9844 | // Send some relative mouse movements and receive them in the window. |
| 9845 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_MOVE, AINPUT_SOURCE_MOUSE_RELATIVE) |
| 9846 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(10).y(11)) |
| 9847 | .build()); |
| 9848 | mWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithCoords(10, 11), |
| 9849 | WithSource(AINPUT_SOURCE_MOUSE_RELATIVE))); |
| 9850 | |
| 9851 | // Stop pointer capture |
| 9852 | requestAndVerifyPointerCapture(mWindow, false); |
| 9853 | |
| 9854 | // Continue hovering on the window |
| 9855 | mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 9856 | .pointer(PointerBuilder(0, ToolType::MOUSE).x(105).y(115)) |
| 9857 | .build()); |
| 9858 | mWindow->consumeMotionEvent( |
| 9859 | AllOf(WithMotionAction(ACTION_HOVER_MOVE), WithSource(AINPUT_SOURCE_MOUSE))); |
| 9860 | |
| 9861 | mWindow->assertNoEvents(); |
| 9862 | } |
| 9863 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9864 | class InputDispatcherUntrustedTouchesTest : public InputDispatcherTest { |
| 9865 | protected: |
| 9866 | constexpr static const float MAXIMUM_OBSCURING_OPACITY = 0.8; |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9867 | |
| 9868 | constexpr static const float OPACITY_ABOVE_THRESHOLD = 0.9; |
| 9869 | static_assert(OPACITY_ABOVE_THRESHOLD > MAXIMUM_OBSCURING_OPACITY); |
| 9870 | |
| 9871 | constexpr static const float OPACITY_BELOW_THRESHOLD = 0.7; |
| 9872 | static_assert(OPACITY_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 9873 | |
| 9874 | // When combined twice, ie 1 - (1 - 0.5)*(1 - 0.5) = 0.75 < 8, is still below the threshold |
| 9875 | constexpr static const float OPACITY_FAR_BELOW_THRESHOLD = 0.5; |
| 9876 | static_assert(OPACITY_FAR_BELOW_THRESHOLD < MAXIMUM_OBSCURING_OPACITY); |
| 9877 | static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < |
| 9878 | MAXIMUM_OBSCURING_OPACITY); |
| 9879 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9880 | static constexpr gui::Uid TOUCHED_APP_UID{10001}; |
| 9881 | static constexpr gui::Uid APP_B_UID{10002}; |
| 9882 | static constexpr gui::Uid APP_C_UID{10003}; |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9883 | |
| 9884 | sp<FakeWindowHandle> mTouchWindow; |
| 9885 | |
| 9886 | virtual void SetUp() override { |
| 9887 | InputDispatcherTest::SetUp(); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9888 | mTouchWindow = getWindow(TOUCHED_APP_UID, "Touched"); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9889 | mDispatcher->setMaximumObscuringOpacityForTouch(MAXIMUM_OBSCURING_OPACITY); |
| 9890 | } |
| 9891 | |
| 9892 | virtual void TearDown() override { |
| 9893 | InputDispatcherTest::TearDown(); |
| 9894 | mTouchWindow.clear(); |
| 9895 | } |
| 9896 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9897 | sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode, |
chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 9898 | float alpha = 1.0f) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9899 | sp<FakeWindowHandle> window = getWindow(uid, name); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 9900 | window->setTouchable(false); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9901 | window->setTouchOcclusionMode(mode); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9902 | window->setAlpha(alpha); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9903 | return window; |
| 9904 | } |
| 9905 | |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 9906 | sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) { |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9907 | std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); |
| 9908 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 9909 | sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9910 | // Generate an arbitrary PID based on the UID |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 9911 | 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] | 9912 | return window; |
| 9913 | } |
| 9914 | |
| 9915 | void touch(const std::vector<PointF>& points = {PointF{100, 200}}) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 9916 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 9917 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 9918 | points)); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9919 | } |
| 9920 | }; |
| 9921 | |
| 9922 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithBlockUntrustedOcclusionMode_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9923 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9924 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9925 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9926 | |
| 9927 | touch(); |
| 9928 | |
| 9929 | mTouchWindow->assertNoEvents(); |
| 9930 | } |
| 9931 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9932 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9933 | WindowWithBlockUntrustedOcclusionModeWithOpacityBelowThreshold_BlocksTouch) { |
| 9934 | const sp<FakeWindowHandle>& w = |
| 9935 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.7f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9936 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 9937 | |
| 9938 | touch(); |
| 9939 | |
| 9940 | mTouchWindow->assertNoEvents(); |
| 9941 | } |
| 9942 | |
| 9943 | TEST_F(InputDispatcherUntrustedTouchesTest, |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9944 | WindowWithBlockUntrustedOcclusionMode_DoesNotReceiveTouch) { |
| 9945 | const sp<FakeWindowHandle>& w = |
| 9946 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9947 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9948 | |
| 9949 | touch(); |
| 9950 | |
| 9951 | w->assertNoEvents(); |
| 9952 | } |
| 9953 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9954 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithAllowOcclusionMode_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9955 | const sp<FakeWindowHandle>& w = getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::ALLOW); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9956 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9957 | |
| 9958 | touch(); |
| 9959 | |
| 9960 | mTouchWindow->consumeAnyMotionDown(); |
| 9961 | } |
| 9962 | |
| 9963 | TEST_F(InputDispatcherUntrustedTouchesTest, TouchOutsideOccludingWindow_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9964 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9965 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9966 | w->setFrame(Rect(0, 0, 50, 50)); |
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({PointF{100, 100}}); |
| 9970 | |
| 9971 | mTouchWindow->consumeAnyMotionDown(); |
| 9972 | } |
| 9973 | |
| 9974 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowFromSameUid_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9975 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 9976 | getOccludingWindow(TOUCHED_APP_UID, "A", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9977 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 9978 | |
| 9979 | touch(); |
| 9980 | |
| 9981 | mTouchWindow->consumeAnyMotionDown(); |
| 9982 | } |
| 9983 | |
| 9984 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_AllowsTouch) { |
| 9985 | const sp<FakeWindowHandle>& w = |
| 9986 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9987 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 9988 | |
| 9989 | touch(); |
| 9990 | |
| 9991 | mTouchWindow->consumeAnyMotionDown(); |
| 9992 | } |
| 9993 | |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9994 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithZeroOpacity_DoesNotReceiveTouch) { |
| 9995 | const sp<FakeWindowHandle>& w = |
| 9996 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 9997 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 9998 | |
| 9999 | touch(); |
| 10000 | |
| 10001 | w->assertNoEvents(); |
| 10002 | } |
| 10003 | |
| 10004 | /** |
| 10005 | * This is important to make sure apps can't indirectly learn the position of touches (outside vs |
| 10006 | * inside) while letting them pass-through. Note that even though touch passes through the occluding |
| 10007 | * window, the occluding window will still receive ACTION_OUTSIDE event. |
| 10008 | */ |
| 10009 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10010 | WindowWithZeroOpacityAndWatchOutside_ReceivesOutsideEvent) { |
| 10011 | const sp<FakeWindowHandle>& w = |
| 10012 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10013 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10014 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 10015 | |
| 10016 | touch(); |
| 10017 | |
| 10018 | w->consumeMotionOutside(); |
| 10019 | } |
| 10020 | |
| 10021 | TEST_F(InputDispatcherUntrustedTouchesTest, OutsideEvent_HasZeroCoordinates) { |
| 10022 | const sp<FakeWindowHandle>& w = |
| 10023 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, 0.0f); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10024 | w->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10025 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 10026 | |
| 10027 | touch(); |
| 10028 | |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 10029 | w->consumeMotionOutsideWithZeroedCoords(); |
Bernardo Rufino | a43a5a4 | 2021-02-17 12:21:14 +0000 | [diff] [blame] | 10030 | } |
| 10031 | |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10032 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityBelowThreshold_AllowsTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10033 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10034 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10035 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10036 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10037 | |
| 10038 | touch(); |
| 10039 | |
| 10040 | mTouchWindow->consumeAnyMotionDown(); |
| 10041 | } |
| 10042 | |
| 10043 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAtThreshold_AllowsTouch) { |
| 10044 | const sp<FakeWindowHandle>& w = |
| 10045 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10046 | MAXIMUM_OBSCURING_OPACITY); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10047 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10048 | |
| 10049 | touch(); |
| 10050 | |
| 10051 | mTouchWindow->consumeAnyMotionDown(); |
| 10052 | } |
| 10053 | |
| 10054 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowWithOpacityAboveThreshold_BlocksTouch) { |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10055 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10056 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10057 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10058 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10059 | |
| 10060 | touch(); |
| 10061 | |
| 10062 | mTouchWindow->assertNoEvents(); |
| 10063 | } |
| 10064 | |
| 10065 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityAboveThreshold_BlocksTouch) { |
| 10066 | // Resulting opacity = 1 - (1 - 0.7)*(1 - 0.7) = .91 |
| 10067 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10068 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 10069 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10070 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10071 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 10072 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10073 | mDispatcher->onWindowInfosChanged( |
| 10074 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10075 | |
| 10076 | touch(); |
| 10077 | |
| 10078 | mTouchWindow->assertNoEvents(); |
| 10079 | } |
| 10080 | |
| 10081 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsWithCombinedOpacityBelowThreshold_AllowsTouch) { |
| 10082 | // Resulting opacity = 1 - (1 - 0.5)*(1 - 0.5) = .75 |
| 10083 | const sp<FakeWindowHandle>& w1 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10084 | getOccludingWindow(APP_B_UID, "B1", TouchOcclusionMode::USE_OPACITY, |
| 10085 | OPACITY_FAR_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10086 | const sp<FakeWindowHandle>& w2 = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10087 | getOccludingWindow(APP_B_UID, "B2", TouchOcclusionMode::USE_OPACITY, |
| 10088 | OPACITY_FAR_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10089 | mDispatcher->onWindowInfosChanged( |
| 10090 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10091 | |
| 10092 | touch(); |
| 10093 | |
| 10094 | mTouchWindow->consumeAnyMotionDown(); |
| 10095 | } |
| 10096 | |
| 10097 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10098 | WindowsFromDifferentAppsEachBelowThreshold_AllowsTouch) { |
| 10099 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10100 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10101 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10102 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10103 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 10104 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10105 | mDispatcher->onWindowInfosChanged( |
| 10106 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10107 | |
| 10108 | touch(); |
| 10109 | |
| 10110 | mTouchWindow->consumeAnyMotionDown(); |
| 10111 | } |
| 10112 | |
| 10113 | TEST_F(InputDispatcherUntrustedTouchesTest, WindowsFromDifferentAppsOneAboveThreshold_BlocksTouch) { |
| 10114 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10115 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10116 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 1d0d1f2 | 2021-02-12 15:08:43 +0000 | [diff] [blame] | 10117 | const sp<FakeWindowHandle>& wC = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10118 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 10119 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10120 | mDispatcher->onWindowInfosChanged( |
| 10121 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | c3b7b8c | 2021-01-29 17:38:07 +0000 | [diff] [blame] | 10122 | |
| 10123 | touch(); |
| 10124 | |
| 10125 | mTouchWindow->assertNoEvents(); |
| 10126 | } |
| 10127 | |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10128 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10129 | WindowWithOpacityAboveThresholdAndSelfWindow_BlocksTouch) { |
| 10130 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10131 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 10132 | OPACITY_BELOW_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10133 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10134 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10135 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10136 | mDispatcher->onWindowInfosChanged( |
| 10137 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10138 | |
| 10139 | touch(); |
| 10140 | |
| 10141 | mTouchWindow->assertNoEvents(); |
| 10142 | } |
| 10143 | |
| 10144 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10145 | WindowWithOpacityBelowThresholdAndSelfWindow_AllowsTouch) { |
| 10146 | const sp<FakeWindowHandle>& wA = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10147 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 10148 | OPACITY_ABOVE_THRESHOLD); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10149 | const sp<FakeWindowHandle>& wB = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10150 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10151 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10152 | mDispatcher->onWindowInfosChanged( |
| 10153 | {{*wA->getInfo(), *wB->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10154 | |
| 10155 | touch(); |
| 10156 | |
| 10157 | mTouchWindow->consumeAnyMotionDown(); |
| 10158 | } |
| 10159 | |
| 10160 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithOpacityAboveThreshold_AllowsTouch) { |
| 10161 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10162 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::USE_OPACITY, |
| 10163 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10164 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10165 | |
| 10166 | touch(); |
| 10167 | |
| 10168 | mTouchWindow->consumeAnyMotionDown(); |
| 10169 | } |
| 10170 | |
| 10171 | TEST_F(InputDispatcherUntrustedTouchesTest, SelfWindowWithBlockUntrustedMode_AllowsTouch) { |
| 10172 | const sp<FakeWindowHandle>& w = |
| 10173 | getOccludingWindow(TOUCHED_APP_UID, "T", TouchOcclusionMode::BLOCK_UNTRUSTED); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10174 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 6d52e54 | 2021-02-15 18:38:10 +0000 | [diff] [blame] | 10175 | |
| 10176 | touch(); |
| 10177 | |
| 10178 | mTouchWindow->consumeAnyMotionDown(); |
| 10179 | } |
| 10180 | |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 10181 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10182 | OpacityThresholdIs0AndWindowAboveThreshold_BlocksTouch) { |
| 10183 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 10184 | const sp<FakeWindowHandle>& w = |
| 10185 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.1f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10186 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 10187 | |
| 10188 | touch(); |
| 10189 | |
| 10190 | mTouchWindow->assertNoEvents(); |
| 10191 | } |
| 10192 | |
| 10193 | TEST_F(InputDispatcherUntrustedTouchesTest, OpacityThresholdIs0AndWindowAtThreshold_AllowsTouch) { |
| 10194 | mDispatcher->setMaximumObscuringOpacityForTouch(0.0f); |
| 10195 | const sp<FakeWindowHandle>& w = |
| 10196 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, 0.0f); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10197 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 10198 | |
| 10199 | touch(); |
| 10200 | |
| 10201 | mTouchWindow->consumeAnyMotionDown(); |
| 10202 | } |
| 10203 | |
| 10204 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10205 | OpacityThresholdIs1AndWindowBelowThreshold_AllowsTouch) { |
| 10206 | mDispatcher->setMaximumObscuringOpacityForTouch(1.0f); |
| 10207 | const sp<FakeWindowHandle>& w = |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10208 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10209 | OPACITY_ABOVE_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10210 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10211 | |
| 10212 | touch(); |
| 10213 | |
| 10214 | mTouchWindow->consumeAnyMotionDown(); |
| 10215 | } |
| 10216 | |
| 10217 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10218 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromSameApp_BlocksTouch) { |
| 10219 | const sp<FakeWindowHandle>& w1 = |
| 10220 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 10221 | OPACITY_BELOW_THRESHOLD); |
| 10222 | const sp<FakeWindowHandle>& w2 = |
| 10223 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::USE_OPACITY, |
| 10224 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10225 | mDispatcher->onWindowInfosChanged( |
| 10226 | {{*w1->getInfo(), *w2->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10227 | |
| 10228 | touch(); |
| 10229 | |
| 10230 | mTouchWindow->assertNoEvents(); |
| 10231 | } |
| 10232 | |
| 10233 | /** |
| 10234 | * Window B of BLOCK_UNTRUSTED occlusion mode is enough to block the touch, we're testing that the |
| 10235 | * addition of another window (C) of USE_OPACITY occlusion mode and opacity below the threshold |
| 10236 | * (which alone would result in allowing touches) does not affect the blocking behavior. |
| 10237 | */ |
| 10238 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10239 | WindowWithBlockUntrustedModeAndWindowWithOpacityBelowFromDifferentApps_BlocksTouch) { |
| 10240 | const sp<FakeWindowHandle>& wB = |
| 10241 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED, |
| 10242 | OPACITY_BELOW_THRESHOLD); |
| 10243 | const sp<FakeWindowHandle>& wC = |
| 10244 | getOccludingWindow(APP_C_UID, "C", TouchOcclusionMode::USE_OPACITY, |
| 10245 | OPACITY_BELOW_THRESHOLD); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10246 | mDispatcher->onWindowInfosChanged( |
| 10247 | {{*wB->getInfo(), *wC->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | 7393d17 | 2021-02-26 13:56:11 +0000 | [diff] [blame] | 10248 | |
| 10249 | touch(); |
| 10250 | |
| 10251 | mTouchWindow->assertNoEvents(); |
| 10252 | } |
| 10253 | |
| 10254 | /** |
| 10255 | * This test is testing that a window from a different UID but with same application token doesn't |
| 10256 | * block the touch. Apps can share the application token for close UI collaboration for example. |
| 10257 | */ |
| 10258 | TEST_F(InputDispatcherUntrustedTouchesTest, |
| 10259 | WindowWithSameApplicationTokenFromDifferentApp_AllowsTouch) { |
| 10260 | const sp<FakeWindowHandle>& w = |
| 10261 | getOccludingWindow(APP_B_UID, "B", TouchOcclusionMode::BLOCK_UNTRUSTED); |
| 10262 | w->setApplicationToken(mTouchWindow->getApplicationToken()); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10263 | mDispatcher->onWindowInfosChanged({{*w->getInfo(), *mTouchWindow->getInfo()}, {}, 0, 0}); |
Bernardo Rufino | ccd3dd6 | 2021-02-15 18:47:42 +0000 | [diff] [blame] | 10264 | |
| 10265 | touch(); |
| 10266 | |
| 10267 | mTouchWindow->consumeAnyMotionDown(); |
| 10268 | } |
| 10269 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10270 | class InputDispatcherDragTests : public InputDispatcherTest { |
| 10271 | protected: |
| 10272 | std::shared_ptr<FakeApplicationHandle> mApp; |
| 10273 | sp<FakeWindowHandle> mWindow; |
| 10274 | sp<FakeWindowHandle> mSecondWindow; |
| 10275 | sp<FakeWindowHandle> mDragWindow; |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10276 | sp<FakeWindowHandle> mSpyWindow; |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10277 | // Mouse would force no-split, set the id as non-zero to verify if drag state could track it. |
| 10278 | static constexpr int32_t MOUSE_POINTER_ID = 1; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10279 | |
| 10280 | void SetUp() override { |
| 10281 | InputDispatcherTest::SetUp(); |
| 10282 | mApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10283 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10284 | mWindow->setFrame(Rect(0, 0, 100, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10285 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10286 | mSecondWindow = |
| 10287 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10288 | mSecondWindow->setFrame(Rect(100, 0, 200, 100)); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10289 | |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10290 | mSpyWindow = |
| 10291 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "SpyWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10292 | mSpyWindow->setSpy(true); |
| 10293 | mSpyWindow->setTrustedOverlay(true); |
| 10294 | mSpyWindow->setFrame(Rect(0, 0, 200, 100)); |
| 10295 | |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10296 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10297 | mDispatcher->onWindowInfosChanged( |
| 10298 | {{*mSpyWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 10299 | {}, |
| 10300 | 0, |
| 10301 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10302 | } |
| 10303 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10304 | void injectDown(int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
| 10305 | switch (fromSource) { |
| 10306 | case AINPUT_SOURCE_TOUCHSCREEN: |
| 10307 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10308 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10309 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10310 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10311 | break; |
| 10312 | case AINPUT_SOURCE_STYLUS: |
| 10313 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10314 | injectMotionEvent(*mDispatcher, |
| 10315 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10316 | AINPUT_SOURCE_STYLUS) |
| 10317 | .buttonState( |
| 10318 | AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
| 10319 | .pointer(PointerBuilder(0, ToolType::STYLUS) |
| 10320 | .x(50) |
| 10321 | .y(50)) |
| 10322 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10323 | break; |
| 10324 | case AINPUT_SOURCE_MOUSE: |
| 10325 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10326 | injectMotionEvent(*mDispatcher, |
| 10327 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10328 | AINPUT_SOURCE_MOUSE) |
| 10329 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
| 10330 | .pointer(PointerBuilder(MOUSE_POINTER_ID, |
| 10331 | ToolType::MOUSE) |
| 10332 | .x(50) |
| 10333 | .y(50)) |
| 10334 | .build())); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10335 | break; |
| 10336 | default: |
| 10337 | FAIL() << "Source " << fromSource << " doesn't support drag and drop"; |
| 10338 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10339 | |
| 10340 | // Window should receive motion event. |
| 10341 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10342 | // Spy window should also receive motion event |
| 10343 | mSpyWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10344 | } |
| 10345 | |
| 10346 | // Start performing drag, we will create a drag window and transfer touch to it. |
| 10347 | // @param sendDown : if true, send a motion down on first window before perform drag and drop. |
| 10348 | // Returns true on success. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10349 | bool startDrag(bool sendDown = true, int fromSource = AINPUT_SOURCE_TOUCHSCREEN) { |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10350 | if (sendDown) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10351 | injectDown(fromSource); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10352 | } |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10353 | |
| 10354 | // The drag window covers the entire display |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10355 | mDragWindow = |
| 10356 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "DragWindow", ADISPLAY_ID_DEFAULT); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10357 | mDragWindow->setTouchableRegion(Region{{0, 0, 0, 0}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10358 | mDispatcher->onWindowInfosChanged({{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), |
| 10359 | *mWindow->getInfo(), *mSecondWindow->getInfo()}, |
| 10360 | {}, |
| 10361 | 0, |
| 10362 | 0}); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10363 | |
| 10364 | // Transfer touch focus to the drag window |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10365 | bool transferred = |
| 10366 | mDispatcher->transferTouchFocus(mWindow->getToken(), mDragWindow->getToken(), |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10367 | /*isDragDrop=*/true); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10368 | if (transferred) { |
| 10369 | mWindow->consumeMotionCancel(); |
| 10370 | mDragWindow->consumeMotionDown(); |
| 10371 | } |
| 10372 | return transferred; |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10373 | } |
| 10374 | }; |
| 10375 | |
| 10376 | TEST_F(InputDispatcherDragTests, DragEnterAndDragExit) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10377 | startDrag(); |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10378 | |
| 10379 | // Move on window. |
| 10380 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10381 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10382 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10383 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10384 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10385 | mWindow->consumeDragEvent(false, 50, 50); |
| 10386 | mSecondWindow->assertNoEvents(); |
| 10387 | |
| 10388 | // Move to another window. |
| 10389 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10390 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10391 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10392 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10393 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10394 | mWindow->consumeDragEvent(true, 150, 50); |
| 10395 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10396 | |
| 10397 | // Move back to original window. |
| 10398 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10399 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10400 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10401 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10402 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10403 | mWindow->consumeDragEvent(false, 50, 50); |
| 10404 | mSecondWindow->consumeDragEvent(true, -50, 50); |
| 10405 | |
| 10406 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10407 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10408 | {50, 50})) |
arthurhung | b89ccb0 | 2020-12-30 16:19:01 +0800 | [diff] [blame] | 10409 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10410 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 10411 | mWindow->assertNoEvents(); |
| 10412 | mSecondWindow->assertNoEvents(); |
| 10413 | } |
| 10414 | |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10415 | TEST_F(InputDispatcherDragTests, DragEnterAndPointerDownPilfersPointers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10416 | startDrag(); |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10417 | |
| 10418 | // No cancel event after drag start |
| 10419 | mSpyWindow->assertNoEvents(); |
| 10420 | |
| 10421 | const MotionEvent secondFingerDownEvent = |
| 10422 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10423 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10424 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10425 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10426 | .build(); |
| 10427 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10428 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 6abcf8f | 2022-06-06 10:08:05 +0000 | [diff] [blame] | 10429 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10430 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10431 | |
| 10432 | // Receives cancel for first pointer after next pointer down |
| 10433 | mSpyWindow->consumeMotionCancel(); |
| 10434 | mSpyWindow->consumeMotionDown(); |
| 10435 | |
| 10436 | mSpyWindow->assertNoEvents(); |
| 10437 | } |
| 10438 | |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10439 | TEST_F(InputDispatcherDragTests, DragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10440 | startDrag(); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10441 | |
| 10442 | // Move on window. |
| 10443 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10444 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10445 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10446 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10447 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10448 | mWindow->consumeDragEvent(false, 50, 50); |
| 10449 | mSecondWindow->assertNoEvents(); |
| 10450 | |
| 10451 | // Move to another window. |
| 10452 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10453 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10454 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10455 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10456 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10457 | mWindow->consumeDragEvent(true, 150, 50); |
| 10458 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10459 | |
| 10460 | // drop to another window. |
| 10461 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10462 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10463 | {150, 50})) |
| 10464 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10465 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10466 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | f452d0b | 2021-01-06 00:19:52 +0800 | [diff] [blame] | 10467 | mWindow->assertNoEvents(); |
| 10468 | mSecondWindow->assertNoEvents(); |
| 10469 | } |
| 10470 | |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10471 | TEST_F(InputDispatcherDragTests, DragAndDropNotCancelledIfSomeOtherPointerIsPilfered) { |
| 10472 | startDrag(); |
| 10473 | |
| 10474 | // No cancel event after drag start |
| 10475 | mSpyWindow->assertNoEvents(); |
| 10476 | |
| 10477 | const MotionEvent secondFingerDownEvent = |
| 10478 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10479 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 10480 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10481 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 10482 | .build(); |
| 10483 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10484 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 10485 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10486 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10487 | |
| 10488 | // Receives cancel for first pointer after next pointer down |
| 10489 | mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_CANCEL)); |
Siarhei Vishniakou | 1ff00cc | 2023-12-13 16:12:13 -0800 | [diff] [blame] | 10490 | mSpyWindow->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithPointerIds({1}))); |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10491 | mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 10492 | |
| 10493 | mSpyWindow->assertNoEvents(); |
| 10494 | |
| 10495 | // Spy window calls pilfer pointers |
| 10496 | EXPECT_EQ(OK, mDispatcher->pilferPointers(mSpyWindow->getToken())); |
| 10497 | mDragWindow->assertNoEvents(); |
| 10498 | |
| 10499 | const MotionEvent firstFingerMoveEvent = |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 10500 | MotionEventBuilder(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10501 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 10502 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(60).y(60)) |
| 10503 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(60).y(60)) |
| 10504 | .build(); |
| 10505 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | 96a1557 | 2023-12-18 10:47:52 -0800 | [diff] [blame] | 10506 | injectMotionEvent(*mDispatcher, firstFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | 110ba32 | 2023-11-17 10:47:16 +0000 | [diff] [blame] | 10507 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10508 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10509 | |
| 10510 | // Drag window should still receive the new event |
| 10511 | mDragWindow->consumeMotionEvent(WithMotionAction(ACTION_MOVE)); |
| 10512 | mDragWindow->assertNoEvents(); |
| 10513 | } |
| 10514 | |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10515 | TEST_F(InputDispatcherDragTests, StylusDragAndDrop) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10516 | startDrag(true, AINPUT_SOURCE_STYLUS); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10517 | |
| 10518 | // Move on window and keep button pressed. |
| 10519 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10520 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10521 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 10522 | .buttonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10523 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10524 | .build())) |
| 10525 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10526 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10527 | mWindow->consumeDragEvent(false, 50, 50); |
| 10528 | mSecondWindow->assertNoEvents(); |
| 10529 | |
| 10530 | // Move to another window and release button, expect to drop item. |
| 10531 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10532 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10533 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 10534 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10535 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10536 | .build())) |
| 10537 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10538 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10539 | mWindow->assertNoEvents(); |
| 10540 | mSecondWindow->assertNoEvents(); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10541 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10542 | |
| 10543 | // nothing to the window. |
| 10544 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10545 | injectMotionEvent(*mDispatcher, |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10546 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS) |
| 10547 | .buttonState(0) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10548 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(150).y(50)) |
arthurhung | 6d4bed9 | 2021-03-17 11:59:33 +0800 | [diff] [blame] | 10549 | .build())) |
| 10550 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10551 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 10552 | mWindow->assertNoEvents(); |
| 10553 | mSecondWindow->assertNoEvents(); |
| 10554 | } |
| 10555 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10556 | TEST_F(InputDispatcherDragTests, DragAndDropOnInvalidWindow) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10557 | startDrag(); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10558 | |
| 10559 | // Set second window invisible. |
| 10560 | mSecondWindow->setVisible(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10561 | mDispatcher->onWindowInfosChanged( |
| 10562 | {{*mDragWindow->getInfo(), *mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10563 | |
| 10564 | // Move on window. |
| 10565 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10566 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10567 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10568 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10569 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10570 | mWindow->consumeDragEvent(false, 50, 50); |
| 10571 | mSecondWindow->assertNoEvents(); |
| 10572 | |
| 10573 | // Move to another window. |
| 10574 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10575 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10576 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10577 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10578 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10579 | mWindow->consumeDragEvent(true, 150, 50); |
| 10580 | mSecondWindow->assertNoEvents(); |
| 10581 | |
| 10582 | // drop to another window. |
| 10583 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10584 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10585 | {150, 50})) |
| 10586 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10587 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10588 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
Arthur Hung | 6d0571e | 2021-04-09 20:18:16 +0800 | [diff] [blame] | 10589 | mWindow->assertNoEvents(); |
| 10590 | mSecondWindow->assertNoEvents(); |
| 10591 | } |
| 10592 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10593 | TEST_F(InputDispatcherDragTests, NoDragAndDropWhenMultiFingers) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10594 | // Ensure window could track pointerIds if it didn't support split touch. |
| 10595 | mWindow->setPreventSplitting(true); |
| 10596 | |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10597 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10598 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10599 | {50, 50})) |
| 10600 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10601 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10602 | |
| 10603 | const MotionEvent secondFingerDownEvent = |
| 10604 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10605 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10606 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10607 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10608 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(75).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10609 | .build(); |
| 10610 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10611 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10612 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10613 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10614 | mWindow->consumeMotionPointerDown(/*pointerIndex=*/1); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10615 | |
| 10616 | // Should not perform drag and drop when window has multi fingers. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10617 | ASSERT_FALSE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10618 | } |
| 10619 | |
| 10620 | TEST_F(InputDispatcherDragTests, DragAndDropWhenSplitTouch) { |
| 10621 | // First down on second window. |
| 10622 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10623 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10624 | {150, 50})) |
| 10625 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10626 | |
| 10627 | mSecondWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10628 | |
| 10629 | // Second down on first window. |
| 10630 | const MotionEvent secondFingerDownEvent = |
| 10631 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10632 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10633 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10634 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10635 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10636 | .build(); |
| 10637 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10638 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10639 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10640 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10641 | mWindow->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10642 | |
| 10643 | // Perform drag and drop from first window. |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10644 | ASSERT_TRUE(startDrag(false)); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10645 | |
| 10646 | // Move on window. |
| 10647 | const MotionEvent secondFingerMoveEvent = |
| 10648 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 10649 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10650 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10651 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10652 | .build(); |
| 10653 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10654 | injectMotionEvent(*mDispatcher, secondFingerMoveEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10655 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 10656 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10657 | mWindow->consumeDragEvent(false, 50, 50); |
| 10658 | mSecondWindow->consumeMotionMove(); |
| 10659 | |
| 10660 | // Release the drag pointer should perform drop. |
| 10661 | const MotionEvent secondFingerUpEvent = |
| 10662 | MotionEventBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 10663 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10664 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10665 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10666 | .build(); |
| 10667 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10668 | injectMotionEvent(*mDispatcher, secondFingerUpEvent, INJECT_EVENT_TIMEOUT, |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10669 | InputEventInjectionSync::WAIT_FOR_RESULT)); |
| 10670 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10671 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mWindow->getToken()); |
Arthur Hung | 5474565 | 2022-04-20 07:17:41 +0000 | [diff] [blame] | 10672 | mWindow->assertNoEvents(); |
| 10673 | mSecondWindow->consumeMotionMove(); |
| 10674 | } |
| 10675 | |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10676 | TEST_F(InputDispatcherDragTests, DragAndDropWhenMultiDisplays) { |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10677 | startDrag(); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10678 | |
| 10679 | // Update window of second display. |
| 10680 | sp<FakeWindowHandle> windowInSecondary = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10681 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "D_2", SECOND_DISPLAY_ID); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10682 | mDispatcher->onWindowInfosChanged( |
| 10683 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 10684 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 10685 | {}, |
| 10686 | 0, |
| 10687 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10688 | |
| 10689 | // Let second display has a touch state. |
| 10690 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10691 | injectMotionEvent(*mDispatcher, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10692 | MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, |
| 10693 | AINPUT_SOURCE_TOUCHSCREEN) |
| 10694 | .displayId(SECOND_DISPLAY_ID) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 10695 | .pointer(PointerBuilder(0, ToolType::FINGER).x(100).y(100)) |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10696 | .build())); |
Prabir Pradhan | 7662a8d | 2023-12-15 01:58:14 +0000 | [diff] [blame] | 10697 | windowInSecondary->consumeMotionDown(SECOND_DISPLAY_ID, /*expectedFlag=*/0); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10698 | // Update window again. |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10699 | mDispatcher->onWindowInfosChanged( |
| 10700 | {{*mDragWindow->getInfo(), *mSpyWindow->getInfo(), *mWindow->getInfo(), |
| 10701 | *mSecondWindow->getInfo(), *windowInSecondary->getInfo()}, |
| 10702 | {}, |
| 10703 | 0, |
| 10704 | 0}); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10705 | |
| 10706 | // Move on window. |
| 10707 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10708 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10709 | ADISPLAY_ID_DEFAULT, {50, 50})) |
| 10710 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10711 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10712 | mWindow->consumeDragEvent(false, 50, 50); |
| 10713 | mSecondWindow->assertNoEvents(); |
| 10714 | |
| 10715 | // Move to another window. |
| 10716 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10717 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10718 | ADISPLAY_ID_DEFAULT, {150, 50})) |
| 10719 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10720 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10721 | mWindow->consumeDragEvent(true, 150, 50); |
| 10722 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10723 | |
| 10724 | // drop to another window. |
| 10725 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10726 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10727 | {150, 50})) |
| 10728 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10729 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10730 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | 3915c1f | 2022-05-31 07:17:17 +0000 | [diff] [blame] | 10731 | mWindow->assertNoEvents(); |
| 10732 | mSecondWindow->assertNoEvents(); |
| 10733 | } |
| 10734 | |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10735 | TEST_F(InputDispatcherDragTests, MouseDragAndDrop) { |
| 10736 | startDrag(true, AINPUT_SOURCE_MOUSE); |
| 10737 | // Move on window. |
| 10738 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10739 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10740 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 10741 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10742 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10743 | .x(50) |
| 10744 | .y(50)) |
| 10745 | .build())) |
| 10746 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10747 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10748 | mWindow->consumeDragEvent(false, 50, 50); |
| 10749 | mSecondWindow->assertNoEvents(); |
| 10750 | |
| 10751 | // Move to another window. |
| 10752 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10753 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10754 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_MOUSE) |
| 10755 | .buttonState(AMOTION_EVENT_BUTTON_PRIMARY) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10756 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10757 | .x(150) |
| 10758 | .y(50)) |
| 10759 | .build())) |
| 10760 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10761 | mDragWindow->consumeMotionMove(ADISPLAY_ID_DEFAULT); |
| 10762 | mWindow->consumeDragEvent(true, 150, 50); |
| 10763 | mSecondWindow->consumeDragEvent(false, 50, 50); |
| 10764 | |
| 10765 | // drop to another window. |
| 10766 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10767 | injectMotionEvent(*mDispatcher, |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10768 | MotionEventBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_MOUSE) |
| 10769 | .buttonState(0) |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 10770 | .pointer(PointerBuilder(MOUSE_POINTER_ID, ToolType::MOUSE) |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10771 | .x(150) |
| 10772 | .y(50)) |
| 10773 | .build())) |
| 10774 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10775 | mDragWindow->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
Siarhei Vishniakou | a66d65e | 2023-06-16 10:32:51 -0700 | [diff] [blame] | 10776 | mFakePolicy->assertDropTargetEquals(*mDispatcher, mSecondWindow->getToken()); |
Arthur Hung | b75c2aa | 2022-07-15 09:35:36 +0000 | [diff] [blame] | 10777 | mWindow->assertNoEvents(); |
| 10778 | mSecondWindow->assertNoEvents(); |
| 10779 | } |
| 10780 | |
Linnan Li | 5af92f9 | 2023-07-14 14:36:22 +0800 | [diff] [blame] | 10781 | /** |
| 10782 | * Start drag and drop with a pointer whose id is not 0, cancel the current touch, and ensure drag |
| 10783 | * and drop is also canceled. Then inject a simple gesture, and ensure dispatcher does not crash. |
| 10784 | */ |
| 10785 | TEST_F(InputDispatcherDragTests, DragAndDropFinishedWhenCancelCurrentTouch) { |
| 10786 | // Down on second window |
| 10787 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10788 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10789 | {150, 50})) |
| 10790 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10791 | |
| 10792 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionDown()); |
| 10793 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionDown()); |
| 10794 | |
| 10795 | // Down on first window |
| 10796 | const MotionEvent secondFingerDownEvent = |
| 10797 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 10798 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10799 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(50)) |
| 10800 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 10801 | .build(); |
| 10802 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10803 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
| 10804 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10805 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10806 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 10807 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionMove()); |
| 10808 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionPointerDown(1)); |
| 10809 | |
| 10810 | // Start drag on first window |
| 10811 | ASSERT_TRUE(startDrag(/*sendDown=*/false, AINPUT_SOURCE_TOUCHSCREEN)); |
| 10812 | |
| 10813 | // Trigger cancel |
| 10814 | mDispatcher->cancelCurrentTouch(); |
| 10815 | ASSERT_NO_FATAL_FAILURE(mSecondWindow->consumeMotionCancel()); |
| 10816 | ASSERT_NO_FATAL_FAILURE(mDragWindow->consumeMotionCancel()); |
| 10817 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionCancel()); |
| 10818 | |
| 10819 | ASSERT_TRUE(mDispatcher->waitForIdle()); |
| 10820 | // The D&D finished with nullptr |
| 10821 | mFakePolicy->assertDropTargetEquals(*mDispatcher, nullptr); |
| 10822 | |
| 10823 | // Remove drag window |
| 10824 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo(), *mSecondWindow->getInfo()}, {}, 0, 0}); |
| 10825 | |
| 10826 | // Inject a simple gesture, ensure dispatcher not crashed |
| 10827 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10828 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10829 | PointF{50, 50})) |
| 10830 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10831 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionDown()); |
| 10832 | |
| 10833 | const MotionEvent moveEvent = |
| 10834 | MotionEventBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 10835 | .displayId(ADISPLAY_ID_DEFAULT) |
| 10836 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 10837 | .build(); |
| 10838 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10839 | injectMotionEvent(*mDispatcher, moveEvent, INJECT_EVENT_TIMEOUT, |
| 10840 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 10841 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10842 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionMove()); |
| 10843 | |
| 10844 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10845 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
| 10846 | {50, 50})) |
| 10847 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 10848 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionUp()); |
| 10849 | } |
| 10850 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 10851 | TEST_F(InputDispatcherDragTests, NoDragAndDropWithHoveringPointer) { |
| 10852 | // Start hovering over the window. |
| 10853 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 10854 | injectMotionEvent(*mDispatcher, ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE, |
| 10855 | ADISPLAY_ID_DEFAULT, {50, 50})); |
| 10856 | |
| 10857 | ASSERT_NO_FATAL_FAILURE(mWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 10858 | ASSERT_NO_FATAL_FAILURE(mSpyWindow->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER))); |
| 10859 | |
| 10860 | ASSERT_FALSE(startDrag(/*sendDown=*/false)) |
| 10861 | << "Drag and drop should not work with a hovering pointer"; |
| 10862 | } |
| 10863 | |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10864 | class InputDispatcherDropInputFeatureTest : public InputDispatcherTest {}; |
| 10865 | |
| 10866 | TEST_F(InputDispatcherDropInputFeatureTest, WindowDropsInput) { |
| 10867 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10868 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 10869 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10870 | window->setDropInput(true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10871 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 10872 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10873 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10874 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10875 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10876 | |
| 10877 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10878 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10879 | window->assertNoEvents(); |
| 10880 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10881 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10882 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 10883 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 10884 | ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | d908f5a | 2023-11-16 10:25:12 -0800 | [diff] [blame] | 10885 | mDispatcher->waitForIdle(); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10886 | window->assertNoEvents(); |
| 10887 | |
| 10888 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10889 | window->setDropInput(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10890 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10891 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10892 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10893 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 10894 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10895 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10896 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10897 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10898 | window->assertNoEvents(); |
| 10899 | } |
| 10900 | |
| 10901 | TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { |
| 10902 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 10903 | std::make_shared<FakeApplicationHandle>(); |
| 10904 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10905 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 10906 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10907 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10908 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10909 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10910 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10911 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 10912 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10913 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10914 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10915 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 10916 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10917 | mDispatcher->onWindowInfosChanged( |
| 10918 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10919 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10920 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10921 | |
| 10922 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10923 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10924 | window->assertNoEvents(); |
| 10925 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10926 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10927 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 10928 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 10929 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10930 | window->assertNoEvents(); |
| 10931 | |
| 10932 | // With the flag cleared, the window should get input |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10933 | window->setDropInputIfObscured(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10934 | mDispatcher->onWindowInfosChanged( |
| 10935 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10936 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10937 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10938 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 10939 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10940 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10941 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10942 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED); |
| 10943 | window->assertNoEvents(); |
| 10944 | } |
| 10945 | |
| 10946 | TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { |
| 10947 | std::shared_ptr<FakeApplicationHandle> obscuringApplication = |
| 10948 | std::make_shared<FakeApplicationHandle>(); |
| 10949 | sp<FakeWindowHandle> obscuringWindow = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10950 | sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", |
| 10951 | ADISPLAY_ID_DEFAULT); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10952 | obscuringWindow->setFrame(Rect(0, 0, 50, 50)); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10953 | obscuringWindow->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 10954 | obscuringWindow->setTouchable(false); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10955 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 10956 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 10957 | "Test window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 10958 | window->setDropInputIfObscured(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 10959 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10960 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
| 10961 | window->setFocusable(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 10962 | mDispatcher->onWindowInfosChanged( |
| 10963 | {{*obscuringWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10964 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 10965 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10966 | |
| 10967 | // With the flag set, window should not get any input |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10968 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10969 | window->assertNoEvents(); |
| 10970 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10971 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10972 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Siarhei Vishniakou | 5c02a71 | 2023-05-15 15:45:02 -0700 | [diff] [blame] | 10973 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, |
| 10974 | ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10975 | window->assertNoEvents(); |
| 10976 | |
| 10977 | // 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] | 10978 | mDispatcher->onWindowInfosChanged( |
| 10979 | {{*window->getInfo(), *obscuringWindow->getInfo()}, {}, 0, 0}); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10980 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10981 | mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_UP, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10982 | window->consumeKeyUp(ADISPLAY_ID_DEFAULT); |
| 10983 | |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 10984 | mDispatcher->notifyMotion(generateMotionArgs(AMOTION_EVENT_ACTION_DOWN, |
| 10985 | AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)); |
Vishnu Nair | 062a867 | 2021-09-03 16:07:44 -0700 | [diff] [blame] | 10986 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 10987 | window->assertNoEvents(); |
| 10988 | } |
| 10989 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10990 | class InputDispatcherTouchModeChangedTests : public InputDispatcherTest { |
| 10991 | protected: |
| 10992 | std::shared_ptr<FakeApplicationHandle> mApp; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10993 | std::shared_ptr<FakeApplicationHandle> mSecondaryApp; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10994 | sp<FakeWindowHandle> mWindow; |
| 10995 | sp<FakeWindowHandle> mSecondWindow; |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 10996 | sp<FakeWindowHandle> mThirdWindow; |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 10997 | |
| 10998 | void SetUp() override { |
| 10999 | InputDispatcherTest::SetUp(); |
| 11000 | |
| 11001 | mApp = std::make_shared<FakeApplicationHandle>(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11002 | mSecondaryApp = std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11003 | mWindow = sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11004 | mWindow->setFocusable(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11005 | setFocusedWindow(mWindow); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11006 | mSecondWindow = |
| 11007 | sp<FakeWindowHandle>::make(mApp, mDispatcher, "TestWindow2", ADISPLAY_ID_DEFAULT); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11008 | mSecondWindow->setFocusable(true); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11009 | mThirdWindow = |
| 11010 | sp<FakeWindowHandle>::make(mSecondaryApp, mDispatcher, |
| 11011 | "TestWindow3_SecondaryDisplay", SECOND_DISPLAY_ID); |
| 11012 | mThirdWindow->setFocusable(true); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11013 | |
| 11014 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApp); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11015 | mDispatcher->onWindowInfosChanged( |
| 11016 | {{*mWindow->getInfo(), *mSecondWindow->getInfo(), *mThirdWindow->getInfo()}, |
| 11017 | {}, |
| 11018 | 0, |
| 11019 | 0}); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11020 | mThirdWindow->setOwnerInfo(SECONDARY_WINDOW_PID, SECONDARY_WINDOW_UID); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11021 | mWindow->consumeFocusEvent(true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11022 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11023 | // Set main display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11024 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11025 | WINDOW_UID, /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)) { |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11026 | mWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
| 11027 | mSecondWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11028 | mThirdWindow->assertNoEvents(); |
| 11029 | } |
| 11030 | |
| 11031 | // Set secondary display initial touch mode to InputDispatcher::kDefaultInTouchMode. |
| 11032 | if (mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, SECONDARY_WINDOW_PID, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11033 | SECONDARY_WINDOW_UID, /*hasPermission=*/true, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11034 | SECOND_DISPLAY_ID)) { |
| 11035 | mWindow->assertNoEvents(); |
| 11036 | mSecondWindow->assertNoEvents(); |
| 11037 | mThirdWindow->consumeTouchModeEvent(InputDispatcher::kDefaultInTouchMode); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11038 | } |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11039 | } |
| 11040 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11041 | void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, gui::Pid pid, gui::Uid uid, |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11042 | bool hasPermission) { |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 11043 | ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, |
| 11044 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11045 | mWindow->consumeTouchModeEvent(inTouchMode); |
| 11046 | mSecondWindow->consumeTouchModeEvent(inTouchMode); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11047 | mThirdWindow->assertNoEvents(); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11048 | } |
| 11049 | }; |
| 11050 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11051 | TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 11052 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11053 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, |
| 11054 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11055 | /* hasPermission=*/false); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11056 | } |
| 11057 | |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11058 | TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { |
| 11059 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11060 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11061 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11062 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11063 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11064 | ownerUid, /*hasPermission=*/false, |
Antonio Kantek | a042c02 | 2022-07-06 16:51:07 -0700 | [diff] [blame] | 11065 | ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11066 | mWindow->assertNoEvents(); |
| 11067 | mSecondWindow->assertNoEvents(); |
| 11068 | } |
| 11069 | |
| 11070 | TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { |
| 11071 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11072 | gui::Pid ownerPid = windowInfo.ownerPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11073 | gui::Uid ownerUid = windowInfo.ownerUid; |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11074 | mWindow->setOwnerInfo(gui::Pid::INVALID, gui::Uid::INVALID); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11075 | changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11076 | ownerUid, /*hasPermission=*/true); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11077 | } |
| 11078 | |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11079 | TEST_F(InputDispatcherTouchModeChangedTests, EventIsNotGeneratedIfNotChangingTouchMode) { |
Antonio Kantek | ea47acb | 2021-12-23 12:41:25 -0800 | [diff] [blame] | 11080 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
Antonio Kantek | 26defcf | 2022-02-08 01:12:27 +0000 | [diff] [blame] | 11081 | ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, |
| 11082 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11083 | /*hasPermission=*/true, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | f16f283 | 2021-09-28 04:39:20 +0000 | [diff] [blame] | 11084 | mWindow->assertNoEvents(); |
| 11085 | mSecondWindow->assertNoEvents(); |
| 11086 | } |
| 11087 | |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11088 | TEST_F(InputDispatcherTouchModeChangedTests, ChangeTouchOnSecondaryDisplayOnly) { |
| 11089 | const WindowInfo& windowInfo = *mThirdWindow->getInfo(); |
| 11090 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 11091 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11092 | /*hasPermission=*/true, SECOND_DISPLAY_ID)); |
Antonio Kantek | 15beb51 | 2022-06-13 22:35:41 +0000 | [diff] [blame] | 11093 | mWindow->assertNoEvents(); |
| 11094 | mSecondWindow->assertNoEvents(); |
| 11095 | mThirdWindow->consumeTouchModeEvent(!InputDispatcher::kDefaultInTouchMode); |
| 11096 | } |
| 11097 | |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11098 | TEST_F(InputDispatcherTouchModeChangedTests, CanChangeTouchModeWhenOwningLastInteractedWindow) { |
| 11099 | // Interact with the window first. |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11100 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11101 | injectKeyDown(*mDispatcher, ADISPLAY_ID_DEFAULT)) |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11102 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 11103 | mWindow->consumeKeyDown(ADISPLAY_ID_DEFAULT); |
| 11104 | |
| 11105 | // Then remove focus. |
| 11106 | mWindow->setFocusable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11107 | mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0}); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11108 | |
| 11109 | // Assert that caller can switch touch mode by owning one of the last interacted window. |
| 11110 | const WindowInfo& windowInfo = *mWindow->getInfo(); |
| 11111 | ASSERT_TRUE(mDispatcher->setInTouchMode(!InputDispatcher::kDefaultInTouchMode, |
| 11112 | windowInfo.ownerPid, windowInfo.ownerUid, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11113 | /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); |
Antonio Kantek | 48710e4 | 2022-03-24 14:19:30 -0700 | [diff] [blame] | 11114 | } |
| 11115 | |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11116 | class InputDispatcherSpyWindowTest : public InputDispatcherTest { |
| 11117 | public: |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11118 | sp<FakeWindowHandle> createSpy() { |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11119 | std::shared_ptr<FakeApplicationHandle> application = |
| 11120 | std::make_shared<FakeApplicationHandle>(); |
| 11121 | std::string name = "Fake Spy "; |
| 11122 | name += std::to_string(mSpyCount++); |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11123 | sp<FakeWindowHandle> spy = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 11124 | name.c_str(), ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11125 | spy->setSpy(true); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 11126 | spy->setTrustedOverlay(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11127 | return spy; |
| 11128 | } |
| 11129 | |
| 11130 | sp<FakeWindowHandle> createForeground() { |
| 11131 | std::shared_ptr<FakeApplicationHandle> application = |
| 11132 | std::make_shared<FakeApplicationHandle>(); |
| 11133 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11134 | sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window", |
| 11135 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11136 | window->setFocusable(true); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11137 | return window; |
| 11138 | } |
| 11139 | |
| 11140 | private: |
| 11141 | int mSpyCount{0}; |
| 11142 | }; |
| 11143 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11144 | using InputDispatcherSpyWindowDeathTest = InputDispatcherSpyWindowTest; |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11145 | /** |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 11146 | * Adding a spy window that is not a trusted overlay causes Dispatcher to abort. |
| 11147 | */ |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11148 | TEST_F(InputDispatcherSpyWindowDeathTest, UntrustedSpy_AbortsDispatcher) { |
Siarhei Vishniakou | 21f77bd | 2023-07-18 17:51:35 -0700 | [diff] [blame] | 11149 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11150 | ScopedSilentDeath _silentDeath; |
| 11151 | |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11152 | auto spy = createSpy(); |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 11153 | spy->setTrustedOverlay(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11154 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | 5c85e05 | 2021-12-22 02:27:12 -0800 | [diff] [blame] | 11155 | ".* not a trusted overlay"); |
| 11156 | } |
| 11157 | |
| 11158 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11159 | * Input injection into a display with a spy window but no foreground windows should succeed. |
| 11160 | */ |
| 11161 | TEST_F(InputDispatcherSpyWindowTest, NoForegroundWindow) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11162 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11163 | mDispatcher->onWindowInfosChanged({{*spy->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11164 | |
| 11165 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11166 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11167 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11168 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11169 | } |
| 11170 | |
| 11171 | /** |
| 11172 | * Verify the order in which different input windows receive events. The touched foreground window |
| 11173 | * (if there is one) should always receive the event first. When there are multiple spy windows, the |
| 11174 | * spy windows will receive the event according to their Z-order, where the top-most spy window will |
| 11175 | * receive events before ones belows it. |
| 11176 | * |
| 11177 | * Here, we set up a scenario with four windows in the following Z order from the top: |
| 11178 | * spy1, spy2, window, spy3. |
| 11179 | * We then inject an event and verify that the foreground "window" receives it first, followed by |
| 11180 | * "spy1" and "spy2". The "spy3" does not receive the event because it is underneath the foreground |
| 11181 | * window. |
| 11182 | */ |
| 11183 | TEST_F(InputDispatcherSpyWindowTest, ReceivesInputInOrder) { |
| 11184 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11185 | auto spy1 = createSpy(); |
| 11186 | auto spy2 = createSpy(); |
| 11187 | auto spy3 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11188 | mDispatcher->onWindowInfosChanged( |
| 11189 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo(), *spy3->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11190 | const std::vector<sp<FakeWindowHandle>> channels{spy1, spy2, window, spy3}; |
| 11191 | const size_t numChannels = channels.size(); |
| 11192 | |
Michael Wright | 8e9a856 | 2022-02-09 13:44:29 +0000 | [diff] [blame] | 11193 | base::unique_fd epollFd(epoll_create1(EPOLL_CLOEXEC)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11194 | if (!epollFd.ok()) { |
| 11195 | FAIL() << "Failed to create epoll fd"; |
| 11196 | } |
| 11197 | |
| 11198 | for (size_t i = 0; i < numChannels; i++) { |
| 11199 | struct epoll_event event = {.events = EPOLLIN, .data.u64 = i}; |
| 11200 | if (epoll_ctl(epollFd.get(), EPOLL_CTL_ADD, channels[i]->getChannelFd(), &event) < 0) { |
| 11201 | FAIL() << "Failed to add fd to epoll"; |
| 11202 | } |
| 11203 | } |
| 11204 | |
| 11205 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11206 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11207 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11208 | |
| 11209 | std::vector<size_t> eventOrder; |
| 11210 | std::vector<struct epoll_event> events(numChannels); |
| 11211 | for (;;) { |
| 11212 | const int nFds = epoll_wait(epollFd.get(), events.data(), static_cast<int>(numChannels), |
| 11213 | (100ms).count()); |
| 11214 | if (nFds < 0) { |
| 11215 | FAIL() << "Failed to call epoll_wait"; |
| 11216 | } |
| 11217 | if (nFds == 0) { |
| 11218 | break; // epoll_wait timed out |
| 11219 | } |
| 11220 | for (int i = 0; i < nFds; i++) { |
Colin Cross | 5b79930 | 2022-10-18 21:52:41 -0700 | [diff] [blame] | 11221 | ASSERT_EQ(static_cast<uint32_t>(EPOLLIN), events[i].events); |
Siarhei Vishniakou | 3197718 | 2022-09-30 08:51:23 -0700 | [diff] [blame] | 11222 | eventOrder.push_back(static_cast<size_t>(events[i].data.u64)); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11223 | channels[i]->consumeMotionDown(); |
| 11224 | } |
| 11225 | } |
| 11226 | |
| 11227 | // Verify the order in which the events were received. |
| 11228 | EXPECT_EQ(3u, eventOrder.size()); |
| 11229 | EXPECT_EQ(2u, eventOrder[0]); // index 2: window |
| 11230 | EXPECT_EQ(0u, eventOrder[1]); // index 0: spy1 |
| 11231 | EXPECT_EQ(1u, eventOrder[2]); // index 1: spy2 |
| 11232 | } |
| 11233 | |
| 11234 | /** |
| 11235 | * A spy window using the NOT_TOUCHABLE flag does not receive events. |
| 11236 | */ |
| 11237 | TEST_F(InputDispatcherSpyWindowTest, NotTouchable) { |
| 11238 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11239 | auto spy = createSpy(); |
| 11240 | spy->setTouchable(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11241 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11242 | |
| 11243 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11244 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11245 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11246 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11247 | spy->assertNoEvents(); |
| 11248 | } |
| 11249 | |
| 11250 | /** |
| 11251 | * A spy window will only receive gestures that originate within its touchable region. Gestures that |
| 11252 | * have their ACTION_DOWN outside of the touchable region of the spy window will not be dispatched |
| 11253 | * to the window. |
| 11254 | */ |
| 11255 | TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { |
| 11256 | auto window = createForeground(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11257 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11258 | spy->setTouchableRegion(Region{{0, 0, 20, 20}}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11259 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11260 | |
| 11261 | // Inject an event outside the spy window's touchable region. |
| 11262 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11263 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11264 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11265 | window->consumeMotionDown(); |
| 11266 | spy->assertNoEvents(); |
| 11267 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11268 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11269 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11270 | window->consumeMotionUp(); |
| 11271 | spy->assertNoEvents(); |
| 11272 | |
| 11273 | // Inject an event inside the spy window's touchable region. |
| 11274 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11275 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11276 | {5, 10})) |
| 11277 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11278 | window->consumeMotionDown(); |
| 11279 | spy->consumeMotionDown(); |
| 11280 | } |
| 11281 | |
| 11282 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11283 | * 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] | 11284 | * 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] | 11285 | */ |
| 11286 | TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { |
| 11287 | auto window = createForeground(); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11288 | window->setOwnerInfo(gui::Pid{12}, gui::Uid{34}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11289 | auto spy = createSpy(); |
| 11290 | spy->setWatchOutsideTouch(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11291 | spy->setOwnerInfo(gui::Pid{56}, gui::Uid{78}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11292 | spy->setFrame(Rect{0, 0, 20, 20}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11293 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11294 | |
| 11295 | // Inject an event outside the spy window's frame and touchable region. |
| 11296 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11297 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 11298 | {100, 200})) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11299 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11300 | window->consumeMotionDown(); |
Prabir Pradhan | dfabf8a | 2022-01-21 08:19:30 -0800 | [diff] [blame] | 11301 | spy->consumeMotionOutsideWithZeroedCoords(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11302 | } |
| 11303 | |
| 11304 | /** |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11305 | * Even when a spy window spans over multiple foreground windows, the spy should receive all |
| 11306 | * pointers that are down within its bounds. |
| 11307 | */ |
| 11308 | TEST_F(InputDispatcherSpyWindowTest, ReceivesMultiplePointers) { |
| 11309 | auto windowLeft = createForeground(); |
| 11310 | windowLeft->setFrame({0, 0, 100, 200}); |
| 11311 | auto windowRight = createForeground(); |
| 11312 | windowRight->setFrame({100, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11313 | auto spy = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11314 | spy->setFrame({0, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11315 | mDispatcher->onWindowInfosChanged( |
| 11316 | {{*spy->getInfo(), *windowLeft->getInfo(), *windowRight->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11317 | |
| 11318 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11319 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11320 | {50, 50})) |
| 11321 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11322 | windowLeft->consumeMotionDown(); |
| 11323 | spy->consumeMotionDown(); |
| 11324 | |
| 11325 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 11326 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11327 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11328 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11329 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11330 | .build(); |
| 11331 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11332 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11333 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11334 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11335 | windowRight->consumeMotionDown(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11336 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11337 | } |
| 11338 | |
| 11339 | /** |
| 11340 | * When the first pointer lands outside the spy window and the second pointer lands inside it, the |
| 11341 | * the spy should receive the second pointer with ACTION_DOWN. |
| 11342 | */ |
| 11343 | TEST_F(InputDispatcherSpyWindowTest, ReceivesSecondPointerAsDown) { |
| 11344 | auto window = createForeground(); |
| 11345 | window->setFrame({0, 0, 200, 200}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11346 | auto spyRight = createSpy(); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11347 | spyRight->setFrame({100, 0, 200, 200}); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11348 | mDispatcher->onWindowInfosChanged({{*spyRight->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11349 | |
| 11350 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11351 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11352 | {50, 50})) |
| 11353 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11354 | window->consumeMotionDown(); |
| 11355 | spyRight->assertNoEvents(); |
| 11356 | |
| 11357 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 11358 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11359 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11360 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 11361 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11362 | .build(); |
| 11363 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11364 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11365 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11366 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11367 | window->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 07e05b6 | 2021-11-19 03:57:24 -0800 | [diff] [blame] | 11368 | spyRight->consumeMotionDown(); |
| 11369 | } |
| 11370 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11371 | /** |
| 11372 | * The spy window should not be able to affect whether or not touches are split. Only the foreground |
| 11373 | * windows should be allowed to control split touch. |
| 11374 | */ |
| 11375 | TEST_F(InputDispatcherSpyWindowTest, SplitIfNoForegroundWindowTouched) { |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 11376 | // 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] | 11377 | // because a foreground window has not disabled splitting. |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11378 | auto spy = createSpy(); |
Prabir Pradhan | 76bdecb | 2022-01-31 11:14:15 -0800 | [diff] [blame] | 11379 | spy->setPreventSplitting(true); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11380 | |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11381 | auto window = createForeground(); |
| 11382 | window->setFrame(Rect(0, 0, 100, 100)); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11383 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11384 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11385 | |
| 11386 | // First finger down, no window touched. |
| 11387 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11388 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11389 | {100, 200})) |
| 11390 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11391 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11392 | window->assertNoEvents(); |
| 11393 | |
| 11394 | // Second finger down on window, the window should receive touch down. |
| 11395 | const MotionEvent secondFingerDownEvent = |
Siarhei Vishniakou | a16e3a2 | 2022-03-02 15:26:40 -0800 | [diff] [blame] | 11396 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11397 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11398 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11399 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 11400 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11401 | .build(); |
| 11402 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11403 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11404 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11405 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11406 | |
| 11407 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11408 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11409 | } |
| 11410 | |
| 11411 | /** |
| 11412 | * A spy window will usually be implemented as an un-focusable window. Verify that these windows |
| 11413 | * do not receive key events. |
| 11414 | */ |
| 11415 | TEST_F(InputDispatcherSpyWindowTest, UnfocusableSpyDoesNotReceiveKeyEvents) { |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11416 | auto spy = createSpy(); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11417 | spy->setFocusable(false); |
| 11418 | |
| 11419 | auto window = createForeground(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11420 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11421 | setFocusedWindow(window); |
| 11422 | window->consumeFocusEvent(true); |
| 11423 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11424 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11425 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 11426 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 11427 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11428 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyUp(*mDispatcher)) |
Prabir Pradhan | 1376fcd | 2022-01-21 09:56:35 -0800 | [diff] [blame] | 11429 | << "Inject key event should return InputEventInjectionResult::SUCCEEDED"; |
| 11430 | window->consumeKeyUp(ADISPLAY_ID_NONE); |
| 11431 | |
| 11432 | spy->assertNoEvents(); |
| 11433 | } |
| 11434 | |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11435 | using InputDispatcherPilferPointersTest = InputDispatcherSpyWindowTest; |
| 11436 | |
| 11437 | /** |
| 11438 | * A spy window can pilfer pointers. When this happens, touch gestures used by the spy window that |
| 11439 | * are currently sent to any other windows - including other spy windows - will also be cancelled. |
| 11440 | */ |
| 11441 | TEST_F(InputDispatcherPilferPointersTest, PilferPointers) { |
| 11442 | auto window = createForeground(); |
| 11443 | auto spy1 = createSpy(); |
| 11444 | auto spy2 = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11445 | mDispatcher->onWindowInfosChanged( |
| 11446 | {{*spy1->getInfo(), *spy2->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11447 | |
| 11448 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11449 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11450 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11451 | window->consumeMotionDown(); |
| 11452 | spy1->consumeMotionDown(); |
| 11453 | spy2->consumeMotionDown(); |
| 11454 | |
| 11455 | // Pilfer pointers from the second spy window. |
| 11456 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy2->getToken())); |
| 11457 | spy2->assertNoEvents(); |
| 11458 | spy1->consumeMotionCancel(); |
| 11459 | window->consumeMotionCancel(); |
| 11460 | |
| 11461 | // The rest of the gesture should only be sent to the second spy window. |
| 11462 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11463 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11464 | ADISPLAY_ID_DEFAULT)) |
| 11465 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11466 | spy2->consumeMotionMove(); |
| 11467 | spy1->assertNoEvents(); |
| 11468 | window->assertNoEvents(); |
| 11469 | } |
| 11470 | |
| 11471 | /** |
| 11472 | * A spy window can pilfer pointers for a gesture even after the foreground window has been removed |
| 11473 | * in the middle of the gesture. |
| 11474 | */ |
| 11475 | TEST_F(InputDispatcherPilferPointersTest, CanPilferAfterWindowIsRemovedMidStream) { |
| 11476 | auto window = createForeground(); |
| 11477 | auto spy = createSpy(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11478 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11479 | |
| 11480 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11481 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11482 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11483 | window->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11484 | spy->consumeMotionDown(ADISPLAY_ID_DEFAULT); |
| 11485 | |
| 11486 | window->releaseChannel(); |
| 11487 | |
| 11488 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11489 | |
| 11490 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11491 | injectMotionUp(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11492 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11493 | spy->consumeMotionUp(ADISPLAY_ID_DEFAULT); |
| 11494 | } |
| 11495 | |
| 11496 | /** |
| 11497 | * After a spy window pilfers pointers, new pointers that go down in its bounds should be sent to |
| 11498 | * the spy, but not to any other windows. |
| 11499 | */ |
| 11500 | TEST_F(InputDispatcherPilferPointersTest, ContinuesToReceiveGestureAfterPilfer) { |
| 11501 | auto spy = createSpy(); |
| 11502 | auto window = createForeground(); |
| 11503 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11504 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11505 | |
| 11506 | // First finger down on the window and the spy. |
| 11507 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11508 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11509 | {100, 200})) |
| 11510 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11511 | spy->consumeMotionDown(); |
| 11512 | window->consumeMotionDown(); |
| 11513 | |
| 11514 | // Spy window pilfers the pointers. |
| 11515 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11516 | window->consumeMotionCancel(); |
| 11517 | |
| 11518 | // Second finger down on the window and spy, but the window should not receive the pointer down. |
| 11519 | const MotionEvent secondFingerDownEvent = |
| 11520 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11521 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11522 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11523 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 11524 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11525 | .build(); |
| 11526 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11527 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11528 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11529 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11530 | |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11531 | spy->consumeMotionPointerDown(/*pointerIndex=*/1); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11532 | |
| 11533 | // Third finger goes down outside all windows, so injection should fail. |
| 11534 | const MotionEvent thirdFingerDownEvent = |
| 11535 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11536 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11537 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11538 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(100).y(200)) |
| 11539 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
| 11540 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(-5).y(-5)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11541 | .build(); |
| 11542 | ASSERT_EQ(InputEventInjectionResult::FAILED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11543 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11544 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
Siarhei Vishniakou | 1ae72f1 | 2023-01-29 12:55:30 -0800 | [diff] [blame] | 11545 | << "Inject motion event should return InputEventInjectionResult::FAILED"; |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11546 | |
| 11547 | spy->assertNoEvents(); |
| 11548 | window->assertNoEvents(); |
| 11549 | } |
| 11550 | |
| 11551 | /** |
| 11552 | * After a spy window pilfers pointers, only the pointers used by the spy should be canceled |
| 11553 | */ |
| 11554 | TEST_F(InputDispatcherPilferPointersTest, PartiallyPilferRequiredPointers) { |
| 11555 | auto spy = createSpy(); |
| 11556 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 11557 | auto window = createForeground(); |
| 11558 | window->setFrame(Rect(0, 0, 200, 200)); |
| 11559 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11560 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11561 | |
| 11562 | // First finger down on the window only |
| 11563 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11564 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11565 | {150, 150})) |
| 11566 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11567 | window->consumeMotionDown(); |
| 11568 | |
| 11569 | // Second finger down on the spy and window |
| 11570 | const MotionEvent secondFingerDownEvent = |
| 11571 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11572 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11573 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11574 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 11575 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11576 | .build(); |
| 11577 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11578 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11579 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11580 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11581 | spy->consumeMotionDown(); |
| 11582 | window->consumeMotionPointerDown(1); |
| 11583 | |
| 11584 | // Third finger down on the spy and window |
| 11585 | const MotionEvent thirdFingerDownEvent = |
| 11586 | MotionEventBuilder(POINTER_2_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11587 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11588 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11589 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(150).y(150)) |
| 11590 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(10).y(10)) |
| 11591 | .pointer(PointerBuilder(/*id=*/2, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11592 | .build(); |
| 11593 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11594 | injectMotionEvent(*mDispatcher, thirdFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11595 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11596 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11597 | spy->consumeMotionPointerDown(1); |
| 11598 | window->consumeMotionPointerDown(2); |
| 11599 | |
| 11600 | // Spy window pilfers the pointers. |
| 11601 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 11602 | window->consumeMotionPointerUp(/*idx=*/2, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
| 11603 | window->consumeMotionPointerUp(/*idx=*/1, ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_CANCELED); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11604 | |
| 11605 | spy->assertNoEvents(); |
| 11606 | window->assertNoEvents(); |
| 11607 | } |
| 11608 | |
| 11609 | /** |
| 11610 | * After a spy window pilfers pointers, all pilfered pointers that have already been dispatched to |
| 11611 | * other windows should be canceled. If this results in the cancellation of all pointers for some |
| 11612 | * window, then that window should receive ACTION_CANCEL. |
| 11613 | */ |
| 11614 | TEST_F(InputDispatcherPilferPointersTest, PilferAllRequiredPointers) { |
| 11615 | auto spy = createSpy(); |
| 11616 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 11617 | auto window = createForeground(); |
| 11618 | window->setFrame(Rect(0, 0, 200, 200)); |
| 11619 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11620 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11621 | |
| 11622 | // First finger down on both spy and window |
| 11623 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11624 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11625 | {10, 10})) |
| 11626 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11627 | window->consumeMotionDown(); |
| 11628 | spy->consumeMotionDown(); |
| 11629 | |
| 11630 | // Second finger down on the spy and window |
| 11631 | const MotionEvent secondFingerDownEvent = |
| 11632 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11633 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11634 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11635 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 11636 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(50).y(50)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11637 | .build(); |
| 11638 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11639 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11640 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11641 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11642 | spy->consumeMotionPointerDown(1); |
| 11643 | window->consumeMotionPointerDown(1); |
| 11644 | |
| 11645 | // Spy window pilfers the pointers. |
| 11646 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11647 | window->consumeMotionCancel(); |
| 11648 | |
| 11649 | spy->assertNoEvents(); |
| 11650 | window->assertNoEvents(); |
| 11651 | } |
| 11652 | |
| 11653 | /** |
| 11654 | * After a spy window pilfers pointers, new pointers that are not touching the spy window can still |
| 11655 | * be sent to other windows |
| 11656 | */ |
| 11657 | TEST_F(InputDispatcherPilferPointersTest, CanReceivePointersAfterPilfer) { |
| 11658 | auto spy = createSpy(); |
| 11659 | spy->setFrame(Rect(0, 0, 100, 100)); |
| 11660 | auto window = createForeground(); |
| 11661 | window->setFrame(Rect(0, 0, 200, 200)); |
| 11662 | |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11663 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11664 | |
| 11665 | // First finger down on both window and spy |
| 11666 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11667 | injectMotionDown(*mDispatcher, AINPUT_SOURCE_TOUCHSCREEN, ADISPLAY_ID_DEFAULT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11668 | {10, 10})) |
| 11669 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11670 | window->consumeMotionDown(); |
| 11671 | spy->consumeMotionDown(); |
| 11672 | |
| 11673 | // Spy window pilfers the pointers. |
| 11674 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11675 | window->consumeMotionCancel(); |
| 11676 | |
| 11677 | // Second finger down on the window only |
| 11678 | const MotionEvent secondFingerDownEvent = |
| 11679 | MotionEventBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11680 | .displayId(ADISPLAY_ID_DEFAULT) |
| 11681 | .eventTime(systemTime(SYSTEM_TIME_MONOTONIC)) |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11682 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(10).y(10)) |
| 11683 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(150)) |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11684 | .build(); |
| 11685 | ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11686 | injectMotionEvent(*mDispatcher, secondFingerDownEvent, INJECT_EVENT_TIMEOUT, |
Vaibhav Devmurari | ff798f3 | 2022-05-09 23:45:04 +0000 | [diff] [blame] | 11687 | InputEventInjectionSync::WAIT_FOR_RESULT)) |
| 11688 | << "Inject motion event should return InputEventInjectionResult::SUCCEEDED"; |
| 11689 | window->consumeMotionDown(); |
| 11690 | window->assertNoEvents(); |
| 11691 | |
| 11692 | // TODO(b/232530217): do not send the unnecessary MOVE event and delete the next line |
| 11693 | spy->consumeMotionMove(); |
| 11694 | spy->assertNoEvents(); |
| 11695 | } |
| 11696 | |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11697 | /** |
| 11698 | * A window on the left and a window on the right. Also, a spy window that's above all of the |
| 11699 | * windows, and spanning both left and right windows. |
| 11700 | * Send simultaneous motion streams from two different devices, one to the left window, and another |
| 11701 | * to the right window. |
| 11702 | * Pilfer from spy window. |
| 11703 | * Check that the pilfering only affects the pointers that are actually being received by the spy. |
| 11704 | */ |
| 11705 | TEST_F(InputDispatcherPilferPointersTest, MultiDevicePilfer) { |
| 11706 | sp<FakeWindowHandle> spy = createSpy(); |
| 11707 | spy->setFrame(Rect(0, 0, 200, 200)); |
| 11708 | sp<FakeWindowHandle> leftWindow = createForeground(); |
| 11709 | leftWindow->setFrame(Rect(0, 0, 100, 100)); |
| 11710 | |
| 11711 | sp<FakeWindowHandle> rightWindow = createForeground(); |
| 11712 | rightWindow->setFrame(Rect(100, 0, 200, 100)); |
| 11713 | |
| 11714 | constexpr int32_t stylusDeviceId = 1; |
| 11715 | constexpr int32_t touchDeviceId = 2; |
| 11716 | |
| 11717 | mDispatcher->onWindowInfosChanged( |
| 11718 | {{*spy->getInfo(), *leftWindow->getInfo(), *rightWindow->getInfo()}, {}, 0, 0}); |
| 11719 | |
| 11720 | // Stylus down on left window and spy |
| 11721 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS) |
| 11722 | .deviceId(stylusDeviceId) |
| 11723 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(50).y(50)) |
| 11724 | .build()); |
| 11725 | leftWindow->consumeMotionEvent( |
| 11726 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 11727 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(stylusDeviceId))); |
| 11728 | |
| 11729 | // Finger down on right window and spy - but spy already has stylus |
| 11730 | mDispatcher->notifyMotion( |
| 11731 | MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 11732 | .deviceId(touchDeviceId) |
| 11733 | .pointer(PointerBuilder(0, ToolType::FINGER).x(150).y(50)) |
| 11734 | .build()); |
| 11735 | rightWindow->consumeMotionEvent( |
| 11736 | AllOf(WithMotionAction(ACTION_DOWN), WithDeviceId(touchDeviceId))); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11737 | spy->assertNoEvents(); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11738 | |
| 11739 | // Act: pilfer from spy. Spy is currently receiving touch events. |
| 11740 | EXPECT_EQ(OK, mDispatcher->pilferPointers(spy->getToken())); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11741 | leftWindow->consumeMotionEvent( |
| 11742 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11743 | rightWindow->consumeMotionEvent( |
| 11744 | AllOf(WithMotionAction(ACTION_CANCEL), WithDeviceId(touchDeviceId))); |
| 11745 | |
| 11746 | // Continue movements from both stylus and touch. Touch will be delivered to spy, but not stylus |
| 11747 | mDispatcher->notifyMotion(MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS) |
| 11748 | .deviceId(stylusDeviceId) |
| 11749 | .pointer(PointerBuilder(0, ToolType::STYLUS).x(51).y(52)) |
| 11750 | .build()); |
| 11751 | mDispatcher->notifyMotion( |
| 11752 | MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN) |
| 11753 | .deviceId(touchDeviceId) |
| 11754 | .pointer(PointerBuilder(0, ToolType::FINGER).x(151).y(52)) |
| 11755 | .build()); |
Siarhei Vishniakou | 2899c55 | 2023-07-10 18:20:46 -0700 | [diff] [blame] | 11756 | spy->consumeMotionEvent(AllOf(WithMotionAction(ACTION_MOVE), WithDeviceId(stylusDeviceId))); |
Siarhei Vishniakou | 0b251a3 | 2023-09-20 16:24:42 -0700 | [diff] [blame] | 11757 | |
| 11758 | spy->assertNoEvents(); |
| 11759 | leftWindow->assertNoEvents(); |
| 11760 | rightWindow->assertNoEvents(); |
| 11761 | } |
| 11762 | |
Prabir Pradhan | 9cd9eb6 | 2023-11-22 17:58:06 +0000 | [diff] [blame] | 11763 | TEST_F(InputDispatcherPilferPointersTest, NoPilferingWithHoveringPointers) { |
| 11764 | auto window = createForeground(); |
| 11765 | auto spy = createSpy(); |
| 11766 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
| 11767 | |
| 11768 | mDispatcher->notifyMotion( |
| 11769 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_MOUSE) |
| 11770 | .deviceId(1) |
| 11771 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(100).y(200)) |
| 11772 | .build()); |
| 11773 | window->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 11774 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 11775 | |
| 11776 | // Pilfer pointers from the spy window should fail. |
| 11777 | EXPECT_NE(OK, mDispatcher->pilferPointers(spy->getToken())); |
| 11778 | spy->assertNoEvents(); |
| 11779 | window->assertNoEvents(); |
| 11780 | } |
| 11781 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11782 | class InputDispatcherStylusInterceptorTest : public InputDispatcherTest { |
| 11783 | public: |
| 11784 | std::pair<sp<FakeWindowHandle>, sp<FakeWindowHandle>> setupStylusOverlayScenario() { |
| 11785 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 11786 | std::make_shared<FakeApplicationHandle>(); |
| 11787 | sp<FakeWindowHandle> overlay = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11788 | sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 11789 | "Stylus interceptor window", ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11790 | overlay->setFocusable(false); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11791 | overlay->setOwnerInfo(gui::Pid{111}, gui::Uid{111}); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 11792 | overlay->setTouchable(false); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11793 | overlay->setInterceptsStylus(true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11794 | overlay->setTrustedOverlay(true); |
| 11795 | |
| 11796 | std::shared_ptr<FakeApplicationHandle> application = |
| 11797 | std::make_shared<FakeApplicationHandle>(); |
| 11798 | sp<FakeWindowHandle> window = |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 11799 | sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", |
| 11800 | ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11801 | window->setFocusable(true); |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11802 | window->setOwnerInfo(gui::Pid{222}, gui::Uid{222}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11803 | |
| 11804 | mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11805 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11806 | setFocusedWindow(window); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11807 | window->consumeFocusEvent(/*hasFocus=*/true, /*inTouchMode=*/true); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11808 | return {std::move(overlay), std::move(window)}; |
| 11809 | } |
| 11810 | |
| 11811 | void sendFingerEvent(int32_t action) { |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11812 | mDispatcher->notifyMotion( |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11813 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11814 | ADISPLAY_ID_DEFAULT, {PointF{20, 20}})); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11815 | } |
| 11816 | |
| 11817 | void sendStylusEvent(int32_t action) { |
| 11818 | NotifyMotionArgs motionArgs = |
| 11819 | generateMotionArgs(action, AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, |
| 11820 | ADISPLAY_ID_DEFAULT, {PointF{30, 40}}); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 11821 | motionArgs.pointerProperties[0].toolType = ToolType::STYLUS; |
Prabir Pradhan | 678438e | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 11822 | mDispatcher->notifyMotion(motionArgs); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11823 | } |
| 11824 | }; |
| 11825 | |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11826 | using InputDispatcherStylusInterceptorDeathTest = InputDispatcherStylusInterceptorTest; |
| 11827 | |
| 11828 | TEST_F(InputDispatcherStylusInterceptorDeathTest, UntrustedOverlay_AbortsDispatcher) { |
Siarhei Vishniakou | ad3b682 | 2023-06-22 14:17:35 -0700 | [diff] [blame] | 11829 | testing::GTEST_FLAG(death_test_style) = "threadsafe"; |
Prabir Pradhan | a3ab87a | 2022-01-27 10:00:21 -0800 | [diff] [blame] | 11830 | ScopedSilentDeath _silentDeath; |
| 11831 | |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11832 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 11833 | overlay->setTrustedOverlay(false); |
| 11834 | // 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] | 11835 | ASSERT_DEATH(mDispatcher->onWindowInfosChanged( |
| 11836 | {{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}), |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11837 | ".* not a trusted overlay"); |
| 11838 | } |
| 11839 | |
| 11840 | TEST_F(InputDispatcherStylusInterceptorTest, ConsmesOnlyStylusEvents) { |
| 11841 | auto [overlay, window] = setupStylusOverlayScenario(); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11842 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11843 | |
| 11844 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11845 | overlay->consumeMotionDown(); |
| 11846 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 11847 | overlay->consumeMotionUp(); |
| 11848 | |
| 11849 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11850 | window->consumeMotionDown(); |
| 11851 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 11852 | window->consumeMotionUp(); |
| 11853 | |
| 11854 | overlay->assertNoEvents(); |
| 11855 | window->assertNoEvents(); |
| 11856 | } |
| 11857 | |
| 11858 | TEST_F(InputDispatcherStylusInterceptorTest, SpyWindowStylusInterceptor) { |
| 11859 | auto [overlay, window] = setupStylusOverlayScenario(); |
Prabir Pradhan | 51e7db0 | 2022-02-07 06:02:57 -0800 | [diff] [blame] | 11860 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11861 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | d65552b | 2021-10-07 11:23:50 -0700 | [diff] [blame] | 11862 | |
| 11863 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11864 | overlay->consumeMotionDown(); |
| 11865 | window->consumeMotionDown(); |
| 11866 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 11867 | overlay->consumeMotionUp(); |
| 11868 | window->consumeMotionUp(); |
| 11869 | |
| 11870 | sendFingerEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11871 | window->consumeMotionDown(); |
| 11872 | sendFingerEvent(AMOTION_EVENT_ACTION_UP); |
| 11873 | window->consumeMotionUp(); |
| 11874 | |
| 11875 | overlay->assertNoEvents(); |
| 11876 | window->assertNoEvents(); |
| 11877 | } |
| 11878 | |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 11879 | /** |
| 11880 | * Set up a scenario to test the behavior used by the stylus handwriting detection feature. |
| 11881 | * The scenario is as follows: |
| 11882 | * - The stylus interceptor overlay is configured as a spy window. |
| 11883 | * - The stylus interceptor spy receives the start of a new stylus gesture. |
| 11884 | * - It pilfers pointers and then configures itself to no longer be a spy. |
| 11885 | * - The stylus interceptor continues to receive the rest of the gesture. |
| 11886 | */ |
| 11887 | TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { |
| 11888 | auto [overlay, window] = setupStylusOverlayScenario(); |
| 11889 | overlay->setSpy(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11890 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 11891 | |
| 11892 | sendStylusEvent(AMOTION_EVENT_ACTION_DOWN); |
| 11893 | overlay->consumeMotionDown(); |
| 11894 | window->consumeMotionDown(); |
| 11895 | |
| 11896 | // The interceptor pilfers the pointers. |
| 11897 | EXPECT_EQ(OK, mDispatcher->pilferPointers(overlay->getToken())); |
| 11898 | window->consumeMotionCancel(); |
| 11899 | |
| 11900 | // The interceptor configures itself so that it is no longer a spy. |
| 11901 | overlay->setSpy(false); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11902 | mDispatcher->onWindowInfosChanged({{*overlay->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 6dfbf26 | 2022-03-14 15:24:30 +0000 | [diff] [blame] | 11903 | |
| 11904 | // It continues to receive the rest of the stylus gesture. |
| 11905 | sendStylusEvent(AMOTION_EVENT_ACTION_MOVE); |
| 11906 | overlay->consumeMotionMove(); |
| 11907 | sendStylusEvent(AMOTION_EVENT_ACTION_UP); |
| 11908 | overlay->consumeMotionUp(); |
| 11909 | |
| 11910 | window->assertNoEvents(); |
| 11911 | } |
| 11912 | |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11913 | struct User { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11914 | gui::Pid mPid; |
Prabir Pradhan | 8a5c41d | 2023-06-08 19:13:46 +0000 | [diff] [blame] | 11915 | gui::Uid mUid; |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11916 | uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; |
| 11917 | std::unique_ptr<InputDispatcher>& mDispatcher; |
| 11918 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11919 | User(std::unique_ptr<InputDispatcher>& dispatcher, gui::Pid pid, gui::Uid uid) |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11920 | : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} |
| 11921 | |
| 11922 | InputEventInjectionResult injectTargetedMotion(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11923 | return injectMotionEvent(*mDispatcher, action, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11924 | ADISPLAY_ID_DEFAULT, {100, 200}, |
| 11925 | {AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 11926 | AMOTION_EVENT_INVALID_CURSOR_POSITION}, |
| 11927 | INJECT_EVENT_TIMEOUT, InputEventInjectionSync::WAIT_FOR_RESULT, |
| 11928 | systemTime(SYSTEM_TIME_MONOTONIC), {mUid}, mPolicyFlags); |
| 11929 | } |
| 11930 | |
| 11931 | InputEventInjectionResult injectTargetedKey(int32_t action) const { |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 11932 | return inputdispatcher::injectKey(*mDispatcher, action, /*repeatCount=*/0, ADISPLAY_ID_NONE, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11933 | InputEventInjectionSync::WAIT_FOR_RESULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 11934 | INJECT_EVENT_TIMEOUT, /*allowKeyRepeat=*/false, {mUid}, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11935 | mPolicyFlags); |
| 11936 | } |
| 11937 | |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11938 | sp<FakeWindowHandle> createWindow(const char* name) const { |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11939 | std::shared_ptr<FakeApplicationHandle> overlayApplication = |
| 11940 | std::make_shared<FakeApplicationHandle>(); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11941 | sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, |
| 11942 | name, ADISPLAY_ID_DEFAULT); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11943 | window->setOwnerInfo(mPid, mUid); |
| 11944 | return window; |
| 11945 | } |
| 11946 | }; |
| 11947 | |
| 11948 | using InputDispatcherTargetedInjectionTest = InputDispatcherTest; |
| 11949 | |
| 11950 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11951 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11952 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11953 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11954 | |
| 11955 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11956 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11957 | window->consumeMotionDown(); |
| 11958 | |
| 11959 | setFocusedWindow(window); |
| 11960 | window->consumeFocusEvent(true); |
| 11961 | |
| 11962 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11963 | owner.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 11964 | window->consumeKeyDown(ADISPLAY_ID_NONE); |
| 11965 | } |
| 11966 | |
| 11967 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11968 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11969 | auto window = owner.createWindow("Owned window"); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11970 | mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11971 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11972 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11973 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 11974 | rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11975 | |
| 11976 | setFocusedWindow(window); |
| 11977 | window->consumeFocusEvent(true); |
| 11978 | |
| 11979 | EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, |
| 11980 | rando.injectTargetedKey(AKEY_EVENT_ACTION_DOWN)); |
| 11981 | window->assertNoEvents(); |
| 11982 | } |
| 11983 | |
| 11984 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11985 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 11986 | auto window = owner.createWindow("Owned window"); |
| 11987 | auto spy = owner.createWindow("Owned spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11988 | spy->setSpy(true); |
| 11989 | spy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 11990 | mDispatcher->onWindowInfosChanged({{*spy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 11991 | |
| 11992 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 11993 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 11994 | spy->consumeMotionDown(); |
| 11995 | window->consumeMotionDown(); |
| 11996 | } |
| 11997 | |
| 11998 | TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 11999 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12000 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12001 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12002 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12003 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12004 | randosSpy->setSpy(true); |
| 12005 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12006 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12007 | |
| 12008 | // The event is targeted at owner's window, so injection should succeed, but the spy should |
| 12009 | // not receive the event. |
| 12010 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12011 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 12012 | randosSpy->assertNoEvents(); |
| 12013 | window->consumeMotionDown(); |
| 12014 | } |
| 12015 | |
| 12016 | TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12017 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12018 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12019 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12020 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12021 | auto randosSpy = rando.createWindow("Rando's spy"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12022 | randosSpy->setSpy(true); |
| 12023 | randosSpy->setTrustedOverlay(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12024 | mDispatcher->onWindowInfosChanged({{*randosSpy->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12025 | |
| 12026 | // A user that has injection permission can inject into any window. |
| 12027 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12028 | injectMotionEvent(*mDispatcher, AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12029 | ADISPLAY_ID_DEFAULT)); |
| 12030 | randosSpy->consumeMotionDown(); |
| 12031 | window->consumeMotionDown(); |
| 12032 | |
| 12033 | setFocusedWindow(randosSpy); |
| 12034 | randosSpy->consumeFocusEvent(true); |
| 12035 | |
Siarhei Vishniakou | b237f9e | 2023-07-21 16:42:23 -0700 | [diff] [blame] | 12036 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, injectKeyDown(*mDispatcher)); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12037 | randosSpy->consumeKeyDown(ADISPLAY_ID_NONE); |
| 12038 | window->assertNoEvents(); |
| 12039 | } |
| 12040 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 12041 | TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) { |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12042 | auto owner = User(mDispatcher, gui::Pid{10}, gui::Uid{11}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12043 | auto window = owner.createWindow("Owned window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12044 | |
Prabir Pradhan | aeebeb4 | 2023-06-13 19:53:03 +0000 | [diff] [blame] | 12045 | auto rando = User(mDispatcher, gui::Pid{20}, gui::Uid{21}); |
Siarhei Vishniakou | e3ce412 | 2023-08-23 10:26:46 -0700 | [diff] [blame] | 12046 | auto randosWindow = rando.createWindow("Rando's window"); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12047 | randosWindow->setFrame(Rect{-10, -10, -5, -5}); |
| 12048 | randosWindow->setWatchOutsideTouch(true); |
Siarhei Vishniakou | c41de37 | 2023-07-20 13:14:26 -0700 | [diff] [blame] | 12049 | mDispatcher->onWindowInfosChanged({{*randosWindow->getInfo(), *window->getInfo()}, {}, 0, 0}); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12050 | |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 12051 | // 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] | 12052 | EXPECT_EQ(InputEventInjectionResult::SUCCEEDED, |
| 12053 | owner.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); |
| 12054 | window->consumeMotionDown(); |
Siarhei Vishniakou | 580fb3a | 2023-05-05 15:02:20 -0700 | [diff] [blame] | 12055 | randosWindow->assertNoEvents(); |
Prabir Pradhan | 5735a32 | 2022-04-11 17:23:34 +0000 | [diff] [blame] | 12056 | } |
| 12057 | |
Prabir Pradhan | 64f21d2 | 2023-11-28 21:19:42 +0000 | [diff] [blame] | 12058 | using InputDispatcherPointerInWindowTest = InputDispatcherTest; |
| 12059 | |
| 12060 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWhenHovering) { |
| 12061 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 12062 | |
| 12063 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 12064 | ADISPLAY_ID_DEFAULT); |
| 12065 | left->setFrame(Rect(0, 0, 100, 100)); |
| 12066 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 12067 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 12068 | right->setFrame(Rect(100, 0, 200, 100)); |
| 12069 | sp<FakeWindowHandle> spy = |
| 12070 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT); |
| 12071 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 12072 | spy->setTrustedOverlay(true); |
| 12073 | spy->setSpy(true); |
| 12074 | |
| 12075 | mDispatcher->onWindowInfosChanged( |
| 12076 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 12077 | |
| 12078 | // Hover into the left window. |
| 12079 | mDispatcher->notifyMotion( |
| 12080 | MotionArgsBuilder(ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS) |
| 12081 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(50).y(50)) |
| 12082 | .build()); |
| 12083 | |
| 12084 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12085 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12086 | |
| 12087 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12088 | /*pointerId=*/0)); |
| 12089 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12090 | /*pointerId=*/0)); |
| 12091 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12092 | /*pointerId=*/0)); |
| 12093 | |
| 12094 | // Hover move to the right window. |
| 12095 | mDispatcher->notifyMotion( |
| 12096 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_STYLUS) |
| 12097 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 12098 | .build()); |
| 12099 | |
| 12100 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12101 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12102 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_MOVE)); |
| 12103 | |
| 12104 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12105 | /*pointerId=*/0)); |
| 12106 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12107 | /*pointerId=*/0)); |
| 12108 | ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12109 | /*pointerId=*/0)); |
| 12110 | |
| 12111 | // Stop hovering. |
| 12112 | mDispatcher->notifyMotion( |
| 12113 | MotionArgsBuilder(ACTION_HOVER_EXIT, AINPUT_SOURCE_STYLUS) |
| 12114 | .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS).x(150).y(50)) |
| 12115 | .build()); |
| 12116 | |
| 12117 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12118 | spy->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12119 | |
| 12120 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12121 | /*pointerId=*/0)); |
| 12122 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12123 | /*pointerId=*/0)); |
| 12124 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12125 | /*pointerId=*/0)); |
| 12126 | } |
| 12127 | |
| 12128 | TEST_F(InputDispatcherPointerInWindowTest, PointerInWindowWithSplitTouch) { |
| 12129 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 12130 | |
| 12131 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 12132 | ADISPLAY_ID_DEFAULT); |
| 12133 | left->setFrame(Rect(0, 0, 100, 100)); |
| 12134 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 12135 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 12136 | right->setFrame(Rect(100, 0, 200, 100)); |
| 12137 | sp<FakeWindowHandle> spy = |
| 12138 | sp<FakeWindowHandle>::make(application, mDispatcher, "Spy Window", ADISPLAY_ID_DEFAULT); |
| 12139 | spy->setFrame(Rect(0, 0, 200, 100)); |
| 12140 | spy->setTrustedOverlay(true); |
| 12141 | spy->setSpy(true); |
| 12142 | |
| 12143 | mDispatcher->onWindowInfosChanged( |
| 12144 | {{*spy->getInfo(), *left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 12145 | |
| 12146 | // First pointer down on left window. |
| 12147 | mDispatcher->notifyMotion( |
| 12148 | MotionArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 12149 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12150 | .build()); |
| 12151 | |
| 12152 | left->consumeMotionDown(); |
| 12153 | spy->consumeMotionDown(); |
| 12154 | |
| 12155 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12156 | /*pointerId=*/0)); |
| 12157 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12158 | /*pointerId=*/0)); |
| 12159 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12160 | /*pointerId=*/0)); |
| 12161 | |
| 12162 | // Second pointer down on right window. |
| 12163 | mDispatcher->notifyMotion( |
| 12164 | MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) |
| 12165 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12166 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 12167 | .build()); |
| 12168 | |
| 12169 | left->consumeMotionMove(); |
| 12170 | right->consumeMotionDown(); |
| 12171 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_DOWN)); |
| 12172 | |
| 12173 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12174 | /*pointerId=*/0)); |
| 12175 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12176 | /*pointerId=*/0)); |
| 12177 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12178 | /*pointerId=*/0)); |
| 12179 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12180 | /*pointerId=*/1)); |
| 12181 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12182 | /*pointerId=*/1)); |
| 12183 | ASSERT_TRUE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12184 | /*pointerId=*/1)); |
| 12185 | |
| 12186 | // Second pointer up. |
| 12187 | mDispatcher->notifyMotion( |
| 12188 | MotionArgsBuilder(POINTER_1_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 12189 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12190 | .pointer(PointerBuilder(/*id=*/1, ToolType::FINGER).x(150).y(50)) |
| 12191 | .build()); |
| 12192 | |
| 12193 | left->consumeMotionMove(); |
| 12194 | right->consumeMotionUp(); |
| 12195 | spy->consumeMotionEvent(WithMotionAction(POINTER_1_UP)); |
| 12196 | |
| 12197 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12198 | /*pointerId=*/0)); |
| 12199 | ASSERT_TRUE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12200 | /*pointerId=*/0)); |
| 12201 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12202 | /*pointerId=*/0)); |
| 12203 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12204 | /*pointerId=*/1)); |
| 12205 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12206 | /*pointerId=*/1)); |
| 12207 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12208 | /*pointerId=*/1)); |
| 12209 | |
| 12210 | // First pointer up. |
| 12211 | mDispatcher->notifyMotion( |
| 12212 | MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) |
| 12213 | .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER).x(50).y(50)) |
| 12214 | .build()); |
| 12215 | |
| 12216 | left->consumeMotionUp(); |
| 12217 | spy->consumeMotionUp(); |
| 12218 | |
| 12219 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12220 | /*pointerId=*/0)); |
| 12221 | ASSERT_FALSE(mDispatcher->isPointerInWindow(spy->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12222 | /*pointerId=*/0)); |
| 12223 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12224 | /*pointerId=*/0)); |
| 12225 | } |
| 12226 | |
| 12227 | TEST_F(InputDispatcherPointerInWindowTest, MultipleDevicesControllingOneMouse) { |
| 12228 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 12229 | |
| 12230 | sp<FakeWindowHandle> left = sp<FakeWindowHandle>::make(application, mDispatcher, "Left Window", |
| 12231 | ADISPLAY_ID_DEFAULT); |
| 12232 | left->setFrame(Rect(0, 0, 100, 100)); |
| 12233 | sp<FakeWindowHandle> right = sp<FakeWindowHandle>::make(application, mDispatcher, |
| 12234 | "Right Window", ADISPLAY_ID_DEFAULT); |
| 12235 | right->setFrame(Rect(100, 0, 200, 100)); |
| 12236 | |
| 12237 | mDispatcher->onWindowInfosChanged({{*left->getInfo(), *right->getInfo()}, {}, 0, 0}); |
| 12238 | |
| 12239 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12240 | /*pointerId=*/0)); |
| 12241 | ASSERT_FALSE(mDispatcher->isPointerInWindow(right->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12242 | /*pointerId=*/0)); |
| 12243 | |
| 12244 | // Hover move into the window. |
| 12245 | mDispatcher->notifyMotion( |
| 12246 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 12247 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(50).y(50)) |
| 12248 | .rawXCursorPosition(50) |
| 12249 | .rawYCursorPosition(50) |
| 12250 | .deviceId(DEVICE_ID) |
| 12251 | .build()); |
| 12252 | |
| 12253 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12254 | |
| 12255 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12256 | /*pointerId=*/0)); |
| 12257 | |
| 12258 | // Move the mouse with another device. This cancels the hovering pointer from the first device. |
| 12259 | mDispatcher->notifyMotion( |
| 12260 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 12261 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(51).y(50)) |
| 12262 | .rawXCursorPosition(51) |
| 12263 | .rawYCursorPosition(50) |
| 12264 | .deviceId(SECOND_DEVICE_ID) |
| 12265 | .build()); |
| 12266 | |
| 12267 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12268 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12269 | |
| 12270 | // TODO(b/313689709): InputDispatcher's touch state is not updated, even though the window gets |
| 12271 | // a HOVER_EXIT from the first device. |
| 12272 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12273 | /*pointerId=*/0)); |
| 12274 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, |
| 12275 | SECOND_DEVICE_ID, |
| 12276 | /*pointerId=*/0)); |
| 12277 | |
| 12278 | // Move the mouse outside the window. Document the current behavior, where the window does not |
| 12279 | // receive HOVER_EXIT even though the mouse left the window. |
| 12280 | mDispatcher->notifyMotion( |
| 12281 | MotionArgsBuilder(ACTION_HOVER_MOVE, AINPUT_SOURCE_MOUSE) |
| 12282 | .pointer(PointerBuilder(/*id=*/0, ToolType::MOUSE).x(150).y(50)) |
| 12283 | .rawXCursorPosition(150) |
| 12284 | .rawYCursorPosition(50) |
| 12285 | .deviceId(SECOND_DEVICE_ID) |
| 12286 | .build()); |
| 12287 | |
| 12288 | left->consumeMotionEvent(WithMotionAction(ACTION_HOVER_EXIT)); |
| 12289 | right->consumeMotionEvent(WithMotionAction(ACTION_HOVER_ENTER)); |
| 12290 | ASSERT_TRUE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, DEVICE_ID, |
| 12291 | /*pointerId=*/0)); |
| 12292 | ASSERT_FALSE(mDispatcher->isPointerInWindow(left->getToken(), ADISPLAY_ID_DEFAULT, |
| 12293 | SECOND_DEVICE_ID, |
| 12294 | /*pointerId=*/0)); |
| 12295 | } |
| 12296 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 12297 | } // namespace android::inputdispatcher |